]>
Commit | Line | Data |
---|---|---|
93d0b3bf | 1 | /* $Header$ |
2 | * | |
3 | * Program to add users en batch to the moira database | |
4 | * | |
5 | * by Mark Rosenstein, July 1992. | |
6 | * | |
7 | * Copyright 1992 by the Massachusetts Institute of Technology. | |
8 | * | |
9 | * (c) Copyright 1992 by the Massachusetts Institute of Technology. | |
10 | * For copying and distribution information, please see the file | |
11 | * <mit-copyright.h>. | |
12 | */ | |
13 | ||
14 | #include <mit-copyright.h> | |
15 | #include <stdio.h> | |
16 | #include <ctype.h> | |
17 | #include <des.h> | |
18 | #include <krb.h> | |
19 | #include <gdss.h> | |
20 | #include <moira.h> | |
21 | #include <moira_site.h> | |
22 | ||
23 | #ifndef LINT | |
24 | static char adduser_rcsid[] = "$Header$"; | |
25 | #endif | |
26 | ||
27 | /* flags from command line */ | |
28 | char *class, *comment, *status_str, *shell, *filename; | |
29 | int reg_only, reg, verbose, nodupcheck; | |
30 | ||
31 | /* argument parsing macro */ | |
32 | #define argis(a,b) ((strcmp(*arg+1, a) == 0) || (strcmp(*arg+1, b) == 0)) | |
33 | ||
34 | char *whoami; | |
35 | int duplicate, errors; | |
36 | ||
37 | extern char *index(); | |
38 | extern char *FixCase(), *RemoveHyphens(), *strtrim(); | |
39 | extern int errno; | |
40 | int usercheck(), scream(); | |
41 | ||
42 | ||
43 | main(argc, argv) | |
44 | int argc; | |
45 | char **argv; | |
46 | { | |
47 | int status, lineno; | |
48 | char **arg = argv, *qargv[U_END]; | |
49 | char *motd, *p, *first, *middle, *last, *id, *login, *passwd, *server; | |
50 | char buf[BUFSIZ], idbuf[32]; | |
51 | FILE *input; | |
52 | ||
53 | /* clear all flags & lists */ | |
54 | reg_only = reg = verbose = lineno = nodupcheck = errors = 0; | |
55 | server = NULL; | |
56 | filename = "-"; | |
57 | shell = "/bin/csh"; | |
58 | class = "TEMP"; | |
59 | comment = ""; | |
60 | status_str = "0"; | |
61 | ||
62 | whoami = argv[0]; | |
63 | ||
64 | /* parse args */ | |
65 | while (++arg - argv < argc) { | |
66 | if (**arg == '-') | |
67 | { | |
68 | if (argis("c", "class")) | |
69 | if (arg - argv < argc - 1) { | |
70 | ++arg; | |
71 | class = *arg; | |
72 | } else | |
73 | usage(argv); | |
74 | else if (argis("C", "comment")) | |
75 | if (arg - argv < argc - 1) { | |
76 | ++arg; | |
77 | comment = *arg; | |
78 | } else | |
79 | usage(argv); | |
80 | else if (argis("s", "status")) | |
81 | if (arg - argv < argc - 1) { | |
82 | ++arg; | |
83 | status_str = *arg; | |
84 | } else | |
85 | usage(argv); | |
86 | else if (argis("h", "shell")) | |
87 | if (arg - argv < argc - 1) { | |
88 | ++arg; | |
89 | shell = *arg; | |
90 | } else | |
91 | usage(argv); | |
92 | else if (argis("r", "reg_only")) | |
93 | reg_only++; | |
94 | else if (argis("R", "register")) | |
95 | reg++; | |
96 | else if (argis("f", "file")) | |
97 | if (arg - argv < argc - 1) { | |
98 | ++arg; | |
99 | filename = *arg; | |
100 | } else | |
101 | usage(argv); | |
102 | else if (argis("v","verbose")) | |
103 | verbose++; | |
104 | else if (argis("d","nodupcheck")) | |
105 | nodupcheck++; | |
106 | else if (argis("S","server")) | |
107 | if (arg - argv < argc - 1) { | |
108 | ++arg; | |
109 | server = *arg; | |
110 | } else | |
111 | usage(argv); | |
112 | else | |
113 | usage(argv); | |
114 | } else | |
115 | usage(argv); | |
116 | } | |
117 | ||
118 | if (!strcmp(filename, "-")) | |
119 | input = stdin; | |
120 | else { | |
121 | input = fopen(filename, "r"); | |
122 | if (input == NULL) { | |
123 | com_err(whoami, errno, "opening input file %s", filename); | |
124 | exit(2); | |
125 | } | |
126 | } | |
127 | ||
128 | if (!reg_only) { | |
129 | /* fire up Moira */ | |
130 | if (status = mr_connect(server)) { | |
131 | com_err(whoami, status, "unable to connect to the Moira server"); | |
132 | exit(2); | |
133 | } | |
134 | if ( status = mr_motd(&motd) ) { | |
135 | com_err(whoami, status, "unable to check server status"); | |
136 | exit(2); | |
137 | } | |
138 | if (motd) { | |
139 | fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd); | |
140 | mr_disconnect(); | |
141 | exit(2); | |
142 | } | |
143 | ||
144 | if (status = mr_auth("addusr")) { | |
145 | if (status == MR_USER_AUTH) | |
146 | com_err(whoami, status, ""); | |
147 | else { | |
148 | com_err(whoami, status, "unable to authenticate to Moira"); | |
149 | exit(2); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | if (reg || reg_only) | |
155 | ureg_init(); | |
156 | ||
157 | qargv[U_NAME] = UNIQUE_LOGIN; | |
158 | qargv[U_UID] = UNIQUE_UID; | |
159 | qargv[U_SHELL] = shell; | |
160 | qargv[U_STATE] = status_str; | |
161 | qargv[U_CLASS] = class; | |
162 | qargv[U_COMMENT] = comment; | |
163 | qargv[U_SIGNATURE] = ""; | |
164 | qargv[U_SECURE] = "0"; | |
165 | while (fgets(buf, BUFSIZ, input)) { | |
166 | /* throw away terminating newline */ | |
167 | p = &buf[strlen(buf) - 1]; | |
168 | if (*p == '\n') | |
169 | *p = 0; | |
170 | lineno++; | |
171 | if (strlen(buf) == 0) | |
172 | continue; | |
173 | /* Last name is first thing on line */ | |
174 | last = buf; | |
175 | /* First name follows a comma */ | |
176 | p = index(last, ','); | |
177 | if (!p) { | |
178 | com_err(whoami, MR_BAD_CHAR, "Missing comma on line %d", lineno); | |
179 | errors++; | |
180 | continue; | |
181 | } | |
182 | *p++ = 0; | |
183 | first = p; | |
184 | while (*p) p++; /* find end-of-line */ | |
185 | if (reg_only || reg) { | |
186 | while (!isspace(*p)) p--; | |
187 | if (p <= first) { | |
188 | com_err(whoami, 0, "Missing password on line %d", lineno); | |
189 | errors++; | |
190 | continue; | |
191 | } | |
192 | passwd = strtrim(&p[1]); | |
193 | *p-- = 0; | |
194 | /* previous field is login name */ | |
195 | while (isspace(*p)) p--; | |
196 | while (!isspace(*p)) p--; | |
197 | if (p <= first) { | |
198 | com_err(whoami, 0, "Missing login on line %d", lineno); | |
199 | errors++; | |
200 | continue; | |
201 | } | |
202 | login = strtrim(&p[1]); | |
203 | *p-- = 0; | |
204 | } else { | |
205 | /* ID is last thing on line */ | |
206 | p--; | |
207 | } | |
208 | while (isspace(*p)) p--; | |
209 | while (!isspace(*p)) p--; | |
210 | if (p <= first) { | |
211 | com_err(whoami, 0, "Missing ID on line %d", lineno); | |
212 | errors++; | |
213 | continue; | |
214 | } | |
215 | id = &p[1]; | |
216 | *p-- = 0; | |
217 | /* If something between first name & ID, it's middle name */ | |
218 | while (isspace(*p)) p--; | |
219 | while (!isspace(*p)) p--; | |
220 | if (p <= first) | |
221 | middle = ""; | |
222 | else { | |
223 | middle = &p[1]; | |
224 | *p = 0; | |
225 | } | |
226 | qargv[U_FIRST] = strtrim(first); | |
227 | qargv[U_MIDDLE] = strtrim(middle); | |
228 | qargv[U_LAST] = strtrim(last); | |
229 | qargv[U_MITID] = strtrim(id); | |
230 | FixCase(qargv[U_FIRST]); | |
231 | FixCase(qargv[U_MIDDLE]); | |
232 | FixCase(qargv[U_LAST]); | |
233 | RemoveHyphens(qargv[U_MITID]); | |
234 | if (!reg_only) { | |
235 | if (!nodupcheck) { | |
236 | char *dargv[2]; | |
237 | ||
238 | dargv[0] = qargv[U_FIRST]; | |
239 | dargv[1] = qargv[U_LAST]; | |
240 | duplicate = 0; | |
241 | status = mr_query("get_user_account_by_name", 2, dargv, | |
242 | usercheck, (char *) qargv); | |
243 | if (status && status != MR_NO_MATCH) { | |
244 | com_err(whoami, status, | |
245 | "checking to see if user %s %s already exists", | |
246 | qargv[0], qargv[1]); | |
247 | com_err(whoami, 0, "NOT ADDING USER"); | |
248 | errors++; | |
249 | continue; | |
250 | } | |
251 | if (duplicate > 0) { | |
252 | com_err(whoami, MR_EXISTS, "user %s %s already exists", | |
253 | qargv[0], qargv[1]); | |
254 | com_err(whoami, 0, "NOT ADDING USER"); | |
255 | errors++; | |
256 | continue; | |
257 | } else if (duplicate < 0) { | |
258 | com_err(whoami, MR_EXISTS, | |
259 | "user %s %s already exists with different ID number", | |
260 | qargv[0], qargv[1]); | |
261 | com_err(whoami, 0, "ADDING user anyway"); | |
262 | } | |
263 | } | |
264 | status = mr_query("add_user_account", U_SECURE + 1, qargv, | |
265 | scream, NULL); | |
266 | if (status) { | |
267 | com_err(whoami, status, "adding user %s %s", first, last); | |
268 | errors++; | |
269 | } else if (verbose) | |
270 | printf("Added user %s %s %s (%s)\n", qargv[U_FIRST], | |
271 | qargv[U_MIDDLE], qargv[U_LAST], qargv[U_MITID]); | |
272 | } | |
273 | if (reg || reg_only) { | |
274 | EncryptID(idbuf, qargv[U_MITID], qargv[U_FIRST], qargv[U_LAST]); | |
275 | status = grab_login(qargv[U_FIRST], qargv[U_LAST], | |
276 | qargv[U_MITID], idbuf, login); | |
277 | if (status) { | |
278 | com_err(whoami, status, "While registering (login) %s %s", | |
279 | qargv[U_FIRST], qargv[U_LAST]); | |
280 | errors++; | |
281 | continue; | |
282 | } else if (verbose) | |
283 | printf("Registered user %s %s as %s\n", qargv[U_FIRST], | |
284 | qargv[U_LAST], login); | |
285 | status = set_password(qargv[U_FIRST], qargv[U_LAST], | |
286 | qargv[U_MITID], idbuf, passwd); | |
287 | if (status) { | |
288 | com_err(whoami, status, "While registering (passwd) %s %s", | |
289 | qargv[U_FIRST], qargv[U_LAST]); | |
290 | errors++; | |
291 | continue; | |
292 | } else if (verbose) | |
293 | printf("Set password for user %s\n", login); | |
294 | } | |
295 | } | |
296 | ||
297 | exit(errors); | |
298 | } | |
299 | ||
300 | ||
301 | usage(argv) | |
302 | char **argv; | |
303 | { | |
304 | fprintf(stderr, "Usage: %s [options]\n",argv[0]); | |
305 | fprintf(stderr, "Options are\n"); | |
306 | fprintf(stderr, " -f | -file filename (default STDIN)\n"); | |
307 | fprintf(stderr, " -c | -class class (default TEMP)\n"); | |
308 | fprintf(stderr, " -C | -comment \"comment\" (default \"\")\n"); | |
309 | fprintf(stderr, " -s | -status status (default 0)\n"); | |
310 | fprintf(stderr, " -h | -shell shell (default /bin/csh)\n"); | |
311 | fprintf(stderr, " -r | -reg_only\n"); | |
312 | fprintf(stderr, " -R | -register (and add to database)\n"); | |
313 | fprintf(stderr, " -v | -verbose\n"); | |
314 | fprintf(stderr, " -d | -nodupcheck (don't check for duplicates)\n"); | |
315 | fprintf(stderr, " -S | -server servername\n"); | |
316 | exit(1); | |
317 | } | |
318 | ||
319 | ||
320 | /* Called only if a query returns a value that we weren't expecting */ | |
321 | ||
322 | scream() | |
323 | { | |
324 | fprintf(stderr, "Programmer botch\n"); | |
325 | exit(3); | |
326 | } | |
327 | ||
328 | ||
329 | /* query callback routine to check for duplicate users */ | |
330 | ||
331 | usercheck(argc, argv, qargv) | |
332 | int argc; | |
333 | char **argv; | |
334 | char **qargv; | |
335 | { | |
336 | if (!strcmp(argv[U_MITID], qargv[U_MITID])) | |
337 | duplicate++; | |
338 | else | |
339 | duplicate--; | |
340 | } |