#include <sys/time.h>
#include <moira.h>
#include <moira_site.h>
+EXEC SQL INCLUDE sqlca;
-##define WHO 11859 /* root */
-##define PROG "emp-tape"
+#define WHO 11859 /* root */
+#define PROG "emp-tape"
-#define MAX_ID_VALUE 32766
+#define MAX_ID_VALUE 31999
#define MIN_ID_VALUE 101
/* File format is:
char *whoami;
int newfinger = 0;
+#define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
+#define SQL_DUPLICATE -40100
+#define SQL_DEADLOCK -49900
+
main(argc, argv)
int argc;
char **argv;
-##{
+{
FILE *in;
struct entry *e, *get_next_entry();
int i, wait = 0;
setlinebuf(stdout);
setlinebuf(stderr);
-## ingres sms
-## range of u is users
+ EXEC SQL CONNECT moira;
+ if (sqlca.sqlcode != 0) {
+ com_err(whoami, 0, "ingres error %d opening database", sqlca.sqlcode);
+ exit(1);
+ }
while (e = get_next_entry(in)) {
+ again:
process_entry(e);
+ EXEC SQL COMMIT WORK;
+ if (sqlca.sqlcode != 0) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "commiting work");
+ goto again;
+ } else {
+ com_err(whoami, 0, "ingres error %d committing work", sqlca.sqlcode);
+ exit(1);
+ }
+ }
if (wait) {
printf("Next");
fflush(stdout);
}
}
-## exit
exit(0);
-##}
+}
char *substr(buf, key)
e.title = strtrim(title);
e.class = "MITS";
- if (!strcmp(e.dept, "PROJECT ATHENA"))
- e.class = "STAFF";
- else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
+ if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
e.class = "FACULTY";
- else if (!strcmp(e.title, "VISITING SCIENTIST"))
- e.class = "VSCIENTI";
strcpy(email, strtrim(username));
if (host[0] == '@')
process_entry(e)
struct entry *e;
-##{
- int changed, nochange;
+{
+ int changed, nochange, encrypted;
char buf[BUFSIZ], *from, *to;
-## char *first, *last, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
-## char *raddr, *rhphone, *rophone;
-## char class[9], oaddr[25], ophone[17], dept[128];
-## int id, status;
-
+ EXEC SQL BEGIN DECLARE SECTION;
+ char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
+ char *raddr, *rhphone, *rophone, *prog;
+ char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
+ int id, status, who;
+ EXEC SQL END DECLARE SECTION;
+
+ /* Don't process Lincoln Labs */
+ if (!strncmp(e->address, "LL", 2))
+ return;
+
+ who = WHO;
+ prog = PROG;
first = e->first;
if (strlen(first) > 16)
first[16] = 0;
last = e->last;
if (strlen(last) > 16)
last[16] = 0;
+ middle = e->middle;
eid = e->eid;
sid = e->id;
id = 0;
+ encrypted = 0;
/* Get user info */
-## repeat retrieve (id = u.users_id, class = u.mit_year, oaddr = u.office_addr,
-## ophone = u.office_phone, dept = u.mit_dept, status = u.#status)
-## where u.#last = @last and u.#first = @first and u.mit_id = @sid
+ EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
+ INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
+ FROM users
+ WHERE clearid = :sid;
+ if (sqlfail()) {
+ if (sqlca.sqlcode == SQL_DUPLICATE) {
+ com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
+ return;
+ } else if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
+ EXEC SQL ROLLBACK;
+ return process_entry(e);
+ } else
+ sqlexit();
+ }
if (id == 0) {
-## repeat retrieve (id = u.users_id, class = u.mit_year,
-## oaddr = u.office_addr, ophone = u.office_phone,
-## dept = u.mit_dept, status = u.#status)
-## where u.#last = @last and u.#first = @first and u.mit_id = @eid
+ EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
+ INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
+ FROM users
+ WHERE last = :last and first = :first and clearid = :eid;
+ if (sqlfail()) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
+ EXEC SQL ROLLBACK;
+ return process_entry(e);
+ } else if (sqlca.sqlcode != SQL_DUPLICATE)
+ sqlexit();
+ }
+ encrypted++;
if (id == 0) {
newuser(e);
return;
}
}
+ /* Update class/state if necessary */
if (strcmp(e->class, strtrim(class)) &&
strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
com_err(whoami, 0, "updating class for %s %s from %s to %s",
if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
status = US_ENROLLED;
strcpy(class, e->class);
-## repeat replace u (mit_year = @class, #status = @status,
-## modtime = "now", modby = WHO, modwith = PROG)
-## where u.users_id = @id
+ EXEC SQL UPDATE users
+ SET type = :class, status = :status, modtime = 'now',
+ modby = :who, modwith = :prog
+ WHERE users_id = :id;
+ if (sqlca.sqlcode != 0) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "updating user");
+ return;
+ } else {
+ com_err(whoami, 0, "ingres error %d updating user", sqlca.sqlcode);
+ exit(1);
+ }
+ }
+ }
+
+ /* Update name if necessary */
+ if (strcmp(first, strtrim(dfirst)) ||
+ strcmp(last, strtrim(dlast)) ||
+ strcmp(middle, strtrim(dmiddle))) {
+ com_err(whoami, 0, "updating real name for %s %s", first, last);
+ EXEC SQL UPDATE users
+ SET first = :first, last = :last, middle = :middle,
+ modby = :who, modwith = :prog, modtime = 'now'
+ WHERE users_id = :id;
+ if (sqlca.sqlcode != 0) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "updating name");
+ return;
+ } else {
+ com_err(whoami, 0, "ingres error %d updating name", sqlca.sqlcode);
+ exit(1);
+ }
+ }
}
changed = nochange = 0;
+ if (encrypted) changed++;
strcpy(buf, e->address);
while (to = index(buf, ','))
*to = ';';
rophone = e->phone2;
if (changed) {
com_err(whoami, 0, "updating finger for %s %s", first, last);
-## repeat replace u (office_addr = @oaddr,
-## office_phone = @ophone, #mit_dept = @dept,
-## fmodtime = "now", fmodby = WHO, fmodwith = PROG,
-## xname = @name, xdept = @rdept, xtitle = @rtitle,
-## xaddress = @raddr, xphone1 = @rhphone, xphone2 = @rophone,
-## xmodtime = date("now"), mit_id = @sid)
-## where u.users_id = @id
+ EXEC SQL REPEATED UPDATE users
+ SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
+ fmodtime = 'now', fmodby = :who, fmodwith = :prog,
+ xname = :name, xdept = :rdept, xtitle = :rtitle,
+ xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
+ xmodtime = 'now', clearid = :sid
+ WHERE users_id = :id;
+ if (sqlca.sqlcode != 0) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
+ EXEC SQL ROLLBACK;
+ return process_entry(e);
+ } else {
+ com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+ exit(1);
+ }
+ }
} else {
-## repeat replace u (xname = @name, xdept = @rdept, xtitle = @rtitle,
-## xaddress = @raddr, xphone1 = @rhphone, xphone2 = @rophone,
-## xmodtime = date("now"), mit_id = @sid)
-## where u.users_id = @id
+ EXEC SQL REPEATED UPDATE users
+ SET xname = :name, xdept = :rdept, xtitle = :rtitle,
+ xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
+ xmodtime = 'now', clearid = :sid
+ WHERE users_id = :id;
+ if (sqlca.sqlcode != 0) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
+ EXEC SQL ROLLBACK;
+ return process_entry(e);
+ } else {
+ com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+ exit(1);
+ }
+ }
}
-##}
+}
newuser(e)
struct entry *e;
-##{
+{
char *from, *to;
-## int id, uid, st;
-## char *last, *first, *class, *middle, login[9], *sid, fullname[65];
-## char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
-## char *rdept, *rhphone, *rophone;
-
+ EXEC SQL BEGIN DECLARE SECTION;
+ int id, uid, st, who;
+ char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
+ char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
+ char *rdept, *rhphone, *rophone;
+ EXEC SQL END DECLARE SECTION;
+
+ who = WHO;
+ prog = PROG;
strncpy(oaddr, e->address, 16);
oaddr[16] = 0;
while (to = index(oaddr, ','))
strncpy(dept, e->dept, 12);
dept[12] = 0;
- id = set_next_object_id("users_id", 0);
- uid = set_next_object_id("uid", 1);
+ id = set_next_users_id(0);
+ uid = set_next_uid(1);
sprintf(login, "#%d", uid);
last = e->last;
first = e->first;
rhphone = e->phone;
rophone = e->phone2;
+ EXEC SQL REPEATED INSERT INTO users
+ (login, users_id, uid, shell, last, first, middle, status,
+ clearid, type, modtime, modby, modwith, fullname, office_addr,
+ office_phone, department, fmodtime, fmodby, fmodwith,
+ potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
+ VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
+ :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
+ :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
+ :title, :oaddr, :rhphone, :rophone, 'now');
+ if (sqlca.sqlcode != 0) {
+ if (sqlca.sqlcode == SQL_DEADLOCK) {
+ com_err(whoami, MR_DEADLOCK, "adding user");
+ } else {
+ com_err(whoami, 0, "ingres error %d adding user", sqlca.sqlcode);
+ exit(1);
+ }
+ } else
+ com_err(whoami, 0, "adding user %s %s", e->first, e->last);
+}
+
-## append users (#login = login, users_id = id, #uid = uid, shell = "/bin/csh",
-## #last = last, #first = first, #middle = middle, status = st,
-## #mit_id = sid, #mit_year = class,
-## modtime = "now", modby = WHO, modwith = PROG,
-## #fullname = fullname, office_addr = oaddr,
-## office_phone = ophone, #mit_dept = dept,
-## fmodtime = "now", fmodby = WHO, fmodwith = PROG,
-## potype = "NONE",
-## xname = name, xdept = rdept, xtitle = title,
-## xaddress = oaddr, xphone1 = rhphone, xphone2 = rophone,
-## xmodtime = date("now"))
+set_next_users_id(limit)
+ int limit;
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int rowcount, flag, value, retval;
+ EXEC SQL END DECLARE SECTION;
+
+ EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
+ WHERE name = 'users_id';
+ if (sqlfail()) sqlexit();
+ if (sqlca.sqlerrd[2] != 1) {
+ EXEC SQL ROLLBACK;
+ com_err(whoami, MR_INTERNAL, "values table inconsistancy");
+ exit(1);
+ }
-##}
+ flag = 0;
+ EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
+ WHERE users_id = :value;
+ if (sqlfail()) sqlexit();
+ if (sqlca.sqlerrd[2] == 0)
+ flag = 0;
+ while (flag) {
+ value++;
+ if (limit && value > MAX_ID_VALUE)
+ value = MIN_ID_VALUE;
+ flag = 0;
+ EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
+ WHERE users_id = :value;
+ if (sqlfail()) sqlexit();
+ if (sqlca.sqlerrd[2] == 0)
+ flag = 0;
+ }
+ retval = value++;
+ if (limit && value > MAX_ID_VALUE)
+ value = MIN_ID_VALUE;
+ EXEC SQL REPEATED UPDATE numvalues SET value = :value
+ WHERE name = 'users_id';
+ if (sqlca.sqlcode != 0) {
+ com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
+ exit(1);
+ }
+ return(retval);
+}
-set_next_object_id(object, limit)
- char *object;
+set_next_uid(limit)
int limit;
-##{
-## char *name;
-## int rowcount, exists, value;
-
- name = object;
-## begin transaction
-## repeat retrieve (value = values.#value) where values.#name = @name
-## inquire_equel(rowcount = "rowcount")
- if (rowcount != 1) {
-## abort
- return(0);
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int rowcount, flag, value, retval;
+ EXEC SQL END DECLARE SECTION;
+
+ EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
+ WHERE name = 'uid';
+ if (sqlfail()) sqlexit();
+ if (sqlca.sqlerrd[2] != 1) {
+ EXEC SQL ROLLBACK;
+ com_err(whoami, MR_INTERNAL, "values table inconsistancy");
+ exit(1);
}
-## retrieve (exists = any(users.name where users.name = value))
-## inquire_equel(rowcount = "rowcount")
- if (rowcount != 1) {
-## abort
- return(0);
- }
- while (exists) {
+ flag = 0;
+ EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
+ if (sqlfail()) sqlexit();
+ if (sqlca.sqlerrd[2] == 0)
+ flag = 0;
+ while (flag) {
value++;
if (limit && value > MAX_ID_VALUE)
value = MIN_ID_VALUE;
-## retrieve (exists = any(users.name where users.name = value))
+ flag = 0;
+ EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
+ if (sqlfail()) sqlexit();
+ if (sqlca.sqlerrd[2] == 0)
+ flag = 0;
}
-## repeat replace values (#value = @value) where values.#name = @name
-## end transaction
- return(value);
-##}
+ retval = value++;
+ if (limit && value > MAX_ID_VALUE)
+ value = MIN_ID_VALUE;
+ EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
+ if (sqlca.sqlcode != 0) {
+ com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
+ exit(1);
+ }
+ return(retval);
+}
+
+
+sqlexit()
+{
+ if (sqlca.sqlcode == SQL_DEADLOCK)
+ com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
+ sqlca.sqlcode);
+ else
+ com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
+ EXEC SQL ROLLBACK WORK;
+ exit(1);
+}