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