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, *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)
271 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
272 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
274 WHERE clearid = :sid;
276 if (sqlca.sqlcode == SQL_DUPLICATE) {
277 com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
283 EXEC SQL REPEATED SELECT users_id, first, last, middle, type, home_addr, home_phone, office_phone, status, department
284 INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, :ophone, :status, :dept
286 WHERE last = :last and first = :first and clearid = :eid;
287 if (sqlfail() && sqlca.sqlcode != SQL_DUPLICATE) sqlexit();
295 /* See if class changed: if it's different, and the value in the database
296 * is not STAFF or SIPB, then update the database. Since they were on the
297 * students tape, make the account usable.
299 if (strcmp(e->class, strtrim(class)) &&
300 strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
301 com_err(whoami, 0, "updating class for user %s %s from %s to %s",
302 first, last, class, e->class);
303 if (status == US_NOT_ALLOWED) status = US_NO_LOGIN_YET;
304 if (status == US_ENROLL_NOT_ALLOWED) status = US_ENROLLED;
305 strcpy(class, e->class);
306 EXEC SQL REPEATED UPDATE users
307 SET type = :class, status = :status, modtime = 'now',
308 modby = :who, modwith = :prog
309 WHERE users_id = :id;
310 if (sqlca.sqlcode != 0) {
311 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
316 /* Deal with updating the finger info if necessary */
318 changed = nochange = 0;
319 if (encrypted) changed++;
320 strcpy(buf, e->address);
323 strcat(buf, e->dorm);
327 strcat(buf, e->city);
332 strcat(buf, e->state);
334 while (to = index(buf, ','))
336 while (to = index(buf, ':'))
339 if (haddr[0] == ' ') {
340 strncpy(haddr, buf, 80);
343 } else if (strncmp(strtrim(haddr), buf, 80))
346 if (strncmp(strtrim(haddr), buf, 80))
348 strncpy(haddr, buf, 80);
360 if (hphone[0] == ' ') {
361 strncpy(hphone, buf, 16);
363 } else if (strncmp(strtrim(hphone), buf, 16))
366 if (strncmp(strtrim(hphone), buf, 16))
368 strncpy(hphone, buf, 16);
380 if (ophone[0] == ' ') {
381 strncpy(ophone, buf, 12);
383 } else if (strncmp(strtrim(ophone), buf, 12))
386 if (strncmp(strtrim(ophone), buf, 12))
388 strncpy(ophone, buf, 12);
391 e->course = e->course;
393 if (dept[0] == ' ') {
394 strncpy(dept, e->course, 12);
396 } else if (strncmp(strtrim(dept), e->course, 11))
399 if (strncmp(strtrim(dept), e->course, 11))
401 strncpy(dept, e->course, 12);
410 strcpy(raddr, e->address);
413 strcat(raddr, e->dorm);
417 strcat(raddr, e->city);
421 if (isupper(e->state[0]) && isupper(e->state[1]) &&
422 isdigit(e->state[2]) && isdigit(e->state[3]) &&
423 isdigit(e->state[4]) && isdigit(e->state[5]) &&
424 isdigit(e->state[6])) {
425 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
429 strcat(raddr, e->state);
433 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
436 com_err(whoami, 0, "updating finger for %s %s", first, last);
437 EXEC SQL REPEATED UPDATE users
438 SET home_addr = :haddr, home_phone = :hphone,
439 office_phone = :ophone, department = :dept,
440 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
441 xname = :name, xdept = :rdept, xtitle = :rtitle,
442 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
443 xmodtime = date('now'), clearid = :sid
444 WHERE users_id = :id;
445 if (sqlca.sqlcode != 0) {
446 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
450 EXEC SQL REPEATED UPDATE users
451 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
452 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
453 xmodtime = date('now'), clearid = :sid
454 WHERE users_id = :id;
455 if (sqlca.sqlcode != 0) {
456 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
466 char buf[512], *from, *to;
467 EXEC SQL BEGIN DECLARE SECTION;
469 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
470 char haddr[81], hphone[17], ophone[13], dept[24], *title, raddr[81], *name;
471 EXEC SQL END DECLARE SECTION;
475 strcpy(buf, e->address);
478 strcat(buf, e->dorm);
482 strcat(buf, e->city);
486 strcat(buf, e->state);
488 strncpy(haddr, buf, 80);
497 strncpy(hphone, buf, 16);
506 strncpy(ophone, buf, 12);
507 strncpy(dept, e->course, 12);
509 id = set_next_users_id(0);
510 uid = set_next_uid(1);
511 sprintf(login, "#%d", uid);
519 sprintf(fullname, "%s %s %s", first, middle, last);
521 sprintf(fullname, "%s %s", first, last);
523 strcpy(raddr, e->address);
526 strcat(raddr, e->dorm);
530 strcat(raddr, e->city);
534 if (isupper(e->state[0]) && isupper(e->state[1]) &&
535 isdigit(e->state[2]) && isdigit(e->state[3]) &&
536 isdigit(e->state[4]) && isdigit(e->state[5]) &&
537 isdigit(e->state[6])) {
538 sprintf(buf, "%c%c %s", e->state[0],e->state[1],
542 strcat(raddr, e->state);
546 strcat(raddr, "MIT INTERDEPARTMENTAL MAIL");
548 EXEC SQL REPEATED INSERT INTO users
549 (login, users_id, uid, shell, last, first, middle, status,
550 clearid, type, modtime, modby, modwith, fullname, home_addr,
551 home_phone, office_phone, department, fmodtime, fmodby, fmodwith,
552 potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
553 VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, 0,
554 :sid, :class, 'now', :who, :prog, :fullname, :haddr, :hphone,
555 :ophone, :dept, 'now', :who, :prog, 'NONE', :name, :dept,
556 :title, :raddr, :hphone, :ophone, date('now'));
557 if (sqlca.sqlcode != 0) {
558 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
561 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
566 set_next_users_id(limit)
569 EXEC SQL BEGIN DECLARE SECTION;
570 int rowcount, flag, value, retval;
571 EXEC SQL END DECLARE SECTION;
573 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
574 WHERE name = 'users_id';
575 if (sqlfail()) sqlexit();
576 if (sqlca.sqlerrd[2] != 1) {
578 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
583 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
584 WHERE users_id = :value;
585 if (sqlfail()) sqlexit();
586 if (sqlca.sqlerrd[2] == 0)
590 if (limit && value > MAX_ID_VALUE)
591 value = MIN_ID_VALUE;
593 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
594 WHERE users_id = :value;
595 if (sqlfail()) sqlexit();
596 if (sqlca.sqlerrd[2] == 0)
601 if (limit && value > MAX_ID_VALUE)
602 value = MIN_ID_VALUE;
603 EXEC SQL REPEATED UPDATE numvalues SET value = :value
604 WHERE name = 'users_id';
605 if (sqlca.sqlcode != 0) {
606 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
615 EXEC SQL BEGIN DECLARE SECTION;
616 int rowcount, flag, value, retval;
617 EXEC SQL END DECLARE SECTION;
619 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
621 if (sqlfail()) sqlexit();
622 if (sqlca.sqlerrd[2] != 1) {
624 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
629 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
630 if (sqlfail()) sqlexit();
631 if (sqlca.sqlerrd[2] == 0)
635 if (limit && value > MAX_ID_VALUE)
636 value = MIN_ID_VALUE;
638 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
639 if (sqlfail()) sqlexit();
640 if (sqlca.sqlerrd[2] == 0)
645 if (limit && value > MAX_ID_VALUE)
646 value = MIN_ID_VALUE;
647 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
648 if (sqlca.sqlcode != 0) {
649 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
658 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
659 EXEC SQL ROLLBACK WORK;