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