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>
18 EXEC SQL INCLUDE sqlca;
19 extern void sqlglm(char *, unsigned int *, unsigned int *);
23 #define WHO 11859 /* root */
24 #define PROG "emp-tape"
26 #define MAX_ID_VALUE 31999
27 #define MIN_ID_VALUE 101
50 #define LOC_USERNAME 157
60 #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[BUFSIZ], *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);
162 struct entry *get_next_entry(FILE *in)
164 static struct entry e;
165 static char buf[BUFSIZ], eid[16];
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 EncryptID(e.eid, e.id, e.first, e.last);
239 e.address = strtrim(office);
240 e.phone = strtrim(phone);
241 e.phone2 = strtrim(phone2);
242 e.dept = strtrim(dept);
243 e.title = strtrim(title);
247 if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE"))
249 if (!strcmp(e.dept, "LINCOLN LAB"))
258 void process_entry(struct entry *e)
260 int changed, nochange, encrypted;
261 char buf[BUFSIZ], *from, *to;
262 EXEC SQL BEGIN DECLARE SECTION;
263 char *first, *last, *middle, *eid, *sid, *name, *rdept;
264 char *rtitle, *raddr, *rhphone, *rophone, *prog;
265 char class[9], oaddr[25], ophone[17], dept[128];
266 char dfirst[17], dlast[17], dmiddle[17];
268 EXEC SQL END DECLARE SECTION;
273 if (strlen(first) > 16)
276 if (strlen(last) > 16)
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",
304 EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
305 office_phone, department, status
306 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr,
307 :ophone, :dept, :status
309 WHERE last = :last and first = :first and clearid = :eid;
310 if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE)
320 /* Update class/state if necessary. (Exclude several spacial cases.) */
321 if (strcmp(e->class, strtrim(class)) &&
322 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
323 strcmp(class, "KNIGHT"))
325 com_err(whoami, 0, "updating class for %s %s from %s to %s",
326 first, last, class, e->class);
327 if (status == US_NOT_ALLOWED)
328 status = US_NO_LOGIN_YET;
329 if (status == US_ENROLL_NOT_ALLOWED)
330 status = US_ENROLLED;
331 strcpy(class, e->class);
332 EXEC SQL UPDATE users
333 SET type = NVL(:class, CHR(0)), status = :status, modtime = SYSDATE,
334 modby = :who, modwith = :prog
335 WHERE users_id = :id;
338 dbmserr("updating user", sqlca.sqlcode);
343 /* Update name if necessary */
344 if (strcmp(first, strtrim(dfirst)) ||
345 strcmp(last, strtrim(dlast)) ||
346 strcmp(middle, strtrim(dmiddle)))
348 com_err(whoami, 0, "updating real name for %s %s", first, last);
349 EXEC SQL UPDATE users
350 SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
351 middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
353 WHERE users_id = :id;
356 dbmserr("updating name", sqlca.sqlcode);
361 changed = nochange = 0;
364 strcpy(buf, e->address);
365 while ((to = strchr(buf, ',')))
367 while ((to = strchr(buf, ':')))
371 if (oaddr[0] == ' ' && buf[0])
373 strncpy(oaddr, buf, 16);
377 else if (strncmp(strtrim(oaddr), buf, 15))
382 if (strncmp(strtrim(oaddr), buf, 15))
384 strncpy(oaddr, buf, 16);
398 if (ophone[0] == ' ')
400 strncpy(ophone, buf, 16);
403 else if (strncmp(strtrim(ophone), buf, 11))
408 if (strncmp(strtrim(ophone), buf, 11))
410 strncpy(ophone, buf, 16);
419 strncpy(dept, e->dept, 12);
422 else if (strncmp(strtrim(dept), e->dept, 11))
427 if (strncmp(strtrim(dept), e->dept, 11))
429 strncpy(dept, e->dept, 12);
441 com_err(whoami, 0, "updating finger for %s %s", first, last);
442 EXEC SQL UPDATE users
443 SET office_addr = NVL(:oaddr, CHR(0)),
444 office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)),
445 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
446 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);
459 EXEC SQL UPDATE users
460 SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)),
461 xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)),
462 xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)),
463 xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0))
464 WHERE users_id = :id;
467 dbmserr(NULL, sqlca.sqlcode);
474 void newuser(struct entry *e)
477 EXEC SQL BEGIN DECLARE SECTION;
478 int id, uid, st, who;
479 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
480 char oaddr[81], ophone[17], dept[128], *name, *title;
481 char *rdept, *rhphone, *rophone;
482 EXEC SQL END DECLARE SECTION;
486 strncpy(oaddr, e->address, 16);
488 while ((to = strchr(oaddr, ',')))
490 while ((to = strchr(oaddr, ':')))
502 strncpy(dept, e->dept, 12);
505 id = set_next_users_id(0);
506 uid = set_next_uid(e->highid);
507 sprintf(login, "#%d", uid);
513 sprintf(fullname, "%s %s %s", first, middle, last);
515 sprintf(fullname, "%s %s", first, last);
516 st = US_NO_LOGIN_YET;
525 EXEC SQL INSERT INTO users
526 (login, users_id, unix_uid, shell, last, first, middle, status,
527 clearid, type, modtime, modby, modwith, fullname, office_addr,
528 office_phone, department, fmodtime, fmodby, fmodwith,
529 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
530 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
531 NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
532 :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog,
533 NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)),
534 NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE',
535 NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)),
536 NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)),
540 dbmserr("adding user", sqlca.sqlcode);
544 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
548 int set_next_users_id(int limit)
550 EXEC SQL BEGIN DECLARE SECTION;
551 int flag, value, retval;
552 EXEC SQL END DECLARE SECTION;
554 EXEC SQL SELECT value INTO :value FROM numvalues
555 WHERE name = 'users_id';
558 if (sqlca.sqlerrd[2] != 1)
561 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
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;
578 EXEC SQL SELECT users_id INTO :flag FROM users
579 WHERE users_id = :value;
582 if (sqlca.sqlerrd[2] == 0)
587 if (limit && value > MAX_ID_VALUE)
588 value = MIN_ID_VALUE;
589 EXEC SQL UPDATE numvalues SET value = :value
590 WHERE name = 'users_id';
593 dbmserr("assigning ID", sqlca.sqlcode);
599 int set_next_uid(int high)
601 EXEC SQL BEGIN DECLARE SECTION;
602 int flag, value, retval;
604 EXEC SQL END DECLARE SECTION;
611 EXEC SQL SELECT value INTO :value FROM numvalues
615 if (sqlca.sqlerrd[2] != 1)
618 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
623 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
626 if (sqlca.sqlerrd[2] == 0)
631 if (!high && value > MAX_ID_VALUE)
632 value = MIN_ID_VALUE;
634 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
637 if (sqlca.sqlerrd[2] == 0)
642 if (!high && value > MAX_ID_VALUE)
643 value = MIN_ID_VALUE;
644 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name;
647 dbmserr("assigning ID", sqlca.sqlcode);
656 dbmserr(NULL, sqlca.sqlcode);
657 EXEC SQL ROLLBACK WORK;
661 void dbmserr(char *where, int what)
664 int bufsize = 256, msglength = 0;
666 sqlglm(err_msg, &bufsize, &msglength);
667 err_msg[msglength] = '\0';
670 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
672 com_err(whoami, 0, "DBMS error %s", err_msg);