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
73 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403)
74 #define SQL_DUPLICATE -2112
82 struct entry *e, *get_next_entry();
84 char buf[BUFSIZ], *file = NULL;
85 EXEC SQL BEGIN DECLARE SECTION;
87 EXEC SQL END DECLARE SECTION;
89 whoami = strrchr(argv[0], '/');
95 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
96 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
98 for (i = 1; i < argc; i++) {
99 if (!strcmp(argv[i], "-w"))
101 else if (!strcmp(argv[i], "-D"))
102 setenv("ING_SET", "set printqry");
103 else if (!strcmp(argv[i], "-n"))
105 else if (file != NULL)
106 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
111 in = fopen(file, "r");
113 fprintf(stderr, "Unable to open %s for input\n", file);
117 initialize_sms_error_table();
119 EXEC SQL CONNECT :db IDENTIFIED BY :db;
120 if (sqlca.sqlcode != 0) {
121 dbmserr("opening database", sqlca.sqlcode);
125 while (e = get_next_entry(in)) {
128 EXEC SQL COMMIT WORK;
129 if (sqlca.sqlcode != 0) {
130 dbmserr("committing work", sqlca.sqlcode);
144 char *substr(buf, key)
150 for (l = strlen(key); *buf; buf++)
151 if (!strncmp(buf, key, l))
157 struct entry *get_next_entry(in)
160 static struct entry e;
161 static char buf[BUFSIZ], mid[16], eid[16];
162 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
163 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
164 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
165 static char host[LEN_HOST+1];
166 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
169 if (fgets(buf, sizeof(buf), in) == NULL)
170 return((struct entry *)NULL);
172 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
173 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
174 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
175 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
176 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
177 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
178 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
179 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
180 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
183 e.name = strtrim(sname);
184 p = strchr(name, ',');
187 e.last = strtrim(name);
193 if (p = strchr(e.first, ' ')) {
195 e.first = strtrim(e.first);
196 e.middle = strtrim(p + 1);
198 e.first = strtrim(e.first);
205 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
208 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
210 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
218 EncryptID(e.eid, e.id, e.first, e.last);
220 e.address = strtrim(office);
221 e.phone = strtrim(phone);
222 e.phone2 = strtrim(phone2);
223 e.dept = strtrim(dept);
224 e.title = strtrim(title);
227 if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
237 int changed, nochange, encrypted;
238 char buf[BUFSIZ], *from, *to;
239 EXEC SQL BEGIN DECLARE SECTION;
240 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
241 char *raddr, *rhphone, *rophone, *prog;
242 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
244 EXEC SQL END DECLARE SECTION;
246 /* Don't process Lincoln Labs */
247 if (!strncmp(e->address, "LL", 2))
253 if (strlen(first) > 16)
256 if (strlen(last) > 16)
265 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
266 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
268 WHERE clearid = :sid;
270 if (sqlca.sqlcode == SQL_DUPLICATE) {
271 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
277 EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
278 INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
280 WHERE last = :last and first = :first and clearid = :eid;
281 if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE) {
291 /* Update class/state if necessary. (Exclude several spacial cases.) */
292 if (strcmp(e->class, strtrim(class)) &&
293 strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") &&
294 strcmp(class, "KNIGHT")) {
295 com_err(whoami, 0, "updating class for %s %s from %s to %s",
296 first, last, class, e->class);
297 if (status == US_NOT_ALLOWED)
298 status = US_NO_LOGIN_YET;
299 if (status == US_ENROLL_NOT_ALLOWED)
300 status = US_ENROLLED;
301 strcpy(class, e->class);
302 EXEC SQL UPDATE users
303 SET type = NVL(:class,CHR(0)), status = :status, modtime = SYSDATE,
304 modby = :who, modwith = :prog
305 WHERE users_id = :id;
306 if (sqlca.sqlcode != 0) {
307 dbmserr("updating user", sqlca.sqlcode);
312 /* Update name if necessary */
313 if (strcmp(first, strtrim(dfirst)) ||
314 strcmp(last, strtrim(dlast)) ||
315 strcmp(middle, strtrim(dmiddle))) {
316 com_err(whoami, 0, "updating real name for %s %s", first, last);
317 EXEC SQL UPDATE users
318 SET first = NVL(:first,CHR(0)), last = NVL(:last,CHR(0)),
319 middle = NVL(:middle,CHR(0)), modby = :who, modwith = :prog,
321 WHERE users_id = :id;
322 if (sqlca.sqlcode != 0) {
323 dbmserr("updating name", sqlca.sqlcode);
328 changed = nochange = 0;
329 if (encrypted) changed++;
330 strcpy(buf, e->address);
331 while (to = strchr(buf, ','))
333 while (to = strchr(buf, ':'))
336 if (oaddr[0] == ' ' && buf[0]) {
337 strncpy(oaddr, buf, 16);
340 } else if (strncmp(strtrim(oaddr), buf, 15))
343 if (strncmp(strtrim(oaddr), buf, 15))
345 strncpy(oaddr, buf, 16);
357 if (ophone[0] == ' ') {
358 strncpy(ophone, buf, 16);
360 } else if (strncmp(strtrim(ophone), buf, 11))
363 if (strncmp(strtrim(ophone), buf, 11))
365 strncpy(ophone, buf, 16);
371 if (dept[0] == ' ') {
372 strncpy(dept, e->dept, 12);
374 } else if (strncmp(strtrim(dept), e->dept, 11))
377 if (strncmp(strtrim(dept), e->dept, 11))
379 strncpy(dept, e->dept, 12);
390 com_err(whoami, 0, "updating finger for %s %s", first, last);
391 EXEC SQL UPDATE users
392 SET office_addr = NVL(:oaddr,CHR(0)),
393 office_phone = NVL(:ophone,CHR(0)), department = NVL(:dept,CHR(0)),
394 fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
395 xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
396 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
397 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
398 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
399 WHERE users_id = :id;
400 if (sqlca.sqlcode != 0) {
401 dbmserr(NULL, sqlca.sqlcode);
405 EXEC SQL UPDATE users
406 SET xname = NVL(:name,CHR(0)), xdept = NVL(:rdept,CHR(0)),
407 xtitle = NVL(:rtitle,CHR(0)), xaddress = NVL(:raddr,CHR(0)),
408 xphone1 = NVL(:rhphone,CHR(0)), xphone2 = NVL(:rophone,CHR(0)),
409 xmodtime = SYSDATE, clearid = NVL(:sid,CHR(0))
410 WHERE users_id = :id;
411 if (sqlca.sqlcode != 0) {
412 dbmserr(NULL, sqlca.sqlcode);
423 EXEC SQL BEGIN DECLARE SECTION;
424 int id, uid, st, who;
425 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
426 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
427 char *rdept, *rhphone, *rophone;
428 EXEC SQL END DECLARE SECTION;
432 strncpy(oaddr, e->address, 16);
434 while (to = strchr(oaddr, ','))
436 while (to = strchr(oaddr, ':'))
447 strncpy(dept, e->dept, 12);
450 id = set_next_users_id(0);
451 uid = set_next_uid(1);
452 sprintf(login, "#%d", uid);
458 sprintf(fullname, "%s %s %s", first, middle, last);
460 sprintf(fullname, "%s %s", first, last);
461 st = US_NO_LOGIN_YET;
470 EXEC SQL INSERT INTO users
471 (login, users_id, unix_uid, shell, last, first, middle, status,
472 clearid, type, modtime, modby, modwith, fullname, office_addr,
473 office_phone, department, fmodtime, fmodby, fmodwith,
474 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
475 VALUES (:login, :id, :uid, '/bin/athena/tcsh',
476 NVL(:last,CHR(0)), NVL(:first,CHR(0)), NVL(:middle,CHR(0)),
477 :st, NVL(:sid,CHR(0)), NVL(:class,CHR(0)), SYSDATE, :who, :prog,
478 NVL(:fullname,CHR(0)), NVL(:oaddr,CHR(0)), NVL(:ophone,CHR(0)),
479 NVL(:dept,CHR(0)), SYSDATE, :who, :prog, 'NONE',
480 NVL(:name,CHR(0)), NVL(:rdept,CHR(0)), NVL(:title,CHR(0)),
481 NVL(:oaddr,CHR(0)), NVL(:rhphone,CHR(0)), NVL(:rophone,CHR(0)),
483 if (sqlca.sqlcode != 0) {
484 dbmserr("adding user", sqlca.sqlcode);
487 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
491 set_next_users_id(limit)
494 EXEC SQL BEGIN DECLARE SECTION;
495 int rowcount, flag, value, retval;
496 EXEC SQL END DECLARE SECTION;
498 EXEC SQL SELECT value INTO :value FROM numvalues
499 WHERE name = 'users_id';
500 if (sqlfail()) sqlexit();
501 if (sqlca.sqlerrd[2] != 1) {
503 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
508 EXEC SQL SELECT users_id INTO :flag FROM users
509 WHERE users_id = :value;
510 if (sqlfail()) sqlexit();
511 if (sqlca.sqlerrd[2] == 0)
515 if (limit && value > MAX_ID_VALUE)
516 value = MIN_ID_VALUE;
518 EXEC SQL SELECT users_id INTO :flag FROM users
519 WHERE users_id = :value;
520 if (sqlfail()) sqlexit();
521 if (sqlca.sqlerrd[2] == 0)
526 if (limit && value > MAX_ID_VALUE)
527 value = MIN_ID_VALUE;
528 EXEC SQL UPDATE numvalues SET value = :value
529 WHERE name = 'users_id';
530 if (sqlca.sqlcode != 0) {
531 dbmserr("assigning ID", sqlca.sqlcode);
540 EXEC SQL BEGIN DECLARE SECTION;
541 int rowcount, flag, value, retval;
542 EXEC SQL END DECLARE SECTION;
544 EXEC SQL SELECT value INTO :value FROM numvalues
545 WHERE name = 'unix_uid';
546 if (sqlfail()) sqlexit();
547 if (sqlca.sqlerrd[2] != 1) {
549 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
554 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
555 if (sqlfail()) sqlexit();
556 if (sqlca.sqlerrd[2] == 0)
560 if (limit && value > MAX_ID_VALUE)
561 value = MIN_ID_VALUE;
563 EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value;
564 if (sqlfail()) sqlexit();
565 if (sqlca.sqlerrd[2] == 0)
570 if (limit && value > MAX_ID_VALUE)
571 value = MIN_ID_VALUE;
572 EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
573 if (sqlca.sqlcode != 0) {
574 dbmserr("assigning ID", sqlca.sqlcode);
583 dbmserr(NULL, sqlca.sqlcode);
584 EXEC SQL ROLLBACK WORK;
588 dbmserr(char *where, int what)
591 int bufsize=256, msglength=0;
593 sqlglm(err_msg, &bufsize, &msglength);
594 err_msg[msglength]=0;
597 com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
599 com_err(whoami, 0, "DBMS error %s", err_msg);