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)
83 int main(int argc, char **argv)
86 struct entry *e, *get_next_entry();
88 char buf[BUFSIZ], *file = NULL;
89 EXEC SQL BEGIN DECLARE SECTION;
91 EXEC SQL END DECLARE SECTION;
93 whoami = strrchr(argv[0], '/');
99 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
100 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
102 for (i = 1; i < argc; i++)
104 if (!strcmp(argv[i], "-w"))
106 else if (!strcmp(argv[i], "-D"))
107 setenv("ING_SET", "set printqry");
108 else if (!strcmp(argv[i], "-n"))
111 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
116 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;
128 dbmserr("connecting", sqlca.sqlcode);
132 while ((e = get_next_entry(in)))
136 EXEC SQL COMMIT WORK;
139 dbmserr("committing work", sqlca.sqlcode);
154 struct entry *get_next_entry(FILE *in)
156 static struct entry e;
157 static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
158 static char name[LEN_NAME + 1], id[LEN_ID + 1], course[LEN_COURSE + 1];
159 static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
160 static char dorm_room[LEN_DORM_ROOM + 1], city[LEN_CITY + 1];
161 static char state[LEN_STATE + 1], dphone[LEN_DPHONE + 1];
162 static char mphone[LEN_MPHONE + 1], sname[LEN_NAME + 1], title[128];
163 static int nyear = 0;
164 int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
172 gettimeofday(&tv, NULL);
173 tm = localtime(&tv.tv_sec);
179 if (!fgets(buf, sizeof(buf), in))
182 strncpy(name, &buf[LOC_NAME], LEN_NAME);
183 name[LEN_NAME] = '\0';
184 strncpy(id, &buf[LOC_ID], LEN_ID);
186 strncpy(course, &buf[LOC_COURSE], LEN_COURSE);
187 course[LEN_COURSE] = '\0';
188 strncpy(year, &buf[LOC_YEAR], LEN_YEAR);
189 year[LEN_YEAR] = '\0';
190 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS);
191 address[LEN_ADDRESS] = '\0';
192 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM);
193 dorm_room[LEN_DORM_ROOM] = '\0';
194 strncpy(city, &buf[LOC_CITY], LEN_CITY);
195 city[LEN_CITY] = '\0';
196 strncpy(state, &buf[LOC_STATE], LEN_STATE);
197 state[LEN_STATE] = '\0';
198 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE);
199 dphone[LEN_DPHONE] = '\0';
200 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE);
201 mphone[LEN_MPHONE] = '\0';
204 e.name = strtrim(sname);
205 p = strchr(name, ',');
208 e.last = strtrim(name);
215 if (p = strchr(e.first, ' '))
218 e.first = strtrim(e.first);
219 e.middle = strtrim(p + 1);
223 e.first = strtrim(e.first);
232 ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
235 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
237 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
246 EncryptID(e.eid, e.id, e.first, e.last);
248 e.year = strtrim(year);
250 if (e.year[0] == 'G')
253 sprintf(title, "Grad Student");
258 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
259 sprintf(title, "Undergrad (class of %s)", classbuf);
262 e.course = strtrim(course);
263 e.address = strtrim(address);
264 e.dorm = strtrim(dorm_room);
265 e.city = strtrim(city);
266 e.state = strtrim(state);
267 e.dphone = strtrim(dphone);
268 e.mphone = strtrim(mphone);
273 process_entry(struct entry *e)
275 int changed, nochange, encrypted;
276 char buf[BUFSIZ], *from, *to;
277 EXEC SQL BEGIN DECLARE SECTION;
278 char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept;
279 char *rtitle, *rophone, *rhphone, *prog;
280 char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
281 char dfirst[17], dlast[17], dmiddle[17];
283 EXEC SQL END DECLARE SECTION;
288 if (strlen(first) > 16)
291 if (strlen(last) > 16)
300 EXEC SQL SELECT users_id, first, last, middle, type, home_addr, home_phone,
301 office_phone, status, department
302 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone,
303 :ophone, :status, :dept
305 WHERE clearid = :sid;
308 if (sqlca.sqlcode == SQL_DUPLICATE)
310 com_err(whoami, 0, "duplicate ID number %s on user %s %s",
319 EXEC SQL SELECT users_id, first, last, middle, type, home_addr,
320 home_phone, office_phone, status, department
321 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr,
322 :hphone, :ophone, :status, :dept
324 WHERE last = :last and first = :first and clearid = :eid;
335 /* See if class changed: if it's different, and the value in the database
336 * is not STAFF or SIPBMEM, and the value from the tape is actually
337 * meaningful, then update the database. Since they were on the
338 * students tape, make the account usable.
340 if (strcmp(e->class, strtrim(class)) &&
341 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
344 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
345 first, last, class, e->class);
346 if (status == US_NOT_ALLOWED)
347 status = US_NO_LOGIN_YET;
348 if (status == US_ENROLL_NOT_ALLOWED)
349 status = US_ENROLLED;
350 strcpy(class, e->class);
351 EXEC SQL UPDATE users
352 SET type = NVL(:class, CHR(0)), status = :status, modtime = SYSDATE,
353 modby = :who, modwith = :prog
354 WHERE users_id = :id;
357 dbmserr("updating class", sqlca.sqlcode);
362 /* Update name if necessary */
363 if (strcmp(first, strtrim(dfirst)) ||
364 strcmp(last, strtrim(dlast)) ||
365 strcmp(middle, strtrim(dmiddle)))
367 com_err(whoami, 0, "updating real name for %s %s", first, last);
368 EXEC SQL UPDATE users
369 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
370 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
372 WHERE users_id = :id;
373 if (sqlca.sqlcode != 0)
375 dbmserr("updating name", sqlca.sqlcode);
380 /* Deal with updating the finger info if necessary */
381 changed = nochange = 0;
384 strcpy(buf, e->address);
388 strcat(buf, e->dorm);
393 strcat(buf, e->city);
399 strcat(buf, e->state);
401 while ((to = strchr(buf, ',')))
403 while ((to = strchr(buf, ':')))
409 strncpy(haddr, buf, 80);
413 else if (strncmp(strtrim(haddr), buf, 80))
418 if (strncmp(strtrim(haddr), buf, 80))
420 strncpy(haddr, buf, 80);
434 if (hphone[0] == ' ')
436 strncpy(hphone, buf, 16);
439 else if (strncmp(strtrim(hphone), buf, 16))
444 if (strncmp(strtrim(hphone), buf, 16))
446 strncpy(hphone, buf, 16);
460 if (ophone[0] == ' ')
462 strncpy(ophone, buf, 12);
465 else if (strncmp(strtrim(ophone), buf, 12))
470 if (strncmp(strtrim(ophone), buf, 12))
472 strncpy(ophone, buf, 12);
475 e->course = e->course;
480 strncpy(dept, e->course, 12);
483 else if (strncmp(strtrim(dept), e->course, 11))
488 if (strncmp(strtrim(dept), e->course, 11))
490 strncpy(dept, e->course, 12);
499 strcpy(raddr, e->address);
503 strcat(raddr, e->dorm);
508 strcat(raddr, e->city);
513 if (isupper(e->state[0]) && isupper(e->state[1]) &&
514 isdigit(e->state[2]) && isdigit(e->state[3]) &&
515 isdigit(e->state[4]) && isdigit(e->state[5]) &&
516 isdigit(e->state[6]))
518 sprintf(buf, "%c%c %s", e->state[0], e->state[1],
523 strcat(raddr, e->state);
529 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
533 com_err(whoami, 0, "updating finger for %s %s", first, last);
534 EXEC SQL UPDATE users
535 SET home_addr = NVL(:haddr, CHR(0)), home_phone = NVL(:hphone, CHR(0)),
536 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
537 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
538 xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
539 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
540 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
541 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
542 WHERE users_id = :id;
545 dbmserr(NULL, sqlca.sqlcode);
551 EXEC SQL UPDATE users
552 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
553 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
554 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
555 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
556 WHERE users_id = :id;
559 dbmserr(NULL, sqlca.sqlcode);
566 newuser(struct entry *e)
568 char buf[512], *from, *to;
569 EXEC SQL BEGIN DECLARE SECTION;
571 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
572 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
573 EXEC SQL END DECLARE SECTION;
577 strcpy(buf, e->address);
581 strcat(buf, e->dorm);
586 strcat(buf, e->city);
591 strcat(buf, e->state);
593 strncpy(haddr, buf, 80);
603 strncpy(hphone, buf, 16);
613 strncpy(ophone, buf, 12);
614 strncpy(dept, e->course, 12);
616 id = set_next_users_id(0);
617 uid = set_next_uid(1);
618 sprintf(login, "#%d", uid);
626 sprintf(fullname, "%s %s %s", first, middle, last);
628 sprintf(fullname, "%s %s", first, last);
630 strcpy(raddr, e->address);
634 strcat(raddr, e->dorm);
639 strcat(raddr, e->city);
644 if (isupper(e->state[0]) && isupper(e->state[1]) &&
645 isdigit(e->state[2]) && isdigit(e->state[3]) &&
646 isdigit(e->state[4]) && isdigit(e->state[5]) &&
647 isdigit(e->state[6]))
649 sprintf(buf, "%c%c %s", e->state[0], e->state[1],
654 strcat(raddr, e->state);
660 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
662 EXEC SQL INSERT INTO users
663 (login, users_id, unix_uid, shell, last, first, middle, status,
664 clearid, type, modtime, modby, modwith, fullname, home_addr,
665 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
666 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
667 VALUES (:login, :id, :uid, '/bin/athena/tcsh', NVL(:last, CHR(0)),
668 NVL(:first, CHR(0)), NVL(:middle, CHR(0)), 0, NVL(:sid, CHR(0)),
669 NVL(:class, CHR(0)), SYSDATE, :who, :prog, NVL(:fullname, CHR(0)),
670 NVL(:haddr, CHR(0)), NVL(:hphone, CHR(0)), NVL(:ophone, CHR(0)),
671 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
672 NVL(:name, CHR(0)), NVL(:dept, CHR(0)), NVL(:title, CHR(0)),
673 NVL(:raddr, CHR(0)), NVL(:hphone, CHR(0)), NVL(:ophone, CHR(0)),
677 dbmserr("adding user", sqlca.sqlcode);
681 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
685 int set_next_users_id(int limit)
687 EXEC SQL BEGIN DECLARE SECTION;
688 int rowcount, flag, value, retval;
689 EXEC SQL END DECLARE SECTION;
691 EXEC SQL SELECT value INTO :value FROM numvalues
692 WHERE name = 'users_id';
695 if (sqlca.sqlerrd[2] != 1)
698 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
703 EXEC SQL SELECT users_id INTO :flag FROM users
704 WHERE users_id = :value;
707 if (sqlca.sqlerrd[2] == 0)
712 if (limit && value > MAX_ID_VALUE)
713 value = MIN_ID_VALUE;
715 EXEC SQL SELECT users_id INTO :flag FROM users
716 WHERE users_id = :value;
719 if (sqlca.sqlerrd[2] == 0)
724 if (limit && value > MAX_ID_VALUE)
725 value = MIN_ID_VALUE;
726 EXEC SQL UPDATE numvalues SET value = :value
727 WHERE name = 'users_id';
730 dbmserr("updating numvalues", sqlca.sqlcode);
736 int set_next_uid(int limit)
738 EXEC SQL BEGIN DECLARE SECTION;
739 int rowcount, flag, value, retval;
740 EXEC SQL END DECLARE SECTION;
742 EXEC SQL SELECT value INTO :value FROM numvalues
743 WHERE name = 'unix_uid';
746 if (sqlca.sqlerrd[2] != 1)
749 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
754 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
757 if (sqlca.sqlerrd[2] == 0)
762 if (limit && value > MAX_ID_VALUE)
763 value = MIN_ID_VALUE;
765 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
768 if (sqlca.sqlerrd[2] == 0)
773 if (limit && value > MAX_ID_VALUE)
774 value = MIN_ID_VALUE;
775 EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
778 dbmserr("updating numvalues", sqlca.sqlcode);
787 dbmserr(NULL, sqlca.sqlcode);
788 EXEC SQL ROLLBACK WORK;
792 dbmserr(char *where, int what)
795 int bufsize = 256, msglength = 0;
797 sqlglm(err_msg, &bufsize, &msglength);
798 err_msg[msglength] = '\0';
801 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
803 com_err(whoami, 0, "DBMS error %s", err_msg);