]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
21c13470 | 2 | * |
3 | * This generates a database extract from the users table for the MIT | |
4 | * Warehouse. | |
5 | * | |
7ac48069 | 6 | * Copyright (C) 1996-1998 by the Massachusetts Institute of Technology. |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
21c13470 | 9 | */ |
10 | ||
11 | #include <mit-copyright.h> | |
21c13470 | 12 | #include <moira.h> |
13 | #include <moira_site.h> | |
7ac48069 | 14 | |
21c13470 | 15 | #include <sys/stat.h> |
7ac48069 | 16 | #include <sys/types.h> |
17 | ||
21c13470 | 18 | #include <fcntl.h> |
7ac48069 | 19 | #include <ctype.h> |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
5eaef520 | 23 | #include <unistd.h> |
7ac48069 | 24 | |
25 | #include <krb.h> | |
26 | #include <gdss.h> | |
27 | ||
28 | #include "util.h" | |
29 | ||
21c13470 | 30 | EXEC SQL INCLUDE sqlca; |
31 | ||
7ac48069 | 32 | RCSID("$Header$"); |
33 | ||
d4bdcedb | 34 | char *whoami = "warehouse.gen"; |
21c13470 | 35 | char *db = "moira/moira"; |
36 | ||
97cd0053 | 37 | #ifndef WAREHOUSE_SUBDIR |
38 | #define WAREHOUSE_SUBDIR "warehouse" | |
39 | #endif | |
40 | ||
dfaf9b68 | 41 | char warehouse_dir[MAXPATHLEN]; |
97cd0053 | 42 | |
5eaef520 | 43 | int main(int argc, char **argv) |
21c13470 | 44 | { |
5eaef520 | 45 | int out = STDOUT_FILENO; |
dfaf9b68 | 46 | char *outf = NULL, outft[MAXPATHLEN]; |
47 | char control_fn[MAXPATHLEN], cmd[256]; | |
5eaef520 | 48 | FILE *f; |
49 | struct stat sb; | |
50 | int flag1; | |
51 | SigInfo si; | |
5eaef520 | 52 | int records = 0; |
53 | EXEC SQL BEGIN DECLARE SECTION; | |
dfaf9b68 | 54 | char login[USERS_LOGIN_SIZE], sig[USERS_SIGNATURE_SIZE]; |
55 | char id[USERS_CLEARID_SIZE], kname[MAX_K_NAME_SZ]; | |
56 | char fname[USERS_FIRST_SIZE], lname[USERS_LAST_SIZE]; | |
57 | char middle[USERS_MIDDLE_SIZE]; | |
58 | EXEC SQL VAR sig IS STRING(USERS_SIGNATURE_SIZE); | |
5eaef520 | 59 | int timestamp, sigwho; |
60 | struct { | |
61 | char login[12]; | |
62 | char id[12]; | |
63 | char fname[20]; | |
64 | char lname[20]; | |
65 | char middle[20]; | |
66 | char sig[256]; | |
67 | } outrec; | |
68 | EXEC SQL END DECLARE SECTION; | |
69 | ||
70 | initialize_sms_error_table(); | |
71 | ||
72 | sprintf(warehouse_dir, "%s/%s", DCM_DIR, WAREHOUSE_SUBDIR); | |
73 | ||
74 | EXEC SQL CONNECT :db; | |
75 | ||
76 | if (argc == 2) | |
77 | { | |
78 | if (stat(argv[1], &sb) == 0) | |
79 | { | |
80 | if (ModDiff(&flag1, "users", sb.st_mtime) == 0 && flag1 < 0) | |
81 | { | |
82 | fprintf(stderr, "File %s does not need to be rebuilt.\n", | |
83 | argv[1]); | |
84 | exit(MR_NO_CHANGE); | |
21c13470 | 85 | } |
86 | } | |
5eaef520 | 87 | outf = argv[1]; |
88 | sprintf(outft, "%s/username_id.map", warehouse_dir); | |
89 | if ((out = open(outft, O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0666)) < 0) | |
90 | { | |
91 | fprintf(stderr, "unable to open %s for output\n", outf); | |
92 | exit(MR_OCONFIG); | |
21c13470 | 93 | } |
21c13470 | 94 | } |
5eaef520 | 95 | else if (argc != 1) |
96 | { | |
97 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
98 | exit(MR_ARGS); | |
99 | } | |
100 | else | |
101 | outf = NULL; | |
102 | ||
103 | /* The following is declarative, not executed, | |
104 | * and so is dependent on where it is in the file, | |
105 | * not in the order of execution of statements. | |
106 | */ | |
107 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
21c13470 | 108 | |
5eaef520 | 109 | EXEC SQL DECLARE x CURSOR FOR |
110 | SELECT u.login, u.clearid, u.signature, u.sigdate, s.string, u.sigwho, | |
111 | u.first, u.last, u.middle | |
112 | FROM users u, strings s | |
113 | WHERE (u.status = 1 or u.status = 2 or u.status = 5 OR | |
114 | u.status = 6 or u.status = 7) | |
115 | AND u.sigwho = s.string_id; | |
116 | EXEC SQL OPEN x; | |
117 | while (1) | |
118 | { | |
119 | EXEC SQL FETCH x INTO :login, :id, :sig, :timestamp, :kname, :sigwho, | |
120 | :fname, :lname, :middle; | |
121 | if (sqlca.sqlcode) | |
122 | break; | |
123 | if (id == 0) | |
124 | continue; | |
125 | if (!isdigit(id[1])) | |
126 | continue; | |
127 | strtrim(login); | |
128 | strtrim(id); | |
129 | strtrim(kname); | |
130 | strtrim(fname); | |
131 | strtrim(lname); | |
132 | strtrim(middle); | |
133 | memset(&outrec, 0, sizeof(outrec)); | |
134 | strcpy(outrec.login, login); | |
135 | strcpy(outrec.id, id); | |
136 | strcpy(outrec.fname, fname); | |
137 | strcpy(outrec.lname, lname); | |
138 | strcpy(outrec.middle, middle); | |
139 | if (sigwho) | |
140 | { | |
141 | si.timestamp = timestamp; | |
142 | si.SigInfoVersion = 0; /* XXXXXX this isn't used */ | |
143 | kname_parse(si.pname, si.pinst, si.prealm, kname); | |
144 | si.rawsig = (unsigned char *)sig; | |
145 | GDSS_Recompose(&si, outrec.sig); | |
21c13470 | 146 | } |
5eaef520 | 147 | write(out, &outrec, sizeof(outrec)); |
148 | records++; | |
21c13470 | 149 | } |
5eaef520 | 150 | EXEC SQL CLOSE x; |
21c13470 | 151 | |
5eaef520 | 152 | EXEC SQL COMMIT; |
21c13470 | 153 | |
5eaef520 | 154 | if (close(out)) |
155 | { | |
156 | perror("close failed"); | |
157 | exit(MR_CCONFIG); | |
21c13470 | 158 | } |
5eaef520 | 159 | sprintf(control_fn, "%s/username_id.ctl", warehouse_dir); |
97cd0053 | 160 | |
5eaef520 | 161 | f = fopen(control_fn, "w"); |
162 | if (!f) | |
163 | { | |
164 | perror("creating username_id control file"); | |
165 | exit(MR_OCONFIG); | |
97cd0053 | 166 | } |
5eaef520 | 167 | fprintf(f, "username_id.map --- Moira feed for MIT Data Warehouse\n"); |
168 | fprintf(f, "%d records\n", records); | |
169 | fprintf(f, "%d bytes\n", records * (12 + 12 + 20 + 20 + 20 + 256)); | |
170 | fprintf(f, "Full feed\n"); | |
171 | fprintf(f, "Fixed format -- binary\n"); | |
172 | fclose(f); | |
173 | if (outf) | |
174 | { | |
175 | fprintf(stderr, "Building tar file.\n"); | |
176 | sprintf(cmd, "(cd %s; tar cf - . ) | compress > %s", | |
177 | warehouse_dir, outf); | |
178 | if (system(cmd)) | |
179 | exit(MR_TAR_FAIL); | |
97cd0053 | 180 | } |
21c13470 | 181 | |
5eaef520 | 182 | exit(MR_SUCCESS); |
183 | ||
184 | sqlerr: | |
185 | db_error(sqlca.sqlcode); | |
186 | exit(MR_DBMS_ERR); | |
21c13470 | 187 | } |