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);
115 initialize_sms_error_table();
117 EXEC SQL CONNECT moira;
118 if (sqlca.sqlcode != 0) {
119 com_err(whoami, 0, "ingres error %d opening database", sqlca.sqlcode);
123 while (e = get_next_entry(in)) {
126 EXEC SQL COMMIT WORK;
127 if (sqlca.sqlcode != 0) {
128 if (sqlca.sqlcode == SQL_DEADLOCK) {
129 com_err(whoami, MR_DEADLOCK, "commiting work");
132 com_err(whoami, 0, "ingres error %d committing work", sqlca.sqlcode);
147 char *substr(buf, key)
153 for (l = strlen(key); *buf; buf++)
154 if (!strncmp(buf, key, l))
160 struct entry *get_next_entry(in)
163 static struct entry e;
164 static char buf[BUFSIZ], mid[16], eid[16], email[256];
165 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
166 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
167 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
168 static char host[LEN_HOST+1];
169 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
172 if (fgets(buf, sizeof(buf), in) == NULL)
173 return((struct entry *)NULL);
175 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
176 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
177 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
178 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
179 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
180 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
181 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
182 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
183 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
186 e.name = strtrim(sname);
187 p = index(name, ',');
190 e.last = strtrim(name);
196 if (p = index(e.first, ' ')) {
198 e.first = strtrim(e.first);
199 e.middle = strtrim(p + 1);
201 e.first = strtrim(e.first);
208 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
211 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
213 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
221 EncryptID(e.eid, e.id, e.first, e.last);
223 e.address = strtrim(office);
224 e.phone = strtrim(phone);
225 e.phone2 = strtrim(phone2);
226 e.dept = strtrim(dept);
227 e.title = strtrim(title);
230 if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
233 strcpy(email, strtrim(username));
235 strncat(email, strtrim(host));
245 int changed, nochange, encrypted;
246 char buf[BUFSIZ], *from, *to;
247 EXEC SQL BEGIN DECLARE SECTION;
248 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
249 char *raddr, *rhphone, *rophone, *prog;
250 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
252 EXEC SQL END DECLARE SECTION;
254 /* Don't process Lincoln Labs */
255 if (!strncmp(e->address, "LL", 2))
261 if (strlen(first) > 16)
264 if (strlen(last) > 16)
273 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
274 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
276 WHERE clearid = :sid;
278 if (sqlca.sqlcode == SQL_DUPLICATE) {
279 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
281 } else if (sqlca.sqlcode == SQL_DEADLOCK) {
282 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
284 return process_entry(e);
289 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
290 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
292 WHERE last = :last and first = :first and clearid = :eid;
294 if (sqlca.sqlcode == SQL_DEADLOCK) {
295 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
297 return process_entry(e);
298 } else if (sqlca.sqlcode != SQL_DUPLICATE)
308 /* Update class/state if necessary. (Exclude several spacial cases.) */
309 if (strcmp(e->class, strtrim(class)) &&
310 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
311 strcmp(class, "KNIGHT")) {
312 com_err(whoami, 0, "updating class for %s %s from %s to %s",
313 first, last, class, e->class);
314 if (status == US_NOT_ALLOWED)
315 status = US_NO_LOGIN_YET;
316 if (status == US_ENROLL_NOT_ALLOWED)
317 status = US_ENROLLED;
318 strcpy(class, e->class);
319 EXEC SQL UPDATE users
320 SET type = :class, status = :status, modtime = 'now',
321 modby = :who, modwith = :prog
322 WHERE users_id = :id;
323 if (sqlca.sqlcode != 0) {
324 if (sqlca.sqlcode == SQL_DEADLOCK) {
325 com_err(whoami, MR_DEADLOCK, "updating user");
328 com_err(whoami, 0, "ingres error %d updating user", sqlca.sqlcode);
334 /* Update name if necessary */
335 if (strcmp(first, strtrim(dfirst)) ||
336 strcmp(last, strtrim(dlast)) ||
337 strcmp(middle, strtrim(dmiddle))) {
338 com_err(whoami, 0, "updating real name for %s %s", first, last);
339 EXEC SQL UPDATE users
340 SET first = :first, last = :last, middle = :middle,
341 modby = :who, modwith = :prog, modtime = 'now'
342 WHERE users_id = :id;
343 if (sqlca.sqlcode != 0) {
344 if (sqlca.sqlcode == SQL_DEADLOCK) {
345 com_err(whoami, MR_DEADLOCK, "updating name");
348 com_err(whoami, 0, "ingres error %d updating name", sqlca.sqlcode);
354 changed = nochange = 0;
355 if (encrypted) changed++;
356 strcpy(buf, e->address);
357 while (to = index(buf, ','))
359 while (to = index(buf, ':'))
362 if (oaddr[0] == ' ' && buf[0]) {
363 strncpy(oaddr, buf, 16);
366 } else if (strncmp(strtrim(oaddr), buf, 15))
369 if (strncmp(strtrim(oaddr), buf, 15))
371 strncpy(oaddr, buf, 16);
383 if (ophone[0] == ' ') {
384 strncpy(ophone, buf, 16);
386 } else if (strncmp(strtrim(ophone), buf, 11))
389 if (strncmp(strtrim(ophone), buf, 11))
391 strncpy(ophone, buf, 16);
397 if (dept[0] == ' ') {
398 strncpy(dept, e->dept, 12);
400 } else if (strncmp(strtrim(dept), e->dept, 11))
403 if (strncmp(strtrim(dept), e->dept, 11))
405 strncpy(dept, e->dept, 12);
416 com_err(whoami, 0, "updating finger for %s %s", first, last);
417 EXEC SQL REPEATED UPDATE users
418 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
419 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
420 xname = :name, xdept = :rdept, xtitle = :rtitle,
421 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
422 xmodtime = 'now', clearid = :sid
423 WHERE users_id = :id;
424 if (sqlca.sqlcode != 0) {
425 if (sqlca.sqlcode == SQL_DEADLOCK) {
426 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
428 return process_entry(e);
430 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
435 EXEC SQL REPEATED UPDATE users
436 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
437 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
438 xmodtime = 'now', clearid = :sid
439 WHERE users_id = :id;
440 if (sqlca.sqlcode != 0) {
441 if (sqlca.sqlcode == SQL_DEADLOCK) {
442 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
444 return process_entry(e);
446 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
458 EXEC SQL BEGIN DECLARE SECTION;
459 int id, uid, st, who;
460 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
461 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
462 char *rdept, *rhphone, *rophone;
463 EXEC SQL END DECLARE SECTION;
467 strncpy(oaddr, e->address, 16);
469 while (to = index(oaddr, ','))
471 while (to = index(oaddr, ':'))
482 strncpy(dept, e->dept, 12);
485 id = set_next_users_id(0);
486 uid = set_next_uid(1);
487 sprintf(login, "#%d", uid);
493 sprintf(fullname, "%s %s %s", first, middle, last);
495 sprintf(fullname, "%s %s", first, last);
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/athena/tcsh', :last, :first,
511 :middle, :st, :sid, :class, 'now', :who, :prog,
512 :fullname, :oaddr, :ophone, :dept, 'now', :who, :prog,
513 'NONE', :name, :rdept, :title, :oaddr, :rhphone,
515 if (sqlca.sqlcode != 0) {
516 if (sqlca.sqlcode == SQL_DEADLOCK) {
517 com_err(whoami, MR_DEADLOCK, "adding user");
519 com_err(whoami, 0, "ingres error %d adding user", sqlca.sqlcode);
523 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
527 set_next_users_id(limit)
530 EXEC SQL BEGIN DECLARE SECTION;
531 int rowcount, flag, value, retval;
532 EXEC SQL END DECLARE SECTION;
534 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
535 WHERE name = 'users_id';
536 if (sqlfail()) sqlexit();
537 if (sqlca.sqlerrd[2] != 1) {
539 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
544 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
545 WHERE users_id = :value;
546 if (sqlfail()) sqlexit();
547 if (sqlca.sqlerrd[2] == 0)
551 if (limit && value > MAX_ID_VALUE)
552 value = MIN_ID_VALUE;
554 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
555 WHERE users_id = :value;
556 if (sqlfail()) sqlexit();
557 if (sqlca.sqlerrd[2] == 0)
562 if (limit && value > MAX_ID_VALUE)
563 value = MIN_ID_VALUE;
564 EXEC SQL REPEATED UPDATE numvalues SET value = :value
565 WHERE name = 'users_id';
566 if (sqlca.sqlcode != 0) {
567 com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
576 EXEC SQL BEGIN DECLARE SECTION;
577 int rowcount, flag, value, retval;
578 EXEC SQL END DECLARE SECTION;
580 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
582 if (sqlfail()) sqlexit();
583 if (sqlca.sqlerrd[2] != 1) {
585 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
590 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
591 if (sqlfail()) sqlexit();
592 if (sqlca.sqlerrd[2] == 0)
596 if (limit && value > MAX_ID_VALUE)
597 value = MIN_ID_VALUE;
599 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
600 if (sqlfail()) sqlexit();
601 if (sqlca.sqlerrd[2] == 0)
606 if (limit && value > MAX_ID_VALUE)
607 value = MIN_ID_VALUE;
608 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
609 if (sqlca.sqlcode != 0) {
610 com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
619 if (sqlca.sqlcode == SQL_DEADLOCK)
620 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
623 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
624 EXEC SQL ROLLBACK WORK;