Deleted Added
full compact
pw.c (20253) pw.c (20267)
1/*-
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 22 unchanged lines hidden (view full) ---

31 *
32 * $Id$
33 */
34
35#include "pw.h"
36
37static char *progname = "pw";
38
1/*-
2 * Copyright (c) 1996 by David L. Nugent <davidn@blaze.net.au>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 22 unchanged lines hidden (view full) ---

31 *
32 * $Id$
33 */
34
35#include "pw.h"
36
37static char *progname = "pw";
38
39const char *Modes[] = {"add", "del", "mod", "show", NULL};
39const char *Modes[] = {"add", "del", "mod", "show", "next", NULL};
40const char *Which[] = {"user", "group", NULL};
40const char *Which[] = {"user", "group", NULL};
41static const char *Combo1[] = {"useradd", "userdel", "usermod", "usershow",
42 "groupadd", "groupdel", "groupmod", "groupshow",
41static const char *Combo1[] = {
42 "useradd", "userdel", "usermod", "usershow", "usernext",
43 "groupadd", "groupdel", "groupmod", "groupshow", "groupnext",
44 NULL};
45static const char *Combo2[] = {
46 "adduser", "deluser", "moduser", "showuser", "nextuser",
47 "addgroup", "delgroup", "modgroup", "showgroup", "nextgroup",
43NULL};
48NULL};
44static const char *Combo2[] = {"adduser", "deluser", "moduser", "showuser",
45 "addgroup", "delgroup", "modgroup", "showgroup",
46NULL};
47
48static struct cargs arglist;
49
50static int getindex(const char *words[], const char *word);
51static void cmdhelp(int mode, int which);
52
53
54int
55main(int argc, char *argv[])
56{
57 int ch;
58 int mode = -1;
59 int which = -1;
60 struct userconf *cnf;
61
62 static const char *opts[W_NUM][M_NUM] =
63 {
49
50static struct cargs arglist;
51
52static int getindex(const char *words[], const char *word);
53static void cmdhelp(int mode, int which);
54
55
56int
57main(int argc, char *argv[])
58{
59 int ch;
60 int mode = -1;
61 int which = -1;
62 struct userconf *cnf;
63
64 static const char *opts[W_NUM][M_NUM] =
65 {
64 /* user */ {"C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db", "C:qn:u:r", "C:qn:u:c:d:e:p:g:G:mk:s:L:h:F", "C:qn:u:Fpa"},
65 /* grp */ {"C:qn:g:h:p", "C:qn:g:", "C:qn:g:l:h:F", "C:qn:g:Fpa"}
66 { /* user */
67 "C:qn:u:c:d:e:p:g:G:mk:s:oL:i:w:h:Db:NP",
68 "C:qn:u:r",
69 "C:qn:u:c:d:e:p:g:G:mk:s:w:L:h:FNP",
70 "C:qn:u:FPa",
71 "C:q"
72 },
73 { /* grp */
74 "C:qn:g:h:M:pNP",
75 "C:qn:g:",
76 "C:qn:g:l:h:FM:m:NP",
77 "C:qn:g:FPa",
78 "C:q"
79 }
66 };
67
68 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
69 { /* Request handlers */
70 pw_user,
71 pw_group
72 };
73

--- 20 unchanged lines hidden (view full) ---

94 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
95 which = tmp / M_NUM;
96 mode = tmp % M_NUM;
97 } else if (strcmp(argv[1], "help") == 0)
98 cmdhelp(mode, which);
99 else if (which != -1 && mode != -1 && arglist.lh_first == NULL)
100 addarg(&arglist, 'n', argv[1]);
101 else
80 };
81
82 static int (*funcs[W_NUM]) (struct userconf * _cnf, int _mode, struct cargs * _args) =
83 { /* Request handlers */
84 pw_user,
85 pw_group
86 };
87

--- 20 unchanged lines hidden (view full) ---

108 else if ((tmp = getindex(Combo1, argv[1])) != -1 || (tmp = getindex(Combo2, argv[1])) != -1) {
109 which = tmp / M_NUM;
110 mode = tmp % M_NUM;
111 } else if (strcmp(argv[1], "help") == 0)
112 cmdhelp(mode, which);
113 else if (which != -1 && mode != -1 && arglist.lh_first == NULL)
114 addarg(&arglist, 'n', argv[1]);
115 else
102 cmderr(X_CMDERR, "Unknown keyword `%s'\n", argv[1]);
116 cmderr(EX_USAGE, "Unknown keyword `%s'\n", argv[1]);
103 ++argv;
104 --argc;
105 }
106
107 /*
108 * Bail out unless the user is specific!
109 */
110 if (mode == -1 || which == -1)
111 cmdhelp(mode, which);
112
113 /*
117 ++argv;
118 --argc;
119 }
120
121 /*
122 * Bail out unless the user is specific!
123 */
124 if (mode == -1 || which == -1)
125 cmdhelp(mode, which);
126
127 /*
114 * Must be root to attempt an update
115 */
116 if (getuid() != 0 && mode != M_PRINT)
117 cmderr(X_PERMERR, "you must be root to run this program\n");
118
119 /*
120 * We know which mode we're in and what we're about to do, so now
121 * let's dispatch the remaining command line args in a genric way.
122 */
123 argv[0] = progname; /* Preserve this */
124 optarg = NULL;
125
126 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
127 if (ch == '?')
128 * We know which mode we're in and what we're about to do, so now
129 * let's dispatch the remaining command line args in a genric way.
130 */
131 argv[0] = progname; /* Preserve this */
132 optarg = NULL;
133
134 while ((ch = getopt(argc, argv, opts[which][mode])) != -1) {
135 if (ch == '?')
128 cmderr(X_CMDERR, NULL);
136 cmderr(EX_USAGE, NULL);
129 else
130 addarg(&arglist, ch, optarg);
131 optarg = NULL;
132 }
133
134 /*
137 else
138 addarg(&arglist, ch, optarg);
139 optarg = NULL;
140 }
141
142 /*
143 * Must be root to attempt an update
144 */
145 if (getuid() != 0 && mode != M_PRINT && mode != M_NEXT && getarg(&arglist, 'N')==NULL)
146 cmderr(EX_NOPERM, "you must be root to run this program\n");
147
148 /*
135 * We should immediately look for the -q 'quiet' switch so that we
136 * don't bother with extraneous errors
137 */
138 if (getarg(&arglist, 'q') != NULL)
139 freopen("/dev/null", "w", stderr);
140
149 * We should immediately look for the -q 'quiet' switch so that we
150 * don't bother with extraneous errors
151 */
152 if (getarg(&arglist, 'q') != NULL)
153 freopen("/dev/null", "w", stderr);
154
141 ch = X_CMDERR;
142
143 /*
144 * Now, let's do the common initialisation
145 */
146 cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL);
155 /*
156 * Now, let's do the common initialisation
157 */
158 cnf = read_userconfig(getarg(&arglist, 'C') ? getarg(&arglist, 'C')->val : NULL);
147
148 if (funcs[which])
149 ch = funcs[which] (cnf, mode, &arglist);
150 else
151 fprintf(stderr, "%s: %s[%s] not yet implemented.\n", progname, Which[which], Modes[mode]);
152
153 return ch;
159 return funcs[which] (cnf, mode, &arglist);
154}
155
156static int
157getindex(const char *words[], const char *word)
158{
159 int i = 0;
160
161 while (words[i]) {

--- 30 unchanged lines hidden (view full) ---

192 exit(ec);
193}
194
195static void
196cmdhelp(int mode, int which)
197{
198 banner();
199 if (which == -1)
160}
161
162static int
163getindex(const char *words[], const char *word)
164{
165 int i = 0;
166
167 while (words[i]) {

--- 30 unchanged lines hidden (view full) ---

198 exit(ec);
199}
200
201static void
202cmdhelp(int mode, int which)
203{
204 banner();
205 if (which == -1)
200 fprintf(stderr, "usage: %s [user|group] [add|del|mod|show] [ help | switches/values ]\n", progname);
206 fprintf(stderr, "usage: %s [user|group] [add|del|mod|show|next] [ help | switches/values ]\n", progname);
201 else if (mode == -1)
207 else if (mode == -1)
202 fprintf(stderr, "usage: %s %s [add|del|mod] [ help | switches/values ]\n", progname, Which[which]);
208 fprintf(stderr, "usage: %s %s [add|del|mod|show|next] [ help | switches/values ]\n", progname, Which[which]);
203 else {
204
205 /*
206 * We need to give mode specific help
207 */
208 static const char *help[W_NUM][M_NUM] =
209 {
210 {

--- 9 unchanged lines hidden (view full) ---

220 "\t-p date password expiry date\n"
221 "\t-g grp initial group\n"
222 "\t-G grp1,grp2 additional groups\n"
223 "\t-m [ -k dir ] create and set up home\n"
224 "\t-s shell name of login shell\n"
225 "\t-o duplicate uid ok\n"
226 "\t-L class user class\n"
227 "\t-h fd read password on fd\n"
209 else {
210
211 /*
212 * We need to give mode specific help
213 */
214 static const char *help[W_NUM][M_NUM] =
215 {
216 {

--- 9 unchanged lines hidden (view full) ---

226 "\t-p date password expiry date\n"
227 "\t-g grp initial group\n"
228 "\t-G grp1,grp2 additional groups\n"
229 "\t-m [ -k dir ] create and set up home\n"
230 "\t-s shell name of login shell\n"
231 "\t-o duplicate uid ok\n"
232 "\t-L class user class\n"
233 "\t-h fd read password on fd\n"
234 "\t-N no update\n"
228 " Setting defaults:\n"
229 "\t-D set user defaults\n"
230 "\t-b dir default home root dir\n"
231 "\t-e period default expiry period\n"
232 "\t-p period default password change period\n"
233 "\t-g group default group\n"
234 "\t-G grp1,grp2 additional groups\n"
235 "\t-L class default user class\n"

--- 17 unchanged lines hidden (view full) ---

253 "\t-e date account expiry date\n"
254 "\t-p date password expiry date\n"
255 "\t-g grp initial group\n"
256 "\t-G grp1,grp2 additional groups\n"
257 "\t-l name new login name\n"
258 "\t-L class user class\n"
259 "\t-m [ -k dir ] create and set up home\n"
260 "\t-s shell name of login shell\n"
235 " Setting defaults:\n"
236 "\t-D set user defaults\n"
237 "\t-b dir default home root dir\n"
238 "\t-e period default expiry period\n"
239 "\t-p period default password change period\n"
240 "\t-g group default group\n"
241 "\t-G grp1,grp2 additional groups\n"
242 "\t-L class default user class\n"

--- 17 unchanged lines hidden (view full) ---

260 "\t-e date account expiry date\n"
261 "\t-p date password expiry date\n"
262 "\t-g grp initial group\n"
263 "\t-G grp1,grp2 additional groups\n"
264 "\t-l name new login name\n"
265 "\t-L class user class\n"
266 "\t-m [ -k dir ] create and set up home\n"
267 "\t-s shell name of login shell\n"
261 "\t-h fd read password on fd\n",
268 "\t-w method set new password using method\n"
269 "\t-h fd read password on fd\n"
270 "\t-N no update\n",
262 "usage: %s usershow [uid|name] [switches]\n"
263 "\t-n name login name\n"
264 "\t-u uid user id\n"
265 "\t-F force print\n"
271 "usage: %s usershow [uid|name] [switches]\n"
272 "\t-n name login name\n"
273 "\t-u uid user id\n"
274 "\t-F force print\n"
266 "\t-p prettier format\n"
267 "\t-a print all users\n"
275 "\t-P prettier format\n"
276 "\t-a print all users\n",
277 "usage: %s usernext [switches]\n"
278 "\t-C config configuration file\n"
268 },
269 {
270 "usage: %s groupadd [group|gid] [switches]\n"
271 "\t-C config configuration file\n"
272 "\t-q quiet operation\n"
273 "\t-n group group name\n"
274 "\t-g gid group id\n"
279 },
280 {
281 "usage: %s groupadd [group|gid] [switches]\n"
282 "\t-C config configuration file\n"
283 "\t-q quiet operation\n"
284 "\t-n group group name\n"
285 "\t-g gid group id\n"
275 "\t-o duplicate gid ok\n",
286 "\t-M usr1,usr2 add users as group members\n"
287 "\t-o duplicate gid ok\n"
288 "\t-N no update\n",
276 "usage: %s groupdel [group|gid] [switches]\n"
277 "\t-n name group name\n"
278 "\t-g gid group id\n",
279 "usage: %s groupmod [group|gid] [switches]\n"
280 "\t-C config configuration file\n"
281 "\t-q quiet operation\n"
282 "\t-F force add if not exists\n"
283 "\t-n name group name\n"
284 "\t-g gid group id\n"
289 "usage: %s groupdel [group|gid] [switches]\n"
290 "\t-n name group name\n"
291 "\t-g gid group id\n",
292 "usage: %s groupmod [group|gid] [switches]\n"
293 "\t-C config configuration file\n"
294 "\t-q quiet operation\n"
295 "\t-F force add if not exists\n"
296 "\t-n name group name\n"
297 "\t-g gid group id\n"
285 "\t-l name new group name\n",
298 "\t-M usr1,usr2 replaces users as group members\n"
299 "\t-m usr1,usr2 add users as group members\n"
300 "\t-l name new group name\n"
301 "\t-N no update\n",
286 "usage: %s groupshow [group|gid] [switches]\n"
287 "\t-n name group name\n"
288 "\t-g gid group id\n"
289 "\t-F force print\n"
302 "usage: %s groupshow [group|gid] [switches]\n"
303 "\t-n name group name\n"
304 "\t-g gid group id\n"
305 "\t-F force print\n"
290 "\t-p prettier format\n"
291 "\t-a print all accounting groups\n"
306 "\t-P prettier format\n"
307 "\t-a print all accounting groups\n",
308 "usage: %s groupnext [switches]\n"
309 "\t-C config configuration file\n"
292 }
293 };
294
295 fprintf(stderr, help[which][mode], progname);
296 }
310 }
311 };
312
313 fprintf(stderr, help[which][mode], progname);
314 }
297 exit(X_CMDERR);
315 exit(EXIT_FAILURE);
298}
299
300struct carg *
301getarg(struct cargs * _args, int ch)
302{
303 struct carg *c = _args->lh_first;
304
305 while (c != NULL && c->ch != ch)
306 c = c->list.le_next;
307 return c;
308}
309
310struct carg *
311addarg(struct cargs * _args, int ch, char *argstr)
312{
313 struct carg *ca = malloc(sizeof(struct carg));
314
315 if (ca == NULL)
316}
317
318struct carg *
319getarg(struct cargs * _args, int ch)
320{
321 struct carg *c = _args->lh_first;
322
323 while (c != NULL && c->ch != ch)
324 c = c->list.le_next;
325 return c;
326}
327
328struct carg *
329addarg(struct cargs * _args, int ch, char *argstr)
330{
331 struct carg *ca = malloc(sizeof(struct carg));
332
333 if (ca == NULL)
316 cmderr(X_MEMERR, "Abort - out of memory\n");
334 cmderr(EX_OSERR, "Abort - out of memory\n");
317 ca->ch = ch;
318 ca->val = argstr;
319 LIST_INSERT_HEAD(_args, ca, list);
320 return ca;
321}
335 ca->ch = ch;
336 ca->val = argstr;
337 LIST_INSERT_HEAD(_args, ca, list);
338 return ca;
339}