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", sqlca.sqlcode);
122 while (e = get_next_entry(in)) {
124 EXEC SQL COMMIT WORK;
125 if (sqlca.sqlcode != 0) {
126 if (sqlca.sqlcode == SQL_DEADLOCK) {
127 com_err(whoami, MR_DEADLOCK, "commiting work");
130 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
145 char *substr(buf, key)
151 for (l = strlen(key); *buf; buf++)
152 if (!strncmp(buf, key, l))
158 struct entry *get_next_entry(in)
161 static struct entry e;
162 static char buf[BUFSIZ], mid[16], eid[16], email[256];
163 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
164 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
165 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
166 static char host[LEN_HOST+1];
167 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
170 if (fgets(buf, sizeof(buf), in) == NULL)
171 return((struct entry *)NULL);
173 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
174 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
175 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
176 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
177 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
178 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
179 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
180 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
181 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
184 e.name = strtrim(sname);
185 p = index(name, ',');
188 e.last = strtrim(name);
194 if (p = index(e.first, ' ')) {
196 e.first = strtrim(e.first);
197 e.middle = strtrim(p + 1);
199 e.first = strtrim(e.first);
206 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
209 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
211 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
219 EncryptID(e.eid, e.id, e.first, e.last);
221 e.address = strtrim(office);
222 e.phone = strtrim(phone);
223 e.phone2 = strtrim(phone2);
224 e.dept = strtrim(dept);
225 e.title = strtrim(title);
228 if (!strcmp(e.dept, "PROJECT ATHENA"))
230 else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
232 else if (!strcmp(e.title, "VISITING SCIENTIST"))
233 e.class = "VSCIENTI";
235 strcpy(email, strtrim(username));
237 strncat(email, strtrim(host));
247 int changed, nochange, encrypted;
248 char buf[BUFSIZ], *from, *to;
249 EXEC SQL BEGIN DECLARE SECTION;
250 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
251 char *raddr, *rhphone, *rophone, *prog;
252 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
254 EXEC SQL END DECLARE SECTION;
259 if (strlen(first) > 16)
262 if (strlen(last) > 16)
271 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
272 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
274 WHERE clearid = :sid;
276 if (sqlca.sqlcode == SQL_DUPLICATE) {
277 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
279 } else if (sqlca.sqlcode == SQL_DEADLOCK) {
280 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
282 return process_entry(e);
287 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
288 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
290 WHERE last = :last and first = :first and clearid = :eid;
292 if (sqlca.sqlcode == SQL_DEADLOCK) {
293 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
295 return process_entry(e);
296 } else if (sqlca.sqlcode != SQL_DUPLICATE)
306 /* Update class/state if necessary */
307 if (strcmp(e->class, strtrim(class)) &&
308 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
309 com_err(whoami, 0, "updating class for %s %s from %s to %s",
310 first, last, class, e->class);
311 if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
312 status = US_NO_LOGIN_YET;
313 if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
314 status = US_ENROLLED;
315 strcpy(class, e->class);
316 EXEC SQL UPDATE users
317 SET type = :class, status = :status, modtime = 'now',
318 modby = :who, modwith = :prog
319 WHERE users_id = :id;
320 if (sqlca.sqlcode != 0) {
321 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
326 /* Update name if necessary */
327 if (strcmp(first, strtrim(dfirst)) ||
328 strcmp(last, strtrim(dlast)) ||
329 strcmp(middle, strtrim(dmiddle))) {
330 com_err(whoami, 0, "updating real name for %s %s", first, last);
331 EXEC SQL UPDATE users
332 SET first = :first, last = :last, middle = :middle,
333 modby = :who, modwith = :prog, modtime = 'now'
334 WHERE users_id = :id;
335 if (sqlca.sqlcode != 0) {
336 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
341 changed = nochange = 0;
342 if (encrypted) changed++;
343 strcpy(buf, e->address);
344 while (to = index(buf, ','))
346 while (to = index(buf, ':'))
349 if (oaddr[0] == ' ' && buf[0]) {
350 strncpy(oaddr, buf, 16);
353 } else if (strncmp(strtrim(oaddr), buf, 15))
356 if (strncmp(strtrim(oaddr), buf, 15))
358 strncpy(oaddr, buf, 16);
370 if (ophone[0] == ' ') {
371 strncpy(ophone, buf, 16);
373 } else if (strncmp(strtrim(ophone), buf, 11))
376 if (strncmp(strtrim(ophone), buf, 11))
378 strncpy(ophone, buf, 16);
384 if (dept[0] == ' ') {
385 strncpy(dept, e->dept, 12);
387 } else if (strncmp(strtrim(dept), e->dept, 11))
390 if (strncmp(strtrim(dept), e->dept, 11))
392 strncpy(dept, e->dept, 12);
403 com_err(whoami, 0, "updating finger for %s %s", first, last);
404 EXEC SQL REPEATED UPDATE users
405 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
406 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
407 xname = :name, xdept = :rdept, xtitle = :rtitle,
408 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
409 xmodtime = 'now', clearid = :sid
410 WHERE users_id = :id;
411 if (sqlca.sqlcode != 0) {
412 if (sqlca.sqlcode == SQL_DEADLOCK) {
413 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
415 return process_entry(e);
417 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
422 EXEC SQL REPEATED UPDATE users
423 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
424 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
425 xmodtime = 'now', clearid = :sid
426 WHERE users_id = :id;
427 if (sqlca.sqlcode != 0) {
428 if (sqlca.sqlcode == SQL_DEADLOCK) {
429 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
431 return process_entry(e);
433 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
445 EXEC SQL BEGIN DECLARE SECTION;
446 int id, uid, st, who;
447 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
448 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
449 char *rdept, *rhphone, *rophone;
450 EXEC SQL END DECLARE SECTION;
454 strncpy(oaddr, e->address, 16);
456 while (to = index(oaddr, ','))
458 while (to = index(oaddr, ':'))
469 strncpy(dept, e->dept, 12);
472 id = set_next_users_id(0);
473 uid = set_next_uid(1);
474 sprintf(login, "#%d", uid);
480 sprintf(fullname, "%s %s %s", first, middle, last);
482 sprintf(fullname, "%s %s", first, last);
484 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
485 st = US_NO_LOGIN_YET;
494 EXEC SQL REPEATED INSERT INTO users
495 (login, users_id, uid, shell, last, first, middle, status,
496 clearid, type, modtime, modby, modwith, fullname, office_addr,
497 office_phone, department, fmodtime, fmodby, fmodwith,
498 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
499 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
500 :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
501 :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
502 :title, :oaddr, :rhphone, :rophone, 'now');
503 if (sqlca.sqlcode != 0) {
504 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
507 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
511 set_next_users_id(limit)
514 EXEC SQL BEGIN DECLARE SECTION;
515 int rowcount, flag, value, retval;
516 EXEC SQL END DECLARE SECTION;
518 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
519 WHERE name = 'users_id';
520 if (sqlfail()) sqlexit();
521 if (sqlca.sqlerrd[2] != 1) {
523 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
528 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
529 WHERE users_id = :value;
530 if (sqlfail()) sqlexit();
531 if (sqlca.sqlerrd[2] == 0)
535 if (limit && value > MAX_ID_VALUE)
536 value = MIN_ID_VALUE;
538 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
539 WHERE users_id = :value;
540 if (sqlfail()) sqlexit();
541 if (sqlca.sqlerrd[2] == 0)
546 if (limit && value > MAX_ID_VALUE)
547 value = MIN_ID_VALUE;
548 EXEC SQL REPEATED UPDATE numvalues SET value = :value
549 WHERE name = 'users_id';
550 if (sqlca.sqlcode != 0) {
551 if (sqlca.sqlcode == SQL_DEADLOCK) {
552 com_err(whoami, MR_DEADLOCK, "adding user %s", sid);
556 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
566 EXEC SQL BEGIN DECLARE SECTION;
567 int rowcount, flag, value, retval;
568 EXEC SQL END DECLARE SECTION;
570 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
572 if (sqlfail()) sqlexit();
573 if (sqlca.sqlerrd[2] != 1) {
575 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
580 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
581 if (sqlfail()) sqlexit();
582 if (sqlca.sqlerrd[2] == 0)
586 if (limit && value > MAX_ID_VALUE)
587 value = MIN_ID_VALUE;
589 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
590 if (sqlfail()) sqlexit();
591 if (sqlca.sqlerrd[2] == 0)
596 if (limit && value > MAX_ID_VALUE)
597 value = MIN_ID_VALUE;
598 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
599 if (sqlca.sqlcode != 0) {
600 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
609 if (sqlca.sqlcode == SQL_DEADLOCK)
610 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
613 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
614 EXEC SQL ROLLBACK WORK;