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