9 #include <moira_site.h>
10 EXEC SQL INCLUDE sqlca;
13 #define WHO 11859 /* root */
14 #define PROG "stu-tape"
16 #define MAX_ID_VALUE 31999
17 #define MIN_ID_VALUE 101
39 #define LOC_ADDRESS 80
40 #define LOC_DORM_ROOM 110
43 #define LOC_DPHONE 155
44 #define LOC_MPHONE 243
50 #define LEN_ADDRESS 30
51 #define LEN_DORM_ROOM 15
80 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
81 #define SQL_DUPLICATE -40100
88 struct entry *e, *get_next_entry();
90 char buf[BUFSIZ], *file = NULL;
92 whoami = rindex(argv[0], '/');
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);
120 EXEC SQL CONNECT moira;
121 if (sqlca.sqlcode != 0) {
122 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
126 while (e = get_next_entry(in)) {
128 EXEC SQL COMMIT WORK;
129 if (sqlca.sqlcode != 0) {
130 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
144 struct entry *get_next_entry(in)
147 static struct entry e;
148 static char buf[BUFSIZ], eid[16], classbuf[10], titlebuf[12];
149 static char name[LEN_NAME+1], id[LEN_ID+1], course[LEN_COURSE+1];
150 static char year[LEN_YEAR+1], address[LEN_ADDRESS+1];
151 static char dorm_room[LEN_DORM_ROOM+1], city[LEN_CITY+1];
152 static char state[LEN_STATE+1], dphone[LEN_DPHONE+1], mphone[LEN_MPHONE+1];
153 static char sname[LEN_NAME+1], title[128];
154 static int nyear = 0;
155 int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v;
162 gettimeofday(&tv, NULL);
163 tm = localtime(&tv.tv_sec);
169 if (fgets(buf, sizeof(buf), in) == NULL)
170 return((struct entry *)NULL);
172 strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
173 strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
174 strncpy(course, &buf[LOC_COURSE], LEN_COURSE); course[LEN_COURSE] = 0;
175 strncpy(year, &buf[LOC_YEAR], LEN_YEAR); year[LEN_YEAR] = 0;
176 strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); address[LEN_ADDRESS] = 0;
177 strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); dorm_room[LEN_DORM_ROOM] = 0;
178 strncpy(city, &buf[LOC_CITY], LEN_CITY); city[LEN_CITY] = 0;
179 strncpy(state, &buf[LOC_STATE], LEN_STATE); state[LEN_STATE] = 0;
180 strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); dphone[LEN_DPHONE] = 0;
181 strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); mphone[LEN_MPHONE] = 0;
184 e.name = strtrim(sname);
185 p = index(name, ',');
188 e.last = strtrim(name);
194 if (p = index(e.first, ' ')) {
196 e.first = strtrim(e.first);
197 e.middle = strtrim(p + 1);
199 e.first = strtrim(e.first);
206 ends_jr = ends_iii = ends_iv = ends_sr = 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,
220 EncryptID(e.eid, e.id, e.first, e.last);
222 e.year = strtrim(year);
224 if (e.year[0] == 'G') {
226 sprintf(title, "Grad Student");
229 sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900);
230 sprintf(title, "Undergrad (class of %s)", classbuf);
233 e.course = strtrim(course);
234 e.address = strtrim(address);
235 e.dorm = strtrim(dorm_room);
236 e.city = strtrim(city);
237 e.state = strtrim(state);
238 e.dphone = strtrim(dphone);
239 e.mphone = strtrim(mphone);
247 int changed, nochange, encrypted;
248 char buf[BUFSIZ], *from, *to;
249 EXEC SQL BEGIN DECLARE SECTION;
250 char *first, *last, *middle, *eid, *title, *sid, *name, *rname, *rdept, *rtitle;
251 char *rophone, *rhphone, *prog;
252 char class[9], haddr[128], hphone[33], ophone[33], dept[33], raddr[128];
253 char dfirst[17], dlast[17], dmiddle[17];
255 EXEC SQL END DECLARE SECTION;
260 if (strlen(first) > 16)
263 if (strlen(last) > 16)
272 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
273 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
275 WHERE clearid = :sid;
277 if (sqlca.sqlcode == SQL_DUPLICATE) {
278 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
284 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
285 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
287 WHERE last = :last and first = :first and clearid = :eid;
288 if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE) sqlexit();
296 /* See if class changed: if it's different, and the value in the database
297 * is not STAFF or SIPB, then update the database. Since they were on the
298 * students tape, make the account usable.
300 if (strcmp(e->class, strtrim(class)) &&
301 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
302 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
303 first, last, class, e->class);
304 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
305 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
306 strcpy(class, e->class);
307 EXEC SQL REPEATED UPDATE users
308 SET type = :class, status = :status, modtime = 'now',
309 modby = :who, modwith = :prog
310 WHERE users_id = :id;
311 if (sqlca.sqlcode != 0) {
312 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
317 /* Update name if necessary */
318 if (strcmp(first, strtrim(dfirst)) ||
319 strcmp(last, strtrim(dlast)) ||
320 strcmp(middle, strtrim(dmiddle))) {
321 com_err(whoami, 0, "updating real name for %s %s", first, last);
322 EXEC SQL UPDATE users
323 SET first = :first, last = :last, middle = :middle,
324 modby = :who, modwith = :prog, modtime = 'now'
325 WHERE users_id = :id;
326 if (sqlca.sqlcode != 0) {
327 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
332 /* Deal with updating the finger info if necessary */
333 changed = nochange = 0;
334 if (encrypted) changed++;
335 strcpy(buf, e->address);
338 strcat(buf, e->dorm);
342 strcat(buf, e->city);
347 strcat(buf, e->state);
349 while (to = index(buf, ','))
351 while (to = index(buf, ':'))
354 if (haddr[0] == ' ') {
355 strncpy(haddr, buf, 80);
358 } else if (strncmp(strtrim(haddr), buf, 80))
361 if (strncmp(strtrim(haddr), buf, 80))
363 strncpy(haddr, buf, 80);
375 if (hphone[0] == ' ') {
376 strncpy(hphone, buf, 16);
378 } else if (strncmp(strtrim(hphone), buf, 16))
381 if (strncmp(strtrim(hphone), buf, 16))
383 strncpy(hphone, buf, 16);
395 if (ophone[0] == ' ') {
396 strncpy(ophone, buf, 12);
398 } else if (strncmp(strtrim(ophone), buf, 12))
401 if (strncmp(strtrim(ophone), buf, 12))
403 strncpy(ophone, buf, 12);
406 e->course = e->course;
408 if (dept[0] == ' ') {
409 strncpy(dept, e->course, 12);
411 } else if (strncmp(strtrim(dept), e->course, 11))
414 if (strncmp(strtrim(dept), e->course, 11))
416 strncpy(dept, e->course, 12);
425 strcpy(raddr, e->address);
428 strcat(raddr, e->dorm);
432 strcat(raddr, e->city);
436 if (isupper(e->state[0]) && isupper(e->state[1]) &&
437 isdigit(e->state[2]) && isdigit(e->state[3]) &&
438 isdigit(e->state[4]) && isdigit(e->state[5]) &&
439 isdigit(e->state[6])) {
440 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
444 strcat(raddr, e->state);
448 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
451 com_err(whoami, 0, "updating finger for %s %s", first, last);
452 EXEC SQL REPEATED UPDATE users
453 SET home_addr = :haddr, home_phone = :hphone,
454 office_phone = :ophone, department = :dept,
455 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
456 xname = :name, xdept = :rdept, xtitle = :rtitle,
457 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
458 xmodtime = date('now'), clearid = :sid
459 WHERE users_id = :id;
460 if (sqlca.sqlcode != 0) {
461 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
465 EXEC SQL REPEATED UPDATE users
466 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
467 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
468 xmodtime = date('now'), clearid = :sid
469 WHERE users_id = :id;
470 if (sqlca.sqlcode != 0) {
471 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
481 char buf[512], *from, *to;
482 EXEC SQL BEGIN DECLARE SECTION;
484 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
485 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
486 EXEC SQL END DECLARE SECTION;
490 strcpy(buf, e->address);
493 strcat(buf, e->dorm);
497 strcat(buf, e->city);
501 strcat(buf, e->state);
503 strncpy(haddr, buf, 80);
512 strncpy(hphone, buf, 16);
521 strncpy(ophone, buf, 12);
522 strncpy(dept, e->course, 12);
524 id = set_next_users_id(0);
525 uid = set_next_uid(1);
526 sprintf(login, "#%d", uid);
534 sprintf(fullname, "%s %s %s", first, middle, last);
536 sprintf(fullname, "%s %s", first, last);
538 strcpy(raddr, e->address);
541 strcat(raddr, e->dorm);
545 strcat(raddr, e->city);
549 if (isupper(e->state[0]) && isupper(e->state[1]) &&
550 isdigit(e->state[2]) && isdigit(e->state[3]) &&
551 isdigit(e->state[4]) && isdigit(e->state[5]) &&
552 isdigit(e->state[6])) {
553 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
557 strcat(raddr, e->state);
561 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
563 EXEC SQL REPEATED INSERT INTO users
564 (login, users_id, uid, shell, last, first, middle, status,
565 clearid, type, modtime, modby, modwith, fullname, home_addr,
566 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
567 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
568 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0,
569 :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
570 :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
571 :title, :raddr, :hphone, :ophone, date('now'));
572 if (sqlca.sqlcode != 0) {
573 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
576 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
581 set_next_users_id(limit)
584 EXEC SQL BEGIN DECLARE SECTION;
585 int rowcount, flag, value, retval;
586 EXEC SQL END DECLARE SECTION;
588 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
589 WHERE name = 'users_id';
590 if (sqlfail()) sqlexit();
591 if (sqlca.sqlerrd[2] != 1) {
593 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
598 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
599 WHERE users_id = :value;
600 if (sqlfail()) sqlexit();
601 if (sqlca.sqlerrd[2] == 0)
605 if (limit && value > MAX_ID_VALUE)
606 value = MIN_ID_VALUE;
608 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
609 WHERE users_id = :value;
610 if (sqlfail()) sqlexit();
611 if (sqlca.sqlerrd[2] == 0)
616 if (limit && value > MAX_ID_VALUE)
617 value = MIN_ID_VALUE;
618 EXEC SQL REPEATED UPDATE numvalues SET value = :value
619 WHERE name = 'users_id';
620 if (sqlca.sqlcode != 0) {
621 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
630 EXEC SQL BEGIN DECLARE SECTION;
631 int rowcount, flag, value, retval;
632 EXEC SQL END DECLARE SECTION;
634 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
636 if (sqlfail()) sqlexit();
637 if (sqlca.sqlerrd[2] != 1) {
639 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
644 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
645 if (sqlfail()) sqlexit();
646 if (sqlca.sqlerrd[2] == 0)
650 if (limit && value > MAX_ID_VALUE)
651 value = MIN_ID_VALUE;
653 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
654 if (sqlfail()) sqlexit();
655 if (sqlca.sqlerrd[2] == 0)
660 if (limit && value > MAX_ID_VALUE)
661 value = MIN_ID_VALUE;
662 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
663 if (sqlca.sqlcode != 0) {
664 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
673 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
674 EXEC SQL ROLLBACK WORK;