9 #include <moira_site.h>
10 EXEC SQL INCLUDE sqlca;
13 #define WHO 11859 /* root */
14 #define PROG "emp-tape"
16 #define MAX_ID_VALUE 31999
17 #define MIN_ID_VALUE 101
40 #define LOC_USERNAME 157
50 #define LEN_USERNAME 30
74 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
75 #define SQL_DUPLICATE -40100
76 #define SQL_DEADLOCK -49900
84 struct entry *e, *get_next_entry();
86 char buf[BUFSIZ], *file = NULL;
88 whoami = rindex(argv[0], '/');
94 for (i = 1; i < argc; i++) {
95 if (!strcmp(argv[i], "-w"))
97 else if (!strcmp(argv[i], "-D"))
98 setenv("ING_SET", "set printqry");
99 else if (!strcmp(argv[i], "-n"))
101 else if (file != NULL)
102 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
107 in = fopen(file, "r");
109 fprintf(stderr, "Unable to open %s for input\n", file);
116 EXEC SQL CONNECT moira;
117 if (sqlca.sqlcode != 0) {
118 com_err(whoami, 0, "ingres error %d opening database", sqlca.sqlcode);
122 while (e = get_next_entry(in)) {
125 EXEC SQL COMMIT WORK;
126 if (sqlca.sqlcode != 0) {
127 if (sqlca.sqlcode == SQL_DEADLOCK) {
128 com_err(whoami, MR_DEADLOCK, "commiting work");
131 com_err(whoami, 0, "ingres error %d committing work", sqlca.sqlcode);
146 char *substr(buf, key)
152 for (l = strlen(key); *buf; buf++)
153 if (!strncmp(buf, key, l))
159 struct entry *get_next_entry(in)
162 static struct entry e;
163 static char buf[BUFSIZ], mid[16], eid[16], email[256];
164 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
165 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
166 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
167 static char host[LEN_HOST+1];
168 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
171 if (fgets(buf, sizeof(buf), in) == NULL)
172 return((struct entry *)NULL);
174 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
175 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
176 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
177 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
178 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
179 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
180 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
181 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
182 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
185 e.name = strtrim(sname);
186 p = index(name, ',');
189 e.last = strtrim(name);
195 if (p = index(e.first, ' ')) {
197 e.first = strtrim(e.first);
198 e.middle = strtrim(p + 1);
200 e.first = strtrim(e.first);
207 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
210 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
212 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
220 EncryptID(e.eid, e.id, e.first, e.last);
222 e.address = strtrim(office);
223 e.phone = strtrim(phone);
224 e.phone2 = strtrim(phone2);
225 e.dept = strtrim(dept);
226 e.title = strtrim(title);
229 if (!strcmp(e.dept, "PROJECT ATHENA"))
231 else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
233 else if (!strcmp(e.title, "VISITING SCIENTIST"))
234 e.class = "VSCIENTI";
236 strcpy(email, strtrim(username));
238 strncat(email, strtrim(host));
248 int changed, nochange, encrypted;
249 char buf[BUFSIZ], *from, *to;
250 EXEC SQL BEGIN DECLARE SECTION;
251 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
252 char *raddr, *rhphone, *rophone, *prog;
253 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
255 EXEC SQL END DECLARE SECTION;
260 if (strlen(first) > 16)
263 if (strlen(last) > 16)
272 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
273 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
275 WHERE clearid = :sid;
277 if (sqlca.sqlcode == SQL_DUPLICATE) {
278 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
280 } else if (sqlca.sqlcode == SQL_DEADLOCK) {
281 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
283 return process_entry(e);
288 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
289 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
291 WHERE last = :last and first = :first and clearid = :eid;
293 if (sqlca.sqlcode == SQL_DEADLOCK) {
294 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
296 return process_entry(e);
297 } else if (sqlca.sqlcode != SQL_DUPLICATE)
307 /* Update class/state if necessary */
308 if (strcmp(e->class, strtrim(class)) &&
309 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
310 com_err(whoami, 0, "updating class for %s %s from %s to %s",
311 first, last, class, e->class);
312 if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
313 status = US_NO_LOGIN_YET;
314 if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
315 status = US_ENROLLED;
316 strcpy(class, e->class);
317 EXEC SQL UPDATE users
318 SET type = :class, status = :status, modtime = 'now',
319 modby = :who, modwith = :prog
320 WHERE users_id = :id;
321 if (sqlca.sqlcode != 0) {
322 if (sqlca.sqlcode == SQL_DEADLOCK) {
323 com_err(whoami, MR_DEADLOCK, "updating user");
326 com_err(whoami, 0, "ingres error %d updating user", sqlca.sqlcode);
332 /* Update name if necessary */
333 if (strcmp(first, strtrim(dfirst)) ||
334 strcmp(last, strtrim(dlast)) ||
335 strcmp(middle, strtrim(dmiddle))) {
336 com_err(whoami, 0, "updating real name for %s %s", first, last);
337 EXEC SQL UPDATE users
338 SET first = :first, last = :last, middle = :middle,
339 modby = :who, modwith = :prog, modtime = 'now'
340 WHERE users_id = :id;
341 if (sqlca.sqlcode != 0) {
342 if (sqlca.sqlcode == SQL_DEADLOCK) {
343 com_err(whoami, MR_DEADLOCK, "updating name");
346 com_err(whoami, 0, "ingres error %d updating name", sqlca.sqlcode);
352 changed = nochange = 0;
353 if (encrypted) changed++;
354 strcpy(buf, e->address);
355 while (to = index(buf, ','))
357 while (to = index(buf, ':'))
360 if (oaddr[0] == ' ' && buf[0]) {
361 strncpy(oaddr, buf, 16);
364 } else if (strncmp(strtrim(oaddr), buf, 15))
367 if (strncmp(strtrim(oaddr), buf, 15))
369 strncpy(oaddr, buf, 16);
381 if (ophone[0] == ' ') {
382 strncpy(ophone, buf, 16);
384 } else if (strncmp(strtrim(ophone), buf, 11))
387 if (strncmp(strtrim(ophone), buf, 11))
389 strncpy(ophone, buf, 16);
395 if (dept[0] == ' ') {
396 strncpy(dept, e->dept, 12);
398 } else if (strncmp(strtrim(dept), e->dept, 11))
401 if (strncmp(strtrim(dept), e->dept, 11))
403 strncpy(dept, e->dept, 12);
414 com_err(whoami, 0, "updating finger for %s %s", first, last);
415 EXEC SQL REPEATED UPDATE users
416 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
417 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
418 xname = :name, xdept = :rdept, xtitle = :rtitle,
419 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
420 xmodtime = 'now', clearid = :sid
421 WHERE users_id = :id;
422 if (sqlca.sqlcode != 0) {
423 if (sqlca.sqlcode == SQL_DEADLOCK) {
424 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
426 return process_entry(e);
428 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
433 EXEC SQL REPEATED UPDATE users
434 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
435 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
436 xmodtime = 'now', clearid = :sid
437 WHERE users_id = :id;
438 if (sqlca.sqlcode != 0) {
439 if (sqlca.sqlcode == SQL_DEADLOCK) {
440 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
442 return process_entry(e);
444 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
456 EXEC SQL BEGIN DECLARE SECTION;
457 int id, uid, st, who;
458 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
459 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
460 char *rdept, *rhphone, *rophone;
461 EXEC SQL END DECLARE SECTION;
465 strncpy(oaddr, e->address, 16);
467 while (to = index(oaddr, ','))
469 while (to = index(oaddr, ':'))
480 strncpy(dept, e->dept, 12);
483 id = set_next_users_id(0);
484 uid = set_next_uid(1);
485 sprintf(login, "#%d", uid);
491 sprintf(fullname, "%s %s %s", first, middle, last);
493 sprintf(fullname, "%s %s", first, last);
495 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
496 st = US_NO_LOGIN_YET;
505 EXEC SQL REPEATED INSERT INTO users
506 (login, users_id, uid, shell, last, first, middle, status,
507 clearid, type, modtime, modby, modwith, fullname, office_addr,
508 office_phone, department, fmodtime, fmodby, fmodwith,
509 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
510 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
511 :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
512 :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
513 :title, :oaddr, :rhphone, :rophone, 'now');
514 if (sqlca.sqlcode != 0) {
515 if (sqlca.sqlcode == SQL_DEADLOCK) {
516 com_err(whoami, MR_DEADLOCK, "adding user");
518 com_err(whoami, 0, "ingres error %d adding user", sqlca.sqlcode);
522 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
526 set_next_users_id(limit)
529 EXEC SQL BEGIN DECLARE SECTION;
530 int rowcount, flag, value, retval;
531 EXEC SQL END DECLARE SECTION;
533 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
534 WHERE name = 'users_id';
535 if (sqlfail()) sqlexit();
536 if (sqlca.sqlerrd[2] != 1) {
538 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
543 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
544 WHERE users_id = :value;
545 if (sqlfail()) sqlexit();
546 if (sqlca.sqlerrd[2] == 0)
550 if (limit && value > MAX_ID_VALUE)
551 value = MIN_ID_VALUE;
553 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
554 WHERE users_id = :value;
555 if (sqlfail()) sqlexit();
556 if (sqlca.sqlerrd[2] == 0)
561 if (limit && value > MAX_ID_VALUE)
562 value = MIN_ID_VALUE;
563 EXEC SQL REPEATED UPDATE numvalues SET value = :value
564 WHERE name = 'users_id';
565 if (sqlca.sqlcode != 0) {
566 com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
575 EXEC SQL BEGIN DECLARE SECTION;
576 int rowcount, flag, value, retval;
577 EXEC SQL END DECLARE SECTION;
579 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
581 if (sqlfail()) sqlexit();
582 if (sqlca.sqlerrd[2] != 1) {
584 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
589 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
590 if (sqlfail()) sqlexit();
591 if (sqlca.sqlerrd[2] == 0)
595 if (limit && value > MAX_ID_VALUE)
596 value = MIN_ID_VALUE;
598 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
599 if (sqlfail()) sqlexit();
600 if (sqlca.sqlerrd[2] == 0)
605 if (limit && value > MAX_ID_VALUE)
606 value = MIN_ID_VALUE;
607 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
608 if (sqlca.sqlcode != 0) {
609 com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
618 if (sqlca.sqlcode == SQL_DEADLOCK)
619 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
622 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
623 EXEC SQL ROLLBACK WORK;