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>
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();
40 char filename[64], *targetfile;
42 ## 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)
117 case INGRES_DEADLOCK:
118 ingres_errno = SMS_DEADLOCK;
121 ingres_errno = SMS_INGRES_ERR;
123 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
124 critical_alert("DCM", "Alias build encountered INGRES ERROR %d", *num);
129 struct hash *users, *machines, *strings, *lists;
143 char description[256];
151 ## int id, maillistp, acl, pid, bid, mid;
152 ## char name[129], type[9], buf[257];
154 register struct user *u;
155 register struct list *l;
156 register struct member *m;
157 register struct list *memberlist;
160 ## retrieve (buf = list.modtime) where list.list_id = 0
161 ## retrieve (buf = users.modtime) where users.users_id = 0
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, '.'))
168 sprintf(buf, "%s.LOCAL", name);
169 hash_store(machines, id, strsave(buf));
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)));
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);
191 fprintf(stderr, "User %s's pobox is on a missing machine!\n",
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);
198 hash_store(users, id, u);
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));
213 l->m = (struct member *) NULL;
214 hash_store(lists, id, l);
218 fprintf(stderr, "Loading members\n");
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 {
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') {
227 if (u = (struct user *) hash_lookup(users, mid))
229 } else if (type[0] == 'L') {
231 if (memberlist = (struct list *) hash_lookup(lists, mid))
232 m->name = memberlist->name;
233 } else if (type[0] == 'S') {
235 if (s = hash_lookup(strings, mid))
238 if (m->name != (char *) NULL) {
247 void save_mlist(id, l, sq)
250 struct save_queue *sq;
253 sq_save_unique_data(sq, id);
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...
267 register struct list *l;
269 register struct member *m;
271 register struct save_queue *sq;
272 struct save_queue *sq_create();
276 fprintf(out, "\n%s\n# Mailing lists\n%s\n", divide, divide);
278 hash_step(lists, save_mlist, sq);
280 while (sq_get_data(sq, &id)) {
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 */
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);
290 sq_save_unique_data(sq, l->acl_id);
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);
296 fprintf(out, "%s: ", l->name);
297 lwid = strlen(l->name) + 2;
299 for (m = l->m; m; m = m->next) {
301 sq_save_unique_data(sq, m->list_id);
302 do_member(out, m->name);
304 fprintf(out, "\n\n");
307 /* Removed for speed, since this take 10 minutes to free, and we don't
308 * really need the memory reclaimed.
313 /* print out strings separated by commas, doing line breaks as appropriate */
325 if (!bol && awid + wwid + 2 > AL_MAX_WID) {
326 fprintf(out, ",\n\tcontinuation-%d\ncontinuation-%d: ", cont, cont);
328 awid = lwid = bol = 17;
334 fprintf(out, "%s", s);
338 if (lwid + wwid + 2 > ML_WID) {
339 fprintf(out, ",\n\t%s", s);
340 awid += lwid + wwid + 2;
345 fprintf(out, ", %s", s);
350 register struct user *u;
354 fprintf(out, "%s: %s\n", u->login, u->pobox);
358 /* Do user poboxes. Just step through the users table, and print any
359 * we extracted earlier.
368 fprintf(out, "\n%s\n# User Poboxes\n%s\n", divide, divide);
370 hash_step(users, do_pobox, out);
374 put_fill(aliases, string)
376 register char *string;
382 if (*string == 0) return;
383 fputs("# ", aliases);
387 while (*string && *string == ' ') string++;
388 c = (char *)index(string, ' ');
390 wwid = strlen(string);
396 if ((lwid + wwid) > ML_WID) {
397 fputs("\n# ", aliases);
399 fputs(string, aliases);
401 fputs(string, aliases);
404 if (c == (char *)0) break;
405 /* add a space after the word */
406 (void) fputc(' ', aliases);
410 /* add another if after a period */
412 (void) fputc(' ', aliases);
417 (void) fputc('\n', aliases);