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