9 #include <moira_site.h>
10 EXEC SQL INCLUDE sqlca;
13 #define WHO 11859 /* root */
14 #define PROG "emp-tape"
16 #define MAX_ID_VALUE 31999
17 #define MIN_ID_VALUE 101
40 #define LOC_USERNAME 157
50 #define LEN_USERNAME 30
74 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403)
75 #define SQL_DUPLICATE -2112
83 struct entry *e, *get_next_entry();
85 char buf[BUFSIZ], *file = NULL;
86 EXEC SQL BEGIN DECLARE SECTION;
88 EXEC SQL END DECLARE SECTION;
90 whoami = strrchr(argv[0], '/');
96 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
97 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
99 for (i = 1; i < argc; i++) {
100 if (!strcmp(argv[i], "-w"))
102 else if (!strcmp(argv[i], "-D"))
103 setenv("ING_SET", "set printqry");
104 else if (!strcmp(argv[i], "-n"))
106 else if (file != NULL)
107 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
112 in = fopen(file, "r");
114 fprintf(stderr, "Unable to open %s for input\n", file);
118 initialize_sms_error_table();
120 EXEC SQL CONNECT :db IDENTIFIED BY :db;
121 if (sqlca.sqlcode != 0) {
122 dbmserr("opening database", sqlca.sqlcode);
126 while (e = get_next_entry(in)) {
129 EXEC SQL COMMIT WORK;
130 if (sqlca.sqlcode != 0) {
131 dbmserr("committing work", sqlca.sqlcode);
145 char *substr(buf, key)
151 for (l = strlen(key); *buf; buf++)
152 if (!strncmp(buf, key, l))
158 struct entry *get_next_entry(in)
161 static struct entry e;
162 static char buf[BUFSIZ], mid[16], eid[16];
163 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
164 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
165 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
166 static char host[LEN_HOST+1];
167 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
170 if (fgets(buf, sizeof(buf), in) == NULL)
171 return((struct entry *)NULL);
173 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
174 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
175 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
176 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
177 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
178 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
179 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
180 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
181 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
184 e.name = strtrim(sname);
185 p = strchr(name, ',');
188 e.last = strtrim(name);
194 if (p = strchr(e.first, ' ')) {
196 e.first = strtrim(e.first);
197 e.middle = strtrim(p + 1);
199 e.first = strtrim(e.first);
206 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
209 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
211 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
219 EncryptID(e.eid, e.id, e.first, e.last);
221 e.address = strtrim(office);
222 e.phone = strtrim(phone);
223 e.phone2 = strtrim(phone2);
224 e.dept = strtrim(dept);
225 e.title = strtrim(title);
229 if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
231 if (!strcmp(e.dept, "LINCOLN LAB")) {
243 int changed, nochange, encrypted;
244 char buf[BUFSIZ], *from, *to;
245 EXEC SQL BEGIN DECLARE SECTION;
246 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
247 char *raddr, *rhphone, *rophone, *prog;
248 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
250 EXEC SQL END DECLARE SECTION;
255 if (strlen(first) > 16)
258 if (strlen(last) > 16)
267 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
268 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
270 WHERE clearid = :sid;
272 if (sqlca.sqlcode == SQL_DUPLICATE) {
273 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
279 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
280 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
282 WHERE last = :last and first = :first and clearid = :eid;
283 if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE) {
293 /* Update class/state if necessary. (Exclude several spacial cases.) */
294 if (strcmp(e->class, strtrim(class)) &&
295 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
296 strcmp(class, "KNIGHT")) {
297 com_err(whoami, 0, "updating class for %s %s from %s to %s",
298 first, last, class, e->class);
299 if (status == US_NOT_ALLOWED)
300 status = US_NO_LOGIN_YET;
301 if (status == US_ENROLL_NOT_ALLOWED)
302 status = US_ENROLLED;
303 strcpy(class, e->class);
304 EXEC SQL UPDATE users
305 SET type = NVL(:class,CHR(0)), status = :status, modtime = SYSDATE,
306 modby = :who, modwith = :prog
307 WHERE users_id = :id;
308 if (sqlca.sqlcode != 0) {
309 dbmserr("updating user", sqlca.sqlcode);
314 /* Update name if necessary */
315 if (strcmp(first, strtrim(dfirst)) ||
316 strcmp(last, strtrim(dlast)) ||
317 strcmp(middle, strtrim(dmiddle))) {
318 com_err(whoami, 0, "updating real name for %s %s", first, last);
319 EXEC SQL UPDATE users
320 SET first = NVL(:first,CHR(0)), last = NVL(:last,CHR(0)),
321 middle = NVL(:middle,CHR(0)), modby = :who, modwith = :prog,
323 WHERE users_id = :id;
324 if (sqlca.sqlcode != 0) {
325 dbmserr("updating name", sqlca.sqlcode);
330 changed = nochange = 0;
331 if (encrypted) changed++;
332 strcpy(buf, e->address);
333 while (to = strchr(buf, ','))
335 while (to = strchr(buf, ':'))
338 if (oaddr[0] == ' ' && buf[0]) {
339 strncpy(oaddr, buf, 16);
342 } else if (strncmp(strtrim(oaddr), buf, 15))
345 if (strncmp(strtrim(oaddr), buf, 15))
347 strncpy(oaddr, buf, 16);
359 if (ophone[0] == ' ') {
360 strncpy(ophone, buf, 16);
362 } else if (strncmp(strtrim(ophone), buf, 11))
365 if (strncmp(strtrim(ophone), buf, 11))
367 strncpy(ophone, buf, 16);
373 if (dept[0] == ' ') {
374 strncpy(dept, e->dept, 12);
376 } else if (strncmp(strtrim(dept), e->dept, 11))
379 if (strncmp(strtrim(dept), e->dept, 11))
381 strncpy(dept, e->dept, 12);
392 com_err(whoami, 0, "updating finger for %s %s", first, last);
393 EXEC SQL UPDATE users
394 SET office_addr = NVL(:oaddr,CHR(0)),
395 office_phone = NVL(:ophone,CHR(0)), department = NVL(:dept,CHR(0)),
396 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
397 xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
398 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
399 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
400 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
401 WHERE users_id = :id;
402 if (sqlca.sqlcode != 0) {
403 dbmserr(NULL, sqlca.sqlcode);
407 EXEC SQL UPDATE users
408 SET xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
409 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
410 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
411 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
412 WHERE users_id = :id;
413 if (sqlca.sqlcode != 0) {
414 dbmserr(NULL, sqlca.sqlcode);
425 EXEC SQL BEGIN DECLARE SECTION;
426 int id, uid, st, who;
427 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
428 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
429 char *rdept, *rhphone, *rophone;
430 EXEC SQL END DECLARE SECTION;
434 strncpy(oaddr, e->address, 16);
436 while (to = strchr(oaddr, ','))
438 while (to = strchr(oaddr, ':'))
449 strncpy(dept, e->dept, 12);
452 id = set_next_users_id(0);
453 uid = set_next_uid(e->highid);
454 sprintf(login, "#%d", uid);
460 sprintf(fullname, "%s %s %s", first, middle, last);
462 sprintf(fullname, "%s %s", first, last);
463 st = US_NO_LOGIN_YET;
472 EXEC SQL INSERT INTO users
473 (login, users_id, unix_uid, shell, last, first, middle, status,
474 clearid, type, modtime, modby, modwith, fullname, office_addr,
475 office_phone, department, fmodtime, fmodby, fmodwith,
476 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
477 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
478 NVL(:last,CHR(0)), NVL(:first,CHR(0)), NVL(:middle,CHR(0)),
479 :st, NVL(:sid,CHR(0)), NVL(:class,CHR(0)), SYSDATE, :who, :prog,
480 NVL(:fullname,CHR(0)), NVL(:oaddr,CHR(0)), NVL(:ophone,CHR(0)),
481 NVL(:dept,CHR(0)), SYSDATE, :who, :prog, 'NONE',
482 NVL(:name,CHR(0)), NVL(:rdept,CHR(0)), NVL(:title,CHR(0)),
483 NVL(:oaddr,CHR(0)), NVL(:rhphone,CHR(0)), NVL(:rophone,CHR(0)),
485 if (sqlca.sqlcode != 0) {
486 dbmserr("adding user", sqlca.sqlcode);
489 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
493 set_next_users_id(limit)
496 EXEC SQL BEGIN DECLARE SECTION;
497 int rowcount, flag, value, retval;
498 EXEC SQL END DECLARE SECTION;
500 EXEC SQL SELECT value INTO :value FROM numvalues
501 WHERE name = 'users_id';
502 if (sqlfail()) sqlexit();
503 if (sqlca.sqlerrd[2] != 1) {
505 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
510 EXEC SQL SELECT users_id INTO :flag FROM users
511 WHERE users_id = :value;
512 if (sqlfail()) sqlexit();
513 if (sqlca.sqlerrd[2] == 0)
517 if (limit && value > MAX_ID_VALUE)
518 value = MIN_ID_VALUE;
520 EXEC SQL SELECT users_id INTO :flag FROM users
521 WHERE users_id = :value;
522 if (sqlfail()) sqlexit();
523 if (sqlca.sqlerrd[2] == 0)
528 if (limit && value > MAX_ID_VALUE)
529 value = MIN_ID_VALUE;
530 EXEC SQL UPDATE numvalues SET value = :value
531 WHERE name = 'users_id';
532 if (sqlca.sqlcode != 0) {
533 dbmserr("assigning ID", sqlca.sqlcode);
542 EXEC SQL BEGIN DECLARE SECTION;
543 int rowcount, flag, value, retval;
545 EXEC SQL END DECLARE SECTION;
552 EXEC SQL SELECT value INTO :value FROM numvalues
554 if (sqlfail()) sqlexit();
555 if (sqlca.sqlerrd[2] != 1) {
557 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
562 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
563 if (sqlfail()) sqlexit();
564 if (sqlca.sqlerrd[2] == 0)
568 if (!high && value > MAX_ID_VALUE)
569 value = MIN_ID_VALUE;
571 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
572 if (sqlfail()) sqlexit();
573 if (sqlca.sqlerrd[2] == 0)
578 if (!high && value > MAX_ID_VALUE)
579 value = MIN_ID_VALUE;
580 EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name;
581 if (sqlca.sqlcode != 0) {
582 dbmserr("assigning ID", sqlca.sqlcode);
591 dbmserr(NULL, sqlca.sqlcode);
592 EXEC SQL ROLLBACK WORK;
596 dbmserr(char *where, int what)
599 int bufsize=256, msglength=0;
601 sqlglm(err_msg, &bufsize, &msglength);
602 err_msg[msglength]=0;
605 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
607 com_err(whoami, 0, "DBMS error %s", err_msg);