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 -2112
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 SIPBMEM, and the value from the tape is actually
304 * meaningful, then update the database. Since they were on the
305 * students tape, make the account usable.
307 if (strcmp(e->class, strtrim(class)) &&
308 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
310 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
311 first, last, class, e->class);
312 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
313 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
314 strcpy(class, e->class);
315 EXEC SQL UPDATE users
316 SET type = NVL(:class,CHR(0)), status = :status, modtime = SYSDATE,
317 modby = :who, modwith = :prog
318 WHERE users_id = :id;
319 if (sqlca.sqlcode != 0) {
320 dbmserr("updating class", sqlca.sqlcode);
325 /* Update name if necessary */
326 if (strcmp(first, strtrim(dfirst)) ||
327 strcmp(last, strtrim(dlast)) ||
328 strcmp(middle, strtrim(dmiddle))) {
329 com_err(whoami, 0, "updating real name for %s %s", first, last);
330 EXEC SQL UPDATE users
331 SET first = NVL(:first,CHR(0)), last = NVL(:last,CHR(0)),
332 middle = NVL(:middle,CHR(0)), modby = :who, modwith = :prog,
334 WHERE users_id = :id;
335 if (sqlca.sqlcode != 0) {
336 dbmserr("updating name", sqlca.sqlcode);
341 /* Deal with updating the finger info if necessary */
342 changed = nochange = 0;
343 if (encrypted) changed++;
344 strcpy(buf, e->address);
347 strcat(buf, e->dorm);
351 strcat(buf, e->city);
356 strcat(buf, e->state);
358 while (to = strchr(buf, ','))
360 while (to = strchr(buf, ':'))
363 if (haddr[0] == ' ') {
364 strncpy(haddr, buf, 80);
367 } else if (strncmp(strtrim(haddr), buf, 80))
370 if (strncmp(strtrim(haddr), buf, 80))
372 strncpy(haddr, buf, 80);
384 if (hphone[0] == ' ') {
385 strncpy(hphone, buf, 16);
387 } else if (strncmp(strtrim(hphone), buf, 16))
390 if (strncmp(strtrim(hphone), buf, 16))
392 strncpy(hphone, buf, 16);
404 if (ophone[0] == ' ') {
405 strncpy(ophone, buf, 12);
407 } else if (strncmp(strtrim(ophone), buf, 12))
410 if (strncmp(strtrim(ophone), buf, 12))
412 strncpy(ophone, buf, 12);
415 e->course = e->course;
417 if (dept[0] == ' ') {
418 strncpy(dept, e->course, 12);
420 } else if (strncmp(strtrim(dept), e->course, 11))
423 if (strncmp(strtrim(dept), e->course, 11))
425 strncpy(dept, e->course, 12);
434 strcpy(raddr, e->address);
437 strcat(raddr, e->dorm);
441 strcat(raddr, e->city);
445 if (isupper(e->state[0]) && isupper(e->state[1]) &&
446 isdigit(e->state[2]) && isdigit(e->state[3]) &&
447 isdigit(e->state[4]) && isdigit(e->state[5]) &&
448 isdigit(e->state[6])) {
449 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
453 strcat(raddr, e->state);
457 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
460 com_err(whoami, 0, "updating finger for %s %s", first, last);
461 EXEC SQL UPDATE users
462 SET home_addr = NVL(:haddr,CHR(0)), home_phone = NVL(:hphone,CHR(0)),
463 office_phone = NVL(:ophone,CHR(0)), department = NVL(:dept,CHR(0)),
464 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
465 xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
466 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
467 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
468 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
469 WHERE users_id = :id;
470 if (sqlca.sqlcode != 0) {
471 dbmserr(NULL, sqlca.sqlcode);
475 EXEC SQL UPDATE users
476 SET xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
477 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
478 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
479 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
480 WHERE users_id = :id;
481 if (sqlca.sqlcode != 0) {
482 dbmserr(NULL, sqlca.sqlcode);
492 char buf[512], *from, *to;
493 EXEC SQL BEGIN DECLARE SECTION;
495 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
496 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
497 EXEC SQL END DECLARE SECTION;
501 strcpy(buf, e->address);
504 strcat(buf, e->dorm);
508 strcat(buf, e->city);
512 strcat(buf, e->state);
514 strncpy(haddr, buf, 80);
523 strncpy(hphone, buf, 16);
532 strncpy(ophone, buf, 12);
533 strncpy(dept, e->course, 12);
535 id = set_next_users_id(0);
536 uid = set_next_uid(1);
537 sprintf(login, "#%d", uid);
545 sprintf(fullname, "%s %s %s", first, middle, last);
547 sprintf(fullname, "%s %s", first, last);
549 strcpy(raddr, e->address);
552 strcat(raddr, e->dorm);
556 strcat(raddr, e->city);
560 if (isupper(e->state[0]) && isupper(e->state[1]) &&
561 isdigit(e->state[2]) && isdigit(e->state[3]) &&
562 isdigit(e->state[4]) && isdigit(e->state[5]) &&
563 isdigit(e->state[6])) {
564 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
568 strcat(raddr, e->state);
572 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
574 EXEC SQL INSERT INTO users
575 (login, users_id, unix_uid, shell, last, first, middle, status,
576 clearid, type, modtime, modby, modwith, fullname, home_addr,
577 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
578 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
579 VALUES (:login, :id, :uid, '/bin/athena/tcsh', NVL(:last,CHR(0)),
580 NVL(:first,CHR(0)), NVL(:middle,CHR(0)), 0, NVL(:sid,CHR(0)),
581 NVL(:class,CHR(0)), SYSDATE, :who, :prog, NVL(:fullname,CHR(0)),
582 NVL(:haddr,CHR(0)), NVL(:hphone,CHR(0)), NVL(:ophone,CHR(0)),
583 NVL(:dept,CHR(0)), SYSDATE, :who, :prog, 'NONE',
584 NVL(:name,CHR(0)), NVL(:dept,CHR(0)), NVL(:title,CHR(0)),
585 NVL(:raddr,CHR(0)), NVL(:hphone,CHR(0)), NVL(:ophone,CHR(0)),
587 if (sqlca.sqlcode != 0) {
588 dbmserr("adding user", sqlca.sqlcode);
591 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
596 set_next_users_id(limit)
599 EXEC SQL BEGIN DECLARE SECTION;
600 int rowcount, flag, value, retval;
601 EXEC SQL END DECLARE SECTION;
603 EXEC SQL SELECT value INTO :value FROM numvalues
604 WHERE name = 'users_id';
605 if (sqlfail()) sqlexit();
606 if (sqlca.sqlerrd[2] != 1) {
608 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
613 EXEC SQL SELECT users_id INTO :flag FROM users
614 WHERE users_id = :value;
615 if (sqlfail()) sqlexit();
616 if (sqlca.sqlerrd[2] == 0)
620 if (limit && value > MAX_ID_VALUE)
621 value = MIN_ID_VALUE;
623 EXEC SQL SELECT users_id INTO :flag FROM users
624 WHERE users_id = :value;
625 if (sqlfail()) sqlexit();
626 if (sqlca.sqlerrd[2] == 0)
631 if (limit && value > MAX_ID_VALUE)
632 value = MIN_ID_VALUE;
633 EXEC SQL UPDATE numvalues SET value = :value
634 WHERE name = 'users_id';
635 if (sqlca.sqlcode != 0) {
636 dbmserr("updating numvalues", sqlca.sqlcode);
645 EXEC SQL BEGIN DECLARE SECTION;
646 int rowcount, flag, value, retval;
647 EXEC SQL END DECLARE SECTION;
649 EXEC SQL SELECT value INTO :value FROM numvalues
650 WHERE name = 'unix_uid';
651 if (sqlfail()) sqlexit();
652 if (sqlca.sqlerrd[2] != 1) {
654 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
659 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
660 if (sqlfail()) sqlexit();
661 if (sqlca.sqlerrd[2] == 0)
665 if (limit && value > MAX_ID_VALUE)
666 value = MIN_ID_VALUE;
668 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
669 if (sqlfail()) sqlexit();
670 if (sqlca.sqlerrd[2] == 0)
675 if (limit && value > MAX_ID_VALUE)
676 value = MIN_ID_VALUE;
677 EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
678 if (sqlca.sqlcode != 0) {
679 dbmserr("updating numvalues", sqlca.sqlcode);
688 dbmserr(NULL, sqlca.sqlcode);
689 EXEC SQL ROLLBACK WORK;
693 dbmserr(char *where, int what)
696 int bufsize=256, msglength=0;
698 sqlglm(err_msg, &bufsize, &msglength);
699 err_msg[msglength]=0;
702 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
704 com_err(whoami, 0, "DBMS error %s", err_msg);