3 * This generates acl files for various servers.
5 * Copyright (C) 1999 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
12 #include <moira_site.h>
16 #include <sys/types.h>
26 EXEC SQL INCLUDE sqlca;
30 char *whoami = "acl.gen";
31 char *db = "moira/moira";
33 void dump_access_file(FILE *out, int lid);
34 char *merge_access_bits(char *t1, char *t2);
35 void dump_discuss_acl(FILE *out, int lid);
36 void dump_passwd_file(FILE *out, int lid);
37 void dump_group_file(FILE *out, int id);
38 char *merge_discuss_acls(char *one, char *two);
41 int main(int argc, char **argv)
43 EXEC SQL BEGIN DECLARE SECTION;
44 int mid, lid, discuss_uid = -1;
45 char target[ACL_TARGET_SIZE], kind[ACL_KIND_SIZE];
46 char host[MACHINE_NAME_SIZE];
47 EXEC SQL END DECLARE SECTION;
48 char filename[MAXPATHLEN];
51 struct save_queue *sq;
52 time_t now = time(NULL);
54 EXEC SQL WHENEVER SQLERROR DO sqlerr();
58 EXEC SQL DECLARE csr_mach CURSOR FOR
59 SELECT UNIQUE mach_id FROM acl;
60 EXEC SQL OPEN csr_mach;
63 EXEC SQL FETCH csr_mach INTO :mid;
67 EXEC SQL SELECT name INTO :host FROM machine
73 sprintf(filename, "%s/acl/%s", DCM_DIR, host);
74 tf = tarfile_open(filename);
76 EXEC SQL DECLARE csr_acl CURSOR FOR
77 SELECT target, kind, list_id
80 EXEC SQL OPEN csr_acl;
83 EXEC SQL FETCH csr_acl INTO :target, :kind, :lid;
90 if (!strcasecmp(kind, "discuss"))
92 /* Discuss acls need to be owned by discuss. */
93 if (discuss_uid == -1)
95 EXEC SQL SELECT unix_uid INTO :discuss_uid
96 FROM users WHERE login = 'discuss';
98 out = tarfile_start(tf, target, 0644, discuss_uid, 1,
99 "discuss", "daemon", now);
100 dump_discuss_acl(out, lid);
104 /* Otherwise own by root? Perhaps the acl table should
107 out = tarfile_start(tf, target, 0644, 0, 0, "root", "root", now);
109 if (!strcasecmp(kind, "kerberos4"))
110 dump_krb_acl(out, "LIST", lid, 4);
111 else if (!strcasecmp(kind, "kerberos5"))
112 dump_krb_acl(out, "LIST", lid, 5);
113 else if (!strcasecmp(kind, "access"))
114 dump_access_file(out, lid);
115 else if (!strcasecmp(kind, "passwd"))
116 dump_passwd_file(out, lid);
117 else if (!strcasecmp(kind, "group"))
118 dump_group_file(out, lid);
119 else if (!strcasecmp(kind, "userlist"))
120 dump_user_list(out, "LIST", lid);
129 EXEC SQL COMMIT RELEASE;
134 void dump_access_file(FILE *out, int lid)
136 struct save_queue *sq = get_acl("LIST", lid, merge_access_bits);
138 char *name, *lasts = NULL;
141 while (sq_remove_data(sq, &m))
143 if (m->type == 'U' ||
144 (m->type == 'S' && m->name[0] == '*'))
147 fprintf(out, "%-10s %s\n", m->name, m->tag);
149 fprintf(out, "%-10s rl\n", m->name);
151 else if (m->type == 'K')
153 name = strtok_r(m->name, "@", &lasts);
154 EXEC SQL SELECT count(login) INTO :i FROM users
155 WHERE login = :name and status != 3;
159 fprintf(out, "%-10s %s\n", m->name, m->tag);
161 fprintf(out, "%-10s rl\n", m->name);
169 char *merge_access_bits(char *t1, char *t2)
171 char *m = NULL, *ans;
174 /* Clear bits that aren't granted by both tags. */
175 if ((*t1 && t1[0] != 'r' && t1[1] != 'r') ||
176 (*t2 && t2[0] != 'r' && t2[1] != 'r'))
178 if ((*t1 && t1[0] != 'l' && t1[1] != 'l') ||
179 (*t2 && t2[0] != 'l' && t2[1] != 'l'))
184 /* Skip to message part of tag 1 */
186 while (*m && !isspace(*m))
191 /* If nothing there, skip to message part of tag 2 */
195 while (*m && !isspace(*m))
201 ans = malloc(4 + strlen(m));
202 sprintf(ans, "%c %s", r ? 'r' : l ? 'l' : '-', m);
212 void dump_discuss_acl(FILE *out, int lid)
214 struct save_queue *sq = get_acl("LIST", lid, merge_discuss_acls);
216 char name[STRINGS_STRING_SIZE], *bits;
217 char starbits[8] = { 0 };
221 while (sq_get_data(sq, &m))
223 if (m->type != 'S' || !strcmp(m->name, "*"))
227 fprintf(out, "%d\n", num);
228 while (sq_remove_data(sq, &m))
230 bits = merge_discuss_acls(m->tag, "");
233 canon_krb(m, 4, name, sizeof(name));
234 fprintf(out, "%s:%s\n", bits, name);
236 else if (!strcmp(m->name, "*"))
237 strcpy(starbits, bits);
243 /* Discuss ACLs are ordered, so "*" must come last. */
245 fprintf(out, "%s:*\n", starbits);
248 char *merge_discuss_acls(char *one, char *two)
252 sprintf(bits, "%c%c%c%c%c%c%c",
253 strchr(one, 'a') || strchr(two, 'a') ? 'a' : ' ',
254 strchr(one, 'c') || strchr(two, 'c') ? 'c' : ' ',
255 strchr(one, 'd') || strchr(two, 'd') ? 'd' : ' ',
256 strchr(one, 'o') || strchr(two, 'o') ? 'o' : ' ',
257 strchr(one, 'r') || strchr(two, 'r') ? 'r' : ' ',
258 strchr(one, 's') || strchr(two, 's') ? 's' : ' ',
259 strchr(one, 'w') || strchr(two, 'w') ? 'w' : ' ');
263 void dump_passwd_file(FILE *out, int lid)
265 struct save_queue *sq = get_acl("LIST", lid, NULL);
267 EXEC SQL BEGIN DECLARE SECTION;
268 char shell[USERS_SHELL_SIZE], fullname[USERS_FULLNAME_SIZE];
269 char nickname[USERS_NICKNAME_SIZE], oa[USERS_OFFICE_ADDR_SIZE];
270 char op[USERS_OFFICE_PHONE_SIZE], hp[USERS_HOME_PHONE_SIZE];
272 char *name, *n, *lasts = NULL;
273 EXEC SQL END DECLARE SECTION;
275 while (sq_remove_data(sq, &m))
282 EXEC SQL SELECT unix_uid, shell, fullname, nickname,
283 office_addr, office_phone, home_phone
284 INTO :uid, :shell, :fullname, :nickname, :oa, :op, :hp
286 WHERE login = :name AND status != 3;
297 fprintf(out, "%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\n",
298 name, uid, fullname, nickname, oa, op, hp, name, shell);
302 name = strtok_r(m->name, "@", &lasts);
304 EXEC SQL SELECT count(login) INTO :i FROM users WHERE
305 login = :name and status != 3;
308 EXEC SQL SELECT unix_uid, shell, fullname, nickname,
309 office_addr, office_phone, home_phone
310 INTO :uid, :shell, :fullname, :nickname, :oa, :op, :hp
312 WHERE login = :name AND status != 3;
323 fprintf(out, "%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\n",
324 name, uid, fullname, nickname, oa, op, hp, name, shell);
333 /* This one is a bit weird since we actually look at top-level
334 * lists rather then flattening them.
336 void dump_group_file(FILE *out, int id)
338 EXEC SQL BEGIN DECLARE SECTION;
339 int lid = id, mid, gid, grouplist, i = 0;
340 char mtype[IMEMBERS_MEMBER_TYPE_SIZE], name[LIST_NAME_SIZE];
341 EXEC SQL END DECLARE SECTION;
342 struct save_queue *sq;
344 char *maybecomma, *s, *n, *lasts = NULL;
346 EXEC SQL DECLARE csr_grp CURSOR FOR
347 SELECT member_type, member_id FROM imembers
348 WHERE list_id = :lid AND direct = 1;
349 EXEC SQL OPEN csr_grp;
352 EXEC SQL FETCH csr_grp INTO :mtype, :mid;
359 EXEC SQL SELECT name, gid, grouplist
360 INTO :name, :gid, :grouplist
362 WHERE list_id = :mid;
363 if (sqlca.sqlcode || !grouplist)
368 fprintf(out, "%s:*:%d:", name, gid);
369 sq = get_acl("LIST", mid, NULL);
371 while (sq_remove_data(sq, &m))
375 fprintf(out, "%s%s", maybecomma, m->name);
378 else if (m->type == 'K')
380 n = strtok_r(m->name, "@", &lasts);
381 EXEC SQL SELECT count(login) INTO :i FROM users
382 WHERE login = :n and status != 3;
385 fprintf(out, "%s%s", maybecomma, n);
396 EXEC SQL CLOSE csr_grp;
401 db_error(sqlca.sqlcode);