[24319] | 1 | /* $Id: events.pc 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
[23095] | 2 | * |
---|
| 3 | * (c) Copyright 2007 by the Massachusetts Institute of Technology. |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | #include <mit-copyright.h> |
---|
| 7 | #include <moira.h> |
---|
| 8 | #include <moira_site.h> |
---|
| 9 | |
---|
| 10 | #include <sys/stat.h> |
---|
| 11 | |
---|
| 12 | #include <ctype.h> |
---|
| 13 | #include <stdio.h> |
---|
| 14 | #include <stdlib.h> |
---|
| 15 | #include <string.h> |
---|
| 16 | |
---|
| 17 | #include "util.h" |
---|
| 18 | |
---|
| 19 | EXEC SQL INCLUDE sqlca; |
---|
| 20 | |
---|
| 21 | #define DEFAULT_REALM "@ATHENA.MIT.EDU" |
---|
| 22 | |
---|
| 23 | char *whoami = "events.gen"; |
---|
| 24 | char *db = "moira/moira"; |
---|
| 25 | |
---|
| 26 | struct hash *lists; |
---|
| 27 | |
---|
| 28 | void output_list(int id, void *list, void *out); |
---|
| 29 | |
---|
| 30 | int main(int argc, char **argv) |
---|
| 31 | { |
---|
| 32 | char filename[MAXPATHLEN], *targetfile, *l; |
---|
| 33 | FILE *out = stdout; |
---|
| 34 | int cnt = 0; |
---|
| 35 | EXEC SQL BEGIN DECLARE SECTION; |
---|
| 36 | int lid; |
---|
| 37 | char lname[LIST_NAME_SIZE]; |
---|
| 38 | EXEC SQL END DECLARE SECTION; |
---|
| 39 | |
---|
| 40 | EXEC SQL CONNECT :db; |
---|
| 41 | |
---|
| 42 | if (argc == 2) |
---|
| 43 | { |
---|
| 44 | targetfile = argv[1]; |
---|
| 45 | sprintf(filename, "%s~", targetfile); |
---|
| 46 | if (!(out = fopen(filename, "w"))) |
---|
| 47 | { |
---|
| 48 | fprintf(stderr, "unable to open %s for output\n", filename); |
---|
| 49 | exit(MR_OCONFIG); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | else if (argc != 1) |
---|
| 53 | { |
---|
| 54 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); |
---|
| 55 | exit(MR_ARGS); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | lists = create_hash(15000); |
---|
| 59 | |
---|
| 60 | EXEC SQL DECLARE l_cursor CURSOR FOR |
---|
| 61 | SELECT l.list_id, l.name FROM list l |
---|
| 62 | WHERE l.active = 1 and l.maillist = 1; |
---|
| 63 | EXEC SQL OPEN l_cursor; |
---|
| 64 | while (1) |
---|
| 65 | { |
---|
| 66 | EXEC SQL FETCH l_cursor INTO :lid, :lname; |
---|
| 67 | if (sqlca.sqlcode) |
---|
| 68 | break; |
---|
| 69 | l = strdup(strtrim(lname)); |
---|
| 70 | if (hash_store(lists, lid, l) < 0) |
---|
| 71 | { |
---|
| 72 | fprintf(stderr, "Out of memory!\n"); |
---|
| 73 | exit(MR_NO_MEM); |
---|
| 74 | } |
---|
| 75 | cnt++; |
---|
| 76 | } |
---|
| 77 | EXEC SQL CLOSE l_cursor; |
---|
| 78 | fprintf(stderr, "Loaded %d lists\n", cnt); |
---|
| 79 | |
---|
| 80 | hash_step(lists, output_list, out); |
---|
| 81 | |
---|
| 82 | if (fclose(out)) |
---|
| 83 | { |
---|
| 84 | perror("close failed"); |
---|
| 85 | exit(MR_CCONFIG); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | if (argc == 2) |
---|
| 89 | fix_file(targetfile); |
---|
| 90 | exit(MR_SUCCESS); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void output_list(int id, void *list, void *out) |
---|
| 94 | { |
---|
| 95 | EXEC SQL BEGIN DECLARE SECTION; |
---|
| 96 | char *l = list; |
---|
| 97 | int lid = id; |
---|
| 98 | char login[USERS_LOGIN_SIZE]; |
---|
| 99 | char principal[STRINGS_STRING_SIZE]; |
---|
| 100 | EXEC SQL END DECLARE SECTION; |
---|
| 101 | char *maybecomma = ""; |
---|
| 102 | |
---|
| 103 | fprintf(out, "%s:", list); |
---|
| 104 | |
---|
| 105 | EXEC SQL DECLARE u_cursor CURSOR FOR |
---|
| 106 | SELECT UNIQUE u.login FROM users u, imembers i, list l |
---|
| 107 | WHERE l.list_id = :lid AND l.list_id = i.list_id AND |
---|
| 108 | i.member_type = 'USER' AND i.member_id = u.users_id; |
---|
| 109 | EXEC SQL OPEN u_cursor; |
---|
| 110 | while (1) |
---|
| 111 | { |
---|
| 112 | EXEC SQL FETCH u_cursor INTO :login; |
---|
| 113 | if (sqlca.sqlcode) |
---|
| 114 | break; |
---|
| 115 | fprintf(out, "%s%s", maybecomma, strtrim(login)); |
---|
| 116 | maybecomma = ","; |
---|
| 117 | } |
---|
| 118 | EXEC SQL CLOSE u_cursor; |
---|
| 119 | |
---|
| 120 | EXEC SQL DECLARE k_cursor CURSOR FOR |
---|
| 121 | SELECT UNIQUE s.string FROM strings s, imembers i, list l |
---|
| 122 | WHERE l.list_id = :lid AND l.list_id = i.list_id AND |
---|
| 123 | i.member_type = 'KERBEROS' AND i.member_id = s.string_id; |
---|
| 124 | EXEC SQL OPEN k_cursor; |
---|
| 125 | while (1) |
---|
| 126 | { |
---|
| 127 | EXEC SQL FETCH k_cursor INTO :principal; |
---|
| 128 | if (sqlca.sqlcode) |
---|
| 129 | break; |
---|
| 130 | if (strstr(principal, DEFAULT_REALM)) |
---|
| 131 | { |
---|
| 132 | *strstr(principal, DEFAULT_REALM) = '\0'; |
---|
| 133 | fprintf(out, "%s%s", maybecomma, strtrim(principal)); |
---|
| 134 | maybecomma = ","; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | EXEC SQL CLOSE k_cursor; |
---|
| 138 | |
---|
| 139 | fprintf(out, "\n"); |
---|
| 140 | } |
---|