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);
120 initialize_sms_error_table();
122 EXEC SQL CONNECT moira;
123 if (sqlca.sqlcode != 0) {
124 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
128 while (e = get_next_entry(in)) {
131 EXEC SQL COMMIT WORK;
132 if (sqlca.sqlcode != 0) {
133 if (sqlca.sqlcode == SQL_DEADLOCK) {
134 com_err(whoami, MR_DEADLOCK, "commiting work");
137 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
152 struct entry *get_next_entry(in)
155 static struct entry e;
156 static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
157 static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
158 static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
159 static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
160 static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
161 static char sname[LEN_NAME+1], title[128];
162 static int nyear = 0;
163 int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
170 gettimeofday(&tv, NULL);
171 tm = localtime(&tv.tv_sec);
177 if (fgets(buf, sizeof(buf), in) == NULL)
178 return((struct entry *)NULL);
180 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
181 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
182 strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
183 strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
184 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
185 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
186 strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
187 strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
188 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
189 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
192 e.name = strtrim(sname);
193 p = index(name, ',');
196 e.last = strtrim(name);
202 if (p = index(e.first, ' ')) {
204 e.first = strtrim(e.first);
205 e.middle = strtrim(p + 1);
207 e.first = strtrim(e.first);
214 ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
217 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
219 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
228 EncryptID(e.eid, e.id, e.first, e.last);
230 e.year = strtrim(year);
232 if (e.year[0] == 'G') {
234 sprintf(title, "Grad Student");
237 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
238 sprintf(title, "Undergrad (class of %s)", classbuf);
241 e.course = strtrim(course);
242 e.address = strtrim(address);
243 e.dorm = strtrim(dorm_room);
244 e.city = strtrim(city);
245 e.state = strtrim(state);
246 e.dphone = strtrim(dphone);
247 e.mphone = strtrim(mphone);
255 int changed, nochange, encrypted;
256 char buf[BUFSIZ], *from, *to;
257 EXEC SQL BEGIN DECLARE SECTION;
258 char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
259 char *rophone, *rhphone, *prog;
260 char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
261 char dfirst[17], dlast[17], dmiddle[17];
263 EXEC SQL END DECLARE SECTION;
268 if (strlen(first) > 16)
271 if (strlen(last) > 16)
280 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
281 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
283 WHERE clearid = :sid;
285 if (sqlca.sqlcode == SQL_DUPLICATE) {
286 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
288 } else if (sqlca.sqlcode == SQL_DEADLOCK) {
289 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
291 return process_entry(e);
296 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
297 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
299 WHERE last = :last and first = :first and clearid = :eid;
301 if (sqlca.sqlcode == SQL_DEADLOCK) {
302 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
304 return process_entry(e);
305 } else if (sqlca.sqlcode != SQL_DUPLICATE)
315 /* See if class changed: if it's different, and the value in the database
316 * is not STAFF or SIPB, then update the database. Since they were on the
317 * students tape, make the account usable.
319 if (strcmp(e->class, strtrim(class)) &&
320 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
321 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
322 first, last, class, e->class);
323 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
324 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
325 strcpy(class, e->class);
326 EXEC SQL REPEATED UPDATE users
327 SET type = :class, status = :status, modtime = 'now',
328 modby = :who, modwith = :prog
329 WHERE users_id = :id;
330 if (sqlca.sqlcode != 0) {
331 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
336 /* Update name if necessary */
337 if (strcmp(first, strtrim(dfirst)) ||
338 strcmp(last, strtrim(dlast)) ||
339 strcmp(middle, strtrim(dmiddle))) {
340 com_err(whoami, 0, "updating real name for %s %s", first, last);
341 EXEC SQL UPDATE users
342 SET first = :first, last = :last, middle = :middle,
343 modby = :who, modwith = :prog, modtime = 'now'
344 WHERE users_id = :id;
345 if (sqlca.sqlcode != 0) {
346 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
351 /* Deal with updating the finger info if necessary */
352 changed = nochange = 0;
353 if (encrypted) changed++;
354 strcpy(buf, e->address);
357 strcat(buf, e->dorm);
361 strcat(buf, e->city);
366 strcat(buf, e->state);
368 while (to = index(buf, ','))
370 while (to = index(buf, ':'))
373 if (haddr[0] == ' ') {
374 strncpy(haddr, buf, 80);
377 } else if (strncmp(strtrim(haddr), buf, 80))
380 if (strncmp(strtrim(haddr), buf, 80))
382 strncpy(haddr, buf, 80);
394 if (hphone[0] == ' ') {
395 strncpy(hphone, buf, 16);
397 } else if (strncmp(strtrim(hphone), buf, 16))
400 if (strncmp(strtrim(hphone), buf, 16))
402 strncpy(hphone, buf, 16);
414 if (ophone[0] == ' ') {
415 strncpy(ophone, buf, 12);
417 } else if (strncmp(strtrim(ophone), buf, 12))
420 if (strncmp(strtrim(ophone), buf, 12))
422 strncpy(ophone, buf, 12);
425 e->course = e->course;
427 if (dept[0] == ' ') {
428 strncpy(dept, e->course, 12);
430 } else if (strncmp(strtrim(dept), e->course, 11))
433 if (strncmp(strtrim(dept), e->course, 11))
435 strncpy(dept, e->course, 12);
444 strcpy(raddr, e->address);
447 strcat(raddr, e->dorm);
451 strcat(raddr, e->city);
455 if (isupper(e->state[0]) && isupper(e->state[1]) &&
456 isdigit(e->state[2]) && isdigit(e->state[3]) &&
457 isdigit(e->state[4]) && isdigit(e->state[5]) &&
458 isdigit(e->state[6])) {
459 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
463 strcat(raddr, e->state);
467 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
470 com_err(whoami, 0, "updating finger for %s %s", first, last);
471 EXEC SQL REPEATED UPDATE users
472 SET home_addr = :haddr, home_phone = :hphone,
473 office_phone = :ophone, department = :dept,
474 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
475 xname = :name, xdept = :rdept, xtitle = :rtitle,
476 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
477 xmodtime = date('now'), clearid = :sid
478 WHERE users_id = :id;
479 if (sqlca.sqlcode != 0) {
480 if (sqlca.sqlcode == SQL_DEADLOCK) {
481 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
483 return process_entry(e);
485 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
490 EXEC SQL REPEATED UPDATE users
491 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
492 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
493 xmodtime = date('now'), clearid = :sid
494 WHERE users_id = :id;
495 if (sqlca.sqlcode != 0) {
496 if (sqlca.sqlcode == SQL_DEADLOCK) {
497 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
499 return process_entry(e);
501 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
512 char buf[512], *from, *to;
513 EXEC SQL BEGIN DECLARE SECTION;
515 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
516 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
517 EXEC SQL END DECLARE SECTION;
521 strcpy(buf, e->address);
524 strcat(buf, e->dorm);
528 strcat(buf, e->city);
532 strcat(buf, e->state);
534 strncpy(haddr, buf, 80);
543 strncpy(hphone, buf, 16);
552 strncpy(ophone, buf, 12);
553 strncpy(dept, e->course, 12);
555 id = set_next_users_id(0);
556 uid = set_next_uid(1);
557 sprintf(login, "#%d", uid);
565 sprintf(fullname, "%s %s %s", first, middle, last);
567 sprintf(fullname, "%s %s", first, last);
569 strcpy(raddr, e->address);
572 strcat(raddr, e->dorm);
576 strcat(raddr, e->city);
580 if (isupper(e->state[0]) && isupper(e->state[1]) &&
581 isdigit(e->state[2]) && isdigit(e->state[3]) &&
582 isdigit(e->state[4]) && isdigit(e->state[5]) &&
583 isdigit(e->state[6])) {
584 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
588 strcat(raddr, e->state);
592 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
594 EXEC SQL REPEATED INSERT INTO users
595 (login, users_id, uid, shell, last, first, middle, status,
596 clearid, type, modtime, modby, modwith, fullname, home_addr,
597 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
598 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
599 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0,
600 :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
601 :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
602 :title, :raddr, :hphone, :ophone, date('now'));
603 if (sqlca.sqlcode != 0) {
604 if (sqlca.sqlcode == SQL_DEADLOCK) {
605 com_err(whoami, MR_DEADLOCK, "adding user %s", sid);
609 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
613 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
618 set_next_users_id(limit)
621 EXEC SQL BEGIN DECLARE SECTION;
622 int rowcount, flag, value, retval;
623 EXEC SQL END DECLARE SECTION;
625 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
626 WHERE name = 'users_id';
627 if (sqlfail()) sqlexit();
628 if (sqlca.sqlerrd[2] != 1) {
630 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
635 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
636 WHERE users_id = :value;
637 if (sqlfail()) sqlexit();
638 if (sqlca.sqlerrd[2] == 0)
642 if (limit && value > MAX_ID_VALUE)
643 value = MIN_ID_VALUE;
645 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
646 WHERE users_id = :value;
647 if (sqlfail()) sqlexit();
648 if (sqlca.sqlerrd[2] == 0)
653 if (limit && value > MAX_ID_VALUE)
654 value = MIN_ID_VALUE;
655 EXEC SQL REPEATED UPDATE numvalues SET value = :value
656 WHERE name = 'users_id';
657 if (sqlca.sqlcode != 0) {
658 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
667 EXEC SQL BEGIN DECLARE SECTION;
668 int rowcount, flag, value, retval;
669 EXEC SQL END DECLARE SECTION;
671 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
673 if (sqlfail()) sqlexit();
674 if (sqlca.sqlerrd[2] != 1) {
676 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
681 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
682 if (sqlfail()) sqlexit();
683 if (sqlca.sqlerrd[2] == 0)
687 if (limit && value > MAX_ID_VALUE)
688 value = MIN_ID_VALUE;
690 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
691 if (sqlfail()) sqlexit();
692 if (sqlca.sqlerrd[2] == 0)
697 if (limit && value > MAX_ID_VALUE)
698 value = MIN_ID_VALUE;
699 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
700 if (sqlca.sqlcode != 0) {
701 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
710 if (sqlca.sqlcode == SQL_DEADLOCK)
711 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
714 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
715 EXEC SQL ROLLBACK WORK;