9 #include <moira_site.h>
10 EXEC SQL INCLUDE sqlca;
13 #define WHO 11859 /* root */
14 #define PROG "stu-tape"
16 #define MAX_ID_VALUE 31999
17 #define MIN_ID_VALUE 101
39 #define LOC_ADDRESS 80
40 #define LOC_DORM_ROOM 110
43 #define LOC_DPHONE 155
44 #define LOC_MPHONE 243
50 #define LEN_ADDRESS 30
51 #define LEN_DORM_ROOM 15
80 #define SQL_DUPLICATE -40100
81 #define SQL_DEADLOCK -49900
82 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
89 struct entry *e, *get_next_entry();
91 char buf[BUFSIZ], *file = NULL;
93 whoami = rindex(argv[0], '/');
99 for (i = 1; i < argc; i++) {
100 if (!strcmp(argv[i], "-w"))
102 else if (!strcmp(argv[i], "-D"))
103 setenv("ING_SET", "set printqry");
104 else if (!strcmp(argv[i], "-n"))
106 else if (file != NULL)
107 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
112 in = fopen(file, "r");
114 fprintf(stderr, "Unable to open %s for input\n", file);
121 EXEC SQL CONNECT moira;
122 if (sqlca.sqlcode != 0) {
123 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
127 while (e = get_next_entry(in)) {
130 EXEC SQL COMMIT WORK;
131 if (sqlca.sqlcode != 0) {
132 if (sqlca.sqlcode == SQL_DEADLOCK) {
133 com_err(whoami, MR_DEADLOCK, "commiting work");
136 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
151 struct entry *get_next_entry(in)
154 static struct entry e;
155 static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
156 static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
157 static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
158 static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
159 static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
160 static char sname[LEN_NAME+1], title[128];
161 static int nyear = 0;
162 int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
169 gettimeofday(&tv, NULL);
170 tm = localtime(&tv.tv_sec);
176 if (fgets(buf, sizeof(buf), in) == NULL)
177 return((struct entry *)NULL);
179 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
180 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
181 strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
182 strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
183 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
184 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
185 strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
186 strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
187 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
188 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
191 e.name = strtrim(sname);
192 p = index(name, ',');
195 e.last = strtrim(name);
201 if (p = index(e.first, ' ')) {
203 e.first = strtrim(e.first);
204 e.middle = strtrim(p + 1);
206 e.first = strtrim(e.first);
213 ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
216 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
218 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
227 EncryptID(e.eid, e.id, e.first, e.last);
229 e.year = strtrim(year);
231 if (e.year[0] == 'G') {
233 sprintf(title, "Grad Student");
236 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
237 sprintf(title, "Undergrad (class of %s)", classbuf);
240 e.course = strtrim(course);
241 e.address = strtrim(address);
242 e.dorm = strtrim(dorm_room);
243 e.city = strtrim(city);
244 e.state = strtrim(state);
245 e.dphone = strtrim(dphone);
246 e.mphone = strtrim(mphone);
254 int changed, nochange, encrypted;
255 char buf[BUFSIZ], *from, *to;
256 EXEC SQL BEGIN DECLARE SECTION;
257 char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
258 char *rophone, *rhphone, *prog;
259 char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
260 char dfirst[17], dlast[17], dmiddle[17];
262 EXEC SQL END DECLARE SECTION;
267 if (strlen(first) > 16)
270 if (strlen(last) > 16)
279 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
280 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
282 WHERE clearid = :sid;
284 if (sqlca.sqlcode == SQL_DUPLICATE) {
285 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
287 } else if (sqlca.sqlcode == SQL_DEADLOCK) {
288 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
290 return process_entry(e);
295 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
296 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
298 WHERE last = :last and first = :first and clearid = :eid;
300 if (sqlca.sqlcode == SQL_DEADLOCK) {
301 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
303 return process_entry(e);
304 } else if (sqlca.sqlcode != SQL_DUPLICATE)
314 /* See if class changed: if it's different, and the value in the database
315 * is not STAFF or SIPB, then update the database. Since they were on the
316 * students tape, make the account usable.
318 if (strcmp(e->class, strtrim(class)) &&
319 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
320 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
321 first, last, class, e->class);
322 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
323 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
324 strcpy(class, e->class);
325 EXEC SQL REPEATED UPDATE users
326 SET type = :class, status = :status, modtime = 'now',
327 modby = :who, modwith = :prog
328 WHERE users_id = :id;
329 if (sqlca.sqlcode != 0) {
330 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
335 /* Update name if necessary */
336 if (strcmp(first, strtrim(dfirst)) ||
337 strcmp(last, strtrim(dlast)) ||
338 strcmp(middle, strtrim(dmiddle))) {
339 com_err(whoami, 0, "updating real name for %s %s", first, last);
340 EXEC SQL UPDATE users
341 SET first = :first, last = :last, middle = :middle,
342 modby = :who, modwith = :prog, modtime = 'now'
343 WHERE users_id = :id;
344 if (sqlca.sqlcode != 0) {
345 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
350 /* Deal with updating the finger info if necessary */
351 changed = nochange = 0;
352 if (encrypted) changed++;
353 strcpy(buf, e->address);
356 strcat(buf, e->dorm);
360 strcat(buf, e->city);
365 strcat(buf, e->state);
367 while (to = index(buf, ','))
369 while (to = index(buf, ':'))
372 if (haddr[0] == ' ') {
373 strncpy(haddr, buf, 80);
376 } else if (strncmp(strtrim(haddr), buf, 80))
379 if (strncmp(strtrim(haddr), buf, 80))
381 strncpy(haddr, buf, 80);
393 if (hphone[0] == ' ') {
394 strncpy(hphone, buf, 16);
396 } else if (strncmp(strtrim(hphone), buf, 16))
399 if (strncmp(strtrim(hphone), buf, 16))
401 strncpy(hphone, buf, 16);
413 if (ophone[0] == ' ') {
414 strncpy(ophone, buf, 12);
416 } else if (strncmp(strtrim(ophone), buf, 12))
419 if (strncmp(strtrim(ophone), buf, 12))
421 strncpy(ophone, buf, 12);
424 e->course = e->course;
426 if (dept[0] == ' ') {
427 strncpy(dept, e->course, 12);
429 } else if (strncmp(strtrim(dept), e->course, 11))
432 if (strncmp(strtrim(dept), e->course, 11))
434 strncpy(dept, e->course, 12);
443 strcpy(raddr, e->address);
446 strcat(raddr, e->dorm);
450 strcat(raddr, e->city);
454 if (isupper(e->state[0]) && isupper(e->state[1]) &&
455 isdigit(e->state[2]) && isdigit(e->state[3]) &&
456 isdigit(e->state[4]) && isdigit(e->state[5]) &&
457 isdigit(e->state[6])) {
458 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
462 strcat(raddr, e->state);
466 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
469 com_err(whoami, 0, "updating finger for %s %s", first, last);
470 EXEC SQL REPEATED UPDATE users
471 SET home_addr = :haddr, home_phone = :hphone,
472 office_phone = :ophone, department = :dept,
473 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
474 xname = :name, xdept = :rdept, xtitle = :rtitle,
475 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
476 xmodtime = date('now'), clearid = :sid
477 WHERE users_id = :id;
478 if (sqlca.sqlcode != 0) {
479 if (sqlca.sqlcode == SQL_DEADLOCK) {
480 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
482 return process_entry(e);
484 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
489 EXEC SQL REPEATED UPDATE users
490 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
491 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
492 xmodtime = date('now'), clearid = :sid
493 WHERE users_id = :id;
494 if (sqlca.sqlcode != 0) {
495 if (sqlca.sqlcode == SQL_DEADLOCK) {
496 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
498 return process_entry(e);
500 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
511 char buf[512], *from, *to;
512 EXEC SQL BEGIN DECLARE SECTION;
514 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
515 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
516 EXEC SQL END DECLARE SECTION;
520 strcpy(buf, e->address);
523 strcat(buf, e->dorm);
527 strcat(buf, e->city);
531 strcat(buf, e->state);
533 strncpy(haddr, buf, 80);
542 strncpy(hphone, buf, 16);
551 strncpy(ophone, buf, 12);
552 strncpy(dept, e->course, 12);
554 id = set_next_users_id(0);
555 uid = set_next_uid(1);
556 sprintf(login, "#%d", uid);
564 sprintf(fullname, "%s %s %s", first, middle, last);
566 sprintf(fullname, "%s %s", first, last);
568 strcpy(raddr, e->address);
571 strcat(raddr, e->dorm);
575 strcat(raddr, e->city);
579 if (isupper(e->state[0]) && isupper(e->state[1]) &&
580 isdigit(e->state[2]) && isdigit(e->state[3]) &&
581 isdigit(e->state[4]) && isdigit(e->state[5]) &&
582 isdigit(e->state[6])) {
583 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
587 strcat(raddr, e->state);
591 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
593 EXEC SQL REPEATED INSERT INTO users
594 (login, users_id, uid, shell, last, first, middle, status,
595 clearid, type, modtime, modby, modwith, fullname, home_addr,
596 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
597 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
598 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0,
599 :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
600 :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
601 :title, :raddr, :hphone, :ophone, date('now'));
602 if (sqlca.sqlcode != 0) {
603 if (sqlca.sqlcode == SQL_DEADLOCK) {
604 com_err(whoami, MR_DEADLOCK, "adding user %s", sid);
608 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
612 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
617 set_next_users_id(limit)
620 EXEC SQL BEGIN DECLARE SECTION;
621 int rowcount, flag, value, retval;
622 EXEC SQL END DECLARE SECTION;
624 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
625 WHERE name = 'users_id';
626 if (sqlfail()) sqlexit();
627 if (sqlca.sqlerrd[2] != 1) {
629 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
634 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
635 WHERE users_id = :value;
636 if (sqlfail()) sqlexit();
637 if (sqlca.sqlerrd[2] == 0)
641 if (limit && value > MAX_ID_VALUE)
642 value = MIN_ID_VALUE;
644 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
645 WHERE users_id = :value;
646 if (sqlfail()) sqlexit();
647 if (sqlca.sqlerrd[2] == 0)
652 if (limit && value > MAX_ID_VALUE)
653 value = MIN_ID_VALUE;
654 EXEC SQL REPEATED UPDATE numvalues SET value = :value
655 WHERE name = 'users_id';
656 if (sqlca.sqlcode != 0) {
657 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
666 EXEC SQL BEGIN DECLARE SECTION;
667 int rowcount, flag, value, retval;
668 EXEC SQL END DECLARE SECTION;
670 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
672 if (sqlfail()) sqlexit();
673 if (sqlca.sqlerrd[2] != 1) {
675 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
680 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
681 if (sqlfail()) sqlexit();
682 if (sqlca.sqlerrd[2] == 0)
686 if (limit && value > MAX_ID_VALUE)
687 value = MIN_ID_VALUE;
689 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
690 if (sqlfail()) sqlexit();
691 if (sqlca.sqlerrd[2] == 0)
696 if (limit && value > MAX_ID_VALUE)
697 value = MIN_ID_VALUE;
698 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
699 if (sqlca.sqlcode != 0) {
700 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
709 if (sqlca.sqlcode == SQL_DEADLOCK)
710 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
713 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
714 EXEC SQL ROLLBACK WORK;