1 | /* $Id: postoffice.pc,v 1.3 2009-12-21 16:35:38 zacheiss Exp $ |
---|
2 | * |
---|
3 | * This generates the post office location data. |
---|
4 | * |
---|
5 | * tom@mit.edu |
---|
6 | * |
---|
7 | * Copyright 1998 by the Massachusetts Institute of Technology. |
---|
8 | * For copying and distribution information, please see the file |
---|
9 | * <mit-copyright.h>. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <mit-copyright.h> |
---|
13 | #include <moira.h> |
---|
14 | #include <moira_site.h> |
---|
15 | #include <sys/stat.h> |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <time.h> |
---|
19 | #include <errno.h> |
---|
20 | #include "util.h" |
---|
21 | |
---|
22 | EXEC SQL INCLUDE sqlca; |
---|
23 | |
---|
24 | RCSID("$Header: /afs/.athena.mit.edu/astaff/project/moiradev/repository/moira/gen/postoffice.pc,v 1.3 2009-12-21 16:35:38 zacheiss Exp $"); |
---|
25 | |
---|
26 | |
---|
27 | #ifndef PO_SUBDIR |
---|
28 | #define PO_SUBDIR "postoffice" |
---|
29 | #endif |
---|
30 | |
---|
31 | #define MAX_RECSIZE 1000 |
---|
32 | |
---|
33 | char *whoami = "postoffice.gen"; |
---|
34 | char *db = "moira/moira"; |
---|
35 | |
---|
36 | void pobox(char *); |
---|
37 | |
---|
38 | |
---|
39 | int main(int argc, char **argv) |
---|
40 | { |
---|
41 | char cmd[BUFSIZ]; |
---|
42 | char po_dir[MAXPATHLEN], wpo_dir[MAXPATHLEN]; |
---|
43 | struct stat sb; |
---|
44 | char *c; |
---|
45 | |
---|
46 | initialize_sms_error_table(); |
---|
47 | |
---|
48 | if (argc == 1) |
---|
49 | sprintf(po_dir, "%s/%s", DCM_DIR, PO_SUBDIR); |
---|
50 | else |
---|
51 | { |
---|
52 | sprintf(po_dir, "%s", argv[1]); |
---|
53 | /* fake out dcm - we'll append .out later */ |
---|
54 | if (c = strrchr(po_dir, '.')) |
---|
55 | *c = '\0'; |
---|
56 | } |
---|
57 | sprintf(wpo_dir, "%s~", po_dir); |
---|
58 | |
---|
59 | if (argc > 2) |
---|
60 | { |
---|
61 | fprintf(stderr, "usage: %s [outdir]\n", argv[0]); |
---|
62 | exit(MR_ARGS); |
---|
63 | } |
---|
64 | |
---|
65 | if (stat(wpo_dir, &sb) < 0) |
---|
66 | { |
---|
67 | if (errno == ENOENT) |
---|
68 | { |
---|
69 | if (mkdir(wpo_dir, 0700) < 0) |
---|
70 | { |
---|
71 | fprintf(stderr, "%s: unable to make directory %s (%s))\n", |
---|
72 | whoami, wpo_dir, strerror(errno)); |
---|
73 | exit(MR_CCONFIG); |
---|
74 | } |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, wpo_dir, |
---|
79 | strerror(errno)); |
---|
80 | exit(MR_CCONFIG); |
---|
81 | } |
---|
82 | } |
---|
83 | else if (!S_ISDIR(sb.st_mode)) |
---|
84 | { |
---|
85 | fprintf(stderr, "%s: %s not a directory\n" , whoami, wpo_dir); |
---|
86 | exit(MR_CCONFIG); |
---|
87 | } |
---|
88 | |
---|
89 | EXEC SQL CONNECT :db; |
---|
90 | |
---|
91 | fprintf(stderr, "%s: building po databases...\n", whoami); |
---|
92 | pobox(wpo_dir); |
---|
93 | |
---|
94 | if (stat(po_dir, &sb) != -1) |
---|
95 | { |
---|
96 | fprintf(stderr, "%s: removing %s\n", whoami, po_dir); |
---|
97 | sprintf(cmd, "rm -r %s", po_dir); |
---|
98 | if (strncmp(po_dir, wpo_dir, strlen(po_dir))) |
---|
99 | exit(MR_CCONFIG); |
---|
100 | if (system(cmd)) |
---|
101 | { |
---|
102 | fprintf(stderr, "%s: unable to remove %s (%s)", whoami, po_dir, |
---|
103 | strerror(errno)); |
---|
104 | exit(MR_CCONFIG); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | rename(wpo_dir, po_dir); |
---|
109 | |
---|
110 | fprintf(stderr, "%s: building tar file...\n", whoami); |
---|
111 | sprintf(cmd, "(cd %s; tar cf - . ) > %s.out", po_dir, po_dir); |
---|
112 | if (system(cmd)) |
---|
113 | exit(MR_TAR_FAIL); |
---|
114 | |
---|
115 | exit(MR_SUCCESS); |
---|
116 | } |
---|
117 | |
---|
118 | void pobox(char *path) |
---|
119 | { |
---|
120 | char *c; |
---|
121 | char mdir[MAXPATHLEN]; |
---|
122 | char mout[MAXPATHLEN], qout[MAXPATHLEN]; |
---|
123 | struct stat sb; |
---|
124 | FILE *moutp, *qoutp; |
---|
125 | |
---|
126 | EXEC SQL BEGIN DECLARE SECTION; |
---|
127 | char machine[MACHINE_NAME_SIZE]; |
---|
128 | char label[FILESYS_LABEL_SIZE], type[FILESYS_TYPE_SIZE]; |
---|
129 | char partition[NFSPHYS_DIR_SIZE]; |
---|
130 | int mid, quota; |
---|
131 | EXEC SQL END DECLARE SECTION; |
---|
132 | |
---|
133 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
134 | |
---|
135 | EXEC SQL DECLARE po_curses CURSOR FOR |
---|
136 | SELECT m.name, m.mach_id from machine m, serverhosts s |
---|
137 | WHERE s.service='POSTOFFICE' AND s.mach_id=m.mach_id |
---|
138 | ORDER by m.name; |
---|
139 | EXEC SQL OPEN po_curses; |
---|
140 | |
---|
141 | while (1) |
---|
142 | { |
---|
143 | EXEC SQL FETCH po_curses INTO :machine, :mid; |
---|
144 | if (sqlca.sqlcode) |
---|
145 | break; |
---|
146 | if (!*strtrim(machine)) |
---|
147 | continue; |
---|
148 | |
---|
149 | sprintf(mdir, "%s/%s", path, machine); |
---|
150 | if (stat(mdir, &sb) < 0) |
---|
151 | { |
---|
152 | if (errno == ENOENT) |
---|
153 | { |
---|
154 | if (mkdir(mdir, 0700) < 0) |
---|
155 | { |
---|
156 | fprintf(stderr, "%s: unable to make directory %s (%s)\n", |
---|
157 | whoami, mdir, strerror(errno)); |
---|
158 | exit(MR_CCONFIG); |
---|
159 | } |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | fprintf(stderr, "%s: cannot stat %s (%s)\n", whoami, mdir, |
---|
164 | strerror(errno)); |
---|
165 | exit(MR_CCONFIG); |
---|
166 | } |
---|
167 | } |
---|
168 | sprintf(mout, "%s/mailboxes", mdir); |
---|
169 | sprintf(qout, "%s/quota", mdir); |
---|
170 | if (!(moutp = fopen(mout, "w"))) |
---|
171 | { |
---|
172 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, |
---|
173 | mout, strerror(errno)); |
---|
174 | exit(MR_OCONFIG); |
---|
175 | } |
---|
176 | |
---|
177 | if (!(qoutp = fopen(qout, "w"))) |
---|
178 | { |
---|
179 | fprintf(stderr, "%s: cannot open %s for writing (%s)\n", whoami, |
---|
180 | qout, strerror(errno)); |
---|
181 | exit(MR_OCONFIG); |
---|
182 | } |
---|
183 | |
---|
184 | EXEC SQL DECLARE filsys_cursor CURSOR FOR |
---|
185 | SELECT f.label, f.lockertype, p.dir, q.quota |
---|
186 | FROM filesys f, nfsphys p, quota q, users u |
---|
187 | WHERE f.mach_id=:mid AND f.type='IMAP' AND f.createflg!=0 |
---|
188 | AND f.filsys_id=q.filsys_id |
---|
189 | AND f.mach_id=p.mach_id AND f.phys_id=p.nfsphys_id |
---|
190 | AND f.owner = u.users_id AND u.status != 3 |
---|
191 | ORDER BY f.label, p.dir, q.quota; |
---|
192 | EXEC SQL OPEN filsys_cursor; |
---|
193 | |
---|
194 | while (1) |
---|
195 | { |
---|
196 | EXEC SQL FETCH filsys_cursor INTO :label, :type, :partition, :quota; |
---|
197 | if (sqlca.sqlcode) |
---|
198 | break; |
---|
199 | if (!*strtrim(label)) |
---|
200 | continue; |
---|
201 | if (!*strtrim(type)) |
---|
202 | continue; |
---|
203 | if (!*strtrim(partition)) |
---|
204 | continue; |
---|
205 | |
---|
206 | if (!strncmp(&label[strlen(label)-3], ".po", 3)) |
---|
207 | label[strlen(label)-3] = '\0'; |
---|
208 | lowercase(label); |
---|
209 | lowercase(type); |
---|
210 | lowercase(partition); |
---|
211 | |
---|
212 | fprintf(moutp, "%s.%s\t%s\t%s\t%s\n", type, label, partition, |
---|
213 | label, "lrswipcda"); |
---|
214 | fprintf(qoutp, "%s.%s\t%d\n", type, label, quota); |
---|
215 | } |
---|
216 | |
---|
217 | EXEC SQL CLOSE filsys_cursor; |
---|
218 | EXEC SQL COMMIT; |
---|
219 | |
---|
220 | if (fclose(moutp)) |
---|
221 | { |
---|
222 | fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami, |
---|
223 | mout, strerror(errno)); |
---|
224 | exit(MR_CCONFIG); |
---|
225 | } |
---|
226 | |
---|
227 | if (fclose(qoutp)) |
---|
228 | { |
---|
229 | fprintf(stderr, "%s: unable to close file %s (%s)\n", whoami, |
---|
230 | qout, strerror(errno)); |
---|
231 | exit(MR_CCONFIG); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | EXEC SQL CLOSE po_curses; |
---|
236 | EXEC SQL COMMIT; |
---|
237 | return; |
---|
238 | |
---|
239 | sqlerr: |
---|
240 | db_error(sqlca.sqlcode); |
---|
241 | exit(MR_DBMS_ERR); |
---|
242 | } |
---|