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);
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], "-h"))
121 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
126 in = fopen(file, "r");
129 fprintf(stderr, "Unable to open %s for input\n", file);
133 initialize_sms_error_table();
135 EXEC SQL CONNECT :db IDENTIFIED BY :db;
138 dbmserr("opening database", sqlca.sqlcode);
142 while ((e = get_next_entry(in)))
145 EXEC SQL COMMIT WORK;
148 dbmserr("committing work", sqlca.sqlcode);
155 fgets(buf, sizeof(buf), stdin);
162 struct entry *get_next_entry(FILE *in)
164 static struct entry e;
165 static char buf[BUFSIZ];
166 static char name[LEN_NAME + 1], sname[LEN_NAME + 1], id[LEN_ID + 1];
167 static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1];
168 static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1];
169 static char username[LEN_USERNAME + 1], host[LEN_HOST + 1];
170 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
173 if (!fgets(buf, sizeof(buf), in))
176 strncpy(id, &buf[LOC_ID], LEN_ID);
178 strncpy(name, &buf[LOC_NAME], LEN_NAME);
179 name[LEN_NAME] = '\0';
180 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE);
181 office[LEN_OFFICE] = '\0';
182 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE);
183 phone[LEN_PHONE] = '\0';
184 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2);
185 phone2[LEN_PHONE2] = '\0';
186 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT);
187 dept[LEN_DEPT] = '\0';
188 strncpy(title, &buf[LOC_TITLE], LEN_TITLE);
189 title[LEN_TITLE] = '\0';
190 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME);
191 username[LEN_USERNAME] = '\0';
192 strncpy(host, &buf[LOC_HOST], LEN_HOST);
193 host[LEN_HOST] = '\0';
196 e.name = strtrim(sname);
197 p = strchr(name, ',');
200 e.last = strtrim(name);
207 if ((p = strchr(e.first, ' ')))
210 e.first = strtrim(e.first);
211 e.middle = strtrim(p + 1);
215 e.first = strtrim(e.first);
224 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
227 LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
229 LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii,
237 /* The following is really gross, but it happens to successfully convert
238 * new-style Warehouse office descriptions into (more-readable) old-style
239 * Personnel Office office descriptions.
241 e.address = p = strtrim(office);
242 while (*p && !isspace(*p))
247 if (*q && q < e.address + LEN_OFFICE / 2)
250 while (*q && q < e.address + LEN_OFFICE / 2)
252 if (*q != ' ' && *q != '-')
258 memset(p, ' ', q - p);
261 p = e.address + LEN_OFFICE / 2;
262 while (*p && !isspace(*p))
272 if (*q != ' ' && *q != '-')
278 memset(p, ' ', q - p);
282 e.phone = strtrim(phone);
283 e.phone2 = strtrim(phone2);
284 e.dept = strtrim(dept);
285 e.title = strtrim(title);
289 if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE"))
291 if (!strcmp(e.dept, "LINCOLN LAB"))
300 void process_entry(struct entry *e)
303 char buf[MAX_FIELD_WIDTH], *from, *to;
304 EXEC SQL BEGIN DECLARE SECTION;
305 char *first, *last, *middle, *sid, *name, *rdept;
306 char *rtitle, *raddr, *rhphone, *rophone, *prog;
307 char class[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE];
308 char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
309 char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
310 char dmiddle[USERS_MIDDLE_SIZE];
312 EXEC SQL END DECLARE SECTION;
317 if (strlen(first) > USERS_FIRST_SIZE - 1)
318 first[USERS_FIRST_SIZE - 1] = '\0';
320 if (strlen(last) > USERS_LAST_SIZE - 1)
321 last[USERS_LAST_SIZE - 1] = '\0';
323 if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
324 middle[USERS_MIDDLE_SIZE - 1] = '\0';
329 EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
330 office_phone, department, status
331 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr,
332 :ophone, :dept, :status
334 WHERE clearid = :sid AND status != 3;
337 if (sqlca.sqlcode == SQL_DUPLICATE)
339 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, 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);
368 EXEC SQL UPDATE users
369 SET type = NVL(:class, CHR(0)), status = :status,
370 modtime = SYSDATE, modby = :who, modwith = :prog
371 WHERE users_id = :id;
374 dbmserr("updating user", sqlca.sqlcode);
379 /* Update name if necessary */
380 if (strcmp(first, strtrim(dfirst)) ||
381 strcmp(last, strtrim(dlast)) ||
382 strcmp(middle, strtrim(dmiddle)))
384 com_err(whoami, 0, "updating real name for %s%s%s %s (was %s%s%s %s)",
385 first, *middle ? " " : "", middle, last,
386 dfirst, *dmiddle ? " " : "", dmiddle, dlast);
387 EXEC SQL UPDATE users
388 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
389 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
391 WHERE users_id = :id;
394 dbmserr("updating name", sqlca.sqlcode);
400 strcpy(buf, e->address);
401 while ((to = strchr(buf, ',')))
403 while ((to = strchr(buf, ':')))
405 if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
408 com_err(whoami, 0, "office for %s %s changed from \"%s\" to \"%s\"",
409 first, last, oaddr, buf);
411 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
412 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
423 if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
426 com_err(whoami, 0, "Phone for %s %s changed from \"%s\" to \"%s\"",
427 first, last, ophone, buf);
429 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
430 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
434 if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
437 com_err(whoami, 0, "Department for %s %s changed from \"%s\" to \"%s\"",
438 first, last, dept, e->dept);
440 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
441 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
453 com_err(whoami, 0, "updating finger for %s %s", first, last);
454 EXEC SQL UPDATE users
455 SET office_addr = NVL(:oaddr, CHR(0)),
456 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
457 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
458 xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
459 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
460 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
461 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
462 WHERE users_id = :id;
465 dbmserr(NULL, sqlca.sqlcode);
471 EXEC SQL UPDATE users
472 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
473 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
474 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
475 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
476 WHERE users_id = :id;
479 dbmserr(NULL, sqlca.sqlcode);
486 void newuser(struct entry *e)
489 EXEC SQL BEGIN DECLARE SECTION;
490 int id, uid, st, who;
491 char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid;
492 char fullname[USERS_FULLNAME_SIZE], *prog;
493 char oaddr[USERS_OFFICE_ADDR_SIZE], ophone[USERS_OFFICE_PHONE_SIZE];
494 char dept[USERS_DEPARTMENT_SIZE], *name, *title;
495 char *rdept, *rhphone, *rophone;
496 EXEC SQL END DECLARE SECTION;
500 strncpy(oaddr, e->address, USERS_OFFICE_ADDR_SIZE - 1);
501 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
502 while ((to = strchr(oaddr, ',')))
504 while ((to = strchr(oaddr, ':')))
516 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
517 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
519 id = set_next_users_id(0);
520 uid = set_next_uid(e->highid);
521 sprintf(login, "#%d", uid);
527 sprintf(fullname, "%s %s %s", first, middle, last);
529 sprintf(fullname, "%s %s", first, last);
530 st = US_NO_LOGIN_YET;
539 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
541 EXEC SQL INSERT INTO users
542 (login, users_id, unix_uid, shell, last, first, middle, status,
543 clearid, type, modtime, modby, modwith, fullname, office_addr,
544 office_phone, department, fmodtime, fmodby, fmodwith,
545 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
546 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
547 NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
548 :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog,
549 NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)),
550 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
551 NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)),
552 NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)),
556 dbmserr("adding user", sqlca.sqlcode);
562 int set_next_users_id(int limit)
564 EXEC SQL BEGIN DECLARE SECTION;
565 int flag, value, retval;
566 EXEC SQL END DECLARE SECTION;
568 EXEC SQL SELECT value INTO :value FROM numvalues
569 WHERE name = 'users_id';
572 if (sqlca.sqlerrd[2] != 1)
575 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
580 EXEC SQL SELECT users_id INTO :flag FROM users
581 WHERE users_id = :value;
584 if (sqlca.sqlerrd[2] == 0)
589 if (limit && value > MAX_ID_VALUE)
590 value = MIN_ID_VALUE;
592 EXEC SQL SELECT users_id INTO :flag FROM users
593 WHERE users_id = :value;
596 if (sqlca.sqlerrd[2] == 0)
601 if (limit && value > MAX_ID_VALUE)
602 value = MIN_ID_VALUE;
603 EXEC SQL UPDATE numvalues SET value = :value
604 WHERE name = 'users_id';
607 dbmserr("assigning ID", sqlca.sqlcode);
613 int set_next_uid(int high)
615 EXEC SQL BEGIN DECLARE SECTION;
616 int flag, initial, value, retval;
618 EXEC SQL END DECLARE SECTION;
625 EXEC SQL SELECT value INTO :initial FROM numvalues
629 if (sqlca.sqlerrd[2] != 1)
632 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
638 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
639 FROM users WHERE unix_uid = :value;
645 if (!high && value > MAX_ID_VALUE)
646 value = MIN_ID_VALUE;
647 if (value == initial)
649 com_err(whoami, 0, "Out of uids!");
650 EXEC SQL ROLLBACK WORK;
654 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
655 FROM users WHERE unix_uid = :value;
661 if (!high && value > MAX_ID_VALUE)
662 value = MIN_ID_VALUE;
663 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name;
666 dbmserr("assigning ID", sqlca.sqlcode);
675 dbmserr(NULL, sqlca.sqlcode);
676 EXEC SQL ROLLBACK WORK;
680 void dbmserr(char *where, int what)
683 int bufsize = 256, msglength = 0;
685 sqlglm(err_msg, &bufsize, &msglength);
686 err_msg[msglength] = '\0';
689 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
691 com_err(whoami, 0, "DBMS error %s", err_msg);