]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
2 | * | |
3 | * Load data into Moira from Personnel Office data file | |
4 | * | |
5 | * Copyright (C) 1990-1998 by the Massachusetts Institute of Technology | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
469f80df | 8 | */ |
9 | ||
7ac48069 | 10 | #include <mit-copyright.h> |
932028de | 11 | #include <moira.h> |
12 | #include <moira_site.h> | |
dfaf9b68 | 13 | #include <moira_schema.h> |
7ac48069 | 14 | |
15 | #include <ctype.h> | |
16 | #include <stdio.h> | |
17 | #include <string.h> | |
18 | ||
02cd9ede | 19 | EXEC SQL INCLUDE sqlca; |
7ac48069 | 20 | extern void sqlglm(char *, unsigned int *, unsigned int *); |
469f80df | 21 | |
7ac48069 | 22 | RCSID("$Header$"); |
469f80df | 23 | |
02cd9ede | 24 | #define WHO 11859 /* root */ |
25 | #define PROG "emp-tape" | |
469f80df | 26 | |
397e9044 | 27 | #define MAX_ID_VALUE 31999 |
469f80df | 28 | #define MIN_ID_VALUE 101 |
29 | ||
30 | /* File format is: | |
31 | ||
32 | 0-8 id number | |
33 | 9-38 name | |
34 | 39-62 office address | |
35 | 63-74 phone1 | |
36 | 75-86 phone2 | |
37 | 87-106 dept | |
38 | 107-156 title | |
39 | 157-186 username | |
40 | 187-241 host | |
41 | ||
42 | */ | |
43 | ||
44 | #define LOC_ID 0 | |
45 | #define LOC_NAME 9 | |
46 | #define LOC_OFFICE 39 | |
47 | #define LOC_PHONE 63 | |
48 | #define LOC_PHONE2 75 | |
49 | #define LOC_DEPT 87 | |
50 | #define LOC_TITLE 107 | |
51 | #define LOC_USERNAME 157 | |
52 | #define LOC_HOST 187 | |
53 | ||
54 | #define LEN_ID 9 | |
a6e9fead | 55 | #define LEN_NAME 30 |
56 | #define LEN_OFFICE 24 | |
469f80df | 57 | #define LEN_PHONE 12 |
58 | #define LEN_PHONE2 12 | |
e5843fe8 | 59 | #define LEN_DEPT 50 |
a6e9fead | 60 | #define LEN_TITLE 50 |
61 | #define LEN_USERNAME 30 | |
469f80df | 62 | #define LEN_HOST 55 |
63 | ||
64 | ||
65 | struct entry { | |
5eaef520 | 66 | char *name; |
67 | char *last; | |
68 | char *first; | |
69 | char *middle; | |
70 | char *title; | |
71 | char *class; | |
72 | char *id; | |
5eaef520 | 73 | char *dept; |
74 | char *address; | |
75 | char *phone; | |
76 | char *phone2; | |
77 | int highid; | |
469f80df | 78 | }; |
79 | ||
7ac48069 | 80 | struct entry *get_next_entry(FILE *in); |
81 | void process_entry(struct entry *e); | |
82 | void newuser(struct entry *e); | |
83 | int set_next_users_id(int limit); | |
84 | int set_next_uid(int high); | |
85 | void sqlexit(void); | |
86 | void dbmserr(char *where, int what); | |
469f80df | 87 | |
88 | char *whoami; | |
e5843fe8 | 89 | int newfinger = 0, debug = 0, highid = 0; |
469f80df | 90 | |
9f5e5c05 | 91 | #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403) |
bd22473a | 92 | #define SQL_DUPLICATE -2112 |
3e77c6a3 | 93 | |
469f80df | 94 | |
5eaef520 | 95 | int main(int argc, char **argv) |
02cd9ede | 96 | { |
5eaef520 | 97 | FILE *in; |
7ac48069 | 98 | struct entry *e; |
5eaef520 | 99 | int i, wait = 0; |
dfaf9b68 | 100 | char buf[80], *file = NULL; |
5eaef520 | 101 | EXEC SQL BEGIN DECLARE SECTION; |
102 | char *db = "moira"; | |
103 | EXEC SQL END DECLARE SECTION; | |
104 | ||
105 | whoami = strrchr(argv[0], '/'); | |
106 | if (whoami) | |
107 | whoami++; | |
108 | else | |
109 | whoami = argv[0]; | |
110 | ||
111 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); | |
112 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); | |
113 | ||
114 | for (i = 1; i < argc; i++) | |
115 | { | |
116 | if (!strcmp(argv[i], "-w")) | |
117 | wait++; | |
5eaef520 | 118 | else if (!strcmp(argv[i], "-n")) |
119 | newfinger++; | |
e5843fe8 | 120 | else if (!strcmp(argv[i], "-d")) |
121 | debug++; | |
122 | else if (!strcmp(argv[i], "-h")) | |
123 | highid++; | |
5eaef520 | 124 | else if (file) |
125 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); | |
126 | else | |
127 | file = argv[i]; | |
469f80df | 128 | } |
129 | ||
5eaef520 | 130 | in = fopen(file, "r"); |
131 | if (!in) | |
132 | { | |
133 | fprintf(stderr, "Unable to open %s for input\n", file); | |
134 | exit(1); | |
469f80df | 135 | } |
136 | ||
5eaef520 | 137 | initialize_sms_error_table(); |
325821e4 | 138 | |
5eaef520 | 139 | EXEC SQL CONNECT :db IDENTIFIED BY :db; |
140 | if (sqlca.sqlcode) | |
141 | { | |
142 | dbmserr("opening database", sqlca.sqlcode); | |
143 | exit(1); | |
3e77c6a3 | 144 | } |
469f80df | 145 | |
5eaef520 | 146 | while ((e = get_next_entry(in))) |
147 | { | |
5eaef520 | 148 | process_entry(e); |
149 | EXEC SQL COMMIT WORK; | |
150 | if (sqlca.sqlcode) | |
151 | { | |
9f5e5c05 | 152 | dbmserr("committing work", sqlca.sqlcode); |
153 | exit(1); | |
3e77c6a3 | 154 | } |
5eaef520 | 155 | if (wait) |
156 | { | |
157 | printf("Next"); | |
158 | fflush(stdout); | |
dfaf9b68 | 159 | fgets(buf, sizeof(buf), stdin); |
469f80df | 160 | } |
161 | } | |
162 | ||
5eaef520 | 163 | exit(0); |
02cd9ede | 164 | } |
469f80df | 165 | |
5eaef520 | 166 | struct entry *get_next_entry(FILE *in) |
469f80df | 167 | { |
5eaef520 | 168 | static struct entry e; |
0e5cfe5f | 169 | static char buf[BUFSIZ]; |
5eaef520 | 170 | static char name[LEN_NAME + 1], sname[LEN_NAME + 1], id[LEN_ID + 1]; |
171 | static char office[LEN_OFFICE + 1], phone[LEN_PHONE + 1]; | |
172 | static char phone2[LEN_PHONE2 + 1], dept[LEN_DEPT + 1], title[LEN_TITLE + 1]; | |
173 | static char username[LEN_USERNAME + 1], host[LEN_HOST + 1]; | |
174 | int ends_sr, ends_jr, ends_iii, ends_iv, ends_ii, ends_v; | |
b92021f7 | 175 | char *p, *q; |
5eaef520 | 176 | |
177 | if (!fgets(buf, sizeof(buf), in)) | |
178 | return NULL; | |
179 | ||
180 | strncpy(id, &buf[LOC_ID], LEN_ID); | |
181 | id[LEN_ID] = '\0'; | |
182 | strncpy(name, &buf[LOC_NAME], LEN_NAME); | |
183 | name[LEN_NAME] = '\0'; | |
184 | strncpy(office, &buf[LOC_OFFICE], LEN_OFFICE); | |
185 | office[LEN_OFFICE] = '\0'; | |
186 | strncpy(phone, &buf[LOC_PHONE], LEN_PHONE); | |
187 | phone[LEN_PHONE] = '\0'; | |
188 | strncpy(phone2, &buf[LOC_PHONE2], LEN_PHONE2); | |
189 | phone2[LEN_PHONE2] = '\0'; | |
190 | strncpy(dept, &buf[LOC_DEPT], LEN_DEPT); | |
191 | dept[LEN_DEPT] = '\0'; | |
192 | strncpy(title, &buf[LOC_TITLE], LEN_TITLE); | |
193 | title[LEN_TITLE] = '\0'; | |
194 | strncpy(username, &buf[LOC_USERNAME], LEN_USERNAME); | |
195 | username[LEN_USERNAME] = '\0'; | |
196 | strncpy(host, &buf[LOC_HOST], LEN_HOST); | |
197 | host[LEN_HOST] = '\0'; | |
198 | ||
199 | strcpy(sname, name); | |
200 | e.name = strtrim(sname); | |
201 | p = strchr(name, ','); | |
202 | if (p) | |
203 | *p = '\0'; | |
204 | e.last = strtrim(name); | |
205 | if (p) | |
206 | { | |
207 | p++; | |
208 | while (isspace(*p)) | |
a6e9fead | 209 | p++; |
5eaef520 | 210 | e.first = p; |
7ac48069 | 211 | if ((p = strchr(e.first, ' '))) |
5eaef520 | 212 | { |
213 | *p = '\0'; | |
214 | e.first = strtrim(e.first); | |
215 | e.middle = strtrim(p + 1); | |
a6e9fead | 216 | } |
5eaef520 | 217 | else |
218 | { | |
219 | e.first = strtrim(e.first); | |
220 | e.middle = ""; | |
221 | } | |
222 | } | |
223 | else | |
224 | { | |
225 | e.first = ""; | |
226 | e.middle = ""; | |
a6e9fead | 227 | } |
5eaef520 | 228 | ends_sr = ends_jr = ends_iii = ends_iv = ends_ii = ends_v = 0; |
229 | LookForSt(e.last); | |
230 | LookForO(e.last); | |
7ac48069 | 231 | LookForJrAndIII(e.last, &ends_jr, &ends_sr, &ends_ii, &ends_iii, |
232 | &ends_iv, &ends_v); | |
233 | LookForJrAndIII(e.first, &ends_jr, &ends_sr, &ends_ii, &ends_iii, | |
234 | &ends_iv, &ends_v); | |
5eaef520 | 235 | FixCase(e.last); |
236 | FixCase(e.first); | |
237 | FixCase(e.middle); | |
238 | ||
239 | e.id = id; | |
5eaef520 | 240 | |
b92021f7 | 241 | /* The following is really gross, but it happens to successfully convert |
242 | * new-style Warehouse office descriptions into (more-readable) old-style | |
243 | * Personnel Office office descriptions. | |
244 | */ | |
245 | e.address = p = strtrim(office); | |
246 | while (*p && !isspace(*p)) | |
247 | p++; | |
248 | q = p; | |
249 | while (isspace(*q)) | |
250 | q++; | |
251 | if (*q && q < e.address + LEN_OFFICE / 2) | |
252 | { | |
253 | *p++ = '-'; | |
254 | while (*q && q < e.address + LEN_OFFICE / 2) | |
255 | { | |
256 | if (*q != ' ' && *q != '-') | |
257 | *p++ = *q; | |
258 | if (q > p) | |
259 | *q = ' '; | |
260 | q++; | |
261 | } | |
262 | memset(p, ' ', q - p); | |
263 | } | |
264 | ||
265 | p = e.address + LEN_OFFICE / 2; | |
266 | while (*p && !isspace(*p)) | |
267 | p++; | |
268 | q = p; | |
269 | while (isspace(*q)) | |
270 | q++; | |
271 | if (*q) | |
272 | { | |
273 | *p++ = '-'; | |
274 | while (*q) | |
275 | { | |
276 | if (*q != ' ' && *q != '-') | |
277 | *p++ = *q; | |
278 | if (q > p) | |
279 | *q = ' '; | |
280 | q++; | |
281 | } | |
282 | memset(p, ' ', q - p); | |
283 | } | |
284 | strtrim(e.address); | |
285 | ||
5eaef520 | 286 | e.phone = strtrim(phone); |
287 | e.phone2 = strtrim(phone2); | |
288 | e.dept = strtrim(dept); | |
289 | e.title = strtrim(title); | |
290 | ||
291 | e.class = "MITS"; | |
e5843fe8 | 292 | e.highid = highid; |
7ac48069 | 293 | if (strstr(e.title, "PROF") || strstr(e.title, "LECTURE")) |
5eaef520 | 294 | e.class = "FACULTY"; |
295 | if (!strcmp(e.dept, "LINCOLN LAB")) | |
296 | { | |
297 | e.class = "LINCOLN"; | |
298 | e.highid = 1; | |
64b4c6b1 | 299 | } |
469f80df | 300 | |
5eaef520 | 301 | return &e; |
469f80df | 302 | } |
303 | ||
7ac48069 | 304 | void process_entry(struct entry *e) |
02cd9ede | 305 | { |
0e5cfe5f | 306 | int changed, nochange; |
dfaf9b68 | 307 | char buf[MAX_FIELD_WIDTH], *from, *to; |
5eaef520 | 308 | EXEC SQL BEGIN DECLARE SECTION; |
0e5cfe5f | 309 | char *first, *last, *middle, *sid, *name, *rdept; |
5eaef520 | 310 | char *rtitle, *raddr, *rhphone, *rophone, *prog; |
dfaf9b68 | 311 | char class[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE]; |
312 | char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE]; | |
313 | char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE]; | |
314 | char dmiddle[USERS_MIDDLE_SIZE]; | |
5eaef520 | 315 | int id, status, who; |
316 | EXEC SQL END DECLARE SECTION; | |
317 | ||
318 | who = WHO; | |
319 | prog = PROG; | |
320 | first = e->first; | |
dfaf9b68 | 321 | if (strlen(first) > USERS_FIRST_SIZE - 1) |
322 | first[USERS_FIRST_SIZE - 1] = '\0'; | |
5eaef520 | 323 | last = e->last; |
dfaf9b68 | 324 | if (strlen(last) > USERS_LAST_SIZE - 1) |
325 | last[USERS_LAST_SIZE - 1] = '\0'; | |
5eaef520 | 326 | middle = e->middle; |
dfaf9b68 | 327 | if (strlen(middle) > USERS_MIDDLE_SIZE - 1) |
328 | middle[USERS_MIDDLE_SIZE - 1] = '\0'; | |
5eaef520 | 329 | sid = e->id; |
330 | id = 0; | |
5eaef520 | 331 | |
332 | /* Get user info */ | |
333 | EXEC SQL SELECT users_id, first, last, middle, type, office_addr, | |
334 | office_phone, department, status | |
335 | INTO :id, :dfirst, :dlast, :dmiddle, :class, :oaddr, | |
336 | :ophone, :dept, :status | |
337 | FROM users | |
338 | WHERE clearid = :sid; | |
339 | if (sqlfail()) | |
340 | { | |
341 | if (sqlca.sqlcode == SQL_DUPLICATE) | |
342 | { | |
343 | com_err(whoami, 0, "duplicate ID number %s on user %s %s", | |
344 | sid, first, last); | |
345 | return; | |
346 | } | |
347 | else | |
348 | sqlexit(); | |
3e77c6a3 | 349 | } |
5eaef520 | 350 | if (id == 0) |
351 | { | |
0e5cfe5f | 352 | newuser(e); |
353 | return; | |
469f80df | 354 | } |
325821e4 | 355 | |
5eaef520 | 356 | /* Update class/state if necessary. (Exclude several spacial cases.) */ |
357 | if (strcmp(e->class, strtrim(class)) && | |
358 | strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") && | |
359 | strcmp(class, "KNIGHT")) | |
360 | { | |
361 | com_err(whoami, 0, "updating class for %s %s from %s to %s", | |
362 | first, last, class, e->class); | |
363 | if (status == US_NOT_ALLOWED) | |
364 | status = US_NO_LOGIN_YET; | |
365 | if (status == US_ENROLL_NOT_ALLOWED) | |
366 | status = US_ENROLLED; | |
367 | strcpy(class, e->class); | |
e5843fe8 | 368 | if (!debug) |
5eaef520 | 369 | { |
e5843fe8 | 370 | EXEC SQL UPDATE users |
371 | SET type = NVL(:class, CHR(0)), status = :status, | |
372 | modtime = SYSDATE, modby = :who, modwith = :prog | |
373 | WHERE users_id = :id; | |
374 | if (sqlca.sqlcode) | |
375 | { | |
376 | dbmserr("updating user", sqlca.sqlcode); | |
377 | exit(1); | |
378 | } | |
3e77c6a3 | 379 | } |
380 | } | |
381 | ||
5eaef520 | 382 | /* Update name if necessary */ |
383 | if (strcmp(first, strtrim(dfirst)) || | |
384 | strcmp(last, strtrim(dlast)) || | |
385 | strcmp(middle, strtrim(dmiddle))) | |
386 | { | |
387 | com_err(whoami, 0, "updating real name for %s %s", first, last); | |
e5843fe8 | 388 | if (!debug) |
5eaef520 | 389 | { |
e5843fe8 | 390 | EXEC SQL UPDATE users |
391 | SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)), | |
392 | middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog, | |
393 | modtime = SYSDATE | |
394 | WHERE users_id = :id; | |
395 | if (sqlca.sqlcode) | |
396 | { | |
397 | dbmserr("updating name", sqlca.sqlcode); | |
398 | exit(1); | |
399 | } | |
400 | } | |
401 | else | |
402 | { | |
403 | com_err(whoami, 0, "was %s %s %s, became %s %s %s", | |
404 | dfirst, dmiddle, dlast, first, middle, last); | |
3e77c6a3 | 405 | } |
469f80df | 406 | } |
325821e4 | 407 | |
5eaef520 | 408 | changed = nochange = 0; |
5eaef520 | 409 | strcpy(buf, e->address); |
410 | while ((to = strchr(buf, ','))) | |
411 | *to = ';'; | |
412 | while ((to = strchr(buf, ':'))) | |
413 | *to = ';'; | |
414 | if (newfinger) | |
415 | { | |
416 | if (oaddr[0] == ' ' && buf[0]) | |
417 | { | |
dfaf9b68 | 418 | strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1); |
419 | oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0'; | |
469f80df | 420 | changed++; |
5eaef520 | 421 | } |
dfaf9b68 | 422 | else if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1)) |
5eaef520 | 423 | nochange++; |
469f80df | 424 | } |
5eaef520 | 425 | else |
426 | { | |
dfaf9b68 | 427 | if (strncmp(strtrim(oaddr), buf, USERS_OFFICE_ADDR_SIZE - 1)) |
e5843fe8 | 428 | { |
429 | changed++; | |
430 | if (debug) | |
431 | { | |
432 | com_err(whoami, 0, "office for %s %s changed from %s to %s", | |
433 | first, last, oaddr, buf); | |
434 | } | |
435 | } | |
dfaf9b68 | 436 | strncpy(oaddr, buf, USERS_OFFICE_ADDR_SIZE - 1); |
437 | oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0'; | |
469f80df | 438 | } |
5eaef520 | 439 | from = e->phone; |
440 | to = buf; | |
441 | while (*from) | |
442 | { | |
443 | if (isdigit(*from)) | |
444 | *to++ = *from; | |
445 | from++; | |
469f80df | 446 | } |
5eaef520 | 447 | *to = '\0'; |
448 | if (newfinger) | |
449 | { | |
450 | if (ophone[0] == ' ') | |
451 | { | |
dfaf9b68 | 452 | strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1); |
453 | ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0'; | |
5eaef520 | 454 | } |
dfaf9b68 | 455 | else if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1)) |
5eaef520 | 456 | nochange++; |
457 | } | |
458 | else | |
459 | { | |
dfaf9b68 | 460 | if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1)) |
e5843fe8 | 461 | { |
462 | changed++; | |
463 | if (debug) | |
464 | { | |
465 | com_err(whoami, 0, "Phone for %s %s changed from %s to %s", | |
466 | first, last, ophone, buf); | |
467 | } | |
468 | } | |
dfaf9b68 | 469 | strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1); |
470 | ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0'; | |
469f80df | 471 | } |
5eaef520 | 472 | FixCase(e->dept); |
473 | FixCase(e->title); | |
474 | if (newfinger) | |
475 | { | |
476 | if (dept[0] == ' ') | |
477 | { | |
dfaf9b68 | 478 | strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1); |
479 | dept[USERS_DEPARTMENT_SIZE - 1] = '\0'; | |
5eaef520 | 480 | } |
dfaf9b68 | 481 | else if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1)) |
5eaef520 | 482 | nochange++; |
483 | } | |
484 | else | |
485 | { | |
dfaf9b68 | 486 | if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1)) |
e5843fe8 | 487 | { |
488 | changed++; | |
489 | if (debug) | |
490 | { | |
491 | com_err(whoami, 0, "Department for %s %s changed from %s to %s", | |
492 | first, last, dept, e->dept); | |
493 | } | |
494 | } | |
dfaf9b68 | 495 | strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1); |
496 | dept[USERS_DEPARTMENT_SIZE - 1] = '\0'; | |
5eaef520 | 497 | } |
498 | sid = e->id; | |
499 | name = e->name; | |
500 | rdept = e->dept; | |
501 | rtitle = e->title; | |
502 | raddr = e->address; | |
503 | rhphone = e->phone; | |
504 | rophone = e->phone2; | |
e5843fe8 | 505 | |
506 | if (debug) | |
507 | return; | |
508 | ||
5eaef520 | 509 | if (changed) |
510 | { | |
511 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
512 | EXEC SQL UPDATE users | |
513 | SET office_addr = NVL(:oaddr, CHR(0)), | |
514 | office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)), | |
515 | fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog, | |
516 | xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)), | |
517 | xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)), | |
518 | xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)), | |
519 | xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0)) | |
520 | WHERE users_id = :id; | |
521 | if (sqlca.sqlcode) | |
522 | { | |
9f5e5c05 | 523 | dbmserr(NULL, sqlca.sqlcode); |
524 | exit(1); | |
3e77c6a3 | 525 | } |
5eaef520 | 526 | } |
527 | else | |
528 | { | |
529 | EXEC SQL UPDATE users | |
530 | SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)), | |
531 | xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)), | |
532 | xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)), | |
533 | xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0)) | |
534 | WHERE users_id = :id; | |
535 | if (sqlca.sqlcode) | |
536 | { | |
9f5e5c05 | 537 | dbmserr(NULL, sqlca.sqlcode); |
538 | exit(1); | |
3e77c6a3 | 539 | } |
a6e9fead | 540 | } |
02cd9ede | 541 | } |
469f80df | 542 | |
543 | ||
7ac48069 | 544 | void newuser(struct entry *e) |
02cd9ede | 545 | { |
5eaef520 | 546 | char *from, *to; |
547 | EXEC SQL BEGIN DECLARE SECTION; | |
548 | int id, uid, st, who; | |
dfaf9b68 | 549 | char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid; |
550 | char fullname[USERS_FULLNAME_SIZE], *prog; | |
551 | char oaddr[USERS_OFFICE_ADDR_SIZE], ophone[USERS_OFFICE_PHONE_SIZE]; | |
552 | char dept[USERS_DEPARTMENT_SIZE], *name, *title; | |
5eaef520 | 553 | char *rdept, *rhphone, *rophone; |
554 | EXEC SQL END DECLARE SECTION; | |
555 | ||
556 | who = WHO; | |
557 | prog = PROG; | |
dfaf9b68 | 558 | strncpy(oaddr, e->address, USERS_OFFICE_ADDR_SIZE - 1); |
559 | oaddr[USERS_OFFICE_ADDR_SIZE - 1] = '\0'; | |
5eaef520 | 560 | while ((to = strchr(oaddr, ','))) |
561 | *to = ';'; | |
562 | while ((to = strchr(oaddr, ':'))) | |
563 | *to = ';'; | |
564 | from = e->phone; | |
565 | to = ophone; | |
566 | while (*from) | |
567 | { | |
568 | if (isdigit(*from)) | |
569 | *to++ = *from; | |
570 | from++; | |
469f80df | 571 | } |
5eaef520 | 572 | *to = '\0'; |
573 | FixCase(e->dept); | |
dfaf9b68 | 574 | strncpy(dept, e->dept, USERS_DEPARTMENT_SIZE - 1); |
af8b98d0 | 575 | dept[USERS_DEPARTMENT_SIZE - 1] = '\0'; |
5eaef520 | 576 | |
577 | id = set_next_users_id(0); | |
578 | uid = set_next_uid(e->highid); | |
579 | sprintf(login, "#%d", uid); | |
580 | last = e->last; | |
581 | first = e->first; | |
582 | middle = e->middle; | |
583 | class = e->class; | |
584 | if (*middle) | |
585 | sprintf(fullname, "%s %s %s", first, middle, last); | |
586 | else | |
587 | sprintf(fullname, "%s %s", first, last); | |
588 | st = US_NO_LOGIN_YET; | |
589 | ||
590 | sid = e->id; | |
591 | name = e->name; | |
592 | rdept = e->dept; | |
593 | title = e->title; | |
594 | rhphone = e->phone; | |
595 | rophone = e->phone2; | |
596 | ||
e5843fe8 | 597 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); |
598 | if (debug) | |
599 | return; | |
600 | ||
5eaef520 | 601 | EXEC SQL INSERT INTO users |
602 | (login, users_id, unix_uid, shell, last, first, middle, status, | |
603 | clearid, type, modtime, modby, modwith, fullname, office_addr, | |
604 | office_phone, department, fmodtime, fmodby, fmodwith, | |
605 | potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime) | |
606 | VALUES (:login, :id, :uid, '/bin/athena/tcsh', | |
607 | NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)), | |
608 | :st, NVL(:sid, CHR(0)), NVL(:class, CHR(0)), SYSDATE, :who, :prog, | |
609 | NVL(:fullname, CHR(0)), NVL(:oaddr, CHR(0)), NVL(:ophone, CHR(0)), | |
610 | NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE', | |
611 | NVL(:name, CHR(0)), NVL(:rdept, CHR(0)), NVL(:title, CHR(0)), | |
612 | NVL(:oaddr, CHR(0)), NVL(:rhphone, CHR(0)), NVL(:rophone, CHR(0)), | |
613 | SYSDATE); | |
614 | if (sqlca.sqlcode) | |
615 | { | |
9f5e5c05 | 616 | dbmserr("adding user", sqlca.sqlcode); |
617 | exit(1); | |
5eaef520 | 618 | } |
02cd9ede | 619 | } |
469f80df | 620 | |
621 | ||
7ac48069 | 622 | int set_next_users_id(int limit) |
02cd9ede | 623 | { |
5eaef520 | 624 | EXEC SQL BEGIN DECLARE SECTION; |
7ac48069 | 625 | int flag, value, retval; |
5eaef520 | 626 | EXEC SQL END DECLARE SECTION; |
627 | ||
e5843fe8 | 628 | if (debug) |
629 | return 0; | |
630 | ||
5eaef520 | 631 | EXEC SQL SELECT value INTO :value FROM numvalues |
632 | WHERE name = 'users_id'; | |
633 | if (sqlfail()) | |
634 | sqlexit(); | |
635 | if (sqlca.sqlerrd[2] != 1) | |
636 | { | |
637 | EXEC SQL ROLLBACK; | |
638 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
639 | exit(1); | |
3e77c6a3 | 640 | } |
641 | ||
5eaef520 | 642 | flag = 0; |
643 | EXEC SQL SELECT users_id INTO :flag FROM users | |
644 | WHERE users_id = :value; | |
645 | if (sqlfail()) | |
646 | sqlexit(); | |
647 | if (sqlca.sqlerrd[2] == 0) | |
3e77c6a3 | 648 | flag = 0; |
5eaef520 | 649 | while (flag) |
650 | { | |
651 | value++; | |
652 | if (limit && value > MAX_ID_VALUE) | |
653 | value = MIN_ID_VALUE; | |
3e77c6a3 | 654 | flag = 0; |
5eaef520 | 655 | EXEC SQL SELECT users_id INTO :flag FROM users |
656 | WHERE users_id = :value; | |
657 | if (sqlfail()) | |
658 | sqlexit(); | |
659 | if (sqlca.sqlerrd[2] == 0) | |
3e77c6a3 | 660 | flag = 0; |
3e77c6a3 | 661 | } |
662 | ||
5eaef520 | 663 | retval = value++; |
664 | if (limit && value > MAX_ID_VALUE) | |
665 | value = MIN_ID_VALUE; | |
666 | EXEC SQL UPDATE numvalues SET value = :value | |
667 | WHERE name = 'users_id'; | |
668 | if (sqlca.sqlcode) | |
669 | { | |
670 | dbmserr("assigning ID", sqlca.sqlcode); | |
671 | exit(1); | |
3e77c6a3 | 672 | } |
5eaef520 | 673 | return retval; |
3e77c6a3 | 674 | } |
675 | ||
7ac48069 | 676 | int set_next_uid(int high) |
3e77c6a3 | 677 | { |
5eaef520 | 678 | EXEC SQL BEGIN DECLARE SECTION; |
e5843fe8 | 679 | int flag, initial, value, retval; |
5eaef520 | 680 | char *name; |
681 | EXEC SQL END DECLARE SECTION; | |
682 | ||
e5843fe8 | 683 | if (debug) |
684 | return 0; | |
685 | ||
5eaef520 | 686 | if (high) |
687 | name = "high_uid"; | |
688 | else | |
689 | name = "unix_uid"; | |
690 | ||
e5843fe8 | 691 | EXEC SQL SELECT value INTO :initial FROM numvalues |
5eaef520 | 692 | WHERE name = :name; |
693 | if (sqlfail()) | |
694 | sqlexit(); | |
695 | if (sqlca.sqlerrd[2] != 1) | |
696 | { | |
697 | EXEC SQL ROLLBACK; | |
698 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
699 | exit(1); | |
469f80df | 700 | } |
701 | ||
e5843fe8 | 702 | value = initial; |
5eaef520 | 703 | flag = 0; |
e5843fe8 | 704 | EXEC SQL SELECT COUNT(unix_uid) INTO :flag |
705 | FROM users WHERE unix_uid = :value; | |
5eaef520 | 706 | if (sqlfail()) |
707 | sqlexit(); | |
5eaef520 | 708 | while (flag) |
709 | { | |
710 | value++; | |
711 | if (!high && value > MAX_ID_VALUE) | |
712 | value = MIN_ID_VALUE; | |
e5843fe8 | 713 | if (value == initial) |
714 | { | |
715 | com_err(whoami, 0, "Out of uids!"); | |
716 | EXEC SQL ROLLBACK WORK; | |
717 | exit(1); | |
718 | } | |
02cd9ede | 719 | flag = 0; |
e5843fe8 | 720 | EXEC SQL SELECT COUNT(unix_uid) INTO :flag |
721 | FROM users WHERE unix_uid = :value; | |
5eaef520 | 722 | if (sqlfail()) |
723 | sqlexit(); | |
469f80df | 724 | } |
725 | ||
5eaef520 | 726 | retval = value++; |
727 | if (!high && value > MAX_ID_VALUE) | |
728 | value = MIN_ID_VALUE; | |
729 | EXEC SQL UPDATE numvalues SET value = :value WHERE name = :name; | |
730 | if (sqlca.sqlcode) | |
731 | { | |
732 | dbmserr("assigning ID", sqlca.sqlcode); | |
733 | exit(1); | |
3e77c6a3 | 734 | } |
5eaef520 | 735 | return retval; |
3e77c6a3 | 736 | } |
737 | ||
738 | ||
7ac48069 | 739 | void sqlexit(void) |
3e77c6a3 | 740 | { |
5eaef520 | 741 | dbmserr(NULL, sqlca.sqlcode); |
742 | EXEC SQL ROLLBACK WORK; | |
743 | exit(1); | |
02cd9ede | 744 | } |
9f5e5c05 | 745 | |
7ac48069 | 746 | void dbmserr(char *where, int what) |
9f5e5c05 | 747 | { |
748 | char err_msg[256]; | |
5eaef520 | 749 | int bufsize = 256, msglength = 0; |
9f5e5c05 | 750 | |
751 | sqlglm(err_msg, &bufsize, &msglength); | |
5eaef520 | 752 | err_msg[msglength] = '\0'; |
9f5e5c05 | 753 | |
5eaef520 | 754 | if (where) |
9f5e5c05 | 755 | com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where); |
756 | else | |
757 | com_err(whoami, 0, "DBMS error %s", err_msg); | |
758 | } |