]> andersk Git - moira.git/blame_incremental - regtape/employee.dc
changing status field in host table now results in only the target host's
[moira.git] / regtape / employee.dc
... / ...
CommitLineData
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>
10EXEC 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
210-8 id number
229-38 name
2339-62 office address
2463-74 phone1
2575-86 phone2
2687-106 dept
27107-156 title
28157-186 username
29187-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
54struct 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
71char *whoami;
72int newfinger = 0;
73
74#define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 100)
75#define SQL_DUPLICATE -40100
76#define SQL_DEADLOCK -49900
77
78
79main(argc, argv)
80int argc;
81char **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 opening database", 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 committing work", 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
146char *substr(buf, key)
147char *buf;
148char *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
159struct entry *get_next_entry(in)
160FILE *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 (substr(e.title, "PROF") || substr(e.title, "LECTURE"))
230 e.class = "FACULTY";
231
232 strcpy(email, strtrim(username));
233 if (host[0] == '@')
234 strncat(email, strtrim(host));
235 e.email = email;
236
237 return(&e);
238}
239
240
241process_entry(e)
242struct entry *e;
243{
244 int changed, nochange, encrypted;
245 char buf[BUFSIZ], *from, *to;
246 EXEC SQL BEGIN DECLARE SECTION;
247 char *first, *last, *middle, *eid, *sid, *name, *title, *phone2, *rdept, *rtitle;
248 char *raddr, *rhphone, *rophone, *prog;
249 char class[9], oaddr[25], ophone[17], dept[128], dfirst[17], dlast[17], dmiddle[17];
250 int id, status, who;
251 EXEC SQL END DECLARE SECTION;
252
253 /* Don't process Lincoln Labs */
254 if (!strncmp(e->address, "LL", 2))
255 return;
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 if (sqlca.sqlcode == SQL_DEADLOCK) {
323 com_err(whoami, MR_DEADLOCK, "updating user");
324 return;
325 } else {
326 com_err(whoami, 0, "ingres error %d updating user", sqlca.sqlcode);
327 exit(1);
328 }
329 }
330 }
331
332 /* Update name if necessary */
333 if (strcmp(first, strtrim(dfirst)) ||
334 strcmp(last, strtrim(dlast)) ||
335 strcmp(middle, strtrim(dmiddle))) {
336 com_err(whoami, 0, "updating real name for %s %s", first, last);
337 EXEC SQL UPDATE users
338 SET first = :first, last = :last, middle = :middle,
339 modby = :who, modwith = :prog, modtime = 'now'
340 WHERE users_id = :id;
341 if (sqlca.sqlcode != 0) {
342 if (sqlca.sqlcode == SQL_DEADLOCK) {
343 com_err(whoami, MR_DEADLOCK, "updating name");
344 return;
345 } else {
346 com_err(whoami, 0, "ingres error %d updating name", sqlca.sqlcode);
347 exit(1);
348 }
349 }
350 }
351
352 changed = nochange = 0;
353 if (encrypted) changed++;
354 strcpy(buf, e->address);
355 while (to = index(buf, ','))
356 *to = ';';
357 while (to = index(buf, ':'))
358 *to = ';';
359 if (newfinger) {
360 if (oaddr[0] == ' ' && buf[0]) {
361 strncpy(oaddr, buf, 16);
362 oaddr[16] = 0;
363 changed++;
364 } else if (strncmp(strtrim(oaddr), buf, 15))
365 nochange++;
366 } else {
367 if (strncmp(strtrim(oaddr), buf, 15))
368 changed++;
369 strncpy(oaddr, buf, 16);
370 oaddr[16] = 0;
371 }
372 from = e->phone;
373 to = buf;
374 while (*from) {
375 if (isdigit(*from))
376 *to++ = *from;
377 from++;
378 }
379 *to = 0;
380 if (newfinger) {
381 if (ophone[0] == ' ') {
382 strncpy(ophone, buf, 16);
383 ophone[16] = 0;
384 } else if (strncmp(strtrim(ophone), buf, 11))
385 nochange++;
386 } else {
387 if (strncmp(strtrim(ophone), buf, 11))
388 changed++;
389 strncpy(ophone, buf, 16);
390 ophone[16] = 0;
391 }
392 FixCase(e->dept);
393 FixCase(e->title);
394 if (newfinger) {
395 if (dept[0] == ' ') {
396 strncpy(dept, e->dept, 12);
397 dept[12] = 0;
398 } else if (strncmp(strtrim(dept), e->dept, 11))
399 nochange++;
400 } else {
401 if (strncmp(strtrim(dept), e->dept, 11))
402 changed++;
403 strncpy(dept, e->dept, 12);
404 dept[12] = 0;
405 }
406 sid = e->id;
407 name = e->name;
408 rdept = e->dept;
409 rtitle = e->title;
410 raddr = e->address;
411 rhphone = e->phone;
412 rophone = e->phone2;
413 if (changed) {
414 com_err(whoami, 0, "updating finger for %s %s", first, last);
415 EXEC SQL REPEATED UPDATE users
416 SET office_addr = :oaddr, office_phone = :ophone, department = :dept,
417 fmodtime = 'now', fmodby = :who, fmodwith = :prog,
418 xname = :name, xdept = :rdept, xtitle = :rtitle,
419 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
420 xmodtime = 'now', clearid = :sid
421 WHERE users_id = :id;
422 if (sqlca.sqlcode != 0) {
423 if (sqlca.sqlcode == SQL_DEADLOCK) {
424 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
425 EXEC SQL ROLLBACK;
426 return process_entry(e);
427 } else {
428 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
429 exit(1);
430 }
431 }
432 } else {
433 EXEC SQL REPEATED UPDATE users
434 SET xname = :name, xdept = :rdept, xtitle = :rtitle,
435 xaddress = :raddr, xphone1 = :rhphone, xphone2 = :rophone,
436 xmodtime = 'now', clearid = :sid
437 WHERE users_id = :id;
438 if (sqlca.sqlcode != 0) {
439 if (sqlca.sqlcode == SQL_DEADLOCK) {
440 com_err(whoami, MR_DEADLOCK, "updating user %s", sid);
441 EXEC SQL ROLLBACK;
442 return process_entry(e);
443 } else {
444 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
445 exit(1);
446 }
447 }
448 }
449}
450
451
452newuser(e)
453struct entry *e;
454{
455 char *from, *to;
456 EXEC SQL BEGIN DECLARE SECTION;
457 int id, uid, st, who;
458 char *last, *first, *class, *middle, login[9], *sid, fullname[65], *prog;
459 char oaddr[81], ophone[17], dept[128], *name, *title, phone2[17];
460 char *rdept, *rhphone, *rophone;
461 EXEC SQL END DECLARE SECTION;
462
463 who = WHO;
464 prog = PROG;
465 strncpy(oaddr, e->address, 16);
466 oaddr[16] = 0;
467 while (to = index(oaddr, ','))
468 *to = ';';
469 while (to = index(oaddr, ':'))
470 *to = ';';
471 from = e->phone;
472 to = ophone;
473 while (*from) {
474 if (isdigit(*from))
475 *to++ = *from;
476 from++;
477 }
478 *to = 0;
479 FixCase(e->dept);
480 strncpy(dept, e->dept, 12);
481 dept[12] = 0;
482
483 id = set_next_users_id(0);
484 uid = set_next_uid(1);
485 sprintf(login, "#%d", uid);
486 last = e->last;
487 first = e->first;
488 middle = e->middle;
489 class = e->class;
490 if (*middle)
491 sprintf(fullname, "%s %s %s", first, middle, last);
492 else
493 sprintf(fullname, "%s %s", first, last);
494 st = US_NOT_ALLOWED;
495 if (!strcmp(e->class, "FACULTY") || !strcmp(e->class, "STAFF"))
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/csh', :last, :first, :middle, :st,
511 :sid, :class, 'now', :who, :prog, :fullname, :oaddr, :ophone,
512 :dept, 'now', :who, :prog, 'NONE', :name, :rdept,
513 :title, :oaddr, :rhphone, :rophone, 'now');
514 if (sqlca.sqlcode != 0) {
515 if (sqlca.sqlcode == SQL_DEADLOCK) {
516 com_err(whoami, MR_DEADLOCK, "adding user");
517 } else {
518 com_err(whoami, 0, "ingres error %d adding user", sqlca.sqlcode);
519 exit(1);
520 }
521 } else
522 com_err(whoami, 0, "adding user %s %s", e->first, e->last);
523}
524
525
526set_next_users_id(limit)
527 int limit;
528{
529 EXEC SQL BEGIN DECLARE SECTION;
530 int rowcount, flag, value, retval;
531 EXEC SQL END DECLARE SECTION;
532
533 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
534 WHERE name = 'users_id';
535 if (sqlfail()) sqlexit();
536 if (sqlca.sqlerrd[2] != 1) {
537 EXEC SQL ROLLBACK;
538 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
539 exit(1);
540 }
541
542 flag = 0;
543 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
544 WHERE users_id = :value;
545 if (sqlfail()) sqlexit();
546 if (sqlca.sqlerrd[2] == 0)
547 flag = 0;
548 while (flag) {
549 value++;
550 if (limit && value > MAX_ID_VALUE)
551 value = MIN_ID_VALUE;
552 flag = 0;
553 EXEC SQL REPEATED SELECT users_id INTO :flag FROM users
554 WHERE users_id = :value;
555 if (sqlfail()) sqlexit();
556 if (sqlca.sqlerrd[2] == 0)
557 flag = 0;
558 }
559
560 retval = value++;
561 if (limit && value > MAX_ID_VALUE)
562 value = MIN_ID_VALUE;
563 EXEC SQL REPEATED UPDATE numvalues SET value = :value
564 WHERE name = 'users_id';
565 if (sqlca.sqlcode != 0) {
566 com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
567 exit(1);
568 }
569 return(retval);
570}
571
572set_next_uid(limit)
573 int limit;
574{
575 EXEC SQL BEGIN DECLARE SECTION;
576 int rowcount, flag, value, retval;
577 EXEC SQL END DECLARE SECTION;
578
579 EXEC SQL REPEATED SELECT value INTO :value FROM numvalues
580 WHERE name = 'uid';
581 if (sqlfail()) sqlexit();
582 if (sqlca.sqlerrd[2] != 1) {
583 EXEC SQL ROLLBACK;
584 com_err(whoami, MR_INTERNAL, "values table inconsistancy");
585 exit(1);
586 }
587
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 while (flag) {
594 value++;
595 if (limit && value > MAX_ID_VALUE)
596 value = MIN_ID_VALUE;
597 flag = 0;
598 EXEC SQL REPEATED SELECT uid INTO :flag FROM users WHERE uid = :value;
599 if (sqlfail()) sqlexit();
600 if (sqlca.sqlerrd[2] == 0)
601 flag = 0;
602 }
603
604 retval = value++;
605 if (limit && value > MAX_ID_VALUE)
606 value = MIN_ID_VALUE;
607 EXEC SQL REPEATED UPDATE numvalues SET value = :value WHERE name = 'uid';
608 if (sqlca.sqlcode != 0) {
609 com_err(whoami, 0, "ingres error %d assigning ID", sqlca.sqlcode);
610 exit(1);
611 }
612 return(retval);
613}
614
615
616sqlexit()
617{
618 if (sqlca.sqlcode == SQL_DEADLOCK)
619 com_err(whoami, MR_DEADLOCK, "unrecoverable ingres error %d",
620 sqlca.sqlcode);
621 else
622 com_err(whoami, 0, "ingres error %d", sqlca.sqlcode);
623 EXEC SQL ROLLBACK WORK;
624 exit(1);
625}
This page took 0.044467 seconds and 5 git commands to generate.