3 * This generates the /usr/lib/aliases mail aliases file for the mailhub.
4 * The aliases file will contain:
7 * sublists of maillists
9 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, please see the file
15 #include <mit-copyright.h>
19 #include <moira_site.h>
20 #include <sys/types.h>
26 #define AL_MAX_WID 896
28 char *divide = "########################################################################";
30 char *whoami = "aliases.gen";
31 char *ingres_date_and_time();
39 char filename[64], *targetfile;
41 ## int flag1, flag2, flag3;
47 initialize_sms_error_table();
49 ## set lockmode session where level = table
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",
67 sprintf(filename, "%s~", targetfile);
68 if ((out = fopen(filename, "w")) == NULL) {
69 fprintf(stderr, "unable to open %s for output\n", filename);
72 } else if (argc != 1) {
73 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
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);
85 fprintf(stderr, "Dumping information\n");
89 fprintf(out, "\n%s\n# End of aliases file\n%s\n", divide, divide);
93 perror("close failed");
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.
108 #define INGRES_DEADLOCK 4700
110 static int ingerr(num)
116 case INGRES_DEADLOCK:
117 ingres_errno = MR_DEADLOCK;
120 ingres_errno = MR_INGRES_ERR;
122 com_err(whoami, MR_INGRES_ERR, " code %d\n", *num);
123 critical_alert("DCM", "Alias build encountered INGRES ERROR %d", *num);
128 struct hash *users, *machines, *strings, *lists;
150 ## int id, maillistp, acl, pid, bid, mid;
151 ## char name[129], type[9], buf[257];
153 register struct user *u;
154 register struct list *l;
155 register struct member *m;
156 register struct list *memberlist;
159 ## retrieve (buf = list.modtime) where list.list_id = 0
160 ## retrieve (buf = users.modtime) where users.users_id = 0
162 fprintf(stderr, "Loading machines\n");
163 machines = create_hash(1000);
164 ## retrieve (id = machine.mach_id, name = machine.#name) {
165 if (s = index(name, '.'))
168 strcat(name, ".LOCAL");
170 hash_store(machines, id, strsave(name));
173 fprintf(stderr, "Loading strings\n");
174 strings = create_hash(2000);
175 ## retrieve (id = strings.string_id, name = strings.string) {
176 hash_store(strings, id, strsave(strtrim(name)));
179 fprintf(stderr, "Loading users\n");
180 users = create_hash(12001);
181 ## range of u is users
182 ## retrieve (id = u.users_id, name = u.login, type = u.potype,
183 ## pid = u.pop_id, bid = u.box_id)
184 ## where u.status = 1 or u.status = 5 or u.status = 6 {
185 u = (struct user *) malloc(sizeof(struct user));
186 strcpy(u->login, strtrim(name));
187 if (type[0] == 'P') {
188 if (s = hash_lookup(machines, pid)) {
189 sprintf(buf, "%s@%s", name, s);
190 u->pobox = strsave(buf);
192 u->pobox = (char *) NULL;
193 fprintf(stderr, "User %s's pobox is on a missing machine!\n",
196 } else if (type[0] == 'S') {
197 if ((u->pobox = hash_lookup(strings, bid)) == NULL) {
198 u->pobox = (char *) NULL;
199 fprintf(stderr, "User %s's pobox string is missing!\n",
203 u->pobox = (char *) NULL;
204 hash_store(users, id, u);
207 fprintf(stderr, "Loading lists\n");
208 lists = create_hash(15001);
209 ## range of l is list
210 ## retrieve (id = l.list_id, name = l.#name, maillistp = l.maillist,
211 ## buf = l.desc, type = l.acl_type, acl = l.acl_id)
212 ## where l.active != 0 {
213 l = (struct list *) malloc(sizeof(struct list));
214 l->name = strsave(strtrim(name));
215 l->maillist = maillistp;
216 l->description = strsave(strtrim(buf));
219 l->m = (struct member *) NULL;
220 hash_store(lists, id, l);
224 fprintf(stderr, "Loading members\n");
225 ## range of m is imembers
226 ## retrieve (id = m.list_id, type = m.member_type, mid = m.member_id)
227 ## where m.direct = 1 {
228 if (l = (struct list *) hash_lookup(lists, id)) {
229 m = (struct member *) malloc(sizeof(struct member));
230 if (type[0] == 'U' &&
231 (u = (struct user *) hash_lookup(users, mid))) {
236 } else if (type[0] == 'L' &&
237 (memberlist = (struct list *) hash_lookup(lists, mid))) {
239 m->name = memberlist->name;
242 } else if (type[0] == 'S' &&
243 (s = hash_lookup(strings, mid))) {
254 save_mlist(id, l, force)
256 register struct list *l;
259 register struct member *m;
262 if (l->maillist == 2 ||
263 (l->maillist == 0 && !force))
266 if (l->m && l->m->next == NULL &&
267 !strcasecmp(l->name, l->m->name)) {
274 if (l->acl_t == 'L' && (l1 = (struct list *)hash_lookup(lists, l->acl_id)))
275 save_mlist(0, l1, 1);
277 for (m = l->m; m; m = m->next) {
278 if (m->list_id && (l1 = (struct list *)hash_lookup(lists, m->list_id)))
279 save_mlist(0, l1, 1);
288 register struct list *l;
291 register struct member *m;
294 put_fill(out, l->description);
295 if (l->acl_t == 'L') {
296 if (l1 = (struct list *) hash_lookup(lists, l->acl_id))
297 fprintf(out, "owner-%s: %s\n", l->name, l1->name);
298 } else if (l->acl_t == 'U') {
299 if (u = (struct user *) hash_lookup(users, l->acl_id))
300 fprintf(out, "owner-%s: %s\n", l->name, u->login);
303 fprintf(out, "%s: ", l->name);
304 lwid = strlen(l->name) + 2;
306 for (m = l->m; m; m = m->next)
307 do_member(out, m->name);
308 if (l->m == (struct member *)NULL)
309 fprintf(out, "/dev/null");
310 fprintf(out, "\n\n");
314 /* Extract mailing lists. Make a list of all mailinglists, then
315 * process them, adding any sub-lists or acl lists to the list of lists
316 * to be processed. If further sublists are encountered, repeat...
322 fprintf(out, "\n%s\n# Mailing lists\n%s\n", divide, divide);
323 hash_step(lists, save_mlist, 0);
327 /* print out strings separated by commas, doing line breaks as appropriate */
338 if (!bol && awid + wwid + 2 > AL_MAX_WID) {
339 fprintf(out, ",\n\tcontinuation-%d\ncontinuation-%d: ", cont, cont);
341 awid = lwid = 17 + wwid;
353 if (lwid + wwid + 2 > ML_WID) {
354 fprintf(out, ",\n\t%s", s);
355 awid += lwid + wwid + 2;
360 fprintf(out, ", %s", s);
365 register struct user *u;
369 fprintf(out, "%s: %s\n", u->login, u->pobox);
373 /* Do user poboxes. Just step through the users table, and print any
374 * we extracted earlier.
380 fprintf(out, "\n%s\n# User Poboxes\n%s\n", divide, divide);
382 hash_step(users, do_pobox, out);
386 put_fill(aliases, string)
388 register char *string;
394 if (*string == 0) return;
395 fputs("# ", aliases);
399 while (*string && *string == ' ') string++;
400 c = (char *)index(string, ' ');
402 wwid = strlen(string);
408 if ((lwid + wwid) > ML_WID) {
409 fputs("\n# ", aliases);
411 fputs(string, aliases);
413 fputs(string, aliases);
416 if (c == (char *)0) break;
417 /* add a space after the word */
418 (void) fputc(' ', aliases);
422 /* add another if after a period */
424 (void) fputc(' ', aliases);
429 (void) fputc('\n', aliases);