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)) {
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", 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 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
327 /* Update name if necessary */
328 if (strcmp(first, strtrim(dfirst)) ||
329 strcmp(last, strtrim(dlast)) ||
330 strcmp(middle, strtrim(dmiddle))) {
331 com_err(whoami, 0, "updating real name for %s %s", first, last);
332 EXEC SQL UPDATE users
333 SET first = :first, last = :last, middle = :middle,
334 modby = :who, modwith = :prog, modtime = 'now'
335 WHERE users_id = :id;
336 if (sqlca.sqlcode != 0) {
337 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
342 changed = nochange = 0;
343 if (encrypted) changed++;
344 strcpy(buf, e->address);
345 while (to = index(buf, ','))
347 while (to = index(buf, ':'))
350 if (oaddr[0] == ' ' && buf[0]) {
351 strncpy(oaddr, buf, 16);
354 } else if (strncmp(strtrim(oaddr), buf, 15))
357 if (strncmp(strtrim(oaddr), buf, 15))
359 strncpy(oaddr, buf, 16);
371 if (ophone[0] == ' ') {
372 strncpy(ophone, buf, 16);
374 } else if (strncmp(strtrim(ophone), buf, 11))
377 if (strncmp(strtrim(ophone), buf, 11))
379 strncpy(ophone, buf, 16);
385 if (dept[0] == ' ') {
386 strncpy(dept, e->dept, 12);
388 } else if (strncmp(strtrim(dept), e->dept, 11))
391 if (strncmp(strtrim(dept), e->dept, 11))
393 strncpy(dept, e->dept, 12);
404 com_err(whoami, 0, "updating finger for %s %s", first, last);
405 EXEC SQL REPEATED UPDATE users
406 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
407 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
408 xname = :name, xdept = :rdept, xtitle = :rtitle,
409 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
410 xmodtime = 'now', clearid = :sid
411 WHERE users_id = :id;
412 if (sqlca.sqlcode != 0) {
413 if (sqlca.sqlcode == SQL_DEADLOCK) {
414 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
416 return process_entry(e);
418 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
423 EXEC SQL REPEATED UPDATE users
424 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
425 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
426 xmodtime = 'now', clearid = :sid
427 WHERE users_id = :id;
428 if (sqlca.sqlcode != 0) {
429 if (sqlca.sqlcode == SQL_DEADLOCK) {
430 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
432 return process_entry(e);
434 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
446 EXEC SQL BEGIN DECLARE SECTION;
447 int id, uid, st, who;
448 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
449 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
450 char *rdept, *rhphone, *rophone;
451 EXEC SQL END DECLARE SECTION;
455 strncpy(oaddr, e->address, 16);
457 while (to = index(oaddr, ','))
459 while (to = index(oaddr, ':'))
470 strncpy(dept, e->dept, 12);
473 id = set_next_users_id(0);
474 uid = set_next_uid(1);
475 sprintf(login, "#%d", uid);
481 sprintf(fullname, "%s %s %s", first, middle, last);
483 sprintf(fullname, "%s %s", first, last);
485 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
486 st = US_NO_LOGIN_YET;
495 EXEC SQL REPEATED INSERT INTO users
496 (login, users_id, uid, shell, last, first, middle, status,
497 clearid, type, modtime, modby, modwith, fullname, office_addr,
498 office_phone, department, fmodtime, fmodby, fmodwith,
499 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
500 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
501 :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
502 :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
503 :title, :oaddr, :rhphone, :rophone, 'now');
504 if (sqlca.sqlcode != 0) {
505 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
508 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
512 set_next_users_id(limit)
515 EXEC SQL BEGIN DECLARE SECTION;
516 int rowcount, flag, value, retval;
517 EXEC SQL END DECLARE SECTION;
519 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
520 WHERE name = 'users_id';
521 if (sqlfail()) sqlexit();
522 if (sqlca.sqlerrd[2] != 1) {
524 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
529 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
530 WHERE users_id = :value;
531 if (sqlfail()) sqlexit();
532 if (sqlca.sqlerrd[2] == 0)
536 if (limit && value > MAX_ID_VALUE)
537 value = MIN_ID_VALUE;
539 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
540 WHERE users_id = :value;
541 if (sqlfail()) sqlexit();
542 if (sqlca.sqlerrd[2] == 0)
547 if (limit && value > MAX_ID_VALUE)
548 value = MIN_ID_VALUE;
549 EXEC SQL REPEATED UPDATE numvalues SET value = :value
550 WHERE name = 'users_id';
551 if (sqlca.sqlcode != 0) {
552 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
561 EXEC SQL BEGIN DECLARE SECTION;
562 int rowcount, flag, value, retval;
563 EXEC SQL END DECLARE SECTION;
565 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
567 if (sqlfail()) sqlexit();
568 if (sqlca.sqlerrd[2] != 1) {
570 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
575 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
576 if (sqlfail()) sqlexit();
577 if (sqlca.sqlerrd[2] == 0)
581 if (limit && value > MAX_ID_VALUE)
582 value = MIN_ID_VALUE;
584 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
585 if (sqlfail()) sqlexit();
586 if (sqlca.sqlerrd[2] == 0)
591 if (limit && value > MAX_ID_VALUE)
592 value = MIN_ID_VALUE;
593 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
594 if (sqlca.sqlcode != 0) {
595 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
604 if (sqlca.sqlcode == SQL_DEADLOCK)
605 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
608 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
609 EXEC SQL ROLLBACK WORK;