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 1422
81 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403)
88 struct entry *e, *get_next_entry();
90 char buf[BUFSIZ], *file = NULL;
91 EXEC SQL BEGIN DECLARE SECTION;
93 EXEC SQL END DECLARE SECTION;
95 whoami = strrchr(argv[0], '/');
101 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
102 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
104 for (i = 1; i < argc; i++) {
105 if (!strcmp(argv[i], "-w"))
107 else if (!strcmp(argv[i], "-D"))
108 setenv("ING_SET", "set printqry");
109 else if (!strcmp(argv[i], "-n"))
111 else if (file != NULL)
112 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
117 in = fopen(file, "r");
119 fprintf(stderr, "Unable to open %s for input\n", file);
123 initialize_sms_error_table();
125 EXEC SQL CONNECT :db IDENTIFIED BY :db;
126 if (sqlca.sqlcode != 0) {
127 dbmserr("connecting", sqlca.sqlcode);
131 while (e = get_next_entry(in)) {
134 EXEC SQL COMMIT WORK;
135 if (sqlca.sqlcode != 0) {
136 dbmserr("committing work", sqlca.sqlcode);
150 struct entry *get_next_entry(in)
153 static struct entry e;
154 static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
155 static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
156 static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
157 static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
158 static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
159 static char sname[LEN_NAME+1], title[128];
160 static int nyear = 0;
161 int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
168 gettimeofday(&tv, NULL);
169 tm = localtime(&tv.tv_sec);
175 if (fgets(buf, sizeof(buf), in) == NULL)
176 return((struct entry *)NULL);
178 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
179 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
180 strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
181 strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
182 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
183 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
184 strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
185 strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
186 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
187 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
190 e.name = strtrim(sname);
191 p = strchr(name, ',');
194 e.last = strtrim(name);
200 if (p = strchr(e.first, ' ')) {
202 e.first = strtrim(e.first);
203 e.middle = strtrim(p + 1);
205 e.first = strtrim(e.first);
212 ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
215 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
217 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
226 EncryptID(e.eid, e.id, e.first, e.last);
228 e.year = strtrim(year);
230 if (e.year[0] == 'G') {
232 sprintf(title, "Grad Student");
235 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
236 sprintf(title, "Undergrad (class of %s)", classbuf);
239 e.course = strtrim(course);
240 e.address = strtrim(address);
241 e.dorm = strtrim(dorm_room);
242 e.city = strtrim(city);
243 e.state = strtrim(state);
244 e.dphone = strtrim(dphone);
245 e.mphone = strtrim(mphone);
253 int changed, nochange, encrypted;
254 char buf[BUFSIZ], *from, *to;
255 EXEC SQL BEGIN DECLARE SECTION;
256 char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
257 char *rophone, *rhphone, *prog;
258 char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
259 char dfirst[17], dlast[17], dmiddle[17];
261 EXEC SQL END DECLARE SECTION;
266 if (strlen(first) > 16)
269 if (strlen(last) > 16)
278 EXEC SQL SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
279 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
281 WHERE clearid = :sid;
283 if (sqlca.sqlcode == SQL_DUPLICATE) {
284 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
290 EXEC SQL SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
291 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
293 WHERE last = :last and first = :first and clearid = :eid;
294 if (sqlfail()) sqlexit();
302 /* See if class changed: if it's different, and the value in the database
303 * is not STAFF or SIPB, then update the database. Since they were on the
304 * students tape, make the account usable.
306 if (strcmp(e->class, strtrim(class)) &&
307 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
308 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
309 first, last, class, e->class);
310 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
311 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
312 strcpy(class, e->class);
313 EXEC SQL UPDATE users
314 SET type = NVL(:class,CHR(0)), status = :status, modtime = SYSDATE,
315 modby = :who, modwith = :prog
316 WHERE users_id = :id;
317 if (sqlca.sqlcode != 0) {
318 dbmserr("updating class", sqlca.sqlcode);
323 /* Update name if necessary */
324 if (strcmp(first, strtrim(dfirst)) ||
325 strcmp(last, strtrim(dlast)) ||
326 strcmp(middle, strtrim(dmiddle))) {
327 com_err(whoami, 0, "updating real name for %s %s", first, last);
328 EXEC SQL UPDATE users
329 SET first = NVL(:first,CHR(0)), last = NVL(:last,CHR(0)),
330 middle = NVL(:middle,CHR(0)), modby = :who, modwith = :prog,
332 WHERE users_id = :id;
333 if (sqlca.sqlcode != 0) {
334 dbmserr("updating name", sqlca.sqlcode);
339 /* Deal with updating the finger info if necessary */
340 changed = nochange = 0;
341 if (encrypted) changed++;
342 strcpy(buf, e->address);
345 strcat(buf, e->dorm);
349 strcat(buf, e->city);
354 strcat(buf, e->state);
356 while (to = strchr(buf, ','))
358 while (to = strchr(buf, ':'))
361 if (haddr[0] == ' ') {
362 strncpy(haddr, buf, 80);
365 } else if (strncmp(strtrim(haddr), buf, 80))
368 if (strncmp(strtrim(haddr), buf, 80))
370 strncpy(haddr, buf, 80);
382 if (hphone[0] == ' ') {
383 strncpy(hphone, buf, 16);
385 } else if (strncmp(strtrim(hphone), buf, 16))
388 if (strncmp(strtrim(hphone), buf, 16))
390 strncpy(hphone, buf, 16);
402 if (ophone[0] == ' ') {
403 strncpy(ophone, buf, 12);
405 } else if (strncmp(strtrim(ophone), buf, 12))
408 if (strncmp(strtrim(ophone), buf, 12))
410 strncpy(ophone, buf, 12);
413 e->course = e->course;
415 if (dept[0] == ' ') {
416 strncpy(dept, e->course, 12);
418 } else if (strncmp(strtrim(dept), e->course, 11))
421 if (strncmp(strtrim(dept), e->course, 11))
423 strncpy(dept, e->course, 12);
432 strcpy(raddr, e->address);
435 strcat(raddr, e->dorm);
439 strcat(raddr, e->city);
443 if (isupper(e->state[0]) && isupper(e->state[1]) &&
444 isdigit(e->state[2]) && isdigit(e->state[3]) &&
445 isdigit(e->state[4]) && isdigit(e->state[5]) &&
446 isdigit(e->state[6])) {
447 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
451 strcat(raddr, e->state);
455 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
458 com_err(whoami, 0, "updating finger for %s %s", first, last);
459 EXEC SQL UPDATE users
460 SET home_addr = NVL(:haddr,CHR(0)), home_phone = NVL(:hphone,CHR(0)),
461 office_phone = NVL(:ophone,CHR(0)), department = NVL(:dept,CHR(0)),
462 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
463 xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
464 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
465 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
466 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
467 WHERE users_id = :id;
468 if (sqlca.sqlcode != 0) {
469 dbmserr(NULL, sqlca.sqlcode);
473 EXEC SQL UPDATE users
474 SET xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
475 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
476 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
477 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
478 WHERE users_id = :id;
479 if (sqlca.sqlcode != 0) {
480 dbmserr(NULL, sqlca.sqlcode);
490 char buf[512], *from, *to;
491 EXEC SQL BEGIN DECLARE SECTION;
493 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
494 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
495 EXEC SQL END DECLARE SECTION;
499 strcpy(buf, e->address);
502 strcat(buf, e->dorm);
506 strcat(buf, e->city);
510 strcat(buf, e->state);
512 strncpy(haddr, buf, 80);
521 strncpy(hphone, buf, 16);
530 strncpy(ophone, buf, 12);
531 strncpy(dept, e->course, 12);
533 id = set_next_users_id(0);
534 uid = set_next_uid(1);
535 sprintf(login, "#%d", uid);
543 sprintf(fullname, "%s %s %s", first, middle, last);
545 sprintf(fullname, "%s %s", first, last);
547 strcpy(raddr, e->address);
550 strcat(raddr, e->dorm);
554 strcat(raddr, e->city);
558 if (isupper(e->state[0]) && isupper(e->state[1]) &&
559 isdigit(e->state[2]) && isdigit(e->state[3]) &&
560 isdigit(e->state[4]) && isdigit(e->state[5]) &&
561 isdigit(e->state[6])) {
562 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
566 strcat(raddr, e->state);
570 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
572 EXEC SQL INSERT INTO users
573 (login, users_id, unix_uid, shell, last, first, middle, status,
574 clearid, type, modtime, modby, modwith, fullname, home_addr,
575 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
576 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
577 VALUES (:login, :id, :uid, '/bin/athena/tcsh', NVL(:last,CHR(0)),
578 NVL(:first,CHR(0)), NVL(:middle,CHR(0)), 0, NVL(:sid,CHR(0)),
579 NVL(:class,CHR(0)), SYSDATE, :who, :prog, NVL(:fullname,CHR(0)),
580 NVL(:haddr,CHR(0)), NVL(:hphone,CHR(0)), NVL(:ophone,CHR(0)),
581 NVL(:dept,CHR(0)), SYSDATE, :who, :prog, 'NONE',
582 NVL(:name,CHR(0)), NVL(:dept,CHR(0)), NVL(:title,CHR(0)),
583 NVL(:raddr,CHR(0)), NVL(:hphone,CHR(0)), NVL(:ophone,CHR(0)),
585 if (sqlca.sqlcode != 0) {
586 dbmserr("adding user", sqlca.sqlcode);
589 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
594 set_next_users_id(limit)
597 EXEC SQL BEGIN DECLARE SECTION;
598 int rowcount, flag, value, retval;
599 EXEC SQL END DECLARE SECTION;
601 EXEC SQL SELECT value INTO :value FROM numvalues
602 WHERE name = 'users_id';
603 if (sqlfail()) sqlexit();
604 if (sqlca.sqlerrd[2] != 1) {
606 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
611 EXEC SQL SELECT users_id INTO :flag FROM users
612 WHERE users_id = :value;
613 if (sqlfail()) sqlexit();
614 if (sqlca.sqlerrd[2] == 0)
618 if (limit && value > MAX_ID_VALUE)
619 value = MIN_ID_VALUE;
621 EXEC SQL SELECT users_id INTO :flag FROM users
622 WHERE users_id = :value;
623 if (sqlfail()) sqlexit();
624 if (sqlca.sqlerrd[2] == 0)
629 if (limit && value > MAX_ID_VALUE)
630 value = MIN_ID_VALUE;
631 EXEC SQL UPDATE numvalues SET value = :value
632 WHERE name = 'users_id';
633 if (sqlca.sqlcode != 0) {
634 dbmserr("updating numvalues", sqlca.sqlcode);
643 EXEC SQL BEGIN DECLARE SECTION;
644 int rowcount, flag, value, retval;
645 EXEC SQL END DECLARE SECTION;
647 EXEC SQL SELECT value INTO :value FROM numvalues
648 WHERE name = 'unix_uid';
649 if (sqlfail()) sqlexit();
650 if (sqlca.sqlerrd[2] != 1) {
652 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
657 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
658 if (sqlfail()) sqlexit();
659 if (sqlca.sqlerrd[2] == 0)
663 if (limit && value > MAX_ID_VALUE)
664 value = MIN_ID_VALUE;
666 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
667 if (sqlfail()) sqlexit();
668 if (sqlca.sqlerrd[2] == 0)
673 if (limit && value > MAX_ID_VALUE)
674 value = MIN_ID_VALUE;
675 EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
676 if (sqlca.sqlcode != 0) {
677 dbmserr("updating numvalues", sqlca.sqlcode);
686 dbmserr(NULL, sqlca.sqlcode);
687 EXEC SQL ROLLBACK WORK;
691 dbmserr(char *where, int what)
694 int bufsize=256, msglength=0;
696 sqlglm(err_msg, &bufsize, &msglength);
697 err_msg[msglength]=0;
700 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
702 com_err(whoami, 0, "DBMS error %s", err_msg);