]> andersk Git - moira.git/blob - gen/aliases.qc
sms -> moira
[moira.git] / gen / aliases.qc
1 /* $Header$
2  *
3  * This generates the /usr/lib/aliases mail aliases file for the mailhub.
4  * The aliases file will contain:
5  *      user pobox entries
6  *      maillist expansions
7  *      sublists of maillists
8  *
9  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
10  *  For copying and distribution information, please see the file
11  *  <mit-copyright.h>.
12  */
13
14
15 #include <mit-copyright.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <moira.h>
19 #include <moira_site.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23
24
25 #define ML_WID  72
26 #define AL_MAX_WID 896
27
28 char *divide = "########################################################################";
29 extern int errno;
30 char *whoami = "aliases.gen";
31 char *ingres_date_and_time();
32 FILE *out;
33
34 main(argc, argv)
35 int argc;
36 char **argv;
37 {
38     long tm = time(NULL);
39     char filename[64], *targetfile;
40     struct stat sb;
41 ##  int flag1, flag2, flag3;
42 ##  char *filetime;
43     int ingerr();
44
45     out= stdout;
46     IIseterr(ingerr);
47     initialize_sms_error_table();
48 ##  ingres sms
49 ##  set lockmode session where level = table
50
51     if (argc == 2) {
52         if (stat(argv[1], &sb) == 0) {
53             filetime = ingres_date_and_time(sb.st_mtime);
54 ##          retrieve (flag1 = int4(interval("min",tblstats.modtime - filetime)))
55 ##              where tblstats.table = "list"
56 ##          retrieve (flag2 = int4(interval("min",tblstats.modtime - filetime)))
57 ##              where tblstats.table = "imembers"
58 ##          retrieve (flag3 = int4(interval("min",tblstats.modtime - filetime)))
59 ##              where tblstats.table = "users"
60             if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
61                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
62                         argv[1]);
63                 exit(MR_NO_CHANGE);
64             }
65         }
66         targetfile = argv[1];
67         sprintf(filename, "%s~", targetfile);
68         if ((out = fopen(filename, "w")) == NULL) {
69             fprintf(stderr, "unable to open %s for output\n", filename);
70             exit(MR_OCONFIG);
71         }
72     } else if (argc != 1) {
73         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
74         exit(MR_ARGS);
75     }
76
77     fprintf(out, "%s\n# Aliases File Extract of %s", divide, ctime(&tm));
78     fprintf(out, "# This file is automatically generated, do not edit it directly.\n%s\n\n", divide);
79
80 ##  begin transaction
81     get_info();
82 ##  end transaction
83 ##  exit
84
85     fprintf(stderr, "Dumping information\n");
86     do_mlists(out);
87     do_poboxes(out);
88
89     fprintf(out, "\n%s\n# End of aliases file\n%s\n", divide, divide);
90
91
92     if (fclose(out)) {
93         perror("close failed");
94         exit(MR_CCONFIG);
95     }
96
97     if (argc == 2)
98       fix_file(targetfile);
99     exit(MR_SUCCESS);
100 }
101
102
103 /*
104  * ingerr: (supposedly) called when Ingres indicates an error.
105  * I have not yet been able to get this to work to intercept a
106  * database open error.
107  */
108 #define INGRES_DEADLOCK 4700
109
110 static int ingerr(num)
111     int *num;
112 {
113     char buf[256];
114     int ingres_errno;
115
116     switch (*num) {
117     case INGRES_DEADLOCK:
118         ingres_errno = MR_DEADLOCK;
119         break;
120     default:
121         ingres_errno = MR_INGRES_ERR;
122     }
123     com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);
124     critical_alert("DCM", "Alias build encountered INGRES ERROR %d", *num);
125     exit(ingres_errno);
126 }
127
128
129 struct hash *users, *machines, *strings, *lists;
130 struct user {
131     char login[9];
132     char *pobox;
133 };
134 struct member {
135     struct member *next;
136     char *name;
137     int list_id;
138 };
139 struct list {
140     char *name;
141     char maillist;
142     char acl_t;
143     char *description;
144     int acl_id;
145     struct member *m;
146 };
147
148
149 get_info()
150 ##{
151 ##  int id, maillistp, acl, pid, bid, mid;
152 ##  char name[129], type[9], buf[257];
153     char *s;
154     register struct user *u;
155     register struct list *l;
156     register struct member *m;
157     register struct list *memberlist;
158
159     /* get locks */
160 ##  retrieve (buf = list.modtime) where list.list_id = 0
161 ##  retrieve (buf = users.modtime) where users.users_id = 0
162
163     fprintf(stderr, "Loading machines\n");
164     machines = create_hash(1000);
165 ##  retrieve (id = machine.mach_id, name = machine.#name) {
166       if (s = index(name, '.'))
167         *s = 0;
168 #ifdef ATHENA
169       strcat(name, ".LOCAL");
170 #endif
171       hash_store(machines, id, strsave(name));
172 ##  }
173
174     fprintf(stderr, "Loading strings\n");
175     strings = create_hash(2000);
176 ##  retrieve (id = strings.string_id, name = strings.string) {
177         hash_store(strings, id, strsave(strtrim(name)));
178 ##  }
179
180     fprintf(stderr, "Loading users\n");
181     users = create_hash(12001);
182 ##  range of u is users
183 ##  retrieve (id = u.users_id, name = u.login, type = u.potype,
184 ##            pid = u.pop_id, bid = u.box_id) 
185 ##      where u.status = 1 or u.status = 5 or u.status = 6 {
186         u = (struct user *) malloc(sizeof(struct user));
187         strcpy(u->login, strtrim(name));
188         if (type[0] == 'P') {
189             if (s = hash_lookup(machines, pid)) {
190                 sprintf(buf, "%s@%s", name, s);
191                 u->pobox = strsave(buf);
192             } else {
193                 u->pobox = (char *) NULL;
194                 fprintf(stderr, "User %s's pobox is on a missing machine!\n",
195                         u->login);
196             }
197         } else if (type[0] ==  'S') {
198             if ((u->pobox = hash_lookup(strings, bid)) == NULL) {
199                 u->pobox = (char *) NULL;
200                 fprintf(stderr, "User %s's pobox string is missing!\n",
201                         u->login);
202             }
203         } else
204           u->pobox = (char *) NULL;
205         hash_store(users, id, u);
206 ##  }
207
208     fprintf(stderr, "Loading lists\n");
209     lists = create_hash(15001);
210 ##  range of l is list
211 ##  retrieve (id = l.list_id, name = l.#name, maillistp = l.maillist,
212 ##            buf = l.desc, type = l.acl_type, acl = l.acl_id)
213 ##    where l.active != 0 {
214         l = (struct list *) malloc(sizeof(struct list));
215         l->name = strsave(strtrim(name));
216         l->maillist = maillistp;
217         l->description = strsave(strtrim(buf));
218         l->acl_t = type[0];
219         l->acl_id = acl;
220         l->m = (struct member *) NULL;
221         hash_store(lists, id, l);
222 ##  }
223
224
225     fprintf(stderr, "Loading members\n");
226 ##  range of m is imembers
227 ##  retrieve (id = m.list_id, type = m.member_type, mid = m.member_id)
228 ##      where m.direct = 1  {
229         if (l = (struct list *) hash_lookup(lists, id)) {
230             m = (struct member *) malloc(sizeof(struct member));
231             if (type[0] == 'U' &&
232                 (u = (struct user *) hash_lookup(users, mid))) {
233                 m->list_id = 0;
234                 m->name = u->login;
235                 m->next = l->m;
236                 l->m = m;
237             } else if (type[0] == 'L' &&
238                        (memberlist = (struct list *) hash_lookup(lists, mid))) {
239                 m->list_id = mid;
240                 m->name = memberlist->name;
241                 m->next = l->m;
242                 l->m = m;
243             } else if (type[0] == 'S' &&
244                        (s = hash_lookup(strings, mid))) {
245                 m->list_id = 0;
246                 m->name = s;
247                 m->next = l->m;
248                 l->m = m;
249             }
250         }
251 ##  }
252 ##}
253
254
255 save_mlist(id, l, force)
256 int id;
257 register struct list *l;
258 int force;
259 {
260     register struct member *m;
261     struct list *l1;
262
263     if (l->maillist == 2 ||
264         (l->maillist == 0 && !force))
265       return;
266
267     if (l->m && l->m->next == NULL &&
268         !strcasecmp(l->name, l->m->name)) {
269         l->maillist = 0;
270         return;
271     }
272     l->maillist = 2;
273     output_mlist(id, l, out);
274
275     if (l->acl_t == 'L' && (l1 = (struct list *)hash_lookup(lists, l->acl_id)))
276       save_mlist(0, l1, 1);
277     
278     for (m = l->m; m; m = m->next) {
279         if (m->list_id && (l1 = (struct list *)hash_lookup(lists, m->list_id)))
280           save_mlist(0, l1, 0);
281     }
282 }
283
284
285 int lwid, bol, awid;
286
287 output_mlist(id, l, dummy)
288 int id;
289 register struct list *l;
290 int dummy;
291 {
292     struct list *l1;
293     register struct member *m;
294     struct user *u;
295     int id;
296
297     put_fill(out, l->description);
298     if (l->acl_t ==  'L') {
299         if (l1 = (struct list *) hash_lookup(lists, l->acl_id))
300           fprintf(out, "owner-%s: %s\n", l->name, l1->name);
301     } else if (l->acl_t ==  'U') {
302         if (u = (struct user *) hash_lookup(users, l->acl_id))
303           fprintf(out, "owner-%s: %s\n", l->name, u->login);
304     }
305
306     fprintf(out, "%s: ", l->name);
307     lwid = strlen(l->name) + 2;
308     bol = 1;
309     for (m = l->m; m; m = m->next)
310       do_member(out, m->name);
311     if (l->m == (struct member *)NULL)
312       fprintf(out, "/dev/null");
313     fprintf(out, "\n\n");
314 }
315
316
317 /* Extract mailing lists.  Make a list of all mailinglists, then
318  * process them, adding any sub-lists or acl lists to the list of lists
319  * to be processed.  If further sublists are encountered, repeat...
320  */
321
322 do_mlists(out)
323 FILE *out;
324 {
325     fprintf(out, "\n%s\n# Mailing lists\n%s\n", divide, divide);
326     hash_step(lists, save_mlist, 0);
327 }
328
329
330 /* print out strings separated by commas, doing line breaks as appropriate */
331
332 do_member(out, s)
333 FILE *out;
334 register char *s;
335 {
336     register wwid;
337     static int cont = 1;
338
339     wwid = strlen(s);
340
341     if (!bol && awid + wwid + 2 > AL_MAX_WID) {
342         fprintf(out, ",\n\tcontinuation-%d\ncontinuation-%d: ", cont, cont);
343         cont++;
344         awid = lwid = 17 + wwid;
345         fputs(s, out);
346         return;
347     }
348
349     if (bol) {
350         lwid += wwid;
351         awid = lwid;
352         fputs(s, out);
353         bol = 0;
354         return;
355     }
356     if (lwid + wwid + 2 > ML_WID) {
357         fprintf(out, ",\n\t%s", s);
358         awid += lwid + wwid + 2;
359         lwid = wwid + 8;
360         return;
361     }
362     lwid += wwid + 2;
363     fprintf(out, ", %s", s);
364 }
365
366 do_pobox(id, u, out)
367 int id;
368 register struct user *u;
369 FILE *out;
370 {
371     if (u->pobox)
372       fprintf(out, "%s: %s\n", u->login, u->pobox);
373 }
374
375
376 /* Do user poboxes.  Just step through the users table, and print any
377  * we extracted earlier.
378  */
379
380 do_poboxes(out)
381 FILE *out;
382 {
383     register char *p;
384     char *index();
385
386     fprintf(out, "\n%s\n# User Poboxes\n%s\n", divide, divide);
387
388     hash_step(users, do_pobox, out);
389 }
390
391
392 put_fill(aliases, string)
393 FILE *aliases;
394 register char *string;
395 {
396     register char *c;
397     register int lwid;
398     register int wwid;
399
400     if (*string == 0) return;
401     fputs("#  ", aliases);
402     lwid = 3;
403
404     while (1) {
405         while (*string && *string == ' ') string++;
406         c = (char *)index(string, ' ');
407         if (c == 0) {
408             wwid = strlen(string);
409         } else {
410             wwid = c - string;
411             *c = 0;
412         }
413
414         if ((lwid + wwid) > ML_WID) {
415             fputs("\n#  ", aliases);
416             lwid = 3;
417             fputs(string, aliases);
418         } else {
419             fputs(string, aliases);
420         }
421
422         if (c == (char *)0) break;
423         /* add a space after the word */
424         (void) fputc(' ', aliases);
425         wwid++;
426         lwid += wwid;
427         string += wwid;
428         /* add another if after a period */
429         if (*--c == '.') {
430             (void) fputc(' ', aliases);
431             lwid++;
432         }
433     }
434
435     (void) fputc('\n', aliases);
436 }
This page took 0.091546 seconds and 5 git commands to generate.