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 != 100)
75 #define SQL_DUPLICATE -40100
83 struct entry *e, *get_next_entry();
85 char buf[BUFSIZ], *file = NULL;
87 whoami = rindex(argv[0], '/');
93 for (i = 1; i < argc; i++) {
94 if (!strcmp(argv[i], "-w"))
96 else if (!strcmp(argv[i], "-D"))
97 setenv("ING_SET", "set printqry");
98 else if (!strcmp(argv[i], "-n"))
100 else if (file != NULL)
101 fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
106 in = fopen(file, "r");
108 fprintf(stderr, "Unable to open %s for input\n", file);
115 EXEC SQL CONNECT moira;
116 if (sqlca.sqlcode != 0) {
117 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
121 while (e = get_next_entry(in)) {
123 EXEC SQL COMMIT WORK;
124 if (sqlca.sqlcode != 0) {
125 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
139 char *substr(buf, key)
145 for (l = strlen(key); *buf; buf++)
146 if (!strncmp(buf, key, l))
152 struct entry *get_next_entry(in)
155 static struct entry e;
156 static char buf[BUFSIZ], mid[16], eid[16], email[256];
157 static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
158 static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
159 static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
160 static char host[LEN_HOST+1];
161 int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
164 if (fgets(buf, sizeof(buf), in) == NULL)
165 return((struct entry *)NULL);
167 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
168 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
169 strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
170 strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
171 strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
172 strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
173 strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
174 strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
175 strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
178 e.name = strtrim(sname);
179 p = index(name, ',');
182 e.last = strtrim(name);
188 if (p = index(e.first, ' ')) {
190 e.first = strtrim(e.first);
191 e.middle = strtrim(p + 1);
193 e.first = strtrim(e.first);
200 ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
203 LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
205 LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
213 EncryptID(e.eid, e.id, e.first, e.last);
215 e.address = strtrim(office);
216 e.phone = strtrim(phone);
217 e.phone2 = strtrim(phone2);
218 e.dept = strtrim(dept);
219 e.title = strtrim(title);
222 if (!strcmp(e.dept, "PROJECT ATHENA"))
224 else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
226 else if (!strcmp(e.title, "VISITING SCIENTIST"))
227 e.class = "VSCIENTI";
229 strcpy(email, strtrim(username));
231 strncat(email, strtrim(host));
241 int changed, nochange, encrypted;
242 char buf[BUFSIZ], *from, *to;
243 EXEC SQL BEGIN DECLARE SECTION;
244 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
245 char *raddr, *rhphone, *rophone, *prog;
246 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
248 EXEC SQL END DECLARE SECTION;
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) sqlexit();
289 /* Update class/state if necessary */
290 if (strcmp(e->class, strtrim(class)) &&
291 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
292 com_err(whoami, 0, "updating class for %s %s from %s to %s",
293 first, last, class, e->class);
294 if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
295 status = US_NO_LOGIN_YET;
296 if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
297 status = US_ENROLLED;
298 strcpy(class, e->class);
299 EXEC SQL UPDATE users
300 SET type = :class, status = :status, modtime = 'now',
301 modby = :who, modwith = :prog
302 WHERE users_id = :id;
303 if (sqlca.sqlcode != 0) {
304 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
309 /* Update name if necessary */
310 if (strcmp(first, strtrim(dfirst)) ||
311 strcmp(last, strtrim(dlast)) ||
312 strcmp(middle, strtrim(dmiddle))) {
313 com_err(whoami, 0, "updating real name for %s %s", first, last);
314 EXEC SQL UPDATE users
315 SET first = :first, last = :last, middle = :middle,
316 modby = :who, modwith = :prog, modtime = 'now'
317 WHERE users_id = :id;
318 if (sqlca.sqlcode != 0) {
319 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
324 changed = nochange = 0;
325 if (encrypted) changed++;
326 strcpy(buf, e->address);
327 while (to = index(buf, ','))
329 while (to = index(buf, ':'))
332 if (oaddr[0] == ' ' && buf[0]) {
333 strncpy(oaddr, buf, 16);
336 } else if (strncmp(strtrim(oaddr), buf, 15))
339 if (strncmp(strtrim(oaddr), buf, 15))
341 strncpy(oaddr, buf, 16);
353 if (ophone[0] == ' ') {
354 strncpy(ophone, buf, 16);
356 } else if (strncmp(strtrim(ophone), buf, 11))
359 if (strncmp(strtrim(ophone), buf, 11))
361 strncpy(ophone, buf, 16);
367 if (dept[0] == ' ') {
368 strncpy(dept, e->dept, 12);
370 } else if (strncmp(strtrim(dept), e->dept, 11))
373 if (strncmp(strtrim(dept), e->dept, 11))
375 strncpy(dept, e->dept, 12);
386 com_err(whoami, 0, "updating finger for %s %s", first, last);
387 EXEC SQL REPEATED UPDATE users
388 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
389 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
390 xname = :name, xdept = :rdept, xtitle = :rtitle,
391 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
392 xmodtime = 'now', clearid = :sid
393 WHERE users_id = :id;
394 if (sqlca.sqlcode != 0) {
395 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
399 EXEC SQL REPEATED UPDATE users
400 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
401 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
402 xmodtime = 'now', clearid = :sid
403 WHERE users_id = :id;
404 if (sqlca.sqlcode != 0) {
405 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
416 EXEC SQL BEGIN DECLARE SECTION;
417 int id, uid, st, who;
418 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
419 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
420 char *rdept, *rhphone, *rophone;
421 EXEC SQL END DECLARE SECTION;
425 strncpy(oaddr, e->address, 16);
427 while (to = index(oaddr, ','))
429 while (to = index(oaddr, ':'))
440 strncpy(dept, e->dept, 12);
443 id = set_next_users_id(0);
444 uid = set_next_uid(1);
445 sprintf(login, "#%d", uid);
451 sprintf(fullname, "%s %s %s", first, middle, last);
453 sprintf(fullname, "%s %s", first, last);
455 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
456 st = US_NO_LOGIN_YET;
465 EXEC SQL REPEATED INSERT INTO users
466 (login, users_id, uid, shell, last, first, middle, status,
467 clearid, type, modtime, modby, modwith, fullname, office_addr,
468 office_phone, department, fmodtime, fmodby, fmodwith,
469 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
470 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
471 :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
472 :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
473 :title, :oaddr, :rhphone, :rophone, 'now');
474 if (sqlca.sqlcode != 0) {
475 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
478 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
482 set_next_users_id(limit)
485 EXEC SQL BEGIN DECLARE SECTION;
486 int rowcount, flag, value, retval;
487 EXEC SQL END DECLARE SECTION;
489 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
490 WHERE name = 'users_id';
491 if (sqlfail()) sqlexit();
492 if (sqlca.sqlerrd[2] != 1) {
494 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
499 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
500 WHERE users_id = :value;
501 if (sqlfail()) sqlexit();
502 if (sqlca.sqlerrd[2] == 0)
506 if (limit && value > MAX_ID_VALUE)
507 value = MIN_ID_VALUE;
509 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
510 WHERE users_id = :value;
511 if (sqlfail()) sqlexit();
512 if (sqlca.sqlerrd[2] == 0)
517 if (limit && value > MAX_ID_VALUE)
518 value = MIN_ID_VALUE;
519 EXEC SQL REPEATED UPDATE numvalues SET value = :value
520 WHERE name = 'users_id';
521 if (sqlca.sqlcode != 0) {
522 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
531 EXEC SQL BEGIN DECLARE SECTION;
532 int rowcount, flag, value, retval;
533 EXEC SQL END DECLARE SECTION;
535 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
537 if (sqlfail()) sqlexit();
538 if (sqlca.sqlerrd[2] != 1) {
540 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
545 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
546 if (sqlfail()) sqlexit();
547 if (sqlca.sqlerrd[2] == 0)
551 if (limit && value > MAX_ID_VALUE)
552 value = MIN_ID_VALUE;
554 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
555 if (sqlfail()) sqlexit();
556 if (sqlca.sqlerrd[2] == 0)
561 if (limit && value > MAX_ID_VALUE)
562 value = MIN_ID_VALUE;
563 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
564 if (sqlca.sqlcode != 0) {
565 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
574 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
575 EXEC SQL ROLLBACK WORK;