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