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>
24 EXEC SQL INCLUDE sqlca;
28 char *whoami = "acl.gen";
29 char *db = "moira/moira";
30 char defaultrealm[REALM_SZ];
32 void dump_access_file(FILE *out, int lid);
33 char *merge_access_bits(char *t1, char *t2);
34 void dump_discuss_acl(FILE *out, int lid);
35 void dump_passwd_file(FILE *out, int lid);
36 void dump_group_file(FILE *out, int id);
37 char *merge_discuss_acls(char *one, char *two);
40 int main(int argc, char **argv)
42 EXEC SQL BEGIN DECLARE SECTION;
44 char target[ACL_TARGET_SIZE], kind[ACL_KIND_SIZE];
45 char host[MACHINE_NAME_SIZE];
46 EXEC SQL END DECLARE SECTION;
47 char filename[MAXPATHLEN];
50 struct save_queue *sq;
51 time_t now = time(NULL);
53 EXEC SQL WHENEVER SQLERROR DO sqlerr();
57 EXEC SQL DECLARE csr_mach CURSOR FOR
58 SELECT UNIQUE mach_id FROM acl;
59 EXEC SQL OPEN csr_mach;
62 EXEC SQL FETCH csr_mach INTO :mid;
66 EXEC SQL SELECT name INTO :host FROM machine
72 sprintf(filename, "%s/acl/%s", DCM_DIR, host);
73 tf = tarfile_open(filename);
75 EXEC SQL DECLARE csr_acl CURSOR FOR
76 SELECT target, kind, list_id
79 EXEC SQL OPEN csr_acl;
82 EXEC SQL FETCH csr_acl INTO :target, :kind, :lid;
89 out = tarfile_start(tf, target, 0644, 0, 0, "root", "root", now);
91 if (!strcasecmp(kind, "kerberos4"))
92 dump_krb_acl(out, "LIST", lid, 4);
93 else if (!strcasecmp(kind, "kerberos5"))
94 dump_krb_acl(out, "LIST", lid, 5);
95 else if (!strcasecmp(kind, "access"))
96 dump_access_file(out, lid);
97 else if (!strcasecmp(kind, "discuss"))
98 dump_discuss_acl(out, lid);
99 else if (!strcasecmp(kind, "passwd"))
100 dump_passwd_file(out, lid);
101 else if (!strcasecmp(kind, "group"))
102 dump_group_file(out, lid);
110 EXEC SQL COMMIT RELEASE;
115 void dump_access_file(FILE *out, int lid)
117 struct save_queue *sq = get_acl("LIST", lid, merge_access_bits);
120 while (sq_remove_data(sq, &m))
122 if (m->type == 'U' ||
123 (m->type == 'S' && m->name[0] == '*'))
126 fprintf(out, "%-10s %s\n", m->name, m->tag);
128 fprintf(out, "%-10s rl\n", m->name);
135 char *merge_access_bits(char *t1, char *t2)
137 char *m = NULL, *ans;
140 /* Clear bits that aren't granted by both tags. */
141 if ((*t1 && t1[0] != 'r' && t1[1] != 'r') ||
142 (*t2 && t2[0] != 'r' && t2[1] != 'r'))
144 if ((*t1 && t1[0] != 'l' && t1[1] != 'l') ||
145 (*t2 && t2[0] != 'l' && t2[1] != 'l'))
150 /* Skip to message part of tag 1 */
152 while (*m && !isspace(*m))
157 /* If nothing there, skip to message part of tag 2 */
161 while (*m && !isspace(*m))
167 ans = malloc(4 + strlen(m));
168 sprintf(ans, "%c %s", r ? 'r' : l ? 'l' : '-', m);
178 void dump_discuss_acl(FILE *out, int lid)
180 struct save_queue *sq = get_acl("LIST", lid, merge_discuss_acls);
182 char name[STRINGS_STRING_SIZE], *bits;
186 while (sq_get_data(sq, &m))
189 fprintf(out, "%d\n", num);
190 while (sq_remove_data(sq, &m))
194 canon_krb(m, 4, name, sizeof(name));
195 bits = merge_discuss_acls(m->tag, "");
196 fprintf(out, "%s %s\n", bits, name);
204 char *merge_discuss_acls(char *one, char *two)
208 sprintf(bits, "%c%c%c%c%c%c%c",
209 strchr(one, 'a') || strchr(two, 'a') ? 'a' : ' ',
210 strchr(one, 'c') || strchr(two, 'c') ? 'c' : ' ',
211 strchr(one, 'd') || strchr(two, 'd') ? 'd' : ' ',
212 strchr(one, 'o') || strchr(two, 'o') ? 'o' : ' ',
213 strchr(one, 'r') || strchr(two, 'r') ? 'r' : ' ',
214 strchr(one, 's') || strchr(two, 's') ? 's' : ' ',
215 strchr(one, 'w') || strchr(two, 'w') ? 'w' : ' ');
219 void dump_passwd_file(FILE *out, int lid)
221 struct save_queue *sq = get_acl("LIST", lid, NULL);
223 EXEC SQL BEGIN DECLARE SECTION;
224 char shell[USERS_SHELL_SIZE], fullname[USERS_FULLNAME_SIZE];
225 char nickname[USERS_NICKNAME_SIZE], oa[USERS_OFFICE_ADDR_SIZE];
226 char op[USERS_OFFICE_PHONE_SIZE], hp[USERS_HOME_PHONE_SIZE];
229 EXEC SQL END DECLARE SECTION;
231 while (sq_remove_data(sq, &m))
238 EXEC SQL SELECT unix_uid, shell, fullname, nickname,
239 office_addr, office_phone, home_phone
240 INTO :uid, :shell, :fullname, :nickname, :oa, :op, :hp
242 WHERE login = :name AND status != 3;
253 fprintf(out, "%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\n",
254 name, uid, fullname, nickname, oa, op, hp, name, shell);
262 /* This one is a bit weird since we actually look at top-level
263 * lists rather then flattening them.
265 void dump_group_file(FILE *out, int id)
267 EXEC SQL BEGIN DECLARE SECTION;
268 int lid = id, mid, gid, grouplist;
269 char mtype[IMEMBERS_MEMBER_TYPE_SIZE], name[LIST_NAME_SIZE];
270 EXEC SQL END DECLARE SECTION;
271 struct save_queue *sq;
273 char *maybecomma, *s;
275 EXEC SQL DECLARE csr_grp CURSOR FOR
276 SELECT member_type, member_id FROM imembers
277 WHERE list_id = :lid AND direct = 1;
278 EXEC SQL OPEN csr_grp;
281 EXEC SQL FETCH csr_grp INTO :mtype, :mid;
288 EXEC SQL SELECT name, gid, grouplist
289 INTO :name, :gid, :grouplist
291 WHERE list_id = :mid;
292 if (sqlca.sqlcode || !grouplist)
297 fprintf(out, "%s:*:%d:", name, gid);
298 sq = get_acl("LIST", mid, NULL);
300 while (sq_remove_data(sq, &m))
304 fprintf(out, "%s%s", maybecomma, m->name);
314 EXEC SQL CLOSE csr_grp;
319 db_error(sqlca.sqlcode);