Mercurial > projects > pwm
annotate cmd.c @ 12:8768fbd09bc5
Add generatepassword command to generate random passwords
Refactor and generalize handling of named arguments.
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Thu, 03 Aug 2017 10:22:07 +0200 |
parents | 85bce13237cf |
children | cf81eb0c2d5a |
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> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
32 #ifdef HAVE_READPASSPHRASE_H |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
33 #include <readpassphrase.h> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
34 #endif /* READPASSPHRASE_H */ |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
35 #include <regex.h> |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
36 #include <stdlib.h> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
37 #include <string.h> |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
38 #include <time.h> |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
39 #include <unistd.h> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
40 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
41 #include "cmd.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 |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
85 static enum cmd_return cmd_info(struct pwm_ctx *, int, char *[]); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
86 static enum cmd_return cmd_list(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
87 static enum cmd_return cmd_create(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
88 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
|
89 static enum cmd_return cmd_generatepassword(struct pwm_ctx *, int, char *[]); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
90 static enum cmd_return cmd_remove(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
91 static enum cmd_return cmd_show(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
92 static enum cmd_return cmd_pipe(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
93 static enum cmd_return cmd_creategroup(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
94 static enum cmd_return cmd_removegroup(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
95 static enum cmd_return cmd_changepassword(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
96 static enum cmd_return cmd_help(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
97 static enum cmd_return cmd_write(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
98 static enum cmd_return cmd_quit(struct pwm_ctx *, int, char *[]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
99 |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
100 static const char *field_namev[] = { |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
101 "group", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
102 "title", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
103 "username", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
104 "password", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
105 "notes", |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
106 "url", |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
107 "ctime", |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
108 "mtime", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
109 NULL |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
110 }; |
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 static const char *field_labels[] = { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
113 "Group: ", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
114 "Title: ", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
115 "Username: ", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
116 "Password: ", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
117 "Notes: ", |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
118 "URL: ", |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
119 "Created: ", |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
120 "Modified: " |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
121 }; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
122 |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
123 static const char *cmd_generatepassword_argv[] = { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
124 "len", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
125 "char", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
126 "charclass", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
127 NULL |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
128 }; |
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 static const char *charclass_namev[] = { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
131 "digit", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
132 "upper", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
133 "lower", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
134 "punct", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
135 "alpha", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
136 "alnum", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
137 "xdigit", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
138 "graph", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
139 NULL |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
140 }; |
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 static const char *charclass_values[] = { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
143 CHARS_DIGIT, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
144 CHARS_UPPER, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
145 CHARS_LOWER, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
146 CHARS_PUNCT, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
147 CHARS_UPPER CHARS_LOWER, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
148 CHARS_DIGIT 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 "abcdef", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
150 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
|
151 }; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
152 |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
153 static struct cmd cmds[] = { |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
154 { "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
|
155 cmd_info }, |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
156 { "ls", "list", "list [field~regex ...]", "List entries", cmd_list }, |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
157 { "c", "create", "create field=value ...", "Create entry", cmd_create }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
158 { "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
|
159 { "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
|
160 "[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
|
161 cmd_generatepassword }, |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
162 { "rm", "remove", "remove id", "Delete entry", cmd_remove }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
163 { "s", "show", "show id field", "Show entry", cmd_show }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
164 { "p", "pipe", "pipe id field command", "Pipe entry to external command", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
165 cmd_pipe }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
166 { "cg", "creategroup", "creategroup name", "Create empty group", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
167 cmd_creategroup }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
168 { "rg", "removegroup", "removegroup name", "Delete empty group", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
169 cmd_removegroup }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
170 { "ch", "changepassword", "changepassword", "Change password", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
171 cmd_changepassword }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
172 { "h", "help", "help", "Show help text", cmd_help }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
173 { "w", "write", "write", "Write the database", cmd_write }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
174 { "q", "quit", "quit", "Quit", cmd_quit }, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
175 { 0 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
176 }; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
177 |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
178 static int |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
179 parse_arg(char *arg, const char *namev[], int sep, char **valuep) |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
180 { |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
181 size_t i; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
182 size_t name_len; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
183 |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
184 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
|
185 name_len = strlen(namev[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
186 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
|
187 (arg[name_len] == sep)) { |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
188 if (valuep != NULL) { |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
189 *valuep = arg + name_len + 1; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
190 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
191 return (i); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
192 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
193 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
194 |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
195 return (-1); |
0
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 static int |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
199 parse_id(const char *arg, unsigned int *idp) |
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 long x; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
202 char *p; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
203 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
204 errno = 0; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
205 x = strtol(arg, &p, 10); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
206 if ((errno != 0) || (*arg == '\0') || (*p != '\0') || (x > UINT_MAX) || |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
207 (x <= 0)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
208 return (-1); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
209 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
210 *idp = (unsigned int)x; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
211 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
212 return (0); |
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 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
215 static enum cmd_return |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
216 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
|
217 { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
218 struct metadata *metadata; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
219 struct tm *tm; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
220 char timebuf[TIME_SIZE]; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
221 |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
222 if (argc != 1) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
223 return (CMD_USAGE); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
224 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
225 |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
226 metadata = pwfile_get_metadata(ctx); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
227 printf("Format: 0x%04x\n", metadata->version); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
228 if (metadata->user != NULL) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
229 printf("User: %s\n", metadata->user); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
230 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
231 if (metadata->user != NULL) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
232 printf("Host: %s\n", metadata->host); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
233 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
234 if (metadata->user != NULL) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
235 printf("Application: %s\n", metadata->application); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
236 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
237 tm = gmtime(&metadata->timestamp); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
238 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
239 printf("Last Saved: %s\n", timebuf); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
240 |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
241 pwfile_destroy_metadata(metadata); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
242 |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
243 return (CMD_OK); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
244 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
245 |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
246 static enum cmd_return |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
247 cmd_list(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
248 { |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
249 int retval = CMD_ERR; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
250 int i; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
251 regex_t *group_re = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
252 regex_t *title_re = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
253 regex_t *username_re = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
254 regex_t *notes_re = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
255 regex_t *url_re = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
256 enum field_type type; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
257 char *value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
258 regex_t **repp; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
259 int errcode; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
260 char *errbuf; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
261 size_t errbuf_size; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
262 union list_item **list = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
263 size_t j; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
264 struct record *record; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
265 |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
266 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
|
267 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
|
268 if (type == FIELD_UNKNOWN) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
269 fprintf(stderr, "bad field name \"%s\"\n", argv[i]); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
270 goto out; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
271 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
272 if (value[0] == '\0') { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
273 /* skip empty expressions */ |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
274 continue; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
275 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
276 switch (type) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
277 case FIELD_GROUP: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
278 repp = &group_re; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
279 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
280 case FIELD_TITLE: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
281 repp = &title_re; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
282 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
283 case FIELD_USERNAME: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
284 repp = &username_re; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
285 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
286 case FIELD_NOTES: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
287 repp = ¬es_re; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
288 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
289 case FIELD_URL: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
290 repp = &url_re; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
291 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
292 default: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
293 fprintf(stderr, "bad field name \"%s\"\n", argv[i]); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
294 goto out; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
295 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
296 |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
297 if (*repp == NULL) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
298 *repp = xmalloc(sizeof (regex_t)); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
299 } else { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
300 regfree(*repp); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
301 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
302 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
|
303 if (errcode != 0) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
304 errbuf_size = regerror(errcode, *repp, "", 0); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
305 errbuf = xmalloc(errbuf_size); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
306 regerror(errcode, *repp, errbuf, errbuf_size); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
307 fprintf(stderr, "bad regular expression \"%s\"\n", |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
308 errbuf); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
309 free(errbuf); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
310 |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
311 free(*repp); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
312 *repp = NULL; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
313 |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
314 goto out; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
315 } |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
316 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
317 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
318 list = pwfile_create_list(ctx); |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
319 for (j = 0; list[j] != NULL; j++) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
320 if (list[j]->any.type == ITEM_TYPE_GROUP) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
321 printf("[%s]\n", list[j]->group.group); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
322 } else { |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
323 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
|
324 if (((group_re == NULL) || (regexec(group_re, |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
325 record->group, 0, NULL, 0) == 0)) && |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
326 ((title_re == NULL) || (regexec(title_re, |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
327 record->title, 0, NULL, 0) == 0)) && |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
328 ((username_re == NULL) || (regexec(username_re, |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
329 record->username, 0, NULL, 0) == 0)) && |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
330 ((notes_re == NULL) || (regexec(notes_re, |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
331 record->notes, 0, NULL, 0) == 0)) && |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
332 ((url_re == NULL) || (regexec(url_re, |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
333 record->url, 0, NULL, 0) == 0))) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
334 printf("%4u %s\n", list[j]->record.id, |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
335 (list[j]->record.title != NULL) ? |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
336 list[j]->record.title : ""); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
337 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
338 pwfile_destroy_record(record); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
339 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
340 } |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
341 retval = CMD_OK; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
342 |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
343 out: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
344 if (group_re != NULL) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
345 regfree(group_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
346 free(group_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
347 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
348 if (title_re != NULL) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
349 regfree(title_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
350 free(title_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
351 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
352 if (username_re != NULL) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
353 regfree(username_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
354 free(username_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
355 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
356 if (notes_re != NULL) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
357 regfree(notes_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
358 free(notes_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
359 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
360 if (url_re != NULL) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
361 regfree(url_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
362 free(url_re); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
363 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
364 |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
365 pwfile_destroy_list(list); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
366 |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
367 return (retval); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
368 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
369 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
370 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
371 cmd_create(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
372 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
373 int i; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
374 struct record record = { 0 }; |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
375 enum field_type type; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
376 char *value; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
377 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
378 if (argc < 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
379 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
380 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
381 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
382 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
|
383 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
|
384 if (type == FIELD_UNKNOWN) { |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
385 fprintf(stderr, "bad field assignment \"%s\"\n", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
386 argv[i]); |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
387 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
388 if (value[0] == '\0') { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
389 /* skip empty assignments */ |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
390 continue; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
391 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
392 switch (type) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
393 case FIELD_GROUP: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
394 record.group = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
395 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
396 case FIELD_TITLE: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
397 record.title = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
398 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
399 case FIELD_USERNAME: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
400 record.username = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
401 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
402 case FIELD_PASSWORD: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
403 record.password = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
404 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
405 case FIELD_NOTES: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
406 record.notes = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
407 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
408 case FIELD_URL: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
409 record.url = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
410 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
411 default: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
412 fprintf(stderr, "bad field name \"%s\"\n", argv[i]); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
413 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
414 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
415 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
416 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
417 pwfile_create_record(ctx, &record); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
418 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
419 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
420 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
421 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
422 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
423 cmd_modify(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
424 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
425 unsigned int id; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
426 int i; |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
427 struct record record = { 0 }; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
428 enum field_type type; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
429 char *value; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
430 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
431 if (argc < 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
432 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
433 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
434 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
435 if (parse_id(argv[1], &id) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
436 fprintf(stderr, "invalid id %s\n", argv[1]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
437 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
438 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
439 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
440 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
|
441 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
|
442 if (type == FIELD_UNKNOWN) { |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
443 fprintf(stderr, "bad field assignment \"%s\"\n", |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
444 argv[i]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
445 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
446 } |
11
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
447 if (value[0] == '\0') { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
448 /* skip empty assignments */ |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
449 continue; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
450 } |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
451 switch (type) { |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
452 case FIELD_GROUP: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
453 record.group = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
454 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
455 case FIELD_TITLE: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
456 record.title = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
457 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
458 case FIELD_USERNAME: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
459 record.username = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
460 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
461 case FIELD_PASSWORD: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
462 record.password = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
463 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
464 case FIELD_NOTES: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
465 record.notes = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
466 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
467 case FIELD_URL: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
468 record.url = value; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
469 break; |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
470 default: |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
471 fprintf(stderr, "bad field name \"%s\"\n", argv[i]); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
472 return (CMD_ERR); |
85bce13237cf
Add filter expressions to list command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
10
diff
changeset
|
473 } |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
474 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
475 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
476 pwfile_modify_record(ctx, id, &record); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
477 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
478 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
479 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
480 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
481 static enum cmd_return |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
482 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
|
483 { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
484 enum cmd_return retval = CMD_ERR; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
485 unsigned int id = 0; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
486 int i = 1; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
487 char *value = NULL; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
488 long x; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
489 char *p; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
490 size_t password_len = 16; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
491 size_t chars_min; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
492 const char *chars; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
493 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
|
494 size_t char_groupv_len = 0; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
495 int charclass; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
496 size_t j; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
497 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
|
498 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
499 /* 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
|
500 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
|
501 i++; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
502 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
503 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
504 for (; i < argc; i++) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
505 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
|
506 &value)) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
507 case CMD_GP_ARG_LEN: |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
508 errno = 0; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
509 x = strtol(value, &p, 10); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
510 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
|
511 (x > PWS3_MAX_PASSWORD_LEN) || (x <= 0)) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
512 fprintf(stderr, "invalid password length " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
513 "\"%s\"\n", argv[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
514 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
515 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
516 password_len = x; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
517 break; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
518 case CMD_GP_ARG_CHARS: |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
519 errno = 0; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
520 x = strtol(value, &p, 10); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
521 if ((errno != 0) || (*value == '\0') || (*p != ':') || |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
522 (x < 0) || (x > PWS3_MAX_PASSWORD_LEN)) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
523 fprintf(stderr, "invalid minimum number of " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
524 "characters \"%s\"\n", argv[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
525 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
526 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
527 chars_min = x; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
528 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
529 chars = ++p; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
530 while (*p != '\0') { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
531 if (!isascii(*p) || !isprint(*p)) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
532 fprintf(stderr, "invalid character in " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
533 "character group \"%s\"\n", |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
534 argv[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
535 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
536 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
537 p++; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
538 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
539 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
540 char_groupv = xrealloc(char_groupv, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
541 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
|
542 1)); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
543 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
|
544 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
|
545 char_groupv_len++; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
546 break; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
547 case CMD_GP_ARG_CHARCLASS: |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
548 errno = 0; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
549 x = strtol(value, &p, 10); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
550 if ((errno != 0) || (*value == '\0') || (*p != ':') || |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
551 (x < 0) || (x > PWS3_MAX_PASSWORD_LEN)) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
552 fprintf(stderr, "invalid minimum number of " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
553 "characters \"%s\"\n", argv[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
554 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
555 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
556 chars_min = x; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
557 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
558 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
|
559 if (charclass < 0) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
560 fprintf(stderr, "unknown character class " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
561 "\"%s\"\n", argv[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
562 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
563 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
564 chars = charclass_values[charclass]; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
565 char_groupv = xrealloc(char_groupv, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
566 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
|
567 1)); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
568 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
|
569 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
|
570 char_groupv_len++; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
571 break; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
572 default: |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
573 fprintf(stderr, "invalid argument \"%s\"\n", argv[i]); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
574 retval = CMD_USAGE; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
575 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
576 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
577 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
578 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
579 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
|
580 if (char_groupv[j].chars_min > password_len) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
581 fprintf(stderr, "invalid minimum number of " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
582 "characters \"%zu:%s\"\n", char_groupv[j].chars_min, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
583 char_groupv[j].chars); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
584 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
585 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
586 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
587 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
588 if (char_groupv_len == 0) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
589 /* use defaults */ |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
590 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
|
591 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
|
592 char_groupv[0].chars_min = 0; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
593 char_groupv_len++; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
594 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
595 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
596 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
|
597 password_len) != 0) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
598 fprintf(stderr, "failed to generate password that meets the " |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
599 "given constraints\n"); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
600 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
601 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
602 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
603 if (id != 0) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
604 if (pwfile_modify_record(ctx, id, |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
605 &(struct record){ .password = password }) != 0) { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
606 fprintf(stderr, "record %u does not exist\n", id); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
607 goto out; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
608 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
609 } else { |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
610 printf("%s\n", password); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
611 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
612 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
613 retval = CMD_OK; |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
614 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
615 out: |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
616 free(char_groupv); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
617 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
618 return (retval); |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
619 } |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
620 |
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
621 static enum cmd_return |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
622 cmd_remove(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
623 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
624 unsigned int id; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
625 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
626 if (argc != 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
627 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
628 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
629 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
630 if (parse_id(argv[1], &id) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
631 fprintf(stderr, "invalid id %s\n", argv[1]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
632 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
633 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
634 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
635 if (pwfile_remove_record(ctx, id) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
636 fprintf(stderr, "failed to remove record %u\n", id); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
637 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
638 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
639 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
640 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
641 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
642 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
643 static int |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
644 print_field(const char *label, const char *value, int show_label, FILE *fp) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
645 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
646 fprintf(fp, "%s%s\n", show_label ? label : "", (value != NULL) ? |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
647 value : ""); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
648 if (ferror(fp)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
649 warn("fprintf"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
650 return (-1); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
651 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
652 return (0); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
653 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
654 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
655 static void |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
656 print_record(struct record *record, int fields[], int show_labels, FILE *fp) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
657 { |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
658 struct tm *tm; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
659 char timebuf[TIME_SIZE]; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
660 |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
661 if (fields[FIELD_TITLE]) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
662 if (print_field(field_labels[FIELD_TITLE], record->title, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
663 show_labels, fp) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
664 return; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
665 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
666 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
667 if (fields[FIELD_GROUP]) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
668 if (print_field(field_labels[FIELD_GROUP], record->group, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
669 show_labels, fp)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
670 return; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
671 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
672 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
673 if (fields[FIELD_USERNAME]) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
674 if (print_field(field_labels[FIELD_USERNAME], record->username, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
675 show_labels, fp)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
676 return; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
677 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
678 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
679 if (fields[FIELD_PASSWORD]) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
680 if (print_field(field_labels[FIELD_PASSWORD], record->password, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
681 show_labels, fp)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
682 return; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
683 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
684 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
685 if (fields[FIELD_NOTES]) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
686 if (print_field(field_labels[FIELD_NOTES], record->notes, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
687 show_labels, fp)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
688 return; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
689 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
690 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
691 if (fields[FIELD_URL]) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
692 if (print_field(field_labels[FIELD_URL], record->url, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
693 show_labels, fp)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
694 return; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
695 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
696 } |
10
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
697 if (fields[FIELD_CTIME]) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
698 tm = gmtime(&record->ctime); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
699 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
700 if (print_field(field_labels[FIELD_CTIME], timebuf, |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
701 show_labels, fp)) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
702 return; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
703 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
704 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
705 if (fields[FIELD_MTIME]) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
706 tm = gmtime(&record->mtime); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
707 strftime(timebuf, sizeof (timebuf), TIME_FORMAT, tm); |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
708 if (print_field(field_labels[FIELD_MTIME], timebuf, |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
709 show_labels, fp)) { |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
710 return; |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
711 } |
17fb30016e64
Enable access to record and file metadata
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
4
diff
changeset
|
712 } |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
713 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
714 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
715 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
716 cmd_show(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
717 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
718 unsigned int id; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
719 struct record *record; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
720 int i; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
721 enum field_type type; |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
722 int fields[COUNTOF(field_namev) - 1] = { 0 }; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
723 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
724 if (argc < 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
725 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
726 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
727 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
728 if (parse_id(argv[1], &id) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
729 fprintf(stderr, "invalid id %s\n", argv[1]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
730 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
731 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
732 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
733 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
|
734 type = parse_arg(argv[i], field_namev, '\0', NULL); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
735 if (type < 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
736 fprintf(stderr, "bad field name \"%s\"\n", argv[i]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
737 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
738 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
739 fields[type] = 1; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
740 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
741 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
742 record = pwfile_get_record(ctx, id); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
743 if (record == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
744 fprintf(stderr, "record %u does not exist\n", id); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
745 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
746 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
747 print_record(record, fields, 1, stdout); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
748 pwfile_destroy_record(record); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
749 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
750 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
751 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
752 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
753 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
754 cmd_pipe(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
755 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
756 enum cmd_return retval = CMD_ERR; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
757 unsigned int id; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
758 struct record *record = NULL; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
759 enum field_type type; |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
760 int fields[COUNTOF(field_namev) - 1] = { 0 }; |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
761 FILE *fp = NULL; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
762 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
763 if (argc != 4) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
764 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
765 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
766 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
767 if (parse_id(argv[1], &id) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
768 fprintf(stderr, "invalid id %s\n", argv[1]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
769 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
770 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
771 |
12
8768fbd09bc5
Add generatepassword command to generate random passwords
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
11
diff
changeset
|
772 type = parse_arg(argv[2], field_namev, '\0', NULL); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
773 if (type < 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
774 fprintf(stderr, "bad field name \"%s\"\n", argv[2]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
775 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
776 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
777 fields[type] = 1; |
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 fp = popen(argv[3], "w"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
780 if (fp == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
781 warn("popen"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
782 goto out; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
783 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
784 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
785 record = pwfile_get_record(ctx, id); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
786 if (record == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
787 fprintf(stderr, "record %u does not exist\n", id); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
788 goto out; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
789 } |
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 print_record(record, fields, 0, fp); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
792 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
793 retval = CMD_OK; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
794 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
795 out: |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
796 pwfile_destroy_record(record); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
797 if (fp != NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
798 pclose(fp); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
799 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
800 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
801 return (retval); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
802 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
803 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
804 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
805 cmd_creategroup(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
806 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
807 if (argc != 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
808 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
809 } |
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 (pwfile_create_group(ctx, argv[1]) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
812 fprintf(stderr, "group \"%s\" already exists\n", argv[1]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
813 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
814 } |
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 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
817 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
818 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
819 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
820 cmd_removegroup(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
821 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
822 if (argc != 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
823 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
824 } |
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 (pwfile_remove_group(ctx, argv[1]) != 0) { |
4
b5c4267a7182
Fix check for successful deletion of a empty group
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
827 fprintf(stderr, "there is no empty group \"%s\"\n", argv[1]); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
828 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
829 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
830 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
831 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
832 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
833 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
834 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
835 cmd_changepassword(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
836 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
837 size_t password_len; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
838 char password_buf[PWS3_MAX_PASSWORD_LEN + 1]; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
839 char confirm_buf[PWS3_MAX_PASSWORD_LEN + 1]; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
840 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
841 if (argc > 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
842 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
843 } else if (argc == 2) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
844 password_len = strlen(argv[1]); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
845 if (password_len == 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
846 fprintf(stderr, "password must not be empty\n"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
847 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
848 } else if (password_len + 1 > sizeof (ctx->password)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
849 fprintf(stderr, "password too long\n"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
850 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
851 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
852 memcpy(ctx->password, argv[1], password_len + 1); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
853 } else { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
854 if (readpassphrase("Enter password: ", password_buf, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
855 sizeof (password_buf), RPP_ECHO_OFF | RPP_REQUIRE_TTY) == |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
856 NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
857 err(1, "readpassphrase"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
858 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
859 password_len = strlen(password_buf); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
860 if (password_len == 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
861 fprintf(stderr, "password must not be empty\n"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
862 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
863 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
864 if (readpassphrase("Confirm password: ", confirm_buf, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
865 sizeof (confirm_buf), |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
866 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
867 err(1, "readpassphrase"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
868 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
869 if (strcmp(password_buf, confirm_buf) != 0) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
870 fprintf(stderr, "passwords do not match\n"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
871 return (CMD_ERR); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
872 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
873 memcpy(ctx->password, password_buf, password_len + 1); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
874 } |
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 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
877 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
878 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
879 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
880 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
881 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
882 struct cmd *cmd; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
883 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
884 if (argc != 1) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
885 return (CMD_USAGE); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
886 } |
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 printf("Commands:\n"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
889 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
890 printf("%-2s %-16s %s\n", cmd->abbrev_cmd, cmd->full_cmd, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
891 cmd->description); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
892 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
893 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
894 return (CMD_OK); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
895 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
896 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
897 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
898 cmd_write(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
899 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
900 if (argc != 1) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
901 return (CMD_USAGE); |
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 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
904 return ((pwfile_write_file(ctx) == 0) ? CMD_OK : CMD_ERR); |
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 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
907 static enum cmd_return |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
908 cmd_quit(struct pwm_ctx *ctx, int argc, char *argv[]) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
909 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
910 if (argc != 1) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
911 return (CMD_USAGE); |
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 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
914 return (CMD_QUIT); |
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 struct cmd * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
918 cmd_match(const char *cmd_name) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
919 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
920 size_t i; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
921 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
922 for (i = 0; cmds[i].cmd_func != NULL; i++) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
923 if ((strcmp(cmds[i].abbrev_cmd, cmd_name) == 0) || |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
924 (strcmp(cmds[i].full_cmd, cmd_name) == 0)) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
925 return (&cmds[i]); |
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 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
928 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
929 return (NULL); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
930 } |