]>
Commit | Line | Data |
---|---|---|
469f80df | 1 | /* $Header$ |
2 | */ | |
3 | ||
4 | #include <stdio.h> | |
5 | #include <strings.h> | |
6 | #include <ctype.h> | |
7 | #include <sys/time.h> | |
932028de | 8 | #include <moira.h> |
9 | #include <moira_site.h> | |
469f80df | 10 | |
11 | ||
12 | ##define WHO 11859 /* root */ | |
13 | ##define PROG "emp-tape" | |
14 | ||
15 | #define MAX_ID_VALUE 32766 | |
16 | #define MIN_ID_VALUE 101 | |
17 | ||
18 | /* File format is: | |
19 | ||
20 | 0-8 id number | |
21 | 9-38 name | |
22 | 39-62 office address | |
23 | 63-74 phone1 | |
24 | 75-86 phone2 | |
25 | 87-106 dept | |
26 | 107-156 title | |
27 | 157-186 username | |
28 | 187-241 host | |
29 | ||
30 | */ | |
31 | ||
32 | #define LOC_ID 0 | |
33 | #define LOC_NAME 9 | |
34 | #define LOC_OFFICE 39 | |
35 | #define LOC_PHONE 63 | |
36 | #define LOC_PHONE2 75 | |
37 | #define LOC_DEPT 87 | |
38 | #define LOC_TITLE 107 | |
39 | #define LOC_USERNAME 157 | |
40 | #define LOC_HOST 187 | |
41 | ||
42 | #define LEN_ID 9 | |
a6e9fead | 43 | #define LEN_NAME 30 |
44 | #define LEN_OFFICE 24 | |
469f80df | 45 | #define LEN_PHONE 12 |
46 | #define LEN_PHONE2 12 | |
a6e9fead | 47 | #define LEN_DEPT 20 |
48 | #define LEN_TITLE 50 | |
49 | #define LEN_USERNAME 30 | |
469f80df | 50 | #define LEN_HOST 55 |
51 | ||
52 | ||
53 | struct entry { | |
a6e9fead | 54 | char *name; |
469f80df | 55 | char *last; |
56 | char *first; | |
57 | char *middle; | |
58 | char *title; | |
59 | char *class; | |
60 | char *id; | |
61 | char *eid; | |
62 | char *dept; | |
63 | char *address; | |
64 | char *phone; | |
a6e9fead | 65 | char *phone2; |
469f80df | 66 | char *email; |
67 | }; | |
68 | ||
69 | ||
70 | char *whoami; | |
71 | int newfinger = 0; | |
a6e9fead | 72 | int addxuser = 0; |
469f80df | 73 | |
74 | ||
75 | main(argc, argv) | |
76 | int argc; | |
77 | char **argv; | |
78 | ##{ | |
79 | FILE *in; | |
80 | struct entry *e, *get_next_entry(); | |
81 | int i, wait = 0; | |
82 | char buf[BUFSIZ], *file = NULL; | |
83 | ||
84 | whoami = rindex(argv[0], '/'); | |
85 | if (whoami) | |
86 | whoami++; | |
87 | else | |
88 | whoami = argv[0]; | |
89 | ||
90 | for (i = 1; i < argc; i++) { | |
91 | if (!strcmp(argv[i], "-w")) | |
92 | wait++; | |
93 | else if (!strcmp(argv[i], "-D")) | |
94 | setenv("ING_SET", "set printqry"); | |
95 | else if (!strcmp(argv[i], "-n")) | |
96 | newfinger++; | |
a6e9fead | 97 | else if (!strcmp(argv[i], "-u")) |
98 | addxuser++; | |
469f80df | 99 | else if (file != NULL) |
a6e9fead | 100 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] [-u] inputfile\n", whoami); |
469f80df | 101 | else |
102 | file = argv[i]; | |
103 | } | |
104 | ||
105 | in = fopen(file, "r"); | |
106 | if (in == NULL) { | |
107 | fprintf(stderr, "Unable to open %s for input\n", file); | |
108 | exit(1); | |
109 | } | |
110 | ||
111 | ## ingres sms | |
112 | ## range of u is users | |
113 | ||
114 | while (e = get_next_entry(in)) { | |
115 | process_entry(e); | |
116 | if (wait) { | |
117 | printf("Next"); | |
118 | fflush(stdout); | |
119 | gets(buf); | |
120 | } | |
121 | } | |
122 | ||
123 | ## exit | |
124 | exit(0); | |
125 | ##} | |
126 | ||
127 | ||
128 | char *substr(buf, key) | |
129 | char *buf; | |
130 | char *key; | |
131 | { | |
132 | int l; | |
133 | ||
134 | for (l = strlen(key); *buf; buf++) | |
135 | if (!strncmp(buf, key, l)) | |
136 | return(buf); | |
137 | return(NULL); | |
138 | } | |
139 | ||
140 | ||
141 | struct entry *get_next_entry(in) | |
142 | FILE *in; | |
143 | { | |
144 | static struct entry e; | |
a6e9fead | 145 | static char buf[BUFSIZ], mid[16], eid[16], email[256]; |
146 | static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1]; | |
147 | static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1]; | |
148 | static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1]; | |
149 | static char host[LEN_HOST+1]; | |
4763be9c | 150 | int ends_sr, ends_jr, ends_iii, ends_iv; |
469f80df | 151 | char *p; |
152 | ||
4763be9c | 153 | if (fgets(buf, sizeof(buf), in) == NULL) |
154 | return((struct entry *)NULL); | |
a6e9fead | 155 | |
156 | strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0; | |
157 | strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0; | |
158 | strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0; | |
159 | strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0; | |
160 | strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0; | |
161 | strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0; | |
162 | strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0; | |
163 | strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0; | |
164 | strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0; | |
165 | ||
166 | strcpy(sname, name); | |
167 | e.name = strtrim(sname); | |
168 | p = index(name, ','); | |
469f80df | 169 | if (p) |
170 | *p = 0; | |
a6e9fead | 171 | e.last = strtrim(name); |
469f80df | 172 | if (p) { |
a6e9fead | 173 | p++; |
174 | while (isspace(*p)) | |
175 | p++; | |
176 | e.first = p; | |
469f80df | 177 | if (p = index(e.first, ' ')) { |
178 | *p = 0; | |
a6e9fead | 179 | e.first = strtrim(e.first); |
469f80df | 180 | e.middle = strtrim(p + 1); |
a6e9fead | 181 | } else { |
182 | e.first = strtrim(e.first); | |
183 | e.middle = ""; | |
184 | } | |
185 | } else { | |
186 | e.first = ""; | |
187 | e.middle = ""; | |
188 | } | |
4763be9c | 189 | ends_sr = ends_jr = ends_iii = ends_iv = 0; |
469f80df | 190 | LookForSt(e.last); |
191 | LookForO(e.last); | |
4763be9c | 192 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv); |
193 | LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv); | |
469f80df | 194 | FixCase(e.last); |
195 | FixCase(e.first); | |
196 | FixCase(e.middle); | |
197 | ||
a6e9fead | 198 | e.id = id; |
469f80df | 199 | e.eid = eid; |
200 | EncryptID(e.eid, e.id, e.first, e.last); | |
201 | ||
a6e9fead | 202 | e.address = strtrim(office); |
203 | e.phone = strtrim(phone); | |
204 | e.phone2 = strtrim(phone2); | |
205 | e.dept = strtrim(dept); | |
206 | e.title = strtrim(title); | |
469f80df | 207 | |
208 | e.class = "MITS"; | |
209 | if (!strcmp(e.dept, "PROJECT ATHENA")) | |
210 | e.class = "STAFF"; | |
e1695a1c | 211 | else if (substr(e.title, "PROF") || substr(e.title, "LECTURE")) |
469f80df | 212 | e.class = "FACULTY"; |
213 | else if (!strcmp(e.title, "VISITING SCIENTIST")) | |
214 | e.class = "VSCIENTI"; | |
215 | ||
a6e9fead | 216 | strcpy(email, strtrim(username)); |
217 | if (host[0] == '@') | |
218 | strncat(email, strtrim(host)); | |
219 | e.email = email; | |
469f80df | 220 | |
221 | return(&e); | |
222 | } | |
223 | ||
224 | ||
225 | process_entry(e) | |
226 | struct entry *e; | |
227 | ##{ | |
228 | int changed, nochange; | |
229 | char buf[BUFSIZ], *from, *to; | |
a6e9fead | 230 | ## char *first, *last, *eid, *sid, *name, *title, *phone2; |
231 | ## char class[9], oaddr[25], ophone[17], dept[128]; | |
232 | ## int id, status; | |
469f80df | 233 | |
234 | first = e->first; | |
9e4455c2 | 235 | if (strlen(first) > 16) |
236 | first[16] = 0; | |
469f80df | 237 | last = e->last; |
9e4455c2 | 238 | if (strlen(last) > 16) |
239 | last[16] = 0; | |
469f80df | 240 | eid = e->eid; |
241 | id = 0; | |
242 | ## repeat retrieve (id = u.users_id, class = u.mit_year, oaddr = u.office_addr, | |
a6e9fead | 243 | ## ophone = u.office_phone, dept = u.mit_dept, status = u.#status) |
469f80df | 244 | ## where u.#last = @last and u.#first = @first and u.mit_id = @eid |
245 | if (id == 0) { | |
246 | newuser(e); | |
247 | return; | |
248 | } | |
e1695a1c | 249 | if (strcmp(e->class, strtrim(class)) && |
250 | strcmp(class, "STAFF") && strcmp(class, "SIPB")) { | |
469f80df | 251 | com_err(whoami, 0, "updating class for %s %s from %s to %s", |
252 | first, last, class, e->class); | |
a6e9fead | 253 | if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY")) |
254 | status = US_NO_LOGIN_YET; | |
255 | if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY")) | |
256 | status = US_ENROLLED; | |
469f80df | 257 | strcpy(class, e->class); |
a6e9fead | 258 | ## repeat replace u (mit_year = @class, #status = @status, ugdefault = 1, |
469f80df | 259 | ## modtime = "now", modby = WHO, modwith = PROG) |
4763be9c | 260 | ## where u.users_id = @id |
469f80df | 261 | } |
262 | changed = nochange = 0; | |
263 | strcpy(buf, e->address); | |
264 | while (to = index(buf, ',')) | |
265 | *to = ';'; | |
266 | while (to = index(buf, ':')) | |
267 | *to = ';'; | |
268 | if (newfinger) { | |
a6e9fead | 269 | if (oaddr[0] == ' ' && buf[0]) { |
469f80df | 270 | strncpy(oaddr, buf, 16); |
271 | oaddr[16] = 0; | |
272 | changed++; | |
6eb764c5 | 273 | } else if (strncmp(strtrim(oaddr), buf, 15)) |
469f80df | 274 | nochange++; |
275 | } else { | |
276 | if (strncmp(strtrim(oaddr), buf, 15)) | |
277 | changed++; | |
278 | strncpy(oaddr, buf, 16); | |
279 | oaddr[16] = 0; | |
280 | } | |
281 | from = e->phone; | |
282 | to = buf; | |
283 | while (*from) { | |
284 | if (isdigit(*from)) | |
285 | *to++ = *from; | |
286 | from++; | |
287 | } | |
288 | *to = 0; | |
289 | if (newfinger) { | |
290 | if (ophone[0] == ' ') { | |
291 | strncpy(ophone, buf, 16); | |
292 | ophone[16] = 0; | |
6eb764c5 | 293 | } else if (strncmp(strtrim(ophone), buf, 11)) |
469f80df | 294 | nochange++; |
295 | } else { | |
296 | if (strncmp(strtrim(ophone), buf, 11)) | |
297 | changed++; | |
298 | strncpy(ophone, buf, 16); | |
299 | ophone[16] = 0; | |
300 | } | |
301 | FixCase(e->dept); | |
a6e9fead | 302 | FixCase(e->title); |
469f80df | 303 | if (newfinger) { |
304 | if (dept[0] == ' ') { | |
305 | strncpy(dept, e->dept, 12); | |
306 | dept[12] = 0; | |
6eb764c5 | 307 | } else if (strncmp(strtrim(dept), e->dept, 11)) |
469f80df | 308 | nochange++; |
309 | } else { | |
310 | if (strncmp(strtrim(dept), e->dept, 11)) | |
311 | changed++; | |
312 | strncpy(dept, e->dept, 12); | |
313 | dept[12] = 0; | |
314 | } | |
315 | if (changed) { | |
316 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
a6e9fead | 317 | ## repeat replace u (office_addr = @oaddr, ugdefault = 1, |
469f80df | 318 | ## office_phone = @ophone, #mit_dept = @dept, |
319 | ## fmodtime = "now", fmodby = WHO, fmodwith = PROG) | |
320 | ## where u.users_id = @id | |
a6e9fead | 321 | } /* else if (nochange) |
469f80df | 322 | com_err(whoami, 0, "NOT updating finger for %s %s", first, last); |
a6e9fead | 323 | */ |
324 | if (!changed) { | |
325 | ## repeat replace u (ugdefault = 1) where u.users_id = @id | |
326 | } | |
327 | if (addxuser) { | |
328 | sid = e->id; | |
329 | name = e->name; | |
330 | strcpy(dept, e->dept); | |
331 | title = e->title; | |
332 | strcpy(oaddr, e->address); | |
333 | phone2 = e->phone2; | |
334 | ||
335 | status = 1; | |
336 | ## repeat retrieve (status = any(xuser.users_id | |
337 | ## where xuser.users_id = @id)) | |
338 | if (!status) { | |
339 | ## append xuser (users_id = id, #id = sid, #name = name, #dept = dept, | |
340 | ## #title = title, address = oaddr, #phone1 = ophone, | |
341 | ## #phone2 = phone2, modtime = "now") | |
342 | } | |
343 | } | |
469f80df | 344 | ##} |
345 | ||
346 | ||
347 | newuser(e) | |
348 | struct entry *e; | |
349 | ##{ | |
350 | char *from, *to; | |
351 | ## int id, uid, st; | |
352 | ## char *last, *first, *class, *middle, login[9], *eid, fullname[65]; | |
a6e9fead | 353 | ## char oaddr[81], ophone[17], dept[128], *sid, *name, *title, phone2[17]; |
469f80df | 354 | |
355 | ||
356 | strncpy(oaddr, e->address, 16); | |
a6e9fead | 357 | oaddr[16] = 0; |
469f80df | 358 | while (to = index(oaddr, ',')) |
359 | *to = ';'; | |
360 | while (to = index(oaddr, ':')) | |
361 | *to = ';'; | |
362 | from = e->phone; | |
363 | to = ophone; | |
364 | while (*from) { | |
365 | if (isdigit(*from)) | |
366 | *to++ = *from; | |
367 | from++; | |
368 | } | |
369 | *to = 0; | |
a6e9fead | 370 | FixCase(e->dept); |
469f80df | 371 | strncpy(dept, e->dept, 12); |
a6e9fead | 372 | dept[12] = 0; |
469f80df | 373 | |
27736587 | 374 | id = set_next_object_id("users_id", 0); |
375 | uid = set_next_object_id("uid", 1); | |
469f80df | 376 | sprintf(login, "#%d", uid); |
377 | last = e->last; | |
378 | first = e->first; | |
379 | middle = e->middle; | |
380 | eid = e->eid; | |
381 | class = e->class; | |
382 | if (*middle) | |
383 | sprintf(fullname, "%s %s %s", first, middle, last); | |
384 | else | |
385 | sprintf(fullname, "%s %s", first, last); | |
386 | st = US_NOT_ALLOWED; | |
e1695a1c | 387 | if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF")) |
469f80df | 388 | st = US_NO_LOGIN_YET; |
389 | ||
390 | ## append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh", | |
391 | ## #last = last, #first = first, #middle = middle, status = st, | |
a6e9fead | 392 | ## #mit_id = eid, #mit_year = class, ugdefault = 1, |
469f80df | 393 | ## modtime = "now", modby = WHO, modwith = PROG, |
394 | ## #fullname = fullname, office_addr = oaddr, | |
395 | ## office_phone = ophone, #mit_dept = dept, | |
396 | ## fmodtime = "now", fmodby = WHO, fmodwith = PROG, | |
397 | ## potype = "NONE") | |
a6e9fead | 398 | |
399 | sid = e->id; | |
400 | name = e->name; | |
401 | strcpy(dept, e->dept); | |
402 | title = e->title; | |
403 | FixCase(title); | |
404 | strcpy(oaddr, e->address); | |
405 | FixCase(oaddr); | |
406 | from = e->phone2; | |
407 | to = phone2; | |
408 | while (*from) { | |
409 | if (isdigit(*from)) | |
410 | *to++ = *from; | |
411 | from++; | |
412 | } | |
413 | *to = 0; | |
414 | ||
415 | if (addxuser) { | |
416 | ## append xuser (users_id = id, #id = sid, #name = fullname, #dept = dept, | |
417 | ## #title = title, address = oaddr, #phone1 = ophone, | |
418 | ## #phone2 = phone2, modtime = "now") | |
419 | } | |
469f80df | 420 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); |
421 | ##} | |
422 | ||
423 | ||
27736587 | 424 | set_next_object_id(object, limit) |
469f80df | 425 | char *object; |
27736587 | 426 | int limit; |
469f80df | 427 | ##{ |
428 | ## char *name; | |
429 | ## int rowcount, exists, value; | |
430 | ||
431 | name = object; | |
432 | ## begin transaction | |
433 | ## repeat retrieve (value = values.#value) where values.#name = @name | |
434 | ## inquire_equel(rowcount = "rowcount") | |
435 | if (rowcount != 1) { | |
436 | ## abort | |
437 | return(0); | |
438 | } | |
439 | ||
440 | ## retrieve (exists = any(users.name where users.name = value)) | |
441 | ## inquire_equel(rowcount = "rowcount") | |
442 | if (rowcount != 1) { | |
443 | ## abort | |
444 | return(0); | |
445 | } | |
446 | while (exists) { | |
447 | value++; | |
27736587 | 448 | if (limit && value > MAX_ID_VALUE) |
469f80df | 449 | value = MIN_ID_VALUE; |
450 | ## retrieve (exists = any(users.name where users.name = value)) | |
451 | } | |
452 | ||
453 | ## repeat replace values (#value = @value) where values.#name = @name | |
454 | ## end transaction | |
455 | return(value); | |
456 | ##} |