3 * Load data into Moira from Personnel 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>
19 EXEC SQL INCLUDE sqlca;
20 extern void sqlglm(char *, unsigned int *, unsigned int *);
24 #define WHO 11859 /* root */
25 #define PROG "emp-tape"
27 #define MAX_ID_VALUE 31999
28 #define MIN_ID_VALUE 101
51 #define LOC_USERNAME 187
61 #define LEN_USERNAME 30
80 struct entry *get_next_entry(FILE *in);
81 void process_entry(struct entry *e);
82 void newuser(struct entry *e);
83 int set_next_users_id(int limit);
84 int set_next_uid(int high);
86 void dbmserr(char *where, int what);
89 int newfinger = 0, debug = 0, highid = 0;
91 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403)
92 #define SQL_DUPLICATE -2112
95 int main(int argc, char **argv)
100 char buf[80], *file = NULL;
101 EXEC SQL BEGIN DECLARE SECTION;
103 EXEC SQL END DECLARE SECTION;
105 whoami = strrchr(argv[0], '/');
111 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
112 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
114 for (i = 1; i < argc; i++)
116 if (!strcmp(argv[i], "-w"))
118 else if (!strcmp(argv[i], "-n"))
120 else if (!strcmp(argv[i], "-d"))
122 else if (!strcmp(argv[i], "-h"))
125 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
130 in = fopen(file, "r");
133 fprintf(stderr, "Unable to open %s for input\n", file);
137 initialize_sms_error_table();
139 EXEC SQL CONNECT :db IDENTIFIED BY :db;
142 dbmserr("opening database", sqlca.sqlcode);
146 while ((e = get_next_entry(in)))
149 EXEC SQL COMMIT WORK;
152 dbmserr("committing work", sqlca.sqlcode);
159 fgets(buf, sizeof(buf), stdin);
166 struct entry *get_next_entry(FILE *in)
168 static struct entry e;
169 static char buf[BUFSIZ];
170 static char name[LEN_NAME + 1], sname[LEN_NAME + 1], id[LEN_ID + 1];
171 static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
172 static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
173 static char username[LEN_USERNAME + 1], host[LEN_HOST + 1];
174 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
177 if (!fgets(buf, sizeof(buf), in))
180 strncpy(id, &buf[LOC_ID], LEN_ID);
182 strncpy(name, &buf[LOC_NAME], LEN_NAME);
183 name[LEN_NAME] = '\0';
184 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE);
185 office[LEN_OFFICE] = '\0';
186 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE);
187 phone[LEN_PHONE] = '\0';
188 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2);
189 phone2[LEN_PHONE2] = '\0';
190 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT);
191 dept[LEN_DEPT] = '\0';
192 strncpy(title, &buf[LOC_TITLE], LEN_TITLE);
193 title[LEN_TITLE] = '\0';
194 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME);
195 username[LEN_USERNAME] = '\0';
196 strncpy(host, &buf[LOC_HOST], LEN_HOST);
197 host[LEN_HOST] = '\0';
200 e.name = strtrim(sname);
201 p = strchr(name, ',');
204 e.last = strtrim(name);
211 if ((p = strchr(e.first, ' ')))
214 e.first = strtrim(e.first);
215 e.middle = strtrim(p + 1);
219 e.first = strtrim(e.first);
228 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
231 LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
233 LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
241 /* The following is really gross, but it happens to successfully convert
242 * new-style Warehouse office descriptions into (more-readable) old-style
243 * Personnel Office office descriptions.
245 e.address = p = strtrim(office);
246 while (*p && !isspace(*p))
251 if (*q && q < e.address + LEN_OFFICE / 2)
254 while (*q && q < e.address + LEN_OFFICE / 2)
256 if (*q != ' ' && *q != '-')
262 memset(p, ' ', q - p);
265 p = e.address + LEN_OFFICE / 2;
266 while (*p && !isspace(*p))
276 if (*q != ' ' && *q != '-')
282 memset(p, ' ', q - p);
286 e.phone = strtrim(phone);
287 e.phone2 = strtrim(phone2);
288 e.dept = strtrim(dept);
289 e.title = strtrim(title);
293 if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE"))
295 if (!strcmp(e.dept, "LINCOLN LAB"))
304 void process_entry(struct entry *e)
306 int changed, nochange;
307 char buf[MAX_FIELD_WIDTH], *from, *to;
308 EXEC SQL BEGIN DECLARE SECTION;
309 char *first, *last, *middle, *sid, *name, *rdept;
310 char *rtitle, *raddr, *rhphone, *rophone, *prog;
311 char class[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE];
312 char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
313 char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
314 char dmiddle[USERS_MIDDLE_SIZE];
316 EXEC SQL END DECLARE SECTION;
321 if (strlen(first) > USERS_FIRST_SIZE - 1)
322 first[USERS_FIRST_SIZE - 1] = '\0';
324 if (strlen(last) > USERS_LAST_SIZE - 1)
325 last[USERS_LAST_SIZE - 1] = '\0';
327 if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
328 middle[USERS_MIDDLE_SIZE - 1] = '\0';
333 EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
334 office_phone, department, status
335 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr,
336 :ophone, :dept, :status
338 WHERE clearid = :sid;
341 if (sqlca.sqlcode == SQL_DUPLICATE)
343 com_err(whoami, 0, "duplicate ID number %s on user %s %s",
356 /* Update class/state if necessary. (Exclude several spacial cases.) */
357 if (strcmp(e->class, strtrim(class)) &&
358 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
359 strcmp(class, "KNIGHT"))
361 com_err(whoami, 0, "updating class for %s %s from %s to %s",
362 first, last, class, e->class);
363 if (status == US_NOT_ALLOWED)
364 status = US_NO_LOGIN_YET;
365 if (status == US_ENROLL_NOT_ALLOWED)
366 status = US_ENROLLED;
367 strcpy(class, e->class);
370 EXEC SQL UPDATE users
371 SET type = NVL(:class, CHR(0)), status = :status,
372 modtime = SYSDATE, modby = :who, modwith = :prog
373 WHERE users_id = :id;
376 dbmserr("updating user", sqlca.sqlcode);
382 /* Update name if necessary */
383 if (strcmp(first, strtrim(dfirst)) ||
384 strcmp(last, strtrim(dlast)) ||
385 strcmp(middle, strtrim(dmiddle)))
387 com_err(whoami, 0, "updating real name for %s %s", first, last);
390 EXEC SQL UPDATE users
391 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
392 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
394 WHERE users_id = :id;
397 dbmserr("updating name", sqlca.sqlcode);
403 com_err(whoami, 0, "was %s %s %s, became %s %s %s",
404 dfirst, dmiddle, dlast, first, middle, last);
408 changed = nochange = 0;
409 strcpy(buf, e->address);
410 while ((to = strchr(buf, ',')))
412 while ((to = strchr(buf, ':')))
416 if (oaddr[0] == ' ' && buf[0])
418 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
419 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
422 else if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
427 if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
432 com_err(whoami, 0, "office for %s %s changed from %s to %s",
433 first, last, oaddr, buf);
436 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
437 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
450 if (ophone[0] == ' ')
452 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
453 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
455 else if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
460 if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
465 com_err(whoami, 0, "Phone for %s %s changed from %s to %s",
466 first, last, ophone, buf);
469 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
470 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
478 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
479 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
481 else if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
486 if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
491 com_err(whoami, 0, "Department for %s %s changed from %s to %s",
492 first, last, dept, e->dept);
495 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
496 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
511 com_err(whoami, 0, "updating finger for %s %s", first, last);
512 EXEC SQL UPDATE users
513 SET office_addr = NVL(:oaddr, CHR(0)),
514 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
515 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
516 xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
517 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
518 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
519 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
520 WHERE users_id = :id;
523 dbmserr(NULL, sqlca.sqlcode);
529 EXEC SQL UPDATE users
530 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
531 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
532 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
533 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
534 WHERE users_id = :id;
537 dbmserr(NULL, sqlca.sqlcode);
544 void newuser(struct entry *e)
547 EXEC SQL BEGIN DECLARE SECTION;
548 int id, uid, st, who;
549 char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid;
550 char fullname[USERS_FULLNAME_SIZE], *prog;
551 char oaddr[USERS_OFFICE_ADDR_SIZE], ophone[USERS_OFFICE_PHONE_SIZE];
552 char dept[USERS_DEPARTMENT_SIZE], *name, *title;
553 char *rdept, *rhphone, *rophone;
554 EXEC SQL END DECLARE SECTION;
558 strncpy(oaddr, e->address, USERS_OFFICE_ADDR_SIZE - 1);
559 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
560 while ((to = strchr(oaddr, ',')))
562 while ((to = strchr(oaddr, ':')))
574 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
575 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
577 id = set_next_users_id(0);
578 uid = set_next_uid(e->highid);
579 sprintf(login, "#%d", uid);
585 sprintf(fullname, "%s %s %s", first, middle, last);
587 sprintf(fullname, "%s %s", first, last);
588 st = US_NO_LOGIN_YET;
597 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
601 EXEC SQL INSERT INTO users
602 (login, users_id, unix_uid, shell, last, first, middle, status,
603 clearid, type, modtime, modby, modwith, fullname, office_addr,
604 office_phone, department, fmodtime, fmodby, fmodwith,
605 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
606 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
607 NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
608 :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog,
609 NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)),
610 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
611 NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)),
612 NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)),
616 dbmserr("adding user", sqlca.sqlcode);
622 int set_next_users_id(int limit)
624 EXEC SQL BEGIN DECLARE SECTION;
625 int flag, value, retval;
626 EXEC SQL END DECLARE SECTION;
631 EXEC SQL SELECT value INTO :value FROM numvalues
632 WHERE name = 'users_id';
635 if (sqlca.sqlerrd[2] != 1)
638 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
643 EXEC SQL SELECT users_id INTO :flag FROM users
644 WHERE users_id = :value;
647 if (sqlca.sqlerrd[2] == 0)
652 if (limit && value > MAX_ID_VALUE)
653 value = MIN_ID_VALUE;
655 EXEC SQL SELECT users_id INTO :flag FROM users
656 WHERE users_id = :value;
659 if (sqlca.sqlerrd[2] == 0)
664 if (limit && value > MAX_ID_VALUE)
665 value = MIN_ID_VALUE;
666 EXEC SQL UPDATE numvalues SET value = :value
667 WHERE name = 'users_id';
670 dbmserr("assigning ID", sqlca.sqlcode);
676 int set_next_uid(int high)
678 EXEC SQL BEGIN DECLARE SECTION;
679 int flag, initial, value, retval;
681 EXEC SQL END DECLARE SECTION;
691 EXEC SQL SELECT value INTO :initial FROM numvalues
695 if (sqlca.sqlerrd[2] != 1)
698 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
704 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
705 FROM users WHERE unix_uid = :value;
711 if (!high && value > MAX_ID_VALUE)
712 value = MIN_ID_VALUE;
713 if (value == initial)
715 com_err(whoami, 0, "Out of uids!");
716 EXEC SQL ROLLBACK WORK;
720 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
721 FROM users WHERE unix_uid = :value;
727 if (!high && value > MAX_ID_VALUE)
728 value = MIN_ID_VALUE;
729 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name;
732 dbmserr("assigning ID", sqlca.sqlcode);
741 dbmserr(NULL, sqlca.sqlcode);
742 EXEC SQL ROLLBACK WORK;
746 void dbmserr(char *where, int what)
749 int bufsize = 256, msglength = 0;
751 sqlglm(err_msg, &bufsize, &msglength);
752 err_msg[msglength] = '\0';
755 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
757 com_err(whoami, 0, "DBMS error %s", err_msg);