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