]> andersk Git - moira.git/blame - regtape/employee.dc
update name if necessary
[moira.git] / regtape / employee.dc
CommitLineData
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>
02cd9ede 10EXEC SQL INCLUDE sqlca;
469f80df 11
12
02cd9ede 13#define WHO 11859 /* root */
14#define PROG "emp-tape"
469f80df 15
397e9044 16#define MAX_ID_VALUE 31999
469f80df 17#define MIN_ID_VALUE 101
18
19/* File format is:
20
210-8 id number
229-38 name
2339-62 office address
2463-74 phone1
2575-86 phone2
2687-106 dept
27107-156 title
28157-186 username
29187-241 host
30
31*/
32
33#define LOC_ID 0
34#define LOC_NAME 9
35#define LOC_OFFICE 39
36#define LOC_PHONE 63
37#define LOC_PHONE2 75
38#define LOC_DEPT 87
39#define LOC_TITLE 107
40#define LOC_USERNAME 157
41#define LOC_HOST 187
42
43#define LEN_ID 9
a6e9fead 44#define LEN_NAME 30
45#define LEN_OFFICE 24
469f80df 46#define LEN_PHONE 12
47#define LEN_PHONE2 12
a6e9fead 48#define LEN_DEPT 20
49#define LEN_TITLE 50
50#define LEN_USERNAME 30
469f80df 51#define LEN_HOST 55
52
53
54struct entry {
a6e9fead 55 char *name;
469f80df 56 char *last;
57 char *first;
58 char *middle;
59 char *title;
60 char *class;
61 char *id;
62 char *eid;
63 char *dept;
64 char *address;
65 char *phone;
a6e9fead 66 char *phone2;
469f80df 67 char *email;
68};
69
70
71char *whoami;
72int newfinger = 0;
73
3e77c6a3 74#define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
75#define SQL_DUPLICATE -40100
76
469f80df 77
78main(argc, argv)
79int argc;
80char **argv;
02cd9ede 81{
469f80df 82 FILE *in;
83 struct entry *e, *get_next_entry();
84 int i, wait = 0;
85 char buf[BUFSIZ], *file = NULL;
86
87 whoami = rindex(argv[0], '/');
88 if (whoami)
89 whoami++;
90 else
91 whoami = argv[0];
92
93 for (i = 1; i < argc; i++) {
94 if (!strcmp(argv[i], "-w"))
95 wait++;
96 else if (!strcmp(argv[i], "-D"))
97 setenv("ING_SET", "set printqry");
98 else if (!strcmp(argv[i], "-n"))
99 newfinger++;
100 else if (file != NULL)
325821e4 101 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
469f80df 102 else
103 file = argv[i];
104 }
105
106 in = fopen(file, "r");
107 if (in == NULL) {
108 fprintf(stderr, "Unable to open %s for input\n", file);
109 exit(1);
110 }
111
325821e4 112 setlinebuf(stdout);
113 setlinebuf(stderr);
114
02cd9ede 115 EXEC SQL CONNECT moira;
3e77c6a3 116 if (sqlca.sqlcode != 0) {
117 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
118 exit(1);
119 }
469f80df 120
121 while (e = get_next_entry(in)) {
122 process_entry(e);
3e77c6a3 123 EXEC SQL COMMIT WORK;
124 if (sqlca.sqlcode != 0) {
125 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
126 exit(1);
127 }
469f80df 128 if (wait) {
129 printf("Next");
130 fflush(stdout);
131 gets(buf);
132 }
133 }
134
469f80df 135 exit(0);
02cd9ede 136}
469f80df 137
138
139char *substr(buf, key)
140char *buf;
141char *key;
142{
143 int l;
144
145 for (l = strlen(key); *buf; buf++)
146 if (!strncmp(buf, key, l))
147 return(buf);
148 return(NULL);
149}
150
151
152struct entry *get_next_entry(in)
153FILE *in;
154{
155 static struct entry e;
a6e9fead 156 static char buf[BUFSIZ], mid[16], eid[16], email[256];
157 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
158 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
159 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
160 static char host[LEN_HOST+1];
039f9637 161 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
469f80df 162 char *p;
163
4763be9c 164 if (fgets(buf, sizeof(buf), in) == NULL)
165 return((struct entry *)NULL);
a6e9fead 166
167 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
168 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
169 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
170 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
171 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
172 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
173 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
174 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
175 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
176
177 strcpy(sname, name);
178 e.name = strtrim(sname);
179 p = index(name, ',');
469f80df 180 if (p)
181 *p = 0;
a6e9fead 182 e.last = strtrim(name);
469f80df 183 if (p) {
a6e9fead 184 p++;
185 while (isspace(*p))
186 p++;
187 e.first = p;
469f80df 188 if (p = index(e.first, ' ')) {
189 *p = 0;
a6e9fead 190 e.first = strtrim(e.first);
469f80df 191 e.middle = strtrim(p + 1);
a6e9fead 192 } else {
193 e.first = strtrim(e.first);
194 e.middle = "";
195 }
196 } else {
197 e.first = "";
198 e.middle = "";
199 }
039f9637 200 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
469f80df 201 LookForSt(e.last);
202 LookForO(e.last);
039f9637 203 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
204 &ends_ii, &ends_v);
205 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
206 &ends_ii, &ends_v);
469f80df 207 FixCase(e.last);
208 FixCase(e.first);
209 FixCase(e.middle);
210
a6e9fead 211 e.id = id;
469f80df 212 e.eid = eid;
213 EncryptID(e.eid, e.id, e.first, e.last);
214
a6e9fead 215 e.address = strtrim(office);
216 e.phone = strtrim(phone);
217 e.phone2 = strtrim(phone2);
218 e.dept = strtrim(dept);
219 e.title = strtrim(title);
469f80df 220
221 e.class = "MITS";
222 if (!strcmp(e.dept, "PROJECT ATHENA"))
223 e.class = "STAFF";
e1695a1c 224 else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
469f80df 225 e.class = "FACULTY";
226 else if (!strcmp(e.title, "VISITING SCIENTIST"))
227 e.class = "VSCIENTI";
228
a6e9fead 229 strcpy(email, strtrim(username));
230 if (host[0] == '@')
231 strncat(email, strtrim(host));
232 e.email = email;
469f80df 233
234 return(&e);
235}
236
237
238process_entry(e)
239struct entry *e;
02cd9ede 240{
3e77c6a3 241 int changed, nochange, encrypted;
469f80df 242 char buf[BUFSIZ], *from, *to;
02cd9ede 243 EXEC SQL BEGIN DECLARE SECTION;
3e77c6a3 244 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
245 char *raddr, *rhphone, *rophone, *prog;
246 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
247 int id, status, who;
02cd9ede 248 EXEC SQL END DECLARE SECTION;
469f80df 249
3e77c6a3 250 who = WHO;
251 prog = PROG;
469f80df 252 first = e->first;
9e4455c2 253 if (strlen(first) > 16)
254 first[16] = 0;
469f80df 255 last = e->last;
9e4455c2 256 if (strlen(last) > 16)
257 last[16] = 0;
3e77c6a3 258 middle = e->middle;
469f80df 259 eid = e->eid;
325821e4 260 sid = e->id;
469f80df 261 id = 0;
3e77c6a3 262 encrypted = 0;
325821e4 263
264 /* Get user info */
3e77c6a3 265 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
266 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
02cd9ede 267 FROM users
3e77c6a3 268 WHERE clearid = :sid;
269 if (sqlfail()) {
270 if (sqlca.sqlcode == SQL_DUPLICATE) {
271 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
272 return;
273 } else
274 sqlexit();
275 }
469f80df 276 if (id == 0) {
3e77c6a3 277 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
278 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
02cd9ede 279 FROM users
280 WHERE last = :last and first = :first and clearid = :eid;
3e77c6a3 281 if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE) sqlexit();
282 encrypted++;
325821e4 283 if (id == 0) {
284 newuser(e);
285 return;
286 }
469f80df 287 }
325821e4 288
3e77c6a3 289 /* Update class/state if necessary */
e1695a1c 290 if (strcmp(e->class, strtrim(class)) &&
291 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
469f80df 292 com_err(whoami, 0, "updating class for %s %s from %s to %s",
293 first, last, class, e->class);
a6e9fead 294 if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
295 status = US_NO_LOGIN_YET;
296 if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
297 status = US_ENROLLED;
469f80df 298 strcpy(class, e->class);
02cd9ede 299 EXEC SQL UPDATE users
300 SET type = :class, status = :status, modtime = 'now',
3e77c6a3 301 modby = :who, modwith = :prog
02cd9ede 302 WHERE users_id = :id;
3e77c6a3 303 if (sqlca.sqlcode != 0) {
304 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
305 exit(1);
306 }
307 }
308
309 /* Update name if necessary */
310 if (strcmp(first, strtrim(dfirst)) ||
311 strcmp(last, strtrim(dlast)) ||
312 strcmp(middle, strtrim(dmiddle))) {
313 com_err(whoami, 0, "updating real name for %s %s", first, last);
314 EXEC SQL UPDATE users
315 SET first = :first, last = :last, middle = :middle,
316 modby = :who, modwith = :prog, modtime = 'now'
317 WHERE users_id = :id;
318 if (sqlca.sqlcode != 0) {
319 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
320 exit(1);
321 }
469f80df 322 }
325821e4 323
469f80df 324 changed = nochange = 0;
3e77c6a3 325 if (encrypted) changed++;
469f80df 326 strcpy(buf, e->address);
327 while (to = index(buf, ','))
328 *to = ';';
329 while (to = index(buf, ':'))
330 *to = ';';
331 if (newfinger) {
a6e9fead 332 if (oaddr[0] == ' ' && buf[0]) {
469f80df 333 strncpy(oaddr, buf, 16);
334 oaddr[16] = 0;
335 changed++;
6eb764c5 336 } else if (strncmp(strtrim(oaddr), buf, 15))
469f80df 337 nochange++;
338 } else {
339 if (strncmp(strtrim(oaddr), buf, 15))
340 changed++;
341 strncpy(oaddr, buf, 16);
342 oaddr[16] = 0;
343 }
344 from = e->phone;
345 to = buf;
346 while (*from) {
347 if (isdigit(*from))
348 *to++ = *from;
349 from++;
350 }
351 *to = 0;
352 if (newfinger) {
353 if (ophone[0] == ' ') {
354 strncpy(ophone, buf, 16);
355 ophone[16] = 0;
6eb764c5 356 } else if (strncmp(strtrim(ophone), buf, 11))
469f80df 357 nochange++;
358 } else {
359 if (strncmp(strtrim(ophone), buf, 11))
360 changed++;
361 strncpy(ophone, buf, 16);
362 ophone[16] = 0;
363 }
364 FixCase(e->dept);
a6e9fead 365 FixCase(e->title);
469f80df 366 if (newfinger) {
367 if (dept[0] == ' ') {
368 strncpy(dept, e->dept, 12);
369 dept[12] = 0;
6eb764c5 370 } else if (strncmp(strtrim(dept), e->dept, 11))
469f80df 371 nochange++;
372 } else {
373 if (strncmp(strtrim(dept), e->dept, 11))
374 changed++;
375 strncpy(dept, e->dept, 12);
376 dept[12] = 0;
377 }
325821e4 378 sid = e->id;
379 name = e->name;
380 rdept = e->dept;
381 rtitle = e->title;
382 raddr = e->address;
383 rhphone = e->phone;
384 rophone = e->phone2;
469f80df 385 if (changed) {
386 com_err(whoami, 0, "updating finger for %s %s", first, last);
02cd9ede 387 EXEC SQL REPEATED UPDATE users
3e77c6a3 388 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
389 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
02cd9ede 390 xname = :name, xdept = :rdept, xtitle = :rtitle,
391 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
3e77c6a3 392 xmodtime = 'now', clearid = :sid
02cd9ede 393 WHERE users_id = :id;
3e77c6a3 394 if (sqlca.sqlcode != 0) {
395 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
396 exit(1);
397 }
325821e4 398 } else {
02cd9ede 399 EXEC SQL REPEATED UPDATE users
400 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
401 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
3e77c6a3 402 xmodtime = 'now', clearid = :sid
02cd9ede 403 WHERE users_id = :id;
3e77c6a3 404 if (sqlca.sqlcode != 0) {
405 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
406 exit(1);
407 }
a6e9fead 408 }
02cd9ede 409}
469f80df 410
411
412newuser(e)
413struct entry *e;
02cd9ede 414{
469f80df 415 char *from, *to;
02cd9ede 416 EXEC SQL BEGIN DECLARE SECTION;
417 int id, uid, st, who;
418 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
419 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
420 char *rdept, *rhphone, *rophone;
421 EXEC SQL END DECLARE SECTION;
422
423 who = WHO;
424 prog = PROG;
469f80df 425 strncpy(oaddr, e->address, 16);
a6e9fead 426 oaddr[16] = 0;
469f80df 427 while (to = index(oaddr, ','))
428 *to = ';';
429 while (to = index(oaddr, ':'))
430 *to = ';';
431 from = e->phone;
432 to = ophone;
433 while (*from) {
434 if (isdigit(*from))
435 *to++ = *from;
436 from++;
437 }
438 *to = 0;
a6e9fead 439 FixCase(e->dept);
469f80df 440 strncpy(dept, e->dept, 12);
a6e9fead 441 dept[12] = 0;
469f80df 442
3e77c6a3 443 id = set_next_users_id(0);
444 uid = set_next_uid(1);
469f80df 445 sprintf(login, "#%d", uid);
446 last = e->last;
447 first = e->first;
448 middle = e->middle;
469f80df 449 class = e->class;
450 if (*middle)
451 sprintf(fullname, "%s %s %s", first, middle, last);
452 else
453 sprintf(fullname, "%s %s", first, last);
454 st = US_NOT_ALLOWED;
e1695a1c 455 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
469f80df 456 st = US_NO_LOGIN_YET;
325821e4 457
458 sid = e->id;
459 name = e->name;
460 rdept = e->dept;
461 title = e->title;
462 rhphone = e->phone;
463 rophone = e->phone2;
464
3e77c6a3 465 EXEC SQL REPEATED INSERT INTO users
02cd9ede 466 (login, users_id, uid, shell, last, first, middle, status,
467 clearid, type, modtime, modby, modwith, fullname, office_addr,
468 office_phone, department, fmodtime, fmodby, fmodwith,
469 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
470 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
471 :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
472 :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
473 :title, :oaddr, :rhphone, :rophone, 'now');
3e77c6a3 474 if (sqlca.sqlcode != 0) {
475 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
476 exit(1);
477 } else
478 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
02cd9ede 479}
469f80df 480
481
3e77c6a3 482set_next_users_id(limit)
27736587 483 int limit;
02cd9ede 484{
485 EXEC SQL BEGIN DECLARE SECTION;
3e77c6a3 486 int rowcount, flag, value, retval;
487 EXEC SQL END DECLARE SECTION;
488
489 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
490 WHERE name = 'users_id';
491 if (sqlfail()) sqlexit();
492 if (sqlca.sqlerrd[2] != 1) {
493 EXEC SQL ROLLBACK;
494 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
495 exit(1);
496 }
497
498 flag = 0;
499 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
500 WHERE users_id = :value;
501 if (sqlfail()) sqlexit();
502 if (sqlca.sqlerrd[2] == 0)
503 flag = 0;
504 while (flag) {
505 value++;
506 if (limit && value > MAX_ID_VALUE)
507 value = MIN_ID_VALUE;
508 flag = 0;
509 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
510 WHERE users_id = :value;
511 if (sqlfail()) sqlexit();
512 if (sqlca.sqlerrd[2] == 0)
513 flag = 0;
514 }
515
516 retval = value++;
517 if (limit && value > MAX_ID_VALUE)
518 value = MIN_ID_VALUE;
519 EXEC SQL REPEATED UPDATE numvalues SET value = :value
520 WHERE name = 'users_id';
521 if (sqlca.sqlcode != 0) {
522 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
523 exit(1);
524 }
525 return(retval);
526}
527
528set_next_uid(limit)
529 int limit;
530{
531 EXEC SQL BEGIN DECLARE SECTION;
532 int rowcount, flag, value, retval;
02cd9ede 533 EXEC SQL END DECLARE SECTION;
469f80df 534
3e77c6a3 535 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
536 WHERE name = 'uid';
537 if (sqlfail()) sqlexit();
02cd9ede 538 if (sqlca.sqlerrd[2] != 1) {
539 EXEC SQL ROLLBACK;
540 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
541 exit(1);
469f80df 542 }
543
02cd9ede 544 flag = 0;
3e77c6a3 545 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
546 if (sqlfail()) sqlexit();
02cd9ede 547 if (sqlca.sqlerrd[2] == 0)
548 flag = 0;
549 while (flag) {
469f80df 550 value++;
27736587 551 if (limit && value > MAX_ID_VALUE)
469f80df 552 value = MIN_ID_VALUE;
02cd9ede 553 flag = 0;
3e77c6a3 554 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
555 if (sqlfail()) sqlexit();
02cd9ede 556 if (sqlca.sqlerrd[2] == 0)
557 flag = 0;
469f80df 558 }
559
3e77c6a3 560 retval = value++;
02cd9ede 561 if (limit && value > MAX_ID_VALUE)
562 value = MIN_ID_VALUE;
3e77c6a3 563 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
564 if (sqlca.sqlcode != 0) {
565 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
566 exit(1);
567 }
568 return(retval);
569}
570
571
572sqlexit()
573{
574 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
575 EXEC SQL ROLLBACK WORK;
576 exit(1);
02cd9ede 577}
This page took 0.152976 seconds and 5 git commands to generate.