]> andersk Git - moira.git/blob - regtape/common.pc
1c4588d902aecbad519a540251efff9434a619b9
[moira.git] / regtape / common.pc
1 /* $Id$
2  *
3  * Staff/Student load common code
4  *
5  * Copyright (C) 1999 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12 #include <moira_site.h>
13 #include <moira_schema.h>
14 #include "common.h"
15
16 #include <ctype.h>
17 #include <string.h>
18
19 EXEC SQL INCLUDE sqlca;
20 extern void sqlglm(char *, unsigned int *, unsigned int *);
21
22 RCSID("$Header$");
23
24 EXEC SQL BEGIN DECLARE SECTION;
25 extern char *prog;
26 extern int who;
27 EXEC SQL END DECLARE SECTION;
28
29 extern char *whoami;
30
31 #define MIN_ID_VALUE 100
32 #define MAX_ID_VALUE 65535
33
34 /* Remove non-digits from a phone number. */
35 void fixphone(char *phone)
36 {
37   char *d = phone;
38
39   while (*phone)
40     {
41       if (isdigit(*phone))
42         *d++ = *phone;
43       phone++;
44     }
45   *d = '\0';
46 }
47
48 /* Remove characters from address that aren't safe for passwd files. */
49 void fixaddress(char *address)
50 {
51   char *p;
52
53   while ((p = strchr(address, ',')))
54     *p = ';';
55   while ((p = strchr(address, ':')))
56     *p = ';';
57 }  
58
59 void process_entry(struct entry *e, int secure)
60 {
61   int changed;
62   char buf[MAX_FIELD_WIDTH], *from, *to;
63   EXEC SQL BEGIN DECLARE SECTION;
64   char *first, *last, *middle, *sid;
65   char *name = e->name, *xtitle = e->xtitle, *xaddr = e->xaddress;
66   char *xphone1 = e->xphone1, *xphone2 = e->xphone2;
67   char type[USERS_TYPE_SIZE], oaddr[USERS_OFFICE_ADDR_SIZE];
68   char ophone[USERS_OFFICE_PHONE_SIZE], dept[USERS_DEPARTMENT_SIZE];
69   char haddr[USERS_HOME_ADDR_SIZE], hphone[USERS_HOME_PHONE_SIZE];
70   char dfirst[USERS_FIRST_SIZE], dlast[USERS_LAST_SIZE];
71   char dmiddle[USERS_MIDDLE_SIZE];
72   int id, status, fmodby, xnlen = USERS_XNAME_SIZE - 1;
73   EXEC SQL END DECLARE SECTION;
74
75   sid = e->id;
76   last = e->last;
77   if (strlen(last) > USERS_LAST_SIZE - 1)
78     last[USERS_LAST_SIZE - 1] = '\0';
79   first = e->first;
80   if (strlen(first) > USERS_FIRST_SIZE - 1)
81     first[USERS_FIRST_SIZE - 1] = '\0';
82   middle = e->middle;
83   if (strlen(middle) > USERS_MIDDLE_SIZE - 1)
84     middle[USERS_MIDDLE_SIZE - 1] = '\0';
85   id = 0;
86
87   /* Get user info */
88   EXEC SQL SELECT users_id, first, last, middle, type, office_addr,
89     office_phone, home_addr, home_phone, department, status, fmodby
90     INTO :id, :dfirst, :dlast, :dmiddle, :type, :oaddr,
91     :ophone, :haddr, :hphone, :dept, :status, :fmodby
92     FROM users
93     WHERE clearid = :sid AND status != 3;
94   if (sqlfail())
95     {
96       if (sqlca.sqlcode == SQL_DUPLICATE)
97         {
98           com_err(whoami, 0, "duplicate ID number %s on user %s %s",
99                   sid, first, last);
100           return;
101         }
102       else
103         sqlexit();
104     }
105   if (id == 0)
106     {
107       newuser(e, secure);
108       return;
109     }
110   strtrim(dfirst);
111   strtrim(dlast);
112   strtrim(dmiddle);
113   strtrim(type);
114
115   /* Update type/state if necessary.  (Exclude several spacial cases.) */
116   if (strcmp(e->type, type) &&
117       strcmp(type, "STAFF") && strcmp(type, "SIPBMEM") &&
118       strcmp(type, "KNIGHT"))
119     {
120       com_err(whoami, 0, "updating type for %s %s from \"%s\" to \"%s\"",
121               first, last, type, e->type);
122       if (status == US_NOT_ALLOWED)
123         status = US_NO_LOGIN_YET;
124       else if (status == US_ENROLL_NOT_ALLOWED)
125         status = US_ENROLLED;
126       strcpy(type, e->type);
127       EXEC SQL UPDATE users
128         SET type = NVL(:type, CHR(0)), status = :status,
129         modtime = SYSDATE, modby = :who, modwith = :prog
130         WHERE users_id = :id;
131       if (sqlca.sqlcode)
132         {
133           dbmserr("updating user", sqlca.sqlcode);
134           exit(1);
135         }
136     }
137
138   /* Update name if necessary */
139   if (strcmp(first, dfirst) || strcmp(last, dlast) || strcmp(middle, dmiddle))
140     {
141       com_err(whoami, 0, "updating real name for %s%s%s %s (was %s%s%s %s)",
142               first, *middle ? " " : "", middle, last,
143               dfirst, *dmiddle ? " " : "", dmiddle, dlast);
144       EXEC SQL UPDATE users
145         SET first = NVL(:first, CHR(0)), last = NVL(:last, CHR(0)),
146         middle = NVL(:middle, CHR(0)), modby = :who, modwith = :prog,
147         modtime = SYSDATE
148         WHERE users_id = :id;
149       if (sqlca.sqlcode)
150         {
151           dbmserr("updating name", sqlca.sqlcode);
152           exit(1);
153         }
154     }
155
156   /* Update finger info fields if they have changed and the user
157    * didn't set them blank.
158    */
159   changed = 0;
160
161   if (strncmp(strtrim(oaddr), e->oaddr, USERS_OFFICE_ADDR_SIZE - 1) &&
162       (*oaddr || fmodby != id))
163     {
164       changed++;
165       com_err(whoami, 0, "office for %s %s changed from \"%s\" to \"%s\"",
166               first, last, oaddr, e->oaddr);
167       strlcpy(oaddr, e->oaddr, USERS_OFFICE_ADDR_SIZE);
168     }
169
170   if (strncmp(strtrim(ophone), e->ophone, USERS_OFFICE_PHONE_SIZE - 1) &&
171       (*ophone || fmodby != id))
172     {
173       changed++;
174       com_err(whoami, 0, "Phone for %s %s changed from \"%s\" to \"%s\"",
175               first, last, ophone, e->ophone);
176       strlcpy(ophone, e->ophone, USERS_OFFICE_PHONE_SIZE);
177     }
178
179   if (strncmp(strtrim(haddr), e->haddr, USERS_HOME_ADDR_SIZE - 1) &&
180       (*haddr || fmodby != id))
181     {
182       changed++;
183       com_err(whoami, 0, "home addr for %s %s changed from \"%s\" to \"%s\"",
184               first, last, haddr, e->haddr);
185       strlcpy(haddr, e->haddr, USERS_HOME_ADDR_SIZE);
186     }
187
188   if (strncmp(strtrim(hphone), e->hphone, USERS_HOME_PHONE_SIZE - 1) &&
189       (*hphone || fmodby != id))
190     {
191       changed++;
192       com_err(whoami, 0, "Phone for %s %s changed from \"%s\" to \"%s\"",
193               first, last, hphone, e->hphone);
194       strlcpy(hphone, e->hphone, USERS_HOME_PHONE_SIZE);
195     }
196
197   if (strncmp(strtrim(dept), e->dept, USERS_DEPARTMENT_SIZE - 1) &&
198       (*dept || fmodby != id))
199     {
200       changed++;
201       com_err(whoami, 0, "Department for %s %s changed from \"%s\" to \"%s\"",
202               first, last, dept, e->dept);
203       strlcpy(dept, e->dept, USERS_DEPARTMENT_SIZE);
204     }
205
206   if (changed)
207     {
208       com_err(whoami, 0, "updating finger for %s %s", first, last);
209       EXEC SQL UPDATE users
210         SET office_addr = NVL(:oaddr, CHR(0)),
211         office_phone = NVL(:ophone, CHR(0)), home_addr = NVL(:haddr, CHR(0)),
212         home_phone = NVL(:hphone, CHR(0)), department = NVL(:dept, CHR(0)),
213         fmodtime = SYSDATE, fmodby = :who, fmodwith = :prog,
214         xname = NVL(SUBSTR(:name, 0, :xnlen), CHR(0)),
215         xdept = NVL(:dept, CHR(0)), xtitle = NVL(:xtitle, CHR(0)),
216         xaddress = NVL(:xaddr, CHR(0)), xphone1 = NVL(:xphone1, CHR(0)),
217         xphone2 = NVL(:xphone2, CHR(0)), xmodtime = SYSDATE
218         WHERE users_id = :id;
219       if (sqlca.sqlcode)
220         {
221           dbmserr(NULL, sqlca.sqlcode);
222           exit(1);
223         }
224     }
225   else
226     {
227       EXEC SQL UPDATE users
228         SET xname = NVL(SUBSTR(:name, 0, :xnlen), CHR(0)), 
229         xdept = NVL(:dept, CHR(0)), xtitle = NVL(:xtitle, CHR(0)), 
230         xaddress = NVL(:xaddr, CHR(0)), xphone1 = NVL(:xphone1, CHR(0)), 
231         xphone2 = NVL(:xphone2, CHR(0)), xmodtime = SYSDATE
232         WHERE users_id = :id;
233       if (sqlca.sqlcode)
234         {
235           dbmserr(NULL, sqlca.sqlcode);
236           exit(1);
237         }
238     }
239 }
240
241 void newuser(struct entry *e, int secure)
242 {
243   EXEC SQL BEGIN DECLARE SECTION;
244   int issecure = secure, users_id, uid, st, xnlen = USERS_XNAME_SIZE - 1;
245   int oadlen = USERS_OFFICE_ADDR_SIZE - 1;
246   int ophlen = USERS_OFFICE_PHONE_SIZE - 1; 
247   char login[USERS_LOGIN_SIZE];
248   char *id, *last, *first, *middle, *type;
249   char *name, *dept, *haddr, *hphone, *oaddr, *ophone;
250   char *xtitle, *xaddress, *xphone1, *xphone2;
251   EXEC SQL END DECLARE SECTION;
252
253   users_id = set_next_users_id();
254   uid = set_next_uid();
255   sprintf(login, "#%d", uid);
256   st = US_NO_LOGIN_YET;
257   last = e->last;
258   first = e->first;
259   middle = e->middle;
260   id = e->id;
261   type = e->type;
262   name = e->name;
263   dept = e->dept;
264   haddr = e->haddr;
265   hphone = e->hphone;
266   oaddr = e->oaddr;
267   ophone = e->ophone;
268   xtitle = e->xtitle;
269   xaddress = e->xaddress;
270   xphone1 = e->xphone1;
271   xphone2 = e->xphone2;
272
273   com_err(whoami, 0, "adding user %s %s", e->first, e->last);
274
275   EXEC SQL INSERT INTO users
276     (login, users_id, unix_uid, shell, winconsoleshell, last, first, 
277      middle, status, clearid, type, modtime, modby, modwith, fullname, 
278      department, home_addr, home_phone, office_addr, office_phone, fmodtime,
279      fmodby, fmodwith, potype, pmodtime, pmodby, pmodwith,
280      xname, xdept, xtitle, xaddress, xphone1, xphone2, xmodtime, secure,
281      created, creator)
282     VALUES (:login, :users_id, :uid, '/bin/athena/tcsh', 'cmd',
283             NVL(:last, CHR(0)), NVL(:first, CHR(0)), NVL(:middle, CHR(0)),
284             :st, NVL(:id, CHR(0)), NVL(:type, CHR(0)), SYSDATE, :who, :prog,
285             NVL(:name, CHR(0)), NVL(:dept, CHR(0)), NVL(:haddr, CHR(0)),
286             NVL(:hphone, CHR(0)), NVL(SUBSTR(:oaddr, 0, :oadlen), CHR(0)), 
287             NVL(SUBSTR(:ophone, 0, :ophlen), CHR(0)), SYSDATE, :who, :prog, 
288             'NONE', SYSDATE, :who, :prog, 
289             NVL(SUBSTR(:name, 0, :xnlen), CHR(0)), NVL(:dept, CHR(0)),
290             NVL(:xtitle, CHR(0)), NVL(:xaddress, CHR(0)),
291             NVL(:xphone1, CHR(0)), NVL(:xphone2, CHR(0)), SYSDATE, :issecure,
292             SYSDATE, :who);
293   if (sqlca.sqlcode)
294     {
295       dbmserr("adding user", sqlca.sqlcode);
296       exit(1);
297     }
298 }
299
300 int set_next_users_id(void)
301 {
302   EXEC SQL BEGIN DECLARE SECTION;
303   int flag, value, retval;
304   EXEC SQL END DECLARE SECTION;
305
306   EXEC SQL SELECT value INTO :value FROM numvalues
307     WHERE name = 'users_id';
308   if (sqlfail())
309     sqlexit();
310   if (sqlca.sqlerrd[2] != 1)
311     {
312       EXEC SQL ROLLBACK;
313       com_err(whoami, MR_INTERNAL, "values table inconsistancy");
314       exit(1);
315     }
316
317   flag = 0;
318   EXEC SQL SELECT users_id INTO :flag FROM users
319     WHERE users_id = :value;
320   if (sqlfail())
321     sqlexit();
322   if (sqlca.sqlerrd[2] == 0)
323     flag = 0;
324   while (flag)
325     {
326       value++;
327       flag = 0;
328       EXEC SQL SELECT users_id INTO :flag FROM users
329         WHERE users_id = :value;
330       if (sqlfail())
331         sqlexit();
332       if (sqlca.sqlerrd[2] == 0)
333         flag = 0;
334     }
335
336   retval = value++;
337   EXEC SQL UPDATE numvalues SET value = :value
338     WHERE name = 'users_id';
339   if (sqlca.sqlcode)
340     {
341       dbmserr("assigning ID", sqlca.sqlcode);
342       exit(1);
343     }
344   return retval;
345 }
346
347 int set_next_uid(void)
348 {
349   EXEC SQL BEGIN DECLARE SECTION;
350   int flag, initial, value, retval;
351   EXEC SQL END DECLARE SECTION;
352
353   EXEC SQL SELECT value INTO :initial FROM numvalues
354     WHERE name = 'unix_uid';
355   if (sqlfail())
356     sqlexit();
357   if (sqlca.sqlerrd[2] != 1)
358     {
359       EXEC SQL ROLLBACK;
360       com_err(whoami, MR_INTERNAL, "values table inconsistancy");
361       exit(1);
362     }
363
364   value = initial;
365   flag = 0;
366   EXEC SQL SELECT COUNT(unix_uid) INTO :flag
367     FROM users WHERE unix_uid = :value;
368   if (sqlfail())
369     sqlexit();
370   if (sqlca.sqlerrd[2] == 0)
371     flag = 0;
372   while (flag)
373     {
374       value++;
375 #ifdef ULTRIX_ID_HOLE
376       if (value > 31999 && value < 32768)
377         value = 32768;
378 #endif
379       if (value > MAX_ID_VALUE)
380         value = MIN_ID_VALUE;
381       if (value == initial)
382         {
383           com_err(whoami, 0, "Out of uids!");
384           EXEC SQL ROLLBACK WORK;
385           exit(1);
386         }
387       flag = 0;
388       EXEC SQL SELECT COUNT(unix_uid) INTO :flag
389         FROM users WHERE unix_uid = :value;
390       if (sqlfail())
391         sqlexit();
392     }
393
394   retval = value++;
395   if (value > MAX_ID_VALUE)
396     value = MIN_ID_VALUE;
397   EXEC SQL UPDATE numvalues SET value = :value WHERE name = 'unix_uid';
398   if (sqlca.sqlcode)
399     {
400       dbmserr("assigning ID", sqlca.sqlcode);
401       exit(1);
402     }
403   return retval;
404 }
405
406 void sqlexit(void)
407 {
408   dbmserr(NULL, sqlca.sqlcode);
409   EXEC SQL ROLLBACK WORK;
410   exit(1);
411 }
412
413 void dbmserr(char *where, int what)
414 {
415   char err_msg[256];
416   int bufsize = 256, msglength = 0;
417
418   sqlglm(err_msg, &bufsize, &msglength);
419   err_msg[msglength] = '\0';
420
421   if (where)
422     com_err(whoami, 0, "DBMS error %swhile %s", err_msg, where);
423   else
424     com_err(whoami, 0, "DBMS error %s", err_msg);
425 }
This page took 0.06889 seconds and 3 git commands to generate.