3 * Utility functions for outputting ACLs
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>
21 EXEC SQL INCLUDE sqlca;
25 static char defaultrealm[REALM_SZ];
27 static struct hash *users, *strings;
29 static void save_imember(struct save_queue *sq, char *type, int id, char *tag);
30 static struct imember *imember(char type, char *name, char *tag);
31 static struct save_queue *merge_imembers(struct save_queue *sq,
32 char *(merge_func)(char *, char *));
36 users = create_hash(2000);
37 strings = create_hash(2000);
38 krb_get_lrealm(defaultrealm, 1);
41 void dump_krb_acl(FILE *out, char *type, int id, int vers)
43 struct save_queue *sq;
45 char kbuf[MAX_K_NAME_SZ];
47 sq = get_acl(type, id, NULL);
48 while (sq_remove_data(sq, &m))
52 fprintf(stderr, "Found string_id with no associated string. Exiting.\n");
57 canon_krb(m, vers, kbuf, sizeof(kbuf));
58 fprintf(out, "%s\n", kbuf);
65 void canon_krb(struct imember *m, int vers, char *buf, int len)
72 snprintf(buf, len, "%s@%s", m->name, defaultrealm);
76 at = strchr(m->name, '@');
78 at = strchr(m->name, '\0');
81 char *dot = strchr(m->name, '.');
83 snprintf(buf, len, "%.*s/%s", dot - m->name, m->name, dot + 1);
85 snprintf(buf, len, "%s", m->name);
89 char *slash = strchr(m->name, '/');
90 if (slash && slash < at)
91 snprintf(buf, len, "%.*s.%s", slash - m->name, m->name, slash + 1);
93 snprintf(buf, len, "%s", m->name);
97 int plen = strlen(buf);
98 snprintf(buf + plen, len - plen, "@%s", defaultrealm);
104 void dump_user_list(FILE *out, char *type, int id)
106 struct save_queue *sq;
109 sq = get_acl(type, id, NULL);
110 while (sq_remove_data(sq, &m))
112 if (m->type == 'U' || (m->type == 'S' && !strchr(m->name, '@')))
113 fprintf(out, "%s\n", m->name);
119 struct save_queue *get_acl(char *type, int id,
120 char *(merge_func)(char *, char *))
122 struct save_queue *sq;
125 save_imember(sq, type, id, NULL);
126 return merge_imembers(sq, merge_func);
129 static void save_imember(struct save_queue *sq, char *type, int id, char *tag)
131 EXEC SQL BEGIN DECLARE SECTION;
132 int lid = id, mid, mid2, tagid, status;
133 char mtype[IMEMBERS_MEMBER_TYPE_SIZE];
134 EXEC SQL END DECLARE SECTION;
140 EXEC SQL SELECT status INTO :status FROM users WHERE users_id = :id;
142 sq_save_data(sq, imember('U', user_lookup(id), tag));
147 sq_save_data(sq, imember(*type, string_lookup(id), tag));
151 EXEC SQL DECLARE csr_acl_mem CURSOR FOR
152 SELECT member_type, member_id, tag FROM imembers
153 WHERE list_id = :lid AND direct = 1;
154 EXEC SQL OPEN csr_acl_mem;
157 EXEC SQL FETCH csr_acl_mem INTO :mtype, :mid, :tagid;
164 mtag = string_lookup(tagid);
167 EXEC SQL DECLARE csr_list CURSOR FOR
168 SELECT member_type, member_id FROM imembers
169 WHERE list_id = :mid AND member_type != 'LIST';
170 EXEC SQL OPEN csr_list;
173 EXEC SQL FETCH csr_list INTO :mtype, :mid;
177 save_imember(sq, mtype, mid, mtag);
179 EXEC SQL CLOSE csr_list;
182 save_imember(sq, mtype, mid, mtag);
187 static struct save_queue *merge_imembers(struct save_queue *sq,
188 char *(merge_func)(char *, char *))
191 struct imember *m1, *m2;
192 struct save_queue *out;
196 while (sq_remove_data(sq, &m1))
198 while (sq_get_data(sq, &m2))
200 if (m1->type == m2->type && !strcmp(m1->name, m2->name))
202 sq_remove_last_data(sq);
206 m1->tag = merge_func(m1->tag, m2->tag);
212 sq_save_data(out, m1);
218 static struct imember *imember(char type, char *name, char *tag)
221 m = malloc(sizeof(struct imember));
224 m->tag = strdup(tag ? tag : "");
228 void freeimember(struct imember *m)
234 char *user_lookup(int users_id)
238 u = hash_lookup(users, users_id);
243 EXEC SQL BEGIN DECLARE SECTION;
244 char login[USERS_LOGIN_SIZE];
245 EXEC SQL END DECLARE SECTION;
247 EXEC SQL SELECT login INTO :login FROM users
248 WHERE users_id = :users_id;
252 u = strdup(strtrim(login));
253 hash_store(users, users_id, u);
258 char *string_lookup(int string_id)
262 s = hash_lookup(strings, string_id);
267 EXEC SQL BEGIN DECLARE SECTION;
268 char string[STRINGS_STRING_SIZE];
269 EXEC SQL END DECLARE SECTION;
271 EXEC SQL SELECT string INTO :string FROM strings
272 WHERE string_id = :string_id;
276 s = strdup(strtrim(string));
277 hash_store(strings, string_id, s);