]> andersk Git - moira.git/blob - regtape/employee.dc
added deadlock detection
[moira.git] / regtape / employee.dc
1 /* $Header$
2  */
3
4 #include <stdio.h>
5 #include <strings.h>
6 #include <ctype.h>
7 #include <sys/time.h>
8 #include <moira.h>
9 #include <moira_site.h>
10 EXEC SQL INCLUDE sqlca;
11
12
13 #define WHO 11859               /* root */
14 #define PROG "emp-tape"
15
16 #define MAX_ID_VALUE    31999
17 #define MIN_ID_VALUE    101
18
19 /* File format is:
20
21 0-8     id number
22 9-38    name
23 39-62   office address
24 63-74   phone1
25 75-86   phone2
26 87-106  dept
27 107-156 title
28 157-186 username
29 187-241 host
30
31 */
32
33 #define LOC_ID 0
34 #define LOC_NAME 9
35 #define LOC_OFFICE 39
36 #define LOC_PHONE 63
37 #define LOC_PHONE2 75
38 #define LOC_DEPT 87
39 #define LOC_TITLE 107
40 #define LOC_USERNAME 157
41 #define LOC_HOST 187
42
43 #define LEN_ID 9
44 #define LEN_NAME 30
45 #define LEN_OFFICE 24
46 #define LEN_PHONE 12
47 #define LEN_PHONE2 12
48 #define LEN_DEPT 20
49 #define LEN_TITLE 50
50 #define LEN_USERNAME 30
51 #define LEN_HOST 55
52
53
54 struct entry {
55     char *name;
56     char *last;
57     char *first;
58     char *middle;
59     char *title;
60     char *class;
61     char *id;
62     char *eid;
63     char *dept;
64     char *address;
65     char *phone;
66     char *phone2;
67     char *email;
68 };
69
70
71 char *whoami;
72 int newfinger = 0;
73
74 #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
75 #define SQL_DUPLICATE -40100
76 #define SQL_DEADLOCK -49900
77
78
79 main(argc, argv)
80 int argc;
81 char **argv;
82 {
83     FILE *in;
84     struct entry *e, *get_next_entry();
85     int i, wait = 0;
86     char buf[BUFSIZ], *file = NULL;
87
88     whoami = rindex(argv[0], '/');
89     if (whoami)
90       whoami++;
91     else
92       whoami = argv[0];
93
94     for (i = 1; i < argc; i++) {
95         if (!strcmp(argv[i], "-w"))
96           wait++;
97         else if (!strcmp(argv[i], "-D"))
98           setenv("ING_SET", "set printqry");
99         else if (!strcmp(argv[i], "-n"))
100           newfinger++;
101         else if (file != NULL)
102           fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami);
103         else
104           file = argv[i];
105     }
106
107     in = fopen(file, "r");
108     if (in == NULL) {
109         fprintf(stderr, "Unable to open %s for input\n", file);
110         exit(1);
111     }
112
113     setlinebuf(stdout);
114     setlinebuf(stderr);
115
116     EXEC SQL CONNECT moira;
117     if (sqlca.sqlcode != 0) {
118         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
119         exit(1);
120     }
121
122     while (e = get_next_entry(in)) {
123         process_entry(e);
124         EXEC SQL COMMIT WORK;
125         if (sqlca.sqlcode != 0) {
126             if (sqlca.sqlcode == SQL_DEADLOCK) {
127                 com_err(whoami, MR_DEADLOCK, "commiting work");
128                 goto again;
129             } else {
130                 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
131                 exit(1);
132             }
133         }
134         if (wait) {
135             printf("Next");
136             fflush(stdout);
137             gets(buf);
138         }
139     }
140
141     exit(0);
142 }
143
144
145 char *substr(buf, key)
146 char *buf;
147 char *key;
148 {
149     int l;
150
151     for (l = strlen(key); *buf; buf++)
152       if (!strncmp(buf, key, l))
153         return(buf);
154     return(NULL);
155 }
156
157
158 struct entry *get_next_entry(in)
159 FILE *in;
160 {
161     static struct entry e;
162     static char buf[BUFSIZ], mid[16], eid[16], email[256];
163     static char name[LEN_NAME+1], sname[LEN_NAME+1], id[LEN_ID+1];
164     static char office[LEN_OFFICE+1], phone[LEN_PHONE+1], phone2[LEN_PHONE2+1];
165     static char dept[LEN_DEPT+1], title[LEN_TITLE+1], username[LEN_USERNAME+1];
166     static char host[LEN_HOST+1];
167     int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v;
168     char *p;
169
170     if (fgets(buf, sizeof(buf), in) == NULL)
171       return((struct entry *)NULL);
172
173     strncpy(id, &buf[LOC_ID], LEN_ID); id[LEN_ID] = 0;
174     strncpy(name, &buf[LOC_NAME], LEN_NAME); name[LEN_NAME] = 0;
175     strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); office[LEN_OFFICE] = 0;
176     strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); phone[LEN_PHONE] = 0;
177     strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); phone2[LEN_PHONE2] = 0;
178     strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); dept[LEN_DEPT] = 0;
179     strncpy(title, &buf[LOC_TITLE], LEN_TITLE); title[LEN_TITLE] = 0;
180     strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); username[LEN_USERNAME] = 0;
181     strncpy(host, &buf[LOC_HOST], LEN_HOST); host[LEN_HOST] = 0;
182
183     strcpy(sname, name);
184     e.name = strtrim(sname);
185     p = index(name, ',');
186     if (p)
187       *p = 0;
188     e.last = strtrim(name);
189     if (p) {
190         p++;
191         while (isspace(*p))
192           p++;
193         e.first = p;
194         if (p = index(e.first, ' ')) {
195             *p = 0;
196             e.first = strtrim(e.first);
197             e.middle = strtrim(p + 1);
198         } else {
199             e.first = strtrim(e.first);
200             e.middle = "";
201         }
202     } else {
203         e.first = "";
204         e.middle = "";
205     }
206     ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0;
207     LookForSt(e.last);
208     LookForO(e.last);
209     LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
210                     &ends_ii, &ends_v);
211     LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv,
212                     &ends_ii, &ends_v);
213     FixCase(e.last);
214     FixCase(e.first);
215     FixCase(e.middle);
216
217     e.id = id;
218     e.eid = eid;
219     EncryptID(e.eid, e.id, e.first, e.last);
220
221     e.address = strtrim(office);
222     e.phone = strtrim(phone);
223     e.phone2 = strtrim(phone2);
224     e.dept = strtrim(dept);
225     e.title = strtrim(title);
226
227     e.class = "MITS";
228     if (!strcmp(e.dept, "PROJECT ATHENA"))
229       e.class = "STAFF";
230     else if (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
231       e.class = "FACULTY";
232     else if (!strcmp(e.title, "VISITING SCIENTIST"))
233       e.class = "VSCIENTI";
234
235     strcpy(email, strtrim(username));
236     if (host[0] == '@')
237       strncat(email, strtrim(host));
238     e.email = email;
239
240     return(&e);
241 }
242
243
244 process_entry(e)
245 struct entry *e;
246 {
247     int changed, nochange, encrypted;
248     char buf[BUFSIZ], *from, *to;
249     EXEC SQL BEGIN DECLARE SECTION;
250     char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
251     char *raddr, *rhphone, *rophone, *prog;
252     char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
253     int id, status, who;
254     EXEC SQL END DECLARE SECTION;
255
256     who = WHO;
257     prog = PROG;
258     first = e->first;
259     if (strlen(first) > 16)
260       first[16] = 0;
261     last = e->last;
262     if (strlen(last) > 16)
263       last[16] = 0;
264     middle = e->middle;
265     eid = e->eid;
266     sid = e->id;
267     id = 0;
268     encrypted = 0;
269
270     /* Get user info */
271     EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
272       INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
273       FROM users
274       WHERE clearid = :sid;
275     if (sqlfail()) {
276         if (sqlca.sqlcode == SQL_DUPLICATE) {
277             com_err(whoami, 0, "duplicate ID number %s on user %s %s", sid, first, last);
278             return;
279         } else if (sqlca.sqlcode == SQL_DEADLOCK) {
280             com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
281             EXEC SQL ROLLBACK;
282             return process_entry(e);
283         } else
284           sqlexit();
285     }
286     if (id == 0) {
287         EXEC SQL SELECT users_id, first, last, middle, type, office_addr, office_phone, department, status
288           INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, :ophone, :dept, :status
289           FROM users
290           WHERE last = :last and first = :first and clearid = :eid;
291         if (sqlfail()) {
292             if (sqlca.sqlcode == SQL_DEADLOCK) {
293                 com_err(whoami, MR_DEADLOCK, "looking up user %s", sid);
294                 EXEC SQL ROLLBACK;
295                 return process_entry(e);
296             } else if (sqlca.sqlcode != SQL_DUPLICATE)
297               sqlexit();
298         }
299         encrypted++;
300         if (id == 0) {
301             newuser(e);
302             return;
303         }
304     }
305
306     /* Update class/state if necessary */
307     if (strcmp(e->class, strtrim(class)) &&
308         strcmp(class, "STAFF") && strcmp(class, "SIPB")) {
309         com_err(whoami, 0, "updating class for %s %s from %s to %s",
310                 first, last, class, e->class);
311         if (status == US_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
312           status = US_NO_LOGIN_YET;
313         if (status == US_ENROLL_NOT_ALLOWED && !strcmp(e->class, "FACULTY"))
314           status = US_ENROLLED;
315         strcpy(class, e->class);
316         EXEC SQL UPDATE users
317           SET type = :class, status = :status, modtime = 'now',
318                 modby = :who, modwith = :prog
319           WHERE users_id = :id;
320         if (sqlca.sqlcode != 0) {
321             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
322             exit(1);
323         }
324     }
325
326     /* Update name if necessary */
327     if (strcmp(first, strtrim(dfirst)) ||
328         strcmp(last, strtrim(dlast)) ||
329         strcmp(middle, strtrim(dmiddle))) {
330         com_err(whoami, 0, "updating real name for %s %s", first, last);
331         EXEC SQL UPDATE users
332           SET first = :first, last = :last, middle = :middle,
333                 modby = :who, modwith = :prog, modtime = 'now'
334           WHERE users_id = :id;
335         if (sqlca.sqlcode != 0) {
336             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
337             exit(1);
338         }
339     }
340
341     changed = nochange = 0;
342     if (encrypted) changed++;
343     strcpy(buf, e->address);
344     while (to = index(buf, ','))
345       *to = ';';
346     while (to = index(buf, ':'))
347       *to = ';';
348     if (newfinger) {
349         if (oaddr[0] == ' ' && buf[0]) {
350             strncpy(oaddr, buf, 16);
351             oaddr[16] = 0;
352             changed++;
353         } else if (strncmp(strtrim(oaddr), buf, 15))
354           nochange++;
355     } else {
356         if (strncmp(strtrim(oaddr), buf, 15))
357           changed++;
358         strncpy(oaddr, buf, 16);
359         oaddr[16] = 0;
360     }
361     from = e->phone;
362     to = buf;
363     while (*from) {
364         if (isdigit(*from))
365           *to++ = *from;
366         from++;
367     }
368     *to = 0;
369     if (newfinger) {
370         if (ophone[0] == ' ') {
371             strncpy(ophone, buf, 16);
372             ophone[16] = 0;
373         } else if (strncmp(strtrim(ophone), buf, 11))
374           nochange++;
375     } else {
376         if (strncmp(strtrim(ophone), buf, 11))
377           changed++;
378         strncpy(ophone, buf, 16);
379         ophone[16] = 0;
380     }
381     FixCase(e->dept);
382     FixCase(e->title);
383     if (newfinger) {
384         if (dept[0] == ' ') {
385             strncpy(dept, e->dept, 12);
386             dept[12] = 0;
387         } else if (strncmp(strtrim(dept), e->dept, 11))
388           nochange++;
389     } else {
390         if (strncmp(strtrim(dept), e->dept, 11))
391           changed++;
392         strncpy(dept, e->dept, 12);
393         dept[12] = 0;
394     }
395     sid = e->id;
396     name = e->name;
397     rdept = e->dept;
398     rtitle = e->title;
399     raddr = e->address;
400     rhphone = e->phone;
401     rophone = e->phone2;
402     if (changed) {
403         com_err(whoami, 0, "updating finger for %s %s", first, last);
404         EXEC SQL REPEATED UPDATE users
405           SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
406             fmodtime = 'now', fmodby = :who, fmodwith = :prog,
407             xname = :name, xdept = :rdept, xtitle = :rtitle,
408             xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
409             xmodtime = 'now', clearid = :sid
410           WHERE users_id = :id;
411         if (sqlca.sqlcode != 0) {
412             if (sqlca.sqlcode == SQL_DEADLOCK) {
413                 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
414                 EXEC SQL ROLLBACK;
415                 return process_entry(e);
416             } else {
417                 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
418                 exit(1);
419             }
420         }
421     } else {
422         EXEC SQL REPEATED UPDATE users
423           SET xname = :name, xdept = :rdept, xtitle = :rtitle,
424             xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
425             xmodtime = 'now', clearid = :sid
426           WHERE users_id = :id;
427         if (sqlca.sqlcode != 0) {
428             if (sqlca.sqlcode == SQL_DEADLOCK) {
429                 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
430                 EXEC SQL ROLLBACK;
431                 return process_entry(e);
432             } else {
433                 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
434                 exit(1);
435             }
436         }
437     }
438 }
439
440
441 newuser(e)
442 struct entry *e;
443 {
444     char *from, *to;
445     EXEC SQL BEGIN DECLARE SECTION;
446     int id, uid, st, who;
447     char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
448     char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
449     char *rdept, *rhphone, *rophone;
450     EXEC SQL END DECLARE SECTION;
451
452     who = WHO;
453     prog = PROG;
454     strncpy(oaddr, e->address, 16);
455     oaddr[16] = 0;
456     while (to = index(oaddr, ','))
457       *to = ';';
458     while (to = index(oaddr, ':'))
459       *to = ';';
460     from = e->phone;
461     to = ophone;
462     while (*from) {
463         if (isdigit(*from))
464           *to++ = *from;
465         from++;
466     }
467     *to = 0;
468     FixCase(e->dept);
469     strncpy(dept, e->dept, 12);
470     dept[12] = 0;
471     
472     id = set_next_users_id(0);
473     uid = set_next_uid(1);
474     sprintf(login, "#%d", uid);
475     last = e->last;
476     first = e->first;
477     middle = e->middle;
478     class = e->class;
479     if (*middle)
480       sprintf(fullname, "%s %s %s", first, middle, last);
481     else
482       sprintf(fullname, "%s %s", first, last);
483     st = US_NOT_ALLOWED;
484     if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
485       st = US_NO_LOGIN_YET;
486
487     sid = e->id;
488     name = e->name;
489     rdept = e->dept;
490     title = e->title;
491     rhphone = e->phone;
492     rophone = e->phone2;
493
494     EXEC SQL REPEATED INSERT INTO users
495       (login, users_id, uid, shell, last, first, middle, status,
496        clearid, type, modtime, modby, modwith, fullname, office_addr,
497        office_phone, department, fmodtime, fmodby, fmodwith,
498        potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime)
499       VALUES (:login, :id, :uid, '/bin/csh', :last, :first, :middle, :st,
500               :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
501               :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
502               :title, :oaddr, :rhphone, :rophone, 'now');
503     if (sqlca.sqlcode != 0) {
504         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
505         exit(1);
506     } else
507       com_err(whoami, 0, "adding user %s %s", e->first, e->last);
508 }
509
510
511 set_next_users_id(limit)
512     int limit;
513 {
514     EXEC SQL BEGIN DECLARE SECTION;
515     int rowcount, flag, value, retval;
516     EXEC SQL END DECLARE SECTION;
517
518     EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
519       WHERE name = 'users_id';
520     if (sqlfail()) sqlexit();
521     if (sqlca.sqlerrd[2] != 1) {
522         EXEC SQL ROLLBACK;
523         com_err(whoami, MR_INTERNAL, "values table inconsistancy");
524         exit(1);
525     }
526
527     flag = 0;
528     EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
529       WHERE users_id = :value;
530     if (sqlfail()) sqlexit();
531     if (sqlca.sqlerrd[2] == 0)
532       flag = 0;
533     while (flag) {
534         value++;
535         if (limit && value > MAX_ID_VALUE)
536             value = MIN_ID_VALUE;
537         flag = 0;
538         EXEC SQL REPEATED SELECT users_id INTO :flag FROM users 
539           WHERE users_id = :value;
540         if (sqlfail()) sqlexit();
541         if (sqlca.sqlerrd[2] == 0)
542           flag = 0;
543     }
544
545     retval = value++;
546     if (limit && value > MAX_ID_VALUE)
547       value = MIN_ID_VALUE;
548     EXEC SQL REPEATED UPDATE numvalues SET value = :value
549       WHERE name = 'users_id';
550     if (sqlca.sqlcode != 0) {
551         if (sqlca.sqlcode == SQL_DEADLOCK) {
552             com_err(whoami, MR_DEADLOCK, "adding user %s", sid);
553             EXEC SQL ROLLBACK;
554             return newuser(e);
555         } else {
556             com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
557             exit(1);
558         }
559     }
560     return(retval);
561 }
562
563 set_next_uid(limit)
564     int limit;
565 {
566     EXEC SQL BEGIN DECLARE SECTION;
567     int rowcount, flag, value, retval;
568     EXEC SQL END DECLARE SECTION;
569
570     EXEC SQL REPEATED SELECT value INTO :value FROM numvalues 
571       WHERE name = 'uid';
572     if (sqlfail()) sqlexit();
573     if (sqlca.sqlerrd[2] != 1) {
574         EXEC SQL ROLLBACK;
575         com_err(whoami, MR_INTERNAL, "values table inconsistancy");
576         exit(1);
577     }
578
579     flag = 0;
580     EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
581     if (sqlfail()) sqlexit();
582     if (sqlca.sqlerrd[2] == 0)
583       flag = 0;
584     while (flag) {
585         value++;
586         if (limit && value > MAX_ID_VALUE)
587             value = MIN_ID_VALUE;
588         flag = 0;
589         EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
590         if (sqlfail()) sqlexit();
591         if (sqlca.sqlerrd[2] == 0)
592           flag = 0;
593     }
594
595     retval = value++;
596     if (limit && value > MAX_ID_VALUE)
597       value = MIN_ID_VALUE;
598     EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
599     if (sqlca.sqlcode != 0) {
600         com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
601         exit(1);
602     }
603     return(retval);
604 }
605
606
607 sqlexit()
608 {
609     if (sqlca.sqlcode == SQL_DEADLOCK)
610       com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
611               sqlca.sqlcode);
612     else
613       com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
614     EXEC SQL ROLLBACK WORK;
615     exit(1);
616 }
This page took 0.086414 seconds and 5 git commands to generate.