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);
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"))
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 e.address = strtrim(office);
238 e.phone = strtrim(phone);
239 e.phone2 = strtrim(phone2);
240 e.dept = strtrim(dept);
241 e.title = strtrim(title);
245 if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE"))
247 if (!strcmp(e.dept, "LINCOLN LAB"))
256 void process_entry(struct entry *e)
258 int changed, nochange;
259 char buf[MAX_FIELD_WIDTH], *from, *to;
260 EXEC SQL BEGIN DECLARE SECTION;
261 char *first, *last, *middle, *sid, *name, *rdept;
262 char *rtitle, *raddr, *rhphone, *rophone, *prog;
263 char class[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE];
264 char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
265 char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
266 char dmiddle[USERS_MIDDLE_SIZE];
268 EXEC SQL END DECLARE SECTION;
273 if (strlen(first) > USERS_FIRST_SIZE - 1)
274 first[USERS_FIRST_SIZE - 1] = '\0';
276 if (strlen(last) > USERS_LAST_SIZE - 1)
277 last[USERS_LAST_SIZE - 1] = '\0';
279 if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
280 middle[USERS_MIDDLE_SIZE - 1] = '\0';
285 EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
286 office_phone, department, status
287 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr,
288 :ophone, :dept, :status
290 WHERE clearid = :sid;
293 if (sqlca.sqlcode == SQL_DUPLICATE)
295 com_err(whoami, 0, "duplicate ID number %s on user %s %s",
308 /* Update class/state if necessary. (Exclude several spacial cases.) */
309 if (strcmp(e->class, strtrim(class)) &&
310 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
311 strcmp(class, "KNIGHT"))
313 com_err(whoami, 0, "updating class for %s %s from %s to %s",
314 first, last, class, e->class);
315 if (status == US_NOT_ALLOWED)
316 status = US_NO_LOGIN_YET;
317 if (status == US_ENROLL_NOT_ALLOWED)
318 status = US_ENROLLED;
319 strcpy(class, e->class);
320 EXEC SQL UPDATE users
321 SET type = NVL(:class, CHR(0)), status = :status, modtime = SYSDATE,
322 modby = :who, modwith = :prog
323 WHERE users_id = :id;
326 dbmserr("updating user", sqlca.sqlcode);
331 /* Update name if necessary */
332 if (strcmp(first, strtrim(dfirst)) ||
333 strcmp(last, strtrim(dlast)) ||
334 strcmp(middle, strtrim(dmiddle)))
336 com_err(whoami, 0, "updating real name for %s %s", first, last);
337 EXEC SQL UPDATE users
338 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
339 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
341 WHERE users_id = :id;
344 dbmserr("updating name", sqlca.sqlcode);
349 changed = nochange = 0;
350 strcpy(buf, e->address);
351 while ((to = strchr(buf, ',')))
353 while ((to = strchr(buf, ':')))
357 if (oaddr[0] == ' ' && buf[0])
359 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
360 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
363 else if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
368 if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1))
370 strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1);
371 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
384 if (ophone[0] == ' ')
386 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
387 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
389 else if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
394 if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1))
396 strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1);
397 ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0';
405 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
406 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
408 else if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
413 if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1))
415 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
416 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
427 com_err(whoami, 0, "updating finger for %s %s", first, last);
428 EXEC SQL UPDATE users
429 SET office_addr = NVL(:oaddr, CHR(0)),
430 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
431 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
432 xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
433 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
434 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
435 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
436 WHERE users_id = :id;
439 dbmserr(NULL, sqlca.sqlcode);
445 EXEC SQL UPDATE users
446 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
447 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
448 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
449 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
450 WHERE users_id = :id;
453 dbmserr(NULL, sqlca.sqlcode);
460 void newuser(struct entry *e)
463 EXEC SQL BEGIN DECLARE SECTION;
464 int id, uid, st, who;
465 char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid;
466 char fullname[USERS_FULLNAME_SIZE], *prog;
467 char oaddr[USERS_OFFICE_ADDR_SIZE], ophone[USERS_OFFICE_PHONE_SIZE];
468 char dept[USERS_DEPARTMENT_SIZE], *name, *title;
469 char *rdept, *rhphone, *rophone;
470 EXEC SQL END DECLARE SECTION;
474 strncpy(oaddr, e->address, USERS_OFFICE_ADDR_SIZE - 1);
475 oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0';
476 while ((to = strchr(oaddr, ',')))
478 while ((to = strchr(oaddr, ':')))
490 strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1);
491 dept[USERS_DEPARTMENT_SIZE - 1] = '\0';
493 id = set_next_users_id(0);
494 uid = set_next_uid(e->highid);
495 sprintf(login, "#%d", uid);
501 sprintf(fullname, "%s %s %s", first, middle, last);
503 sprintf(fullname, "%s %s", first, last);
504 st = US_NO_LOGIN_YET;
513 EXEC SQL INSERT INTO users
514 (login, users_id, unix_uid, shell, last, first, middle, status,
515 clearid, type, modtime, modby, modwith, fullname, office_addr,
516 office_phone, department, fmodtime, fmodby, fmodwith,
517 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
518 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
519 NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
520 :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog,
521 NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)),
522 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
523 NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)),
524 NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)),
528 dbmserr("adding user", sqlca.sqlcode);
532 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
536 int set_next_users_id(int limit)
538 EXEC SQL BEGIN DECLARE SECTION;
539 int flag, value, retval;
540 EXEC SQL END DECLARE SECTION;
542 EXEC SQL SELECT value INTO :value FROM numvalues
543 WHERE name = 'users_id';
546 if (sqlca.sqlerrd[2] != 1)
549 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
554 EXEC SQL SELECT users_id INTO :flag FROM users
555 WHERE users_id = :value;
558 if (sqlca.sqlerrd[2] == 0)
563 if (limit && value > MAX_ID_VALUE)
564 value = MIN_ID_VALUE;
566 EXEC SQL SELECT users_id INTO :flag FROM users
567 WHERE users_id = :value;
570 if (sqlca.sqlerrd[2] == 0)
575 if (limit && value > MAX_ID_VALUE)
576 value = MIN_ID_VALUE;
577 EXEC SQL UPDATE numvalues SET value = :value
578 WHERE name = 'users_id';
581 dbmserr("assigning ID", sqlca.sqlcode);
587 int set_next_uid(int high)
589 EXEC SQL BEGIN DECLARE SECTION;
590 int flag, value, retval;
592 EXEC SQL END DECLARE SECTION;
599 EXEC SQL SELECT value INTO :value FROM numvalues
603 if (sqlca.sqlerrd[2] != 1)
606 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
611 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
614 if (sqlca.sqlerrd[2] == 0)
619 if (!high && value > MAX_ID_VALUE)
620 value = MIN_ID_VALUE;
622 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
625 if (sqlca.sqlerrd[2] == 0)
630 if (!high && value > MAX_ID_VALUE)
631 value = MIN_ID_VALUE;
632 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name;
635 dbmserr("assigning ID", sqlca.sqlcode);
644 dbmserr(NULL, sqlca.sqlcode);
645 EXEC SQL ROLLBACK WORK;
649 void dbmserr(char *where, int what)
652 int bufsize = 256, msglength = 0;
654 sqlglm(err_msg, &bufsize, &msglength);
655 err_msg[msglength] = '\0';
658 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
660 com_err(whoami, 0, "DBMS error %s", err_msg);