]> andersk Git - moira.git/blame - gen/aliases.qc
use TCP keepalives on the update connection
[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();
67796c83 32
33
34main(argc, argv)
35int argc;
36char **argv;
37{
38 long tm = time(NULL);
39 FILE *out= stdout;
39b94410 40 char filename[64], *targetfile;
5076cbb9 41 struct stat sb;
39b94410 42## int flag1, flag2, flag3;
5076cbb9 43## char *filetime;
335b0c5e 44 int ingerr();
5076cbb9 45
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 {
140 char name[33];
141 char maillist;
142 char acl_t;
143 char description[256];
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;
168 sprintf(buf, "%s.LOCAL", name);
169 hash_store(machines, id, strsave(buf));
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");
179 users = create_hash(15000);
180## range of u is users
181## retrieve (id = u.users_id, name = u.login, type = u.potype,
182## pid = u.pop_id, bid = u.box_id) where u.status = 1 {
183 u = (struct user *) malloc(sizeof(struct user));
184 strcpy(u->login, strtrim(name));
185 u->pobox = (char *) NULL;
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 {
191 fprintf(stderr, "User %s's pobox is on a missing machine!\n",
192 u->login);
193 }
194 } else if (type[0] == 'S') {
195 if ((u->pobox = hash_lookup(strings, bid)) == NULL)
196 fprintf(stderr, "User %s's pobox string is missing!\n", u->login);
197 }
198 hash_store(users, id, u);
199## }
200
201 fprintf(stderr, "Loading lists\n");
202 lists = create_hash(15000);
203## range of l is list
204## retrieve (id = l.list_id, name = l.#name, maillistp = l.maillist,
205## buf = l.desc, type = l.acl_type, acl = l.acl_id)
206## where l.active != 0 {
207 l = (struct list *) malloc(sizeof(struct list));
208 strcpy(l->name, strtrim(name));
209 l->maillist = maillistp;
210 strcpy(l->description, strtrim(buf));
211 l->acl_t = type[0];
212 l->acl_id = acl;
213 l->m = (struct member *) NULL;
214 hash_store(lists, id, l);
215## }
216
217
218 fprintf(stderr, "Loading members\n");
4b6e833d 219## range of m is imembers
220## retrieve (id = m.list_id, type = m.member_type, mid = m.member_id)
221## where m.direct = 1 {
39b94410 222 if (l = (struct list *) hash_lookup(lists, id)) {
223 m = (struct member *) malloc(sizeof(struct member));
224 m->name = (char *) NULL;
225 if (type[0] == 'U') {
226 m->list_id = 0;
227 if (u = (struct user *) hash_lookup(users, mid))
228 m->name = u->login;
229 } else if (type[0] == 'L') {
230 m->list_id = mid;
12116e76 231 if (memberlist = (struct list *) hash_lookup(lists, mid))
232 m->name = memberlist->name;
39b94410 233 } else if (type[0] == 'S') {
234 m->list_id = 0;
235 if (s = hash_lookup(strings, mid))
236 m->name = s;
237 }
238 if (m->name != (char *) NULL) {
239 m->next = l->m;
240 l->m = m;
241 }
242 }
243## }
244##}
245
246
247void save_mlist(id, l, sq)
248int id;
249struct list *l;
250struct save_queue *sq;
251{
252 if (l->maillist)
253 sq_save_unique_data(sq, id);
254}
255
256
39b94410 257/* Extract mailing lists. Make a list of all mailinglists, then
258 * process them, adding any sub-lists or acl lists to the list of lists
259 * to be processed. If further sublists are encountered, repeat...
67796c83 260 */
261
12116e76 262int lwid, bol, awid;
67796c83 263
264do_mlists(out)
265FILE *out;
39b94410 266{
267 register struct list *l;
268 struct list *l1;
269 register struct member *m;
270 struct user *u;
271 register struct save_queue *sq;
272 struct save_queue *sq_create();
273 int id;
67796c83 274
275 sq = sq_create();
276 fprintf(out, "\n%s\n# Mailing lists\n%s\n", divide, divide);
277
39b94410 278 hash_step(lists, save_mlist, sq);
67796c83 279
67796c83 280 while (sq_get_data(sq, &id)) {
39b94410 281 l = (struct list *) hash_lookup(lists, id);
282 if (l->m && /* there's at least one member */
283 l->m->next == NULL && /* there's only one member */
284 !strcmp(l->name, l->m->name)) /* the member is same as list */
285 continue;
286 put_fill(out, l->description);
287 if (l->acl_t == 'L') {
288 if (l1 = (struct list *) hash_lookup(lists, l->acl_id)) {
289 fprintf(out, "owner-%s: %s\n", l->name, l1->name);
12116e76 290 sq_save_unique_data(sq, l->acl_id);
39b94410 291 }
292 } else if (l->acl_t == 'U') {
293 if (u = (struct user *) hash_lookup(users, l->acl_id))
294 fprintf(out, "owner-%s: %s\n", l->name, u->login);
67796c83 295 }
39b94410 296 fprintf(out, "%s: ", l->name);
297 lwid = strlen(l->name) + 2;
67796c83 298 bol = 1;
39b94410 299 for (m = l->m; m; m = m->next) {
12116e76 300 if (m->list_id != 0)
39b94410 301 sq_save_unique_data(sq, m->list_id);
302 do_member(out, m->name);
303 }
67796c83 304 fprintf(out, "\n\n");
305 }
306
12116e76 307/* Removed for speed, since this take 10 minutes to free, and we don't
308 * really need the memory reclaimed.
309 * sq_destroy(sq); */
39b94410 310}
67796c83 311
312
313/* print out strings separated by commas, doing line breaks as appropriate */
314
315do_member(out, s)
316FILE *out;
317register char *s;
318{
319 register wwid;
67796c83 320 static int cont = 1;
321
39b94410 322 strtrim(s);
67796c83 323 wwid = strlen(s);
324
12116e76 325 if (!bol && awid + wwid + 2 > AL_MAX_WID) {
67796c83 326 fprintf(out, ",\n\tcontinuation-%d\ncontinuation-%d: ", cont, cont);
327 cont++;
328 awid = lwid = bol = 17;
329 }
330
331 if (bol) {
332 lwid += wwid;
333 awid = lwid;
334 fprintf(out, "%s", s);
335 bol = 0;
336 return;
337 }
338 if (lwid + wwid + 2 > ML_WID) {
339 fprintf(out, ",\n\t%s", s);
12116e76 340 awid += lwid + wwid + 2;
67796c83 341 lwid = wwid + 8;
342 return;
343 }
344 lwid += wwid + 2;
345 fprintf(out, ", %s", s);
346}
347
39b94410 348do_pobox(id, u, out)
349int id;
350register struct user *u;
351FILE *out;
352{
353 if (u->pobox)
354 fprintf(out, "%s: %s\n", u->login, u->pobox);
355}
356
67796c83 357
39b94410 358/* Do user poboxes. Just step through the users table, and print any
359 * we extracted earlier.
67796c83 360 */
361
362do_poboxes(out)
363FILE *out;
39b94410 364{
365 register char *p;
366 char *index();
67796c83 367
368 fprintf(out, "\n%s\n# User Poboxes\n%s\n", divide, divide);
369
39b94410 370 hash_step(users, do_pobox, out);
371}
67796c83 372
373
374put_fill(aliases, string)
375FILE *aliases;
376register char *string;
377{
378 register char *c;
379 register int lwid;
380 register int wwid;
381
382 if (*string == 0) return;
383 fputs("# ", aliases);
384 lwid = 3;
385
386 while (1) {
387 while (*string && *string == ' ') string++;
388 c = (char *)index(string, ' ');
389 if (c == 0) {
390 wwid = strlen(string);
391 } else {
392 wwid = c - string;
393 *c = 0;
394 }
395
396 if ((lwid + wwid) > ML_WID) {
397 fputs("\n# ", aliases);
398 lwid = 3;
399 fputs(string, aliases);
400 } else {
401 fputs(string, aliases);
402 }
403
404 if (c == (char *)0) break;
405 /* add a space after the word */
406 (void) fputc(' ', aliases);
407 wwid++;
408 lwid += wwid;
409 string += wwid;
410 /* add another if after a period */
411 if (*--c == '.') {
412 (void) fputc(' ', aliases);
413 lwid++;
414 }
415 }
416
417 (void) fputc('\n', aliases);
418}
This page took 0.159003 seconds and 5 git commands to generate.