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 157
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 e.address = strtrim(office);
242 e.phone = strtrim(phone);
243 e.phone2 = strtrim(phone2);
244 e.dept = strtrim(dept);
245 e.title = strtrim(title);
249 if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE"))
251 if (!strcmp(e.dept, "LINCOLN LAB"))
260 void process_entry(struct entry *e)
262 int changed, nochange;
263 char buf[MAX_FIELD_WIDTH], *from, *to;
264 EXEC SQL BEGIN DECLARE SECTION;
265 char *first, *last, *middle, *sid, *name, *rdept;
266 char *rtitle, *raddr, *rhphone, *rophone, *prog;
267 char class[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE];
268 char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
269 char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
270 char dmiddle[USERS_MIDDLE_SIZE];
272 EXEC SQL END DECLARE SECTION;
277 if (strlen(first) > USERS_FIRST_SIZE - 1)
278 first[USERS_FIRST_SIZE - 1] = '\0';
280 if (strlen(last) > USERS_LAST_SIZE - 1)
281 last[USERS_LAST_SIZE - 1] = '\0';
283 if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
284 middle[USERS_MIDDLE_SIZE - 1] = '\0';
289 EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
290 office_phone, department, status
291 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr,
292 :ophone, :dept, :status
294 WHERE clearid = :sid;
297 if (sqlca.sqlcode == SQL_DUPLICATE)
299 com_err(whoami, 0, "duplicate ID number %s on user %s %s",
312 /* Update class/state if necessary. (Exclude several spacial cases.) */
313 if (strcmp(e->class, strtrim(class)) &&
314 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
315 strcmp(class, "KNIGHT"))
317 com_err(whoami, 0, "updating class for %s %s from %s to %s",
318 first, last, class, e->class);
319 if (status == US_NOT_ALLOWED)
320 status = US_NO_LOGIN_YET;
321 if (status == US_ENROLL_NOT_ALLOWED)
322 status = US_ENROLLED;
323 strcpy(class, e->class);
326 EXEC SQL UPDATE users
327 SET type = NVL(:class, CHR(0)), status = :status,
328 modtime = SYSDATE, modby = :who, modwith = :prog
329 WHERE users_id = :id;
332 dbmserr("updating user", sqlca.sqlcode);
338 /* Update name if necessary */
339 if (strcmp(first, strtrim(dfirst)) ||
340 strcmp(last, strtrim(dlast)) ||
341 strcmp(middle, strtrim(dmiddle)))
343 com_err(whoami, 0, "updating real name for %s %s", first, last);
346 EXEC SQL UPDATE users
347 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
348 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
350 WHERE users_id = :id;
353 dbmserr("updating name", sqlca.sqlcode);
359 com_err(whoami, 0, "was %s %s %s, became %s %s %s",
360 dfirst, dmiddle, dlast, first, middle, last);
364 changed = nochange = 0;
365 strcpy(buf, e->address);
366 while ((to = strchr(buf, ',')))
368 while ((to = strchr(buf, ':')))
372 if (oaddr[0] == ' ' && buf[0])
374 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
375 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
378 else if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
383 if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
388 com_err(whoami, 0, "office for %s %s changed from %s to %s",
389 first, last, oaddr, buf);
392 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
393 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
406 if (ophone[0] == ' ')
408 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
409 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
411 else if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
416 if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
421 com_err(whoami, 0, "Phone for %s %s changed from %s to %s",
422 first, last, ophone, buf);
425 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
426 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
434 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
435 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
437 else if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
442 if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
447 com_err(whoami, 0, "Department for %s %s changed from %s to %s",
448 first, last, dept, e->dept);
451 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
452 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
467 com_err(whoami, 0, "updating finger for %s %s", first, last);
468 EXEC SQL UPDATE users
469 SET office_addr = NVL(:oaddr, CHR(0)),
470 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
471 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
472 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);
485 EXEC SQL UPDATE users
486 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
487 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
488 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
489 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
490 WHERE users_id = :id;
493 dbmserr(NULL, sqlca.sqlcode);
500 void newuser(struct entry *e)
503 EXEC SQL BEGIN DECLARE SECTION;
504 int id, uid, st, who;
505 char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid;
506 char fullname[USERS_FULLNAME_SIZE], *prog;
507 char oaddr[USERS_OFFICE_ADDR_SIZE], ophone[USERS_OFFICE_PHONE_SIZE];
508 char dept[USERS_DEPARTMENT_SIZE], *name, *title;
509 char *rdept, *rhphone, *rophone;
510 EXEC SQL END DECLARE SECTION;
514 strncpy(oaddr, e->address, USERS_OFFICE_ADDR_SIZE - 1);
515 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
516 while ((to = strchr(oaddr, ',')))
518 while ((to = strchr(oaddr, ':')))
530 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
531 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
533 id = set_next_users_id(0);
534 uid = set_next_uid(e->highid);
535 sprintf(login, "#%d", uid);
541 sprintf(fullname, "%s %s %s", first, middle, last);
543 sprintf(fullname, "%s %s", first, last);
544 st = US_NO_LOGIN_YET;
553 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
557 EXEC SQL INSERT INTO users
558 (login, users_id, unix_uid, shell, last, first, middle, status,
559 clearid, type, modtime, modby, modwith, fullname, office_addr,
560 office_phone, department, fmodtime, fmodby, fmodwith,
561 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
562 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
563 NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
564 :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog,
565 NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)),
566 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
567 NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)),
568 NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)),
572 dbmserr("adding user", sqlca.sqlcode);
578 int set_next_users_id(int limit)
580 EXEC SQL BEGIN DECLARE SECTION;
581 int flag, value, retval;
582 EXEC SQL END DECLARE SECTION;
587 EXEC SQL SELECT value INTO :value FROM numvalues
588 WHERE name = 'users_id';
591 if (sqlca.sqlerrd[2] != 1)
594 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
599 EXEC SQL SELECT users_id INTO :flag FROM users
600 WHERE users_id = :value;
603 if (sqlca.sqlerrd[2] == 0)
608 if (limit && value > MAX_ID_VALUE)
609 value = MIN_ID_VALUE;
611 EXEC SQL SELECT users_id INTO :flag FROM users
612 WHERE users_id = :value;
615 if (sqlca.sqlerrd[2] == 0)
620 if (limit && value > MAX_ID_VALUE)
621 value = MIN_ID_VALUE;
622 EXEC SQL UPDATE numvalues SET value = :value
623 WHERE name = 'users_id';
626 dbmserr("assigning ID", sqlca.sqlcode);
632 int set_next_uid(int high)
634 EXEC SQL BEGIN DECLARE SECTION;
635 int flag, initial, value, retval;
637 EXEC SQL END DECLARE SECTION;
647 EXEC SQL SELECT value INTO :initial FROM numvalues
651 if (sqlca.sqlerrd[2] != 1)
654 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
660 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
661 FROM users WHERE unix_uid = :value;
667 if (!high && value > MAX_ID_VALUE)
668 value = MIN_ID_VALUE;
669 if (value == initial)
671 com_err(whoami, 0, "Out of uids!");
672 EXEC SQL ROLLBACK WORK;
676 EXEC SQL SELECT COUNT(unix_uid) INTO :flag
677 FROM users WHERE unix_uid = :value;
683 if (!high && value > MAX_ID_VALUE)
684 value = MIN_ID_VALUE;
685 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name;
688 dbmserr("assigning ID", sqlca.sqlcode);
697 dbmserr(NULL, sqlca.sqlcode);
698 EXEC SQL ROLLBACK WORK;
702 void dbmserr(char *where, int what)
705 int bufsize = 256, msglength = 0;
707 sqlglm(err_msg, &bufsize, &msglength);
708 err_msg[msglength] = '\0';
711 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
713 com_err(whoami, 0, "DBMS error %s", err_msg);