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
79 struct entry *get_next_entry(FILE *in);
80 void process_entry(struct entry *e);
81 void newuser(struct entry *e);
82 int set_next_users_id(int limit);
83 int set_next_uid(void);
85 void dbmserr(char *where, int what);
89 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403)
90 #define SQL_DUPLICATE -2112
93 int main(int argc, char **argv)
98 char buf[80], *file = NULL;
99 EXEC SQL BEGIN DECLARE SECTION;
101 EXEC SQL END DECLARE SECTION;
103 whoami = strrchr(argv[0], '/');
109 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
110 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
112 for (i = 1; i < argc; i++)
114 if (!strcmp(argv[i], "-w"))
117 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
122 in = fopen(file, "r");
125 fprintf(stderr, "Unable to open %s for input\n", file);
129 initialize_sms_error_table();
131 EXEC SQL CONNECT :db IDENTIFIED BY :db;
134 dbmserr("opening database", sqlca.sqlcode);
138 while ((e = get_next_entry(in)))
141 EXEC SQL COMMIT WORK;
144 dbmserr("committing work", sqlca.sqlcode);
151 fgets(buf, sizeof(buf), stdin);
158 struct entry *get_next_entry(FILE *in)
160 static struct entry e;
161 static char buf[BUFSIZ];
162 static char name[LEN_NAME + 1], sname[LEN_NAME + 1], id[LEN_ID + 1];
163 static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
164 static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
165 static char username[LEN_USERNAME + 1], host[LEN_HOST + 1];
166 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
169 if (!fgets(buf, sizeof(buf), in))
172 strncpy(id, &buf[LOC_ID], LEN_ID);
174 strncpy(name, &buf[LOC_NAME], LEN_NAME);
175 name[LEN_NAME] = '\0';
176 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE);
177 office[LEN_OFFICE] = '\0';
178 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE);
179 phone[LEN_PHONE] = '\0';
180 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2);
181 phone2[LEN_PHONE2] = '\0';
182 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT);
183 dept[LEN_DEPT] = '\0';
184 strncpy(title, &buf[LOC_TITLE], LEN_TITLE);
185 title[LEN_TITLE] = '\0';
186 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME);
187 username[LEN_USERNAME] = '\0';
188 strncpy(host, &buf[LOC_HOST], LEN_HOST);
189 host[LEN_HOST] = '\0';
192 e.name = strtrim(sname);
193 p = strchr(name, ',');
196 e.last = strtrim(name);
203 if ((p = strchr(e.first, ' ')))
206 e.first = strtrim(e.first);
207 e.middle = strtrim(p + 1);
211 e.first = strtrim(e.first);
220 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
223 LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
225 LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
233 /* The following is really gross, but it happens to successfully convert
234 * new-style Warehouse office descriptions into (more-readable) old-style
235 * Personnel Office office descriptions.
237 e.address = p = strtrim(office);
238 while (*p && !isspace(*p))
243 if (*q && q < e.address + LEN_OFFICE / 2)
246 while (*q && q < e.address + LEN_OFFICE / 2)
248 if (*q != ' ' && *q != '-')
254 memset(p, ' ', q - p);
257 p = e.address + LEN_OFFICE / 2;
258 while (*p && !isspace(*p))
268 if (*q != ' ' && *q != '-')
274 memset(p, ' ', q - p);
278 e.phone = strtrim(phone);
279 e.phone2 = strtrim(phone2);
280 e.dept = strtrim(dept);
281 e.title = strtrim(title);
284 if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE"))
286 if (!strcmp(e.dept, "LINCOLN LAB"))
292 void process_entry(struct entry *e)
295 char buf[MAX_FIELD_WIDTH], *from, *to;
296 EXEC SQL BEGIN DECLARE SECTION;
297 char *first, *last, *middle, *sid, *name, *rdept;
298 char *rtitle, *raddr, *rhphone, *rophone, *prog;
299 char class[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE];
300 char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
301 char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
302 char dmiddle[USERS_MIDDLE_SIZE];
304 EXEC SQL END DECLARE SECTION;
309 if (strlen(first) > USERS_FIRST_SIZE - 1)
310 first[USERS_FIRST_SIZE - 1] = '\0';
312 if (strlen(last) > USERS_LAST_SIZE - 1)
313 last[USERS_LAST_SIZE - 1] = '\0';
315 if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
316 middle[USERS_MIDDLE_SIZE - 1] = '\0';
321 EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
322 office_phone, department, status
323 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr,
324 :ophone, :dept, :status
326 WHERE clearid = :sid AND status != 3;
329 if (sqlca.sqlcode == SQL_DUPLICATE)
331 com_err(whoami, 0, "duplicate ID number %s on user %s %s",
348 /* Update class/state if necessary. (Exclude several spacial cases.) */
349 if (strcmp(e->class, class) &&
350 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
351 strcmp(class, "KNIGHT"))
353 com_err(whoami, 0, "updating class for %s %s from \"%s\" to \"%s\"",
354 first, last, class, e->class);
355 if (status == US_NOT_ALLOWED)
356 status = US_NO_LOGIN_YET;
357 if (status == US_ENROLL_NOT_ALLOWED)
358 status = US_ENROLLED;
359 strcpy(class, e->class);
360 EXEC SQL UPDATE users
361 SET type = NVL(:class, CHR(0)), status = :status,
362 modtime = SYSDATE, modby = :who, modwith = :prog
363 WHERE users_id = :id;
366 dbmserr("updating user", sqlca.sqlcode);
371 /* Update name if necessary */
372 if (strcmp(first, strtrim(dfirst)) ||
373 strcmp(last, strtrim(dlast)) ||
374 strcmp(middle, strtrim(dmiddle)))
376 com_err(whoami, 0, "updating real name for %s%s%s %s (was %s%s%s %s)",
377 first, *middle ? " " : "", middle, last,
378 dfirst, *dmiddle ? " " : "", dmiddle, dlast);
379 EXEC SQL UPDATE users
380 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
381 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
383 WHERE users_id = :id;
386 dbmserr("updating name", sqlca.sqlcode);
392 strcpy(buf, e->address);
393 while ((to = strchr(buf, ',')))
395 while ((to = strchr(buf, ':')))
397 if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
400 com_err(whoami, 0, "office for %s %s changed from \"%s\" to \"%s\"",
401 first, last, oaddr, buf);
403 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
404 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
415 if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
418 com_err(whoami, 0, "Phone for %s %s changed from \"%s\" to \"%s\"",
419 first, last, ophone, buf);
421 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
422 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
426 if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
429 com_err(whoami, 0, "Department for %s %s changed from \"%s\" to \"%s\"",
430 first, last, dept, e->dept);
432 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
433 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
445 com_err(whoami, 0, "updating finger for %s %s", first, last);
446 EXEC SQL UPDATE users
447 SET office_addr = NVL(:oaddr, CHR(0)),
448 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
449 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
450 xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
451 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
452 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
453 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
454 WHERE users_id = :id;
457 dbmserr(NULL, sqlca.sqlcode);
463 EXEC SQL UPDATE users
464 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
465 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
466 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
467 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
468 WHERE users_id = :id;
471 dbmserr(NULL, sqlca.sqlcode);
478 void newuser(struct entry *e)
481 EXEC SQL BEGIN DECLARE SECTION;
482 int id, uid, st, who;
483 char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid;
484 char fullname[USERS_FULLNAME_SIZE], *prog;
485 char oaddr[USERS_OFFICE_ADDR_SIZE], ophone[USERS_OFFICE_PHONE_SIZE];
486 char dept[USERS_DEPARTMENT_SIZE], *name, *title;
487 char *rdept, *rhphone, *rophone;
488 EXEC SQL END DECLARE SECTION;
492 strncpy(oaddr, e->address, USERS_OFFICE_ADDR_SIZE - 1);
493 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
494 while ((to = strchr(oaddr, ',')))
496 while ((to = strchr(oaddr, ':')))
508 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
509 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
511 id = set_next_users_id(0);
512 uid = set_next_uid();
513 sprintf(login, "#%d", uid);
519 sprintf(fullname, "%s %s %s", first, middle, last);
521 sprintf(fullname, "%s %s", first, last);
522 st = US_NO_LOGIN_YET;
531 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
533 EXEC SQL INSERT INTO users
534 (login, users_id, unix_uid, shell, last, first, middle, status,
535 clearid, type, modtime, modby, modwith, fullname, office_addr,
536 office_phone, department, fmodtime, fmodby, fmodwith,
537 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
538 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
539 NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
540 :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog,
541 NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)),
542 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
543 NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)),
544 NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)),
548 dbmserr("adding user", sqlca.sqlcode);
554 int set_next_users_id(int limit)
556 EXEC SQL BEGIN DECLARE SECTION;
557 int flag, value, retval;
558 EXEC SQL END DECLARE SECTION;
560 EXEC SQL SELECT value INTO :value FROM numvalues
561 WHERE name = 'users_id';
564 if (sqlca.sqlerrd[2] != 1)
567 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
572 EXEC SQL SELECT users_id INTO :flag FROM users
573 WHERE users_id = :value;
576 if (sqlca.sqlerrd[2] == 0)
581 if (limit && value > MAX_ID_VALUE)
582 value = MIN_ID_VALUE;
584 EXEC SQL SELECT users_id INTO :flag FROM users
585 WHERE users_id = :value;
588 if (sqlca.sqlerrd[2] == 0)
593 if (limit && value > MAX_ID_VALUE)
594 value = MIN_ID_VALUE;
595 EXEC SQL UPDATE numvalues SET value = :value
596 WHERE name = 'users_id';
599 dbmserr("assigning ID", sqlca.sqlcode);
605 int set_next_uid(void)
607 EXEC SQL BEGIN DECLARE SECTION;
608 int flag, initial, value, retval;
610 EXEC SQL END DECLARE SECTION;
612 EXEC SQL SELECT value INTO :initial FROM numvalues
613 WHERE name = 'unix_uid';
616 if (sqlca.sqlerrd[2] != 1)
619 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
625 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
626 FROM users WHERE unix_uid = :value;
632 #ifdef ULTRIX_ID_HOLE
633 if (value > 31999 && value < 32768)
636 if (value > MAX_ID_VALUE)
637 value = MIN_ID_VALUE;
638 if (value == initial)
640 com_err(whoami, 0, "Out of uids!");
641 EXEC SQL ROLLBACK WORK;
645 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
646 FROM users WHERE unix_uid = :value;
652 if (value > MAX_ID_VALUE)
653 value = MIN_ID_VALUE;
654 EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
657 dbmserr("assigning ID", sqlca.sqlcode);
666 dbmserr(NULL, sqlca.sqlcode);
667 EXEC SQL ROLLBACK WORK;
671 void dbmserr(char *where, int what)
674 int bufsize = 256, msglength = 0;
676 sqlglm(err_msg, &bufsize, &msglength);
677 err_msg[msglength] = '\0';
680 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
682 com_err(whoami, 0, "DBMS error %s", err_msg);