]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
2 | * | |
3 | * Load data into Moira from Registrar's 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>. | |
093c49c9 | 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 <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <time.h> | |
20 | ||
8638fcf9 | 21 | EXEC SQL INCLUDE sqlca; |
7ac48069 | 22 | extern void sqlglm(char *, unsigned int *, unsigned int *); |
093c49c9 | 23 | |
7ac48069 | 24 | RCSID("$Header$"); |
093c49c9 | 25 | |
8638fcf9 | 26 | #define WHO 11859 /* root */ |
27 | #define PROG "stu-tape" | |
5e2b5b5f | 28 | |
397e9044 | 29 | #define MAX_ID_VALUE 31999 |
5e2b5b5f | 30 | #define MIN_ID_VALUE 101 |
31 | ||
093c49c9 | 32 | /* File format is: |
33 | ||
34 | 0-29 name | |
35 | 30-38 id number | |
36 | 50-54 school code | |
37 | 55-79 year | |
38 | 80-109 address | |
39 | 110-124 room | |
40 | 125-144 city | |
41 | 145-158 state | |
42 | 159-168 dorm phone | |
43 | 169-212 home address | |
44 | 213-232 home city | |
45 | 243-251 mit phone (?) | |
46 | */ | |
47 | ||
48 | #define LOC_NAME 0 | |
49 | #define LOC_ID 30 | |
50 | #define LOC_COURSE 50 | |
51 | #define LOC_YEAR 55 | |
52 | #define LOC_ADDRESS 80 | |
53 | #define LOC_DORM_ROOM 110 | |
54 | #define LOC_CITY 125 | |
55 | #define LOC_STATE 145 | |
c48e3c93 | 56 | #define LOC_DPHONE 155 |
e3c8ca24 | 57 | #define LOC_MPHONE 243 |
093c49c9 | 58 | |
e3c8ca24 | 59 | #define LEN_NAME 30 |
093c49c9 | 60 | #define LEN_ID 9 |
e3c8ca24 | 61 | #define LEN_COURSE 5 |
62 | #define LEN_YEAR 25 | |
63 | #define LEN_ADDRESS 30 | |
64 | #define LEN_DORM_ROOM 15 | |
65 | #define LEN_CITY 20 | |
c48e3c93 | 66 | #define LEN_STATE 10 |
67 | #define LEN_DPHONE 12 | |
68 | #define LEN_MPHONE 12 | |
093c49c9 | 69 | |
70 | struct entry { | |
5eaef520 | 71 | char *name; |
72 | char *last; | |
73 | char *first; | |
74 | char *middle; | |
75 | char *title; | |
76 | char *id; | |
5eaef520 | 77 | char *course; |
78 | char *year; | |
79 | char *address; | |
80 | char *dorm; | |
81 | char *city; | |
82 | char *state; | |
83 | char *dphone; | |
84 | char *mphone; | |
85 | char *class; | |
093c49c9 | 86 | }; |
87 | ||
093c49c9 | 88 | char *whoami; |
a97bc039 | 89 | int newfinger = 0; |
093c49c9 | 90 | |
7ac48069 | 91 | struct entry *get_next_entry(FILE *in); |
92 | void process_entry(struct entry *e); | |
93 | void newuser(struct entry *e); | |
94 | int set_next_users_id(int limit); | |
95 | int set_next_uid(int limit); | |
96 | void sqlexit(void); | |
97 | void dbmserr(char *where, int what); | |
98 | ||
bd22473a | 99 | #define SQL_DUPLICATE -2112 |
9f5e5c05 | 100 | #define sqlfail() (sqlca.sqlcode && sqlca.sqlcode != 1403) |
093c49c9 | 101 | |
5eaef520 | 102 | int main(int argc, char **argv) |
8638fcf9 | 103 | { |
5eaef520 | 104 | FILE *in; |
7ac48069 | 105 | struct entry *e; |
5eaef520 | 106 | int i, wait = 0; |
dfaf9b68 | 107 | char buf[80], *file = NULL; |
5eaef520 | 108 | EXEC SQL BEGIN DECLARE SECTION; |
109 | char *db = "moira"; | |
110 | EXEC SQL END DECLARE SECTION; | |
111 | ||
112 | whoami = strrchr(argv[0], '/'); | |
113 | if (whoami) | |
114 | whoami++; | |
115 | else | |
116 | whoami = argv[0]; | |
117 | ||
118 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); | |
119 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); | |
120 | ||
121 | for (i = 1; i < argc; i++) | |
122 | { | |
123 | if (!strcmp(argv[i], "-w")) | |
124 | wait++; | |
5eaef520 | 125 | else if (!strcmp(argv[i], "-n")) |
126 | newfinger++; | |
127 | else if (file) | |
128 | fprintf(stderr, "Usage: %s [-w] [-D] [-n] inputfile\n", whoami); | |
129 | else | |
130 | file = argv[i]; | |
131 | } | |
132 | ||
133 | in = fopen(file, "r"); | |
134 | if (!in) | |
135 | { | |
136 | fprintf(stderr, "Unable to open %s for input\n", file); | |
137 | exit(1); | |
138 | } | |
139 | ||
140 | initialize_sms_error_table(); | |
141 | ||
142 | EXEC SQL CONNECT :db IDENTIFIED BY :db; | |
143 | if (sqlca.sqlcode) | |
144 | { | |
145 | dbmserr("connecting", sqlca.sqlcode); | |
146 | exit(1); | |
147 | } | |
148 | ||
149 | while ((e = get_next_entry(in))) | |
150 | { | |
5eaef520 | 151 | process_entry(e); |
152 | EXEC SQL COMMIT WORK; | |
153 | if (sqlca.sqlcode) | |
154 | { | |
9f5e5c05 | 155 | dbmserr("committing work", sqlca.sqlcode); |
156 | exit(1); | |
837b0ec6 | 157 | } |
5eaef520 | 158 | if (wait) |
159 | { | |
160 | printf("Next"); | |
161 | fflush(stdout); | |
dfaf9b68 | 162 | fgets(buf, sizeof(buf), stdin); |
093c49c9 | 163 | } |
164 | } | |
165 | ||
5eaef520 | 166 | exit(0); |
8638fcf9 | 167 | } |
093c49c9 | 168 | |
169 | ||
5eaef520 | 170 | struct entry *get_next_entry(FILE *in) |
093c49c9 | 171 | { |
5eaef520 | 172 | static struct entry e; |
0e5cfe5f | 173 | static char buf[BUFSIZ], classbuf[10]; |
5eaef520 | 174 | static char name[LEN_NAME + 1], id[LEN_ID + 1], course[LEN_COURSE + 1]; |
175 | static char year[LEN_YEAR + 1], address[LEN_ADDRESS + 1]; | |
176 | static char dorm_room[LEN_DORM_ROOM + 1], city[LEN_CITY + 1]; | |
177 | static char state[LEN_STATE + 1], dphone[LEN_DPHONE + 1]; | |
178 | static char mphone[LEN_MPHONE + 1], sname[LEN_NAME + 1], title[128]; | |
179 | static int nyear = 0; | |
180 | int ends_jr, ends_iii, ends_iv, ends_sr, ends_ii, ends_v; | |
181 | char *p; | |
182 | ||
183 | if (nyear == 0) | |
184 | { | |
185 | struct tm *tm; | |
186 | struct timeval tv; | |
187 | ||
188 | gettimeofday(&tv, NULL); | |
189 | tm = localtime(&tv.tv_sec); | |
190 | nyear = tm->tm_year; | |
191 | if (tm->tm_mon > 5) | |
192 | nyear++; | |
193 | } | |
194 | ||
195 | if (!fgets(buf, sizeof(buf), in)) | |
196 | return NULL; | |
197 | ||
198 | strncpy(name, &buf[LOC_NAME], LEN_NAME); | |
199 | name[LEN_NAME] = '\0'; | |
200 | strncpy(id, &buf[LOC_ID], LEN_ID); | |
201 | id[LEN_ID] = '\0'; | |
202 | strncpy(course, &buf[LOC_COURSE], LEN_COURSE); | |
203 | course[LEN_COURSE] = '\0'; | |
204 | strncpy(year, &buf[LOC_YEAR], LEN_YEAR); | |
205 | year[LEN_YEAR] = '\0'; | |
206 | strncpy(address, &buf[LOC_ADDRESS], LEN_ADDRESS); | |
207 | address[LEN_ADDRESS] = '\0'; | |
208 | strncpy(dorm_room, &buf[LOC_DORM_ROOM], LEN_DORM_ROOM); | |
209 | dorm_room[LEN_DORM_ROOM] = '\0'; | |
210 | strncpy(city, &buf[LOC_CITY], LEN_CITY); | |
211 | city[LEN_CITY] = '\0'; | |
212 | strncpy(state, &buf[LOC_STATE], LEN_STATE); | |
213 | state[LEN_STATE] = '\0'; | |
214 | strncpy(dphone, &buf[LOC_DPHONE], LEN_DPHONE); | |
215 | dphone[LEN_DPHONE] = '\0'; | |
216 | strncpy(mphone, &buf[LOC_MPHONE], LEN_MPHONE); | |
217 | mphone[LEN_MPHONE] = '\0'; | |
218 | ||
219 | strcpy(sname, name); | |
220 | e.name = strtrim(sname); | |
221 | p = strchr(name, ','); | |
222 | if (p) | |
223 | *p = '\0'; | |
224 | e.last = strtrim(name); | |
225 | if (p) | |
226 | { | |
227 | p++; | |
228 | while (isspace(*p)) | |
e3c8ca24 | 229 | p++; |
5eaef520 | 230 | e.first = p; |
7ac48069 | 231 | if ((p = strchr(e.first, ' '))) |
5eaef520 | 232 | { |
233 | *p = '\0'; | |
234 | e.first = strtrim(e.first); | |
235 | e.middle = strtrim(p + 1); | |
236 | } | |
237 | else | |
238 | { | |
239 | e.first = strtrim(e.first); | |
240 | e.middle = ""; | |
6d6c60e6 | 241 | } |
5eaef520 | 242 | } |
243 | else | |
244 | { | |
245 | e.first = ""; | |
246 | e.middle = ""; | |
247 | } | |
248 | ends_jr = ends_iii = ends_iv = ends_sr = ends_ii = ends_v = 0; | |
249 | LookForSt(e.last); | |
250 | LookForO(e.last); | |
251 | LookForJrAndIII(e.last, &ends_sr, &ends_jr, &ends_iii, &ends_iv, | |
252 | &ends_ii, &ends_v); | |
253 | LookForJrAndIII(e.first, &ends_sr, &ends_jr, &ends_iii, &ends_iv, | |
254 | &ends_ii, &ends_v); | |
255 | FixCase(e.last); | |
256 | FixCase(e.first); | |
257 | FixCase(e.middle); | |
258 | ||
259 | e.id = id; | |
260 | e.id[LEN_ID] = '\0'; | |
5eaef520 | 261 | |
262 | e.year = strtrim(year); | |
263 | e.title = title; | |
264 | if (e.year[0] == 'G') | |
265 | { | |
266 | e.class = "G"; | |
267 | sprintf(title, "Grad Student"); | |
268 | } | |
269 | else | |
270 | { | |
271 | e.class = classbuf; | |
272 | sprintf(classbuf, "%d", nyear + 4 - atoi(e.year) + 1900); | |
273 | sprintf(title, "Undergrad (class of %s)", classbuf); | |
274 | } | |
275 | ||
276 | e.course = strtrim(course); | |
277 | e.address = strtrim(address); | |
278 | e.dorm = strtrim(dorm_room); | |
279 | e.city = strtrim(city); | |
280 | e.state = strtrim(state); | |
281 | e.dphone = strtrim(dphone); | |
282 | e.mphone = strtrim(mphone); | |
283 | return &e; | |
093c49c9 | 284 | } |
285 | ||
286 | ||
7ac48069 | 287 | void process_entry(struct entry *e) |
8638fcf9 | 288 | { |
0e5cfe5f | 289 | int changed, nochange; |
dfaf9b68 | 290 | char buf[MAX_FIELD_WIDTH], *from, *to; |
5eaef520 | 291 | EXEC SQL BEGIN DECLARE SECTION; |
0e5cfe5f | 292 | char *first, *last, *middle, *sid, *name, *rdept; |
5eaef520 | 293 | char *rtitle, *rophone, *rhphone, *prog; |
dfaf9b68 | 294 | char class[USERS_TYPE_SIZE], haddr[USERS_HOME_ADDR_SIZE]; |
295 | char hphone[USERS_HOME_PHONE_SIZE], ophone[USERS_OFFICE_PHONE_SIZE]; | |
296 | char dept[USERS_DEPARTMENT_SIZE], raddr[USERS_XADDRESS_SIZE]; | |
297 | char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE]; | |
298 | char dmiddle[USERS_MIDDLE_SIZE]; | |
5eaef520 | 299 | int id, status, who; |
300 | EXEC SQL END DECLARE SECTION; | |
301 | ||
302 | who = WHO; | |
303 | prog = PROG; | |
304 | first = e->first; | |
dfaf9b68 | 305 | if (strlen(first) > USERS_FIRST_SIZE - 1) |
306 | first[USERS_FIRST_SIZE - 1] = '\0'; | |
5eaef520 | 307 | last = e->last; |
dfaf9b68 | 308 | if (strlen(last) > USERS_LAST_SIZE - 1) |
309 | last[USERS_LAST_SIZE - 1] = '\0'; | |
5eaef520 | 310 | middle = e->middle; |
dfaf9b68 | 311 | if (strlen(middle) > USERS_MIDDLE_SIZE - 1) |
312 | middle[USERS_MIDDLE_SIZE - 1] = '\0'; | |
5eaef520 | 313 | sid = e->id; |
314 | id = 0; | |
5eaef520 | 315 | |
316 | /* Get user info */ | |
317 | EXEC SQL SELECT users_id, first, last, middle, type, home_addr, home_phone, | |
318 | office_phone, status, department | |
319 | INTO :id, :dfirst, :dlast, :dmiddle, :class, :haddr, :hphone, | |
320 | :ophone, :status, :dept | |
321 | FROM users | |
322 | WHERE clearid = :sid; | |
323 | if (sqlfail()) | |
324 | { | |
325 | if (sqlca.sqlcode == SQL_DUPLICATE) | |
326 | { | |
327 | com_err(whoami, 0, "duplicate ID number %s on user %s %s", | |
328 | sid, first, last); | |
329 | return; | |
330 | } | |
331 | else | |
332 | sqlexit(); | |
333 | } | |
334 | if (id == 0) | |
335 | { | |
0e5cfe5f | 336 | newuser(e); |
337 | return; | |
093c49c9 | 338 | } |
c48e3c93 | 339 | |
5eaef520 | 340 | /* See if class changed: if it's different, and the value in the database |
341 | * is not STAFF or SIPBMEM, and the value from the tape is actually | |
342 | * meaningful, then update the database. Since they were on the | |
343 | * students tape, make the account usable. | |
344 | */ | |
345 | if (strcmp(e->class, strtrim(class)) && | |
346 | strcmp(class, "STAFF") && strcmp(class, "SIPBMEM") && | |
347 | e->year[0]) | |
348 | { | |
349 | com_err(whoami, 0, "updating class for user %s %s from %s to %s", | |
350 | first, last, class, e->class); | |
351 | if (status == US_NOT_ALLOWED) | |
352 | status = US_NO_LOGIN_YET; | |
353 | if (status == US_ENROLL_NOT_ALLOWED) | |
354 | status = US_ENROLLED; | |
355 | strcpy(class, e->class); | |
356 | EXEC SQL UPDATE users | |
357 | SET type = NVL(:class, CHR(0)), status = :status, modtime = SYSDATE, | |
358 | modby = :who, modwith = :prog | |
359 | WHERE users_id = :id; | |
360 | if (sqlca.sqlcode) | |
361 | { | |
362 | dbmserr("updating class", sqlca.sqlcode); | |
363 | exit(1); | |
837b0ec6 | 364 | } |
093c49c9 | 365 | } |
c48e3c93 | 366 | |
5eaef520 | 367 | /* Update name if necessary */ |
368 | if (strcmp(first, strtrim(dfirst)) || | |
369 | strcmp(last, strtrim(dlast)) || | |
370 | strcmp(middle, strtrim(dmiddle))) | |
371 | { | |
372 | com_err(whoami, 0, "updating real name for %s %s", first, last); | |
373 | EXEC SQL UPDATE users | |
374 | SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)), | |
375 | middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog, | |
376 | modtime = SYSDATE | |
377 | WHERE users_id = :id; | |
378 | if (sqlca.sqlcode != 0) | |
379 | { | |
380 | dbmserr("updating name", sqlca.sqlcode); | |
381 | exit(1); | |
ca64f2fb | 382 | } |
383 | } | |
c48e3c93 | 384 | |
5eaef520 | 385 | /* Deal with updating the finger info if necessary */ |
386 | changed = nochange = 0; | |
5eaef520 | 387 | strcpy(buf, e->address); |
388 | if (*e->dorm) | |
389 | { | |
390 | strcat(buf, " "); | |
391 | strcat(buf, e->dorm); | |
392 | } | |
393 | if (*e->city) | |
394 | { | |
395 | strcat(buf, " "); | |
396 | strcat(buf, e->city); | |
397 | } | |
398 | FixCase(buf); | |
399 | if (*e->state) | |
400 | { | |
401 | strcat(buf, " "); | |
402 | strcat(buf, e->state); | |
403 | } | |
404 | while ((to = strchr(buf, ','))) | |
405 | *to = ';'; | |
406 | while ((to = strchr(buf, ':'))) | |
407 | *to = ';'; | |
408 | if (newfinger) | |
409 | { | |
410 | if (haddr[0] == ' ') | |
411 | { | |
dfaf9b68 | 412 | strncpy(haddr, buf, USERS_HOME_ADDR_SIZE - 1); |
413 | haddr[USERS_HOME_ADDR_SIZE - 1] = '\0'; | |
a97bc039 | 414 | changed++; |
8638fcf9 | 415 | } |
dfaf9b68 | 416 | else if (strncmp(strtrim(haddr), buf, USERS_HOME_ADDR_SIZE - 1)) |
5eaef520 | 417 | nochange++; |
418 | } | |
419 | else | |
420 | { | |
dfaf9b68 | 421 | if (strncmp(strtrim(haddr), buf, USERS_HOME_ADDR_SIZE - 1)) |
5eaef520 | 422 | changed++; |
dfaf9b68 | 423 | strncpy(haddr, buf, USERS_HOME_ADDR_SIZE - 1); |
424 | haddr[USERS_HOME_ADDR_SIZE - 1] = '\0'; | |
5eaef520 | 425 | } |
426 | from = e->dphone; | |
427 | to = buf; | |
428 | while (*from) | |
429 | { | |
430 | if (isdigit(*from)) | |
431 | *to++ = *from; | |
432 | from++; | |
433 | } | |
434 | *to = '\0'; | |
435 | if (newfinger) | |
436 | { | |
437 | if (hphone[0] == ' ') | |
438 | { | |
dfaf9b68 | 439 | strncpy(hphone, buf, USERS_HOME_PHONE_SIZE - 1); |
440 | hphone[USERS_HOME_PHONE_SIZE - 1] = '\0'; | |
5eaef520 | 441 | } |
dfaf9b68 | 442 | else if (strncmp(strtrim(hphone), buf, USERS_HOME_PHONE_SIZE - 1)) |
5eaef520 | 443 | nochange++; |
444 | } | |
445 | else | |
446 | { | |
dfaf9b68 | 447 | if (strncmp(strtrim(hphone), buf, USERS_HOME_PHONE_SIZE - 1)) |
5eaef520 | 448 | changed++; |
dfaf9b68 | 449 | strncpy(hphone, buf, USERS_HOME_PHONE_SIZE - 1); |
450 | hphone[USERS_HOME_PHONE_SIZE - 1] = '\0'; | |
5eaef520 | 451 | } |
452 | from = e->mphone; | |
453 | to = buf; | |
454 | while (*from) | |
455 | { | |
456 | if (isdigit(*from)) | |
457 | *to++ = *from; | |
458 | from++; | |
459 | } | |
460 | *to = '\0'; | |
461 | if (newfinger) | |
462 | { | |
463 | if (ophone[0] == ' ') | |
464 | { | |
dfaf9b68 | 465 | strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1); |
466 | ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0'; | |
5eaef520 | 467 | } |
dfaf9b68 | 468 | else if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1)) |
5eaef520 | 469 | nochange++; |
470 | } | |
471 | else | |
472 | { | |
dfaf9b68 | 473 | if (strncmp(strtrim(ophone), buf, USERS_OFFICE_PHONE_SIZE - 1)) |
5eaef520 | 474 | changed++; |
dfaf9b68 | 475 | strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1); |
476 | ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0'; | |
5eaef520 | 477 | } |
478 | e->course = e->course; | |
479 | if (newfinger) | |
480 | { | |
481 | if (dept[0] == ' ') | |
482 | { | |
dfaf9b68 | 483 | strncpy(dept, e->course, USERS_DEPARTMENT_SIZE - 1); |
484 | dept[USERS_DEPARTMENT_SIZE - 1] = '\0'; | |
5eaef520 | 485 | } |
dfaf9b68 | 486 | else if (strncmp(strtrim(dept), e->course, USERS_DEPARTMENT_SIZE - 1)) |
5eaef520 | 487 | nochange++; |
488 | } | |
489 | else | |
490 | { | |
dfaf9b68 | 491 | if (strncmp(strtrim(dept), e->course, USERS_DEPARTMENT_SIZE - 1)) |
5eaef520 | 492 | changed++; |
dfaf9b68 | 493 | strncpy(dept, e->course, USERS_DEPARTMENT_SIZE - 1); |
494 | dept[USERS_DEPARTMENT_SIZE - 1] = '\0'; | |
5eaef520 | 495 | } |
496 | sid = e->id; | |
497 | name = e->name; | |
498 | rdept = e->course; | |
499 | rtitle = e->title; | |
500 | rophone = e->mphone; | |
501 | rhphone = e->dphone; | |
502 | strcpy(raddr, e->address); | |
503 | if (*e->dorm) | |
504 | { | |
505 | strcat(raddr, " "); | |
506 | strcat(raddr, e->dorm); | |
507 | } | |
508 | strcat(raddr, "|"); | |
509 | if (*e->city) | |
510 | { | |
511 | strcat(raddr, e->city); | |
512 | FixCase(raddr); | |
513 | if (*e->state) | |
514 | { | |
515 | strcat(raddr, " "); | |
516 | if (isupper(e->state[0]) && isupper(e->state[1]) && | |
517 | isdigit(e->state[2]) && isdigit(e->state[3]) && | |
518 | isdigit(e->state[4]) && isdigit(e->state[5]) && | |
519 | isdigit(e->state[6])) | |
520 | { | |
521 | sprintf(buf, "%c%c %s", e->state[0], e->state[1], | |
522 | &(e->state[2])); | |
523 | strcat(raddr, buf); | |
524 | } | |
525 | else | |
526 | strcat(raddr, e->state); | |
527 | } | |
528 | } | |
529 | else | |
530 | { | |
531 | FixCase(raddr); | |
532 | strcat(raddr, "MIT INTERDEPARTMENTAL MAIL"); | |
533 | } | |
534 | if (changed) | |
535 | { | |
536 | com_err(whoami, 0, "updating finger for %s %s", first, last); | |
537 | EXEC SQL UPDATE users | |
538 | SET home_addr = NVL(:haddr, CHR(0)), home_phone = NVL(:hphone, CHR(0)), | |
539 | office_phone = NVL(:ophone, CHR(0)), department = NVL(:dept, CHR(0)), | |
540 | fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog, | |
541 | xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)), | |
542 | xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)), | |
543 | xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)), | |
544 | xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0)) | |
545 | WHERE users_id = :id; | |
546 | if (sqlca.sqlcode) | |
547 | { | |
9f5e5c05 | 548 | dbmserr(NULL, sqlca.sqlcode); |
549 | exit(1); | |
837b0ec6 | 550 | } |
5eaef520 | 551 | } |
552 | else | |
553 | { | |
554 | EXEC SQL UPDATE users | |
555 | SET xname = NVL(:name, CHR(0)), xdept = NVL(:rdept, CHR(0)), | |
556 | xtitle = NVL(:rtitle, CHR(0)), xaddress = NVL(:raddr, CHR(0)), | |
557 | xphone1 = NVL(:rhphone, CHR(0)), xphone2 = NVL(:rophone, CHR(0)), | |
558 | xmodtime = SYSDATE, clearid = NVL(:sid, CHR(0)) | |
559 | WHERE users_id = :id; | |
560 | if (sqlca.sqlcode) | |
561 | { | |
9f5e5c05 | 562 | dbmserr(NULL, sqlca.sqlcode); |
563 | exit(1); | |
837b0ec6 | 564 | } |
e3c8ca24 | 565 | } |
8638fcf9 | 566 | } |
093c49c9 | 567 | |
7ac48069 | 568 | void newuser(struct entry *e) |
8638fcf9 | 569 | { |
5eaef520 | 570 | char buf[512], *from, *to; |
571 | EXEC SQL BEGIN DECLARE SECTION; | |
572 | int id, uid, who; | |
dfaf9b68 | 573 | char *last, *first, *class, *middle, login[USERS_LOGIN_SIZE], *sid; |
574 | char fullname[USERS_FULLNAME_SIZE], *prog; | |
575 | char haddr[USERS_HOME_ADDR_SIZE], hphone[USERS_HOME_PHONE_SIZE]; | |
576 | char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE]; | |
577 | char *title, raddr[USERS_XADDRESS_SIZE], *name; | |
5eaef520 | 578 | EXEC SQL END DECLARE SECTION; |
579 | ||
580 | who = WHO; | |
581 | prog = PROG; | |
582 | strcpy(buf, e->address); | |
583 | if (*e->dorm) | |
584 | { | |
585 | strcat(buf, " "); | |
586 | strcat(buf, e->dorm); | |
587 | } | |
588 | if (*e->city) | |
589 | { | |
590 | strcat(buf, " "); | |
591 | strcat(buf, e->city); | |
592 | } | |
593 | if (*e->state) | |
594 | { | |
595 | strcat(buf, " "); | |
596 | strcat(buf, e->state); | |
597 | } | |
dfaf9b68 | 598 | strncpy(haddr, buf, USERS_HOME_ADDR_SIZE - 1); |
599 | haddr[USERS_HOME_ADDR_SIZE - 1] = '\0'; | |
5eaef520 | 600 | from = e->dphone; |
601 | to = buf; | |
602 | while (*from) | |
603 | { | |
604 | if (isdigit(*from)) | |
605 | *to++ = *from; | |
606 | from++; | |
607 | } | |
608 | *to = '\0'; | |
dfaf9b68 | 609 | strncpy(hphone, buf, USERS_HOME_PHONE_SIZE - 1); |
610 | hphone[USERS_HOME_PHONE_SIZE - 1] = '\0'; | |
5eaef520 | 611 | from = e->mphone; |
612 | to = buf; | |
613 | while (*from) | |
614 | { | |
615 | if (isdigit(*from)) | |
616 | *to++ = *from; | |
617 | from++; | |
618 | } | |
619 | *to = '\0'; | |
dfaf9b68 | 620 | strncpy(ophone, buf, USERS_OFFICE_PHONE_SIZE - 1); |
621 | ophone[USERS_OFFICE_PHONE_SIZE - 1] = '\0'; | |
622 | strncpy(dept, e->course, USERS_DEPARTMENT_SIZE - 1); | |
623 | dept[USERS_DEPARTMENT_SIZE - 1] = '\0'; | |
5eaef520 | 624 | |
625 | id = set_next_users_id(0); | |
626 | uid = set_next_uid(1); | |
627 | sprintf(login, "#%d", uid); | |
628 | last = e->last; | |
629 | first = e->first; | |
630 | middle = e->middle; | |
631 | sid = e->id; | |
632 | class = e->class; | |
633 | title = e->title; | |
634 | if (*middle) | |
635 | sprintf(fullname, "%s %s %s", first, middle, last); | |
636 | else | |
637 | sprintf(fullname, "%s %s", first, last); | |
638 | name = e->name; | |
639 | strcpy(raddr, e->address); | |
640 | if (*e->dorm) | |
641 | { | |
642 | strcat(raddr, " "); | |
643 | strcat(raddr, e->dorm); | |
644 | } | |
645 | strcat(raddr, "|"); | |
646 | if (*e->city) | |
647 | { | |
648 | strcat(raddr, e->city); | |
649 | FixCase(raddr); | |
650 | if (*e->state) | |
651 | { | |
652 | strcat(raddr, " "); | |
653 | if (isupper(e->state[0]) && isupper(e->state[1]) && | |
654 | isdigit(e->state[2]) && isdigit(e->state[3]) && | |
655 | isdigit(e->state[4]) && isdigit(e->state[5]) && | |
656 | isdigit(e->state[6])) | |
657 | { | |
658 | sprintf(buf, "%c%c %s", e->state[0], e->state[1], | |
659 | &(e->state[2])); | |
660 | strcat(raddr, buf); | |
661 | } | |
662 | else | |
663 | strcat(raddr, e->state); | |
8638fcf9 | 664 | } |
5eaef520 | 665 | } |
666 | else | |
667 | { | |
668 | FixCase(raddr); | |
669 | strcat(raddr, "MIT INTERDEPARTMENTAL MAIL"); | |
670 | } | |
671 | EXEC SQL INSERT INTO users | |
e937b97c | 672 | (login, users_id, unix_uid, shell, winconsoleshell, last, first, |
673 | middle, status, clearid, type, modtime, modby, modwith, fullname, | |
674 | home_addr, home_phone, office_phone, department, fmodtime, fmodby, | |
675 | fmodwith, potype, xname, xdept, xtitle, xaddress, xphone1, xphone2, | |
676 | xmodtime, secure) | |
677 | VALUES (:login, :id, :uid, '/bin/athena/tcsh', 'cmd', NVL(:last, CHR(0)), | |
5eaef520 | 678 | NVL(:first, CHR(0)), NVL(:middle, CHR(0)), 0, NVL(:sid, CHR(0)), |
679 | NVL(:class, CHR(0)), SYSDATE, :who, :prog, NVL(:fullname, CHR(0)), | |
680 | NVL(:haddr, CHR(0)), NVL(:hphone, CHR(0)), NVL(:ophone, CHR(0)), | |
681 | NVL(:dept, CHR(0)), SYSDATE, :who, :prog, 'NONE', | |
682 | NVL(:name, CHR(0)), NVL(:dept, CHR(0)), NVL(:title, CHR(0)), | |
683 | NVL(:raddr, CHR(0)), NVL(:hphone, CHR(0)), NVL(:ophone, CHR(0)), | |
0e0db404 | 684 | SYSDATE, 1); |
5eaef520 | 685 | if (sqlca.sqlcode) |
686 | { | |
9f5e5c05 | 687 | dbmserr("adding user", sqlca.sqlcode); |
688 | exit(1); | |
5eaef520 | 689 | } |
690 | else | |
691 | com_err(whoami, 0, "adding user %s %s", e->first, e->last); | |
8638fcf9 | 692 | } |
5e2b5b5f | 693 | |
694 | ||
5eaef520 | 695 | int set_next_users_id(int limit) |
8638fcf9 | 696 | { |
5eaef520 | 697 | EXEC SQL BEGIN DECLARE SECTION; |
7ac48069 | 698 | int flag, value, retval; |
5eaef520 | 699 | EXEC SQL END DECLARE SECTION; |
700 | ||
701 | EXEC SQL SELECT value INTO :value FROM numvalues | |
702 | WHERE name = 'users_id'; | |
703 | if (sqlfail()) | |
704 | sqlexit(); | |
705 | if (sqlca.sqlerrd[2] != 1) | |
706 | { | |
707 | EXEC SQL ROLLBACK; | |
708 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
709 | exit(1); | |
093c49c9 | 710 | } |
5e2b5b5f | 711 | |
5eaef520 | 712 | flag = 0; |
713 | EXEC SQL SELECT users_id INTO :flag FROM users | |
714 | WHERE users_id = :value; | |
715 | if (sqlfail()) | |
716 | sqlexit(); | |
717 | if (sqlca.sqlerrd[2] == 0) | |
8638fcf9 | 718 | flag = 0; |
5eaef520 | 719 | while (flag) |
720 | { | |
721 | value++; | |
722 | if (limit && value > MAX_ID_VALUE) | |
723 | value = MIN_ID_VALUE; | |
8638fcf9 | 724 | flag = 0; |
5eaef520 | 725 | EXEC SQL SELECT users_id INTO :flag FROM users |
726 | WHERE users_id = :value; | |
727 | if (sqlfail()) | |
728 | sqlexit(); | |
729 | if (sqlca.sqlerrd[2] == 0) | |
8638fcf9 | 730 | flag = 0; |
5eaef520 | 731 | } |
732 | ||
733 | retval = value++; | |
734 | if (limit && value > MAX_ID_VALUE) | |
735 | value = MIN_ID_VALUE; | |
736 | EXEC SQL UPDATE numvalues SET value = :value | |
737 | WHERE name = 'users_id'; | |
738 | if (sqlca.sqlcode) | |
739 | { | |
740 | dbmserr("updating numvalues", sqlca.sqlcode); | |
741 | exit(1); | |
742 | } | |
743 | return retval; | |
837b0ec6 | 744 | } |
745 | ||
5eaef520 | 746 | int set_next_uid(int limit) |
837b0ec6 | 747 | { |
5eaef520 | 748 | EXEC SQL BEGIN DECLARE SECTION; |
7ac48069 | 749 | int flag, value, retval; |
5eaef520 | 750 | EXEC SQL END DECLARE SECTION; |
751 | ||
752 | EXEC SQL SELECT value INTO :value FROM numvalues | |
753 | WHERE name = 'unix_uid'; | |
754 | if (sqlfail()) | |
755 | sqlexit(); | |
756 | if (sqlca.sqlerrd[2] != 1) | |
757 | { | |
758 | EXEC SQL ROLLBACK; | |
759 | com_err(whoami, MR_INTERNAL, "values table inconsistancy"); | |
760 | exit(1); | |
837b0ec6 | 761 | } |
762 | ||
5eaef520 | 763 | flag = 0; |
764 | EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value; | |
765 | if (sqlfail()) | |
766 | sqlexit(); | |
767 | if (sqlca.sqlerrd[2] == 0) | |
837b0ec6 | 768 | flag = 0; |
5eaef520 | 769 | while (flag) |
770 | { | |
771 | value++; | |
772 | if (limit && value > MAX_ID_VALUE) | |
773 | value = MIN_ID_VALUE; | |
837b0ec6 | 774 | flag = 0; |
5eaef520 | 775 | EXEC SQL SELECT unix_uid INTO :flag FROM users WHERE unix_uid = :value; |
776 | if (sqlfail()) | |
777 | sqlexit(); | |
778 | if (sqlca.sqlerrd[2] == 0) | |
837b0ec6 | 779 | flag = 0; |
837b0ec6 | 780 | } |
781 | ||
5eaef520 | 782 | retval = value++; |
783 | if (limit && value > MAX_ID_VALUE) | |
784 | value = MIN_ID_VALUE; | |
785 | EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid'; | |
786 | if (sqlca.sqlcode) | |
787 | { | |
788 | dbmserr("updating numvalues", sqlca.sqlcode); | |
789 | exit(1); | |
837b0ec6 | 790 | } |
5eaef520 | 791 | return retval; |
837b0ec6 | 792 | } |
793 | ||
794 | ||
7ac48069 | 795 | void sqlexit(void) |
837b0ec6 | 796 | { |
5eaef520 | 797 | dbmserr(NULL, sqlca.sqlcode); |
798 | EXEC SQL ROLLBACK WORK; | |
799 | exit(1); | |
8638fcf9 | 800 | } |
9f5e5c05 | 801 | |
7ac48069 | 802 | void dbmserr(char *where, int what) |
9f5e5c05 | 803 | { |
804 | char err_msg[256]; | |
5eaef520 | 805 | int bufsize = 256, msglength = 0; |
9f5e5c05 | 806 | |
807 | sqlglm(err_msg, &bufsize, &msglength); | |
5eaef520 | 808 | err_msg[msglength] = '\0'; |
9f5e5c05 | 809 | |
5eaef520 | 810 | if (where) |
9f5e5c05 | 811 | com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where); |
812 | else | |
813 | com_err(whoami, 0, "DBMS error %s", err_msg); | |
814 | } |