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