3 * Load data into Moira from Registrar's Office data file
5 * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
12 #include <moira_site.h>
13 #include <moira_schema.h>
21 EXEC SQL INCLUDE sqlca;
22 extern void sqlglm(char *, unsigned int *, unsigned int *);
26 #define WHO 11859 /* root */
27 #define PROG "stu-tape"
29 #define MAX_ID_VALUE 31999
30 #define MIN_ID_VALUE 101
52 #define LOC_ADDRESS 80
53 #define LOC_DORM_ROOM 110
56 #define LOC_DPHONE 155
57 #define LOC_MPHONE 243
63 #define LEN_ADDRESS 30
64 #define LEN_DORM_ROOM 15
91 struct entry *get_next_entry(FILE *in);
92 void process_entry(struct entry *e);
93 void newuser(struct entry *e);
94 int set_next_users_id(int limit);
95 int set_next_uid(int limit);
97 void dbmserr(char *where, int what);
99 #define SQL_DUPLICATE -2112
100 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403)
102 int main(int argc, char **argv)
107 char buf[80], *file = NULL;
108 EXEC SQL BEGIN DECLARE SECTION;
110 EXEC SQL END DECLARE SECTION;
112 whoami = strrchr(argv[0], '/');
118 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
119 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
121 for (i = 1; i < argc; i++)
123 if (!strcmp(argv[i], "-w"))
125 else if (!strcmp(argv[i], "-n"))
128 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
133 in = fopen(file, "r");
136 fprintf(stderr, "Unable to open %s for input\n", file);
140 initialize_sms_error_table();
142 EXEC SQL CONNECT :db IDENTIFIED BY :db;
145 dbmserr("connecting", sqlca.sqlcode);
149 while ((e = get_next_entry(in)))
152 EXEC SQL COMMIT WORK;
155 dbmserr("committing work", sqlca.sqlcode);
162 fgets(buf, sizeof(buf), stdin);
170 struct entry *get_next_entry(FILE *in)
172 static struct entry e;
173 static char buf[BUFSIZ], classbuf[10];
174 static char name[LEN_NAME + 1], id[LEN_ID + 1], course[LEN_COURSE + 1];
175 static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1];
176 static char dorm_room[LEN_DORM_ROOM + 1], city[LEN_CITY + 1];
177 static char state[LEN_STATE + 1], dphone[LEN_DPHONE + 1];
178 static char mphone[LEN_MPHONE + 1], sname[LEN_NAME + 1], title[128];
179 static int nyear = 0;
180 int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
188 gettimeofday(&tv, NULL);
189 tm = localtime(&tv.tv_sec);
195 if (!fgets(buf, sizeof(buf), in))
198 strncpy(name, &buf[LOC_NAME], LEN_NAME);
199 name[LEN_NAME] = '\0';
200 strncpy(id, &buf[LOC_ID], LEN_ID);
202 strncpy(course, &buf[LOC_COURSE], LEN_COURSE);
203 course[LEN_COURSE] = '\0';
204 strncpy(year, &buf[LOC_YEAR], LEN_YEAR);
205 year[LEN_YEAR] = '\0';
206 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS);
207 address[LEN_ADDRESS] = '\0';
208 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM);
209 dorm_room[LEN_DORM_ROOM] = '\0';
210 strncpy(city, &buf[LOC_CITY], LEN_CITY);
211 city[LEN_CITY] = '\0';
212 strncpy(state, &buf[LOC_STATE], LEN_STATE);
213 state[LEN_STATE] = '\0';
214 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE);
215 dphone[LEN_DPHONE] = '\0';
216 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE);
217 mphone[LEN_MPHONE] = '\0';
220 e.name = strtrim(sname);
221 p = strchr(name, ',');
224 e.last = strtrim(name);
231 if ((p = strchr(e.first, ' ')))
234 e.first = strtrim(e.first);
235 e.middle = strtrim(p + 1);
239 e.first = strtrim(e.first);
248 ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0;
251 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
253 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
262 e.year = strtrim(year);
264 if (e.year[0] == 'G')
267 sprintf(title, "Grad Student");
272 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
273 sprintf(title, "Undergrad (class of %s)", classbuf);
276 e.course = strtrim(course);
277 e.address = strtrim(address);
278 e.dorm = strtrim(dorm_room);
279 e.city = strtrim(city);
280 e.state = strtrim(state);
281 e.dphone = strtrim(dphone);
282 e.mphone = strtrim(mphone);
287 void process_entry(struct entry *e)
289 int changed, nochange;
290 char buf[MAX_FIELD_WIDTH], *from, *to;
291 EXEC SQL BEGIN DECLARE SECTION;
292 char *first, *last, *middle, *sid, *name, *rdept;
293 char *rtitle, *rophone, *rhphone, *prog;
294 char class[USERS_TYPE_SIZE], haddr[USERS_HOME_ADDR_SIZE];
295 char hphone[USERS_HOME_PHONE_SIZE], ophone[USERS_OFFICE_PHONE_SIZE];
296 char dept[USERS_DEPARTMENT_SIZE], raddr[USERS_XADDRESS_SIZE];
297 char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
298 char dmiddle[USERS_MIDDLE_SIZE];
300 EXEC SQL END DECLARE SECTION;
305 if (strlen(first) > USERS_FIRST_SIZE - 1)
306 first[USERS_FIRST_SIZE - 1] = '\0';
308 if (strlen(last) > USERS_LAST_SIZE - 1)
309 last[USERS_LAST_SIZE - 1] = '\0';
311 if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
312 middle[USERS_MIDDLE_SIZE - 1] = '\0';
317 EXEC SQL SELECT users_id, first, last, middle, type, home_addr, home_phone,
318 office_phone, status, department
319 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone,
320 :ophone, :status, :dept
322 WHERE clearid = :sid;
325 if (sqlca.sqlcode == SQL_DUPLICATE)
327 com_err(whoami, 0, "duplicate ID number %s on user %s %s",
340 /* See if class changed: if it's different, and the value in the database
341 * is not STAFF or SIPBMEM, and the value from the tape is actually
342 * meaningful, then update the database. Since they were on the
343 * students tape, make the account usable.
345 if (strcmp(e->class, strtrim(class)) &&
346 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
349 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
350 first, last, class, e->class);
351 if (status == US_NOT_ALLOWED)
352 status = US_NO_LOGIN_YET;
353 if (status == US_ENROLL_NOT_ALLOWED)
354 status = US_ENROLLED;
355 strcpy(class, e->class);
356 EXEC SQL UPDATE users
357 SET type = NVL(:class, CHR(0)), status = :status, modtime = SYSDATE,
358 modby = :who, modwith = :prog
359 WHERE users_id = :id;
362 dbmserr("updating class", sqlca.sqlcode);
367 /* Update name if necessary */
368 if (strcmp(first, strtrim(dfirst)) ||
369 strcmp(last, strtrim(dlast)) ||
370 strcmp(middle, strtrim(dmiddle)))
372 com_err(whoami, 0, "updating real name for %s %s", first, last);
373 EXEC SQL UPDATE users
374 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
375 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
377 WHERE users_id = :id;
378 if (sqlca.sqlcode != 0)
380 dbmserr("updating name", sqlca.sqlcode);
385 /* Deal with updating the finger info if necessary */
386 changed = nochange = 0;
387 strcpy(buf, e->address);
391 strcat(buf, e->dorm);
396 strcat(buf, e->city);
402 strcat(buf, e->state);
404 while ((to = strchr(buf, ',')))
406 while ((to = strchr(buf, ':')))
412 strncpy(haddr, buf, USERS_HOME_ADDR_SIZE - 1);
413 haddr[USERS_HOME_ADDR_SIZE - 1] = '\0';
416 else if (strncmp(strtrim(haddr), buf, USERS_HOME_ADDR_SIZE - 1))
421 if (strncmp(strtrim(haddr), buf, USERS_HOME_ADDR_SIZE - 1))
423 strncpy(haddr, buf, USERS_HOME_ADDR_SIZE - 1);
424 haddr[USERS_HOME_ADDR_SIZE - 1] = '\0';
437 if (hphone[0] == ' ')
439 strncpy(hphone, buf, USERS_HOME_PHONE_SIZE - 1);
440 hphone[USERS_HOME_PHONE_SIZE - 1] = '\0';
442 else if (strncmp(strtrim(hphone), buf, USERS_HOME_PHONE_SIZE - 1))
447 if (strncmp(strtrim(hphone), buf, USERS_HOME_PHONE_SIZE - 1))
449 strncpy(hphone, buf, USERS_HOME_PHONE_SIZE - 1);
450 hphone[USERS_HOME_PHONE_SIZE - 1] = '\0';
463 if (ophone[0] == ' ')
465 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
466 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
468 else if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
473 if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
475 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
476 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
478 e->course = e->course;
483 strncpy(dept, e->course, USERS_DEPARTMENT_SIZE - 1);
484 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
486 else if (strncmp(strtrim(dept), e->course, USERS_DEPARTMENT_SIZE - 1))
491 if (strncmp(strtrim(dept), e->course, USERS_DEPARTMENT_SIZE - 1))
493 strncpy(dept, e->course, USERS_DEPARTMENT_SIZE - 1);
494 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
502 strcpy(raddr, e->address);
506 strcat(raddr, e->dorm);
511 strcat(raddr, e->city);
516 if (isupper(e->state[0]) && isupper(e->state[1]) &&
517 isdigit(e->state[2]) && isdigit(e->state[3]) &&
518 isdigit(e->state[4]) && isdigit(e->state[5]) &&
519 isdigit(e->state[6]))
521 sprintf(buf, "%c%c %s", e->state[0], e->state[1],
526 strcat(raddr, e->state);
532 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
536 com_err(whoami, 0, "updating finger for %s %s", first, last);
537 EXEC SQL UPDATE users
538 SET home_addr = NVL(:haddr, CHR(0)), home_phone = NVL(:hphone, CHR(0)),
539 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
540 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
541 xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
542 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
543 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
544 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
545 WHERE users_id = :id;
548 dbmserr(NULL, sqlca.sqlcode);
554 EXEC SQL UPDATE users
555 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
556 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
557 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
558 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
559 WHERE users_id = :id;
562 dbmserr(NULL, sqlca.sqlcode);
568 void newuser(struct entry *e)
570 char buf[512], *from, *to;
571 EXEC SQL BEGIN DECLARE SECTION;
573 char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid;
574 char fullname[USERS_FULLNAME_SIZE], *prog;
575 char haddr[USERS_HOME_ADDR_SIZE], hphone[USERS_HOME_PHONE_SIZE];
576 char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
577 char *title, raddr[USERS_XADDRESS_SIZE], *name;
578 EXEC SQL END DECLARE SECTION;
582 strcpy(buf, e->address);
586 strcat(buf, e->dorm);
591 strcat(buf, e->city);
596 strcat(buf, e->state);
598 strncpy(haddr, buf, USERS_HOME_ADDR_SIZE - 1);
599 haddr[USERS_HOME_ADDR_SIZE - 1] = '\0';
609 strncpy(hphone, buf, USERS_HOME_PHONE_SIZE - 1);
610 hphone[USERS_HOME_PHONE_SIZE - 1] = '\0';
620 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
621 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
622 strncpy(dept, e->course, USERS_DEPARTMENT_SIZE - 1);
623 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
625 id = set_next_users_id(0);
626 uid = set_next_uid(1);
627 sprintf(login, "#%d", uid);
635 sprintf(fullname, "%s %s %s", first, middle, last);
637 sprintf(fullname, "%s %s", first, last);
639 strcpy(raddr, e->address);
643 strcat(raddr, e->dorm);
648 strcat(raddr, e->city);
653 if (isupper(e->state[0]) && isupper(e->state[1]) &&
654 isdigit(e->state[2]) && isdigit(e->state[3]) &&
655 isdigit(e->state[4]) && isdigit(e->state[5]) &&
656 isdigit(e->state[6]))
658 sprintf(buf, "%c%c %s", e->state[0], e->state[1],
663 strcat(raddr, e->state);
669 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
671 EXEC SQL INSERT INTO users
672 (login, users_id, unix_uid, shell, winconsoleshell, last, first,
673 middle, status, clearid, type, modtime, modby, modwith, fullname,
674 home_addr, home_phone, office_phone, department, fmodtime, fmodby,
675 fmodwith, potype, xname, xdept, xtitle, xaddress, xphone1, xphone2,
677 VALUES (:login, :id, :uid, '/bin/athena/tcsh', 'cmd', NVL(:last, CHR(0)),
678 NVL(:first, CHR(0)), NVL(:middle, CHR(0)), 0, NVL(:sid, CHR(0)),
679 NVL(:class, CHR(0)), SYSDATE, :who, :prog, NVL(:fullname, CHR(0)),
680 NVL(:haddr, CHR(0)), NVL(:hphone, CHR(0)), NVL(:ophone, CHR(0)),
681 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
682 NVL(:name, CHR(0)), NVL(:dept, CHR(0)), NVL(:title, CHR(0)),
683 NVL(:raddr, CHR(0)), NVL(:hphone, CHR(0)), NVL(:ophone, CHR(0)),
687 dbmserr("adding user", sqlca.sqlcode);
691 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
695 int set_next_users_id(int limit)
697 EXEC SQL BEGIN DECLARE SECTION;
698 int flag, value, retval;
699 EXEC SQL END DECLARE SECTION;
701 EXEC SQL SELECT value INTO :value FROM numvalues
702 WHERE name = 'users_id';
705 if (sqlca.sqlerrd[2] != 1)
708 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
713 EXEC SQL SELECT users_id INTO :flag FROM users
714 WHERE users_id = :value;
717 if (sqlca.sqlerrd[2] == 0)
722 if (limit && value > MAX_ID_VALUE)
723 value = MIN_ID_VALUE;
725 EXEC SQL SELECT users_id INTO :flag FROM users
726 WHERE users_id = :value;
729 if (sqlca.sqlerrd[2] == 0)
734 if (limit && value > MAX_ID_VALUE)
735 value = MIN_ID_VALUE;
736 EXEC SQL UPDATE numvalues SET value = :value
737 WHERE name = 'users_id';
740 dbmserr("updating numvalues", sqlca.sqlcode);
746 int set_next_uid(int limit)
748 EXEC SQL BEGIN DECLARE SECTION;
749 int flag, value, retval;
750 EXEC SQL END DECLARE SECTION;
752 EXEC SQL SELECT value INTO :value FROM numvalues
753 WHERE name = 'unix_uid';
756 if (sqlca.sqlerrd[2] != 1)
759 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
764 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
767 if (sqlca.sqlerrd[2] == 0)
772 if (limit && value > MAX_ID_VALUE)
773 value = MIN_ID_VALUE;
775 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
778 if (sqlca.sqlerrd[2] == 0)
783 if (limit && value > MAX_ID_VALUE)
784 value = MIN_ID_VALUE;
785 EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
788 dbmserr("updating numvalues", sqlca.sqlcode);
797 dbmserr(NULL, sqlca.sqlcode);
798 EXEC SQL ROLLBACK WORK;
802 void dbmserr(char *where, int what)
805 int bufsize = 256, msglength = 0;
807 sqlglm(err_msg, &bufsize, &msglength);
808 err_msg[msglength] = '\0';
811 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
813 com_err(whoami, 0, "DBMS error %s", err_msg);