annotate cmd.c @ 22:ec01c579024a

Add fully interactive mode
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu, 07 Sep 2017 12:40:50 +0200
parents 5c6155c8e9b6
children 1b89066d992c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1 /*
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
3 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
4 * Permission is hereby granted, free of charge, to any person obtaining
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
5 * a copy of this software and associated documentation files (the
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
6 * "Software"), to deal in the Software without restriction, including
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
7 * without limitation the rights to use, copy, modify, merge, publish,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
8 * distribute, sublicense, and/or sell copies of the Software, and to
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
9 * permit persons to whom the Software is furnished to do so, subject to
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
10 * the following conditions:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
11 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
12 * The above copyright notice and this permission notice shall be included
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
13 * in all copies or substantial portions of the Software.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
14 *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
22 */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
23
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
24 #include "compat.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
25
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
26 #include <ctype.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
27 #ifdef HAVE_ERR_H
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
28 #include <err.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
29 #endif /* HAVE_ERR_H */
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
30 #include <errno.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
31 #include <limits.h>
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
32 #include <regex.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
33 #include <stdlib.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
34 #include <string.h>
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
35 #include <time.h>
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
36 #include <unistd.h>
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
37
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
38 #include "cmd.h"
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
39 #include "io.h"
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
40 #include "pager.h"
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
41 #include "proc.h"
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
42 #include "pw.h"
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
43 #include "pwfile.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
44 #include "util.h"
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
45
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
46 #define TIME_FORMAT "%Y-%m-%dT%TZ"
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
47 #define TIME_SIZE (4 + 1 + 2 + 1 + 2 + 1 + 8 + 1 + 1)
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
48
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
49 #define CHARS_DIGIT "0123456789"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
50 #define CHARS_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
51 #define CHARS_LOWER "abcdefghijklmnopqrstuvwxyz"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
52 #define CHARS_PUNCT "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
53
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
54 enum field_type {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
55 FIELD_UNKNOWN = -1,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
56 FIELD_GROUP,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
57 FIELD_TITLE,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
58 FIELD_USERNAME,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
59 FIELD_PASSWORD,
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
60 FIELD_NOTES,
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
61 FIELD_URL,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
62 FIELD_MTIME,
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
63 FIELD_CTIME
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
64 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
65
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
66 enum cmd_generatepassword_arg_type {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
67 CMD_GP_ARG_UNKNOWN = -1,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
68 CMD_GP_ARG_LEN,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
69 CMD_GP_ARG_CHARS,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
70 CMD_GP_ARG_CHARCLASS
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
71 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
72
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
73 enum charclass_type {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
74 CHARCLASS_UNKNOWN = -1,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
75 CHARCLASS_DIGIT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
76 CHARCLASS_UPPER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
77 CHARCLASS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
78 CHARCLASS_PUNCT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
79 CHARCLASS_ALPHA,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
80 CHARCLASS_ALNUM,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
81 CHARCLASS_XDIGIT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
82 CHARCLASS_GRAPH
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
83 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
84
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
85 static enum cmd_return cmd_status(struct pwm_ctx *, int, char *[]);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
86 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
87 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
88 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
89 static enum cmd_return cmd_modify(struct pwm_ctx *, int, char *[]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
90 static enum cmd_return cmd_generatepassword(struct pwm_ctx *, int, char *[]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
91 static enum cmd_return cmd_remove(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
92 static enum cmd_return cmd_show(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
93 static enum cmd_return cmd_pipe(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
94 static enum cmd_return cmd_creategroup(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
95 static enum cmd_return cmd_removegroup(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
96 static enum cmd_return cmd_changepassword(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
97 static enum cmd_return cmd_help(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
98 static enum cmd_return cmd_write(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
99 static enum cmd_return cmd_quit(struct pwm_ctx *, int, char *[]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
100
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
101 static const char *field_namev[] = {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
102 "group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
103 "title",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
104 "username",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
105 "password",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
106 "notes",
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
107 "url",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
108 "ctime",
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
109 "mtime",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
110 NULL
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
111 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
112
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
113 static const char *field_labels[] = {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
114 "Group: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
115 "Title: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
116 "Username: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
117 "Password: ",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
118 "Notes: ",
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
119 "URL: ",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
120 "Created: ",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
121 "Modified: "
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
122 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
123
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
124 static const char *cmd_generatepassword_argv[] = {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
125 "len",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
126 "char",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
127 "charclass",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
128 NULL
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
129 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
130
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
131 static const char *charclass_namev[] = {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
132 "digit",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
133 "upper",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
134 "lower",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
135 "punct",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
136 "alpha",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
137 "alnum",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
138 "xdigit",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
139 "graph",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
140 NULL
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
141 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
142
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
143 static const char *charclass_values[] = {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
144 CHARS_DIGIT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
145 CHARS_UPPER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
146 CHARS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
147 CHARS_PUNCT,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
148 CHARS_UPPER CHARS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
149 CHARS_DIGIT CHARS_UPPER CHARS_LOWER,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
150 CHARS_DIGIT "abcdef",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
151 CHARS_DIGIT CHARS_UPPER CHARS_LOWER CHARS_PUNCT
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
152 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
153
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
154 static struct cmd cmds[] = {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
155 { "t", "status", "status", "Redisplay an error message of the previous "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
156 "command and unsaved changes", cmd_status },
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
157 { "i", "info", "info", "Show metadata information about the current file",
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
158 cmd_info },
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
159 { "ls", "list", "list [field~regex ...]", "List entries", cmd_list },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
160 { "c", "create", "create field=value ...", "Create entry", cmd_create },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
161 { "m", "modify", "modify id field=value ...", "Modify entry", cmd_modify },
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
162 { "gp", "generatepassword", "generatepassword [id] [len=n] [chars=n:chars] "
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
163 "[charclass=n:class] ...", "Randomly generate a password",
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
164 cmd_generatepassword },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
165 { "rm", "remove", "remove id", "Delete entry", cmd_remove },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
166 { "s", "show", "show id field", "Show entry", cmd_show },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
167 { "p", "pipe", "pipe id field command", "Pipe entry to external command",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
168 cmd_pipe },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
169 { "cg", "creategroup", "creategroup name", "Create empty group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
170 cmd_creategroup },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
171 { "rg", "removegroup", "removegroup name", "Delete empty group",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
172 cmd_removegroup },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
173 { "ch", "changepassword", "changepassword", "Change password",
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
174 cmd_changepassword },
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
175 { "h", "help", "help [command]", "Show help text", cmd_help },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
176 { "w", "write", "write", "Write the database", cmd_write },
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
177 { "q", "quit", "quit", "Quit", cmd_quit },
13
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
178 { "Q", "Quit", "Quit", "Quit without checking", cmd_quit },
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
179 { 0 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
180 };
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
181
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
182 static int
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
183 parse_arg(char *arg, const char *namev[], int sep, char **valuep)
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
184 {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
185 size_t i;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
186 size_t name_len;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
187
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
188 for (i = 0; namev[i] != NULL; i++) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
189 name_len = strlen(namev[i]);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
190 if ((strncmp(namev[i], arg, name_len) == 0) &&
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
191 (arg[name_len] == sep)) {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
192 if (valuep != NULL) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
193 *valuep = arg + name_len + 1;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
194 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
195 return (i);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
196 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
197 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
198
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
199 return (-1);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
200 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
201
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
202 static int
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
203 parse_id(const char *arg, unsigned int *idp)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
204 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
205 long x;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
206 char *p;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
207
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
208 errno = 0;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
209 x = strtol(arg, &p, 10);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
210 if ((errno != 0) || (*arg == '\0') || (*p != '\0') || (x > UINT_MAX) ||
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
211 (x <= 0)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
212 return (-1);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
213 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
214 *idp = (unsigned int)x;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
215
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
216 return (0);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
217 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
218
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
219 static enum cmd_return
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
220 cmd_status(struct pwm_ctx *ctx, int argc, char *argv[])
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
221 {
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
222 if (argc != 1) {
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
223 return (CMD_USAGE);
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
224 }
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
225
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
226 if (ctx->errmsg != NULL) {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
227 if (io_printf("%s\n", ctx->errmsg) == IO_SIGNAL) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
228 return (CMD_SIGNAL);
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
229 }
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
230 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
231 if (io_printf("There are%sunsaved changes\n",
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
232 ctx->unsaved_changes ? " " : " no ") == IO_SIGNAL) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
233 return (CMD_SIGNAL);
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
234 }
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
235
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
236 return (CMD_STATUS);
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
237 }
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
238
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
239 static enum cmd_return
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
240 cmd_info(struct pwm_ctx *ctx, int argc, char *argv[])
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
241 {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
242 enum cmd_return retval;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
243 struct metadata *metadata;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
244 struct pager *pager;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
245 struct tm *tm;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
246 char timebuf[TIME_SIZE];
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
247
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
248 if (argc != 1) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
249 return (CMD_USAGE);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
250 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
251
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
252 metadata = pwfile_get_metadata(ctx);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
253
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
254 pager = pager_create(STDOUT_FILENO);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
255 pager_printf(pager, "Format: 0x%04x\n", metadata->version);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
256 if (metadata->user != NULL) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
257 pager_printf(pager, "User: %s\n", metadata->user);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
258 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
259 if (metadata->user != NULL) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
260 pager_printf(pager, "Host: %s\n", metadata->host);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
261 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
262 if (metadata->user != NULL) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
263 pager_printf(pager, "Application: %s\n", metadata->application);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
264 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
265 tm = gmtime(&metadata->timestamp);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
266 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
267 pager_printf(pager, "Last Saved: %s\n", timebuf);
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
268 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
269 pager_destroy(pager);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
270
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
271 pwfile_destroy_metadata(metadata);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
272
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
273 return (retval);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
274 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
275
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
276 static enum cmd_return
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
277 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
278 {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
279 int retval = CMD_ERR;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
280 int i;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
281 regex_t *group_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
282 regex_t *title_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
283 regex_t *username_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
284 regex_t *notes_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
285 regex_t *url_re = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
286 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
287 char *value;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
288 regex_t **repp;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
289 int errcode;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
290 char *errbuf;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
291 size_t errbuf_size;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
292 struct pager *pager = NULL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
293 union list_item **list = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
294 size_t j;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
295 struct record *record;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
296
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
297 for (i = 1; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
298 type = parse_arg(argv[i], field_namev, '~', &value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
299 if (type == FIELD_UNKNOWN) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
300 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
301 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
302 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
303 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
304 /* skip empty expressions */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
305 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
306 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
307 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
308 case FIELD_GROUP:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
309 repp = &group_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
310 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
311 case FIELD_TITLE:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
312 repp = &title_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
313 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
314 case FIELD_USERNAME:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
315 repp = &username_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
316 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
317 case FIELD_NOTES:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
318 repp = &notes_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
319 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
320 case FIELD_URL:
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
321 repp = &url_re;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
322 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
323 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
324 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
325 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
326 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
327
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
328 if (*repp == NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
329 *repp = xmalloc(sizeof (regex_t));
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
330 } else {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
331 regfree(*repp);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
332 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
333 errcode = regcomp(*repp, value, REG_EXTENDED | REG_NOSUB);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
334 if (errcode != 0) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
335 errbuf_size = regerror(errcode, *repp, "", 0);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
336 errbuf = xmalloc(errbuf_size);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
337 regerror(errcode, *repp, errbuf, errbuf_size);
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
338 pwm_err(ctx, "bad regular expression \"%s\"", errbuf);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
339 free(errbuf);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
340
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
341 free(*repp);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
342 *repp = NULL;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
343
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
344 goto out;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
345 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
346 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
347
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
348 pager = pager_create(STDOUT_FILENO);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
349 list = pwfile_create_list(ctx);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
350 for (j = 0; list[j] != NULL; j++) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
351 if (list[j]->any.type == ITEM_TYPE_GROUP) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
352 pager_printf(pager, "[%s]\n", list[j]->group.group);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
353 } else {
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
354 record = pwfile_get_record(ctx, list[j]->record.id);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
355 if (((group_re == NULL) || (regexec(group_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
356 record->group, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
357 ((title_re == NULL) || (regexec(title_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
358 record->title, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
359 ((username_re == NULL) || (regexec(username_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
360 record->username, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
361 ((notes_re == NULL) || (regexec(notes_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
362 record->notes, 0, NULL, 0) == 0)) &&
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
363 ((url_re == NULL) || (regexec(url_re,
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
364 record->url, 0, NULL, 0) == 0))) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
365 pager_printf(pager, "%4u %s\n",
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
366 list[j]->record.id,
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
367 (list[j]->record.title != NULL) ?
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
368 list[j]->record.title : "");
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
369 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
370 pwfile_destroy_record(record);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
371 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
372 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
373 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
374
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
375 out:
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
376 pager_destroy(pager);
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
377
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
378 if (group_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
379 regfree(group_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
380 free(group_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
381 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
382 if (title_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
383 regfree(title_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
384 free(title_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
385 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
386 if (username_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
387 regfree(username_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
388 free(username_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
389 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
390 if (notes_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
391 regfree(notes_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
392 free(notes_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
393 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
394 if (url_re != NULL) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
395 regfree(url_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
396 free(url_re);
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
397 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
398
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
399 pwfile_destroy_list(list);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
400
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
401
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
402 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
403 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
404
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
405 static int
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
406 read_record_fields(struct pwm_ctx *ctx, struct record *record)
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
407 {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
408 char group_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
409 char title_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
410 char username_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
411 char password_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
412 char notes_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
413 char url_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
414
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
415 if (io_get_line(NULL, "Group: ", 0, record->group, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
416 sizeof (group_buf), group_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
417 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
418 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
419 io_trim_nl(group_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
420
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
421 if (io_get_line(NULL, "Title: ", 0, record->title, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
422 sizeof (title_buf), title_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
423 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
424 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
425 io_trim_nl(title_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
426
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
427 if (io_get_line(NULL, "Username: ", 0, record->username, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
428 sizeof (username_buf), username_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
429 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
430 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
431 io_trim_nl(username_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
432
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
433 for (;;) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
434 switch (io_get_password("Password: ", "Confirm Password: ",
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
435 sizeof (password_buf), password_buf)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
436 case IO_OK: /* FALLTHROUGH */
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
437 case IO_PASSWORD_EMPTY:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
438 goto password_done;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
439 case IO_SIGNAL:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
440 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
441 case IO_PASSWORD_MISMATCH:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
442 pwm_err(ctx, "passwords do not match");
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
443 continue;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
444 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
445 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
446
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
447 password_done:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
448 if (io_get_line(NULL, "Notes: ", 0, record->notes, -1,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
449 sizeof (notes_buf), notes_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
450 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
451 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
452 io_trim_nl(notes_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
453
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
454 if (io_get_line(NULL, "URL: ", 0, record->url, -1, sizeof (url_buf),
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
455 url_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
456 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
457 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
458 io_trim_nl(url_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
459
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
460 free(record->group);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
461 record->group = (group_buf[0] != '\0') ? xstrdup(group_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
462 free(record->title);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
463 record->title = (title_buf[0] != '\0') ? xstrdup(title_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
464 free(record->username);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
465 record->username = (username_buf[0] != '\0') ? xstrdup(username_buf) :
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
466 NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
467 /*
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
468 * the current password cannot be edited, keep the current password if
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
469 * the user pressed return or ^D instead of deleting it like other
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
470 * fields
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
471 */
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
472 if (password_buf[0] != '\0') {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
473 free(record->password);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
474 record->password = xstrdup(password_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
475 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
476 free(record->notes);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
477 record->notes = (notes_buf[0] != '\0') ? xstrdup(notes_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
478 free(record->url);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
479 record->url = (url_buf[0] != '\0') ? xstrdup(url_buf) : NULL;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
480
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
481 return (CMD_OK);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
482 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
483
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
484 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
485 cmd_create(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
486 {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
487 enum cmd_return retval = CMD_ERR;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
488 int i;
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
489 struct record *record = NULL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
490 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
491 char *value;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
492
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
493 if (!ctx->is_interactive && (argc < 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
494 retval = CMD_USAGE;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
495 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
496 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
497
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
498 record = pwfile_create_record();
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
499
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
500 for (i = 1; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
501 type = parse_arg(argv[i], field_namev, '=', &value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
502 if (type == FIELD_UNKNOWN) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
503 pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
504 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
505 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
506 /* skip empty assignments */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
507 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
508 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
509 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
510 case FIELD_GROUP:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
511 free(record->group);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
512 record->group = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
513 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
514 case FIELD_TITLE:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
515 free(record->title);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
516 record->title = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
517 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
518 case FIELD_USERNAME:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
519 free(record->username);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
520 record->username = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
521 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
522 case FIELD_PASSWORD:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
523 free(record->password);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
524 record->password = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
525 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
526 case FIELD_NOTES:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
527 free(record->notes);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
528 record->notes = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
529 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
530 case FIELD_URL:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
531 free(record->url);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
532 record->url = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
533 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
534 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
535 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
536 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
537 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
538 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
539
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
540 if (ctx->is_interactive && (argc < 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
541 if (read_record_fields(ctx, record) != 0) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
542 goto out;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
543 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
544 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
545
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
546 pwfile_create_pws_record(ctx, record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
547 retval = CMD_OK;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
548
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
549 out:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
550 pwfile_destroy_record(record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
551
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
552 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
553 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
554
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
555 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
556 cmd_modify(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
557 {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
558 int retval = CMD_ERR;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
559 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
560 int i;
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
561 struct record *record = NULL;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
562 enum field_type type;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
563 char *value;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
564
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
565 if (!ctx->is_interactive && (argc < 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
566 retval = CMD_USAGE;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
567 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
568 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
569
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
570 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
571 pwm_err(ctx, "invalid id %s", argv[1]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
572 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
573 }
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
574 record = pwfile_get_record(ctx, id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
575
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
576 for (i = 2; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
577 type = parse_arg(argv[i], field_namev, '=', &value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
578 if (type == FIELD_UNKNOWN) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
579 pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
580 goto out;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
581 }
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
582 if (value[0] == '\0') {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
583 /* skip empty assignments */
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
584 continue;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
585 }
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
586 switch (type) {
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
587 case FIELD_GROUP:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
588 free(record->group);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
589 record->group = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
590 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
591 case FIELD_TITLE:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
592 free(record->title);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
593 record->title = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
594 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
595 case FIELD_USERNAME:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
596 free(record->username);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
597 record->username = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
598 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
599 case FIELD_PASSWORD:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
600 free(record->password);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
601 record->password = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
602 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
603 case FIELD_NOTES:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
604 free(record->notes);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
605 record->notes = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
606 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
607 case FIELD_URL:
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
608 free(record->url);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
609 record->url = xstrdup(value);
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
610 break;
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
611 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
612 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
613 goto out;
11
85bce13237cf Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 10
diff changeset
614 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
615 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
616
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
617 if (ctx->is_interactive && (argc < 3)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
618 if (read_record_fields(ctx, record) != 0) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
619 goto out;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
620 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
621 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
622
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
623 pwfile_modify_pws_record(ctx, id, record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
624 retval = CMD_OK;
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
625
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
626 out:
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
627 pwfile_destroy_record(record);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
628
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
629 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
630 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
631
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
632 static enum cmd_return
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
633 cmd_generatepassword(struct pwm_ctx *ctx, int argc, char *argv[])
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
634 {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
635 enum cmd_return retval = CMD_ERR;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
636 unsigned int id = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
637 int i = 1;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
638 char *value = NULL;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
639 long x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
640 char *p;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
641 size_t password_len = 16;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
642 size_t chars_min;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
643 const char *chars;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
644 struct pw_char_group *char_groupv = NULL;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
645 size_t char_groupv_len = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
646 int charclass;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
647 size_t j;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
648 char password[PWS3_MAX_PASSWORD_LEN + 1] = { 0 };
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
649
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
650 /* check if first argument is an id */
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
651 if ((argc > 1) && (parse_id(argv[1], &id) == 0)) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
652 i++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
653 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
654
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
655 for (; i < argc; i++) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
656 switch (parse_arg(argv[i], cmd_generatepassword_argv, '=',
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
657 &value)) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
658 case CMD_GP_ARG_LEN:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
659 errno = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
660 x = strtol(value, &p, 10);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
661 if ((errno != 0) || (*value == '\0') || (*p != '\0') ||
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
662 (x > PWS3_MAX_PASSWORD_LEN) || (x <= 0)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
663 pwm_err(ctx, "invalid password length \"%s\"",
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
664 argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
665 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
666 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
667 password_len = x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
668 break;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
669 case CMD_GP_ARG_CHARS:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
670 errno = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
671 x = strtol(value, &p, 10);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
672 if ((errno != 0) || (*value == '\0') || (*p != ':') ||
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
673 (x < 0) || (x > PWS3_MAX_PASSWORD_LEN)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
674 pwm_err(ctx, "invalid minimum number of "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
675 "characters \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
676 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
677 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
678 chars_min = x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
679
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
680 chars = ++p;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
681 while (*p != '\0') {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
682 if (!isascii(*p) || !isprint(*p)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
683 pwm_err(ctx, "invalid character in "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
684 "character group \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
685 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
686 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
687 p++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
688 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
689
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
690 char_groupv = xrealloc(char_groupv,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
691 sizeof (struct pw_char_group) * (char_groupv_len +
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
692 1));
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
693 char_groupv[char_groupv_len].chars = chars;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
694 char_groupv[char_groupv_len].chars_min = chars_min;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
695 char_groupv_len++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
696 break;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
697 case CMD_GP_ARG_CHARCLASS:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
698 errno = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
699 x = strtol(value, &p, 10);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
700 if ((errno != 0) || (*value == '\0') || (*p != ':') ||
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
701 (x < 0) || (x > PWS3_MAX_PASSWORD_LEN)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
702 pwm_err(ctx, "invalid minimum number of "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
703 "characters \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
704 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
705 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
706 chars_min = x;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
707
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
708 charclass = parse_arg(++p, charclass_namev, '\0', NULL);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
709 if (charclass < 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
710 pwm_err(ctx, "unknown character class \"%s\"",
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
711 argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
712 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
713 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
714 chars = charclass_values[charclass];
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
715 char_groupv = xrealloc(char_groupv,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
716 sizeof (struct pw_char_group) * (char_groupv_len +
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
717 1));
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
718 char_groupv[char_groupv_len].chars = chars;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
719 char_groupv[char_groupv_len].chars_min = chars_min;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
720 char_groupv_len++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
721 break;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
722 default:
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
723 pwm_err(ctx, "invalid argument \"%s\"", argv[i]);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
724 retval = CMD_USAGE;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
725 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
726 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
727 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
728
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
729 for (j = 0; j < char_groupv_len; j++) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
730 if (char_groupv[j].chars_min > password_len) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
731 pwm_err(ctx, "invalid minimum number of characters "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
732 "\"%zu:%s\"", char_groupv[j].chars_min,
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
733 char_groupv[j].chars);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
734 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
735 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
736 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
737
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
738 if (char_groupv_len == 0) {
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
739 /* use defaults */
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
740 char_groupv = xmalloc(sizeof (struct pw_char_group));
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
741 char_groupv[0].chars = charclass_values[CHARCLASS_GRAPH];
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
742 char_groupv[0].chars_min = 0;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
743 char_groupv_len++;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
744 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
745
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
746 if (pw_genrandom(char_groupv, char_groupv_len, password,
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
747 password_len) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
748 pwm_err(ctx, "failed to generate password that meets the given "
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
749 "constraints");
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
750 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
751 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
752
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
753 if (id != 0) {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
754 if (pwfile_modify_pws_record(ctx, id,
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
755 &(struct record){ .password = password }) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
756 pwm_err(ctx, "record %u does not exist", id);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
757 goto out;
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
758 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
759 retval = CMD_OK;
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
760 } else {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
761 retval = io_printf("%s\n", password);
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
762 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
763
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
764 out:
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
765 free(char_groupv);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
766
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
767 return (retval);
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
768 }
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
769
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
770 static enum cmd_return
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
771 cmd_remove(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
772 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
773 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
774
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
775 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
776 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
777 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
778
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
779 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
780 pwm_err(ctx, "invalid id %s", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
781 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
782 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
783
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
784 if (pwfile_remove_pws_record(ctx, id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
785 pwm_err(ctx, "failed to remove record %u", id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
786 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
787 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
788
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
789 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
790 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
791
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
792 static int
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
793 print_record(struct record *record, int fields[], int show_labels, int fd)
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
794 {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
795 struct pager *pager;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
796 struct tm *tm;
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
797 char timebuf[TIME_SIZE];
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
798 int retval;
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
799
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
800 pager = pager_create(fd);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
801 if (fields[FIELD_TITLE]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
802 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
803 field_labels[FIELD_TITLE] : "", (record->title != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
804 record->title : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
805 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
806 if (fields[FIELD_GROUP]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
807 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
808 field_labels[FIELD_GROUP] : "", (record->group != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
809 record->group : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
810 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
811 if (fields[FIELD_USERNAME]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
812 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
813 field_labels[FIELD_USERNAME] : "",
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
814 (record->username != NULL) ? record->username : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
815 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
816 if (fields[FIELD_PASSWORD]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
817 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
818 field_labels[FIELD_PASSWORD] : "",
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
819 (record->password != NULL) ? record->password : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
820 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
821 if (fields[FIELD_NOTES]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
822 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
823 field_labels[FIELD_NOTES] : "", (record->notes != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
824 record->notes : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
825 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
826 if (fields[FIELD_URL]) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
827 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
828 field_labels[FIELD_URL] : "", (record->url != NULL) ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
829 record->url : "");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
830 }
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
831 if (fields[FIELD_CTIME]) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
832 tm = gmtime(&record->ctime);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
833 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
834 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
835 field_labels[FIELD_CTIME] : "", timebuf);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
836 }
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
837 if (fields[FIELD_MTIME]) {
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
838 tm = gmtime(&record->mtime);
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
839 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
840 pager_printf(pager, "%s%s\n", show_labels ?
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
841 field_labels[FIELD_MTIME] : "", timebuf);
10
17fb30016e64 Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 4
diff changeset
842 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
843 retval = pager_show(pager);
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
844 pager_destroy(pager);
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
845
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
846 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
847 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
848
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
849 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
850 cmd_show(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
851 {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
852 enum cmd_return retval;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
853 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
854 struct record *record;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
855 int i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
856 enum field_type type;
15
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
857 int fields[COUNTOF(field_namev) - 1] = {
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
858 [FIELD_GROUP] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
859 [FIELD_TITLE] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
860 [FIELD_USERNAME] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
861 [FIELD_PASSWORD] = 0,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
862 [FIELD_NOTES] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
863 [FIELD_URL] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
864 [FIELD_MTIME] = 1,
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
865 [FIELD_CTIME] = 1
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
866 };
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
867
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
868 if (argc < 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
869 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
870 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
871
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
872 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
873 pwm_err(ctx, "invalid id %s", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
874 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
875 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
876
15
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
877 if (argc > 2) {
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
878 /* show only explicitly given field names */
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
879 memset(fields, 0, sizeof (fields));
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
880 }
3380c8fd9776 Show all record fields except the password by default
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 14
diff changeset
881
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
882 for (i = 2; i < argc; i++) {
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
883 type = parse_arg(argv[i], field_namev, '\0', NULL);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
884 if (type < 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
885 pwm_err(ctx, "bad field name \"%s\"", argv[i]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
886 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
887 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
888 fields[type] = 1;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
889 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
890
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
891 record = pwfile_get_record(ctx, id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
892 if (record == NULL) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
893 pwm_err(ctx, "record %u does not exist", id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
894 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
895 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
896 retval = (print_record(record, fields, 1, STDOUT_FILENO) != IO_SIGNAL) ?
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
897 CMD_OK : CMD_SIGNAL;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
898 pwfile_destroy_record(record);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
899
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
900 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
901 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
902
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
903 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
904 cmd_pipe(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
905 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
906 enum cmd_return retval = CMD_ERR;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
907 unsigned int id;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
908 struct record *record = NULL;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
909 enum field_type type;
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
910 int fields[COUNTOF(field_namev) - 1] = { 0 };
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
911 struct proc proc = { 0 };
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
912
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
913 if (argc != 4) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
914 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
915 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
916
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
917 if (parse_id(argv[1], &id) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
918 pwm_err(ctx, "invalid id %s", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
919 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
920 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
921
12
8768fbd09bc5 Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 11
diff changeset
922 type = parse_arg(argv[2], field_namev, '\0', NULL);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
923 if (type < 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
924 pwm_err(ctx, "bad field name \"%s\"", argv[2]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
925 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
926 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
927 fields[type] = 1;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
928
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
929 if (proc_open(&proc, argv[3], "w") != IO_OK) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
930 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
931 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
932
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
933 record = pwfile_get_record(ctx, id);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
934 if (record == NULL) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
935 pwm_err(ctx, "record %u does not exist", id);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
936 goto out;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
937 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
938
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
939 retval = (print_record(record, fields, 0, proc.fd) != IO_SIGNAL) ?
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
940 CMD_OK : CMD_SIGNAL;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
941
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
942 out:
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
943 pwfile_destroy_record(record);
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
944 if (proc.pid != 0) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
945 if (proc_close(&proc) == IO_SIGNAL) {
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
946 retval = CMD_SIGNAL;
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
947 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
948 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
949
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
950 return (retval);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
951 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
952
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
953 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
954 cmd_creategroup(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
955 {
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
956 char group_buf[PWM_LINE_MAX] = { '\0' };
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
957
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
958 if (!ctx->is_interactive && (argc != 2)) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
959 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
960 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
961
22
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
962 if (ctx->is_interactive && (argc != 2)) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
963 if (io_get_line(NULL, "Group: ", 0, NULL, 0,
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
964 sizeof (group_buf), group_buf) == IO_SIGNAL) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
965 return (CMD_SIGNAL);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
966 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
967 io_trim_nl(group_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
968 } else {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
969 strcpy(group_buf, argv[1]);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
970 }
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
971
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
972 if (group_buf[0] != '\0') {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
973 if (pwfile_create_group(ctx, group_buf) != 0) {
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
974 pwm_err(ctx, "group \"%s\" already exists", group_buf);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
975 return (CMD_ERR);
ec01c579024a Add fully interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 19
diff changeset
976 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
977 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
978
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
979 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
980 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
981
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
982 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
983 cmd_removegroup(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
984 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
985 if (argc != 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
986 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
987 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
988
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
989 if (pwfile_remove_group(ctx, argv[1]) != 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
990 pwm_err(ctx, "empty group \"%s\" does not exist", argv[1]);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
991 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
992 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
993
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
994 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
995 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
996
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
997 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
998 cmd_changepassword(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
999 {
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1000 size_t len;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1001
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1002 if (argc > 2) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1003 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1004 } else if (argc == 2) {
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1005 len = strlen(argv[1]);
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1006 if (len == 0) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1007 pwm_err(ctx, "password must not be empty");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1008 return (CMD_ERR);
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1009 } else if (len + 1 > sizeof (ctx->password)) {
16
a07665727c19 Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 15
diff changeset
1010 pwm_err(ctx, "password too long");
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1011 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1012 }
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1013 memcpy(ctx->password, argv[1], len + 1);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1014 } else {
18
1e39a251cbe9 Use libtecla for interactive input
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 17
diff changeset
1015 if (pwm_read_password(ctx, 1) != 0) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1016 return (CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1017 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1018 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1019
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1020 return (CMD_OK);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1021 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1022
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1023 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1024 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1025 {
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1026 enum cmd_return retval = CMD_OK;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1027 struct pager *pager;
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1028 struct cmd *cmd;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1029
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1030 if (argc > 2) {
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1031 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1032 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1033
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1034 pager = pager_create(STDOUT_FILENO);
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1035 if (argc == 2) {
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1036 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1037 if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) ||
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1038 (strcmp(argv[1], cmd->full_cmd) == 0)) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1039 pager_printf(pager, "%s\n", cmd->usage);
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1040 break;
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1041 }
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1042 }
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1043 } else {
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1044 printf("Commands:\n");
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1045 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1046 pager_printf(pager, "%-2s %-16s %s\n", cmd->abbrev_cmd,
14
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1047 cmd->full_cmd, cmd->description);
a01899a6e4bb Extend help command to show usage information for commands
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 13
diff changeset
1048 }
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1049 }
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1050 retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;
17
a08ef0674d8e Page long output in interactive mode
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 16
diff changeset
1051 pager_destroy(pager);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1052
19
5c6155c8e9b6 Handle signals
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 18
diff changeset
1053 return (retval);
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1054 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1055
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1056 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1057 cmd_write(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1058 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1059 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1060 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1061 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1062
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1063 return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1064 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1065
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1066 static enum cmd_return
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1067 cmd_quit(struct pwm_ctx *ctx, int argc, char *argv[])
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1068 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1069 if (argc != 1) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1070 return (CMD_USAGE);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1071 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1072
13
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1073 if ((argv[0][0] == 'q') && ctx->unsaved_changes &&
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1074 (ctx->prev_cmd != NULL) && (strcmp(ctx->prev_cmd, "quit") != 0)) {
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1075 printf("Warning: There are unsaved changes\n");
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1076 return (CMD_OK);
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1077 }
cf81eb0c2d5a Warn before quitting if there are unsaved changes
Guido Berhoerster <guido+pwm@berhoerster.name>
parents: 12
diff changeset
1078
0
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1079 return (CMD_QUIT);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1080 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1081
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1082 struct cmd *
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1083 cmd_match(const char *cmd_name)
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1084 {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1085 size_t i;
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1086
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1087 for (i = 0; cmds[i].cmd_func != NULL; i++) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1088 if ((strcmp(cmds[i].abbrev_cmd, cmd_name) == 0) ||
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1089 (strcmp(cmds[i].full_cmd, cmd_name) == 0)) {
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1090 return (&cmds[i]);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1091 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1092 }
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1093
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1094 return (NULL);
a7e41e1a79c8 Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff changeset
1095 }