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;
133 char mtype[IMEMBERS_MEMBER_TYPE_SIZE];
134 EXEC SQL END DECLARE SECTION;
140 sq_save_data(sq, imember('U', user_lookup(id), tag));
145 sq_save_data(sq, imember(*type, string_lookup(id), tag));
149 EXEC SQL DECLARE csr_acl_mem CURSOR FOR
150 SELECT member_type, member_id, tag FROM imembers
151 WHERE list_id = :lid AND direct = 1;
152 EXEC SQL OPEN csr_acl_mem;
155 EXEC SQL FETCH csr_acl_mem INTO :mtype, :mid, :tagid;
162 mtag = string_lookup(tagid);
165 EXEC SQL DECLARE csr_list CURSOR FOR
166 SELECT member_type, member_id FROM imembers
167 WHERE list_id = :mid AND member_type != 'LIST';
168 EXEC SQL OPEN csr_list;
171 EXEC SQL FETCH csr_list INTO :mtype, :mid;
175 save_imember(sq, mtype, mid, mtag);
177 EXEC SQL CLOSE csr_list;
180 save_imember(sq, mtype, mid, mtag);
185 static struct save_queue *merge_imembers(struct save_queue *sq,
186 char *(merge_func)(char *, char *))
189 struct imember *m1, *m2;
190 struct save_queue *out;
194 while (sq_remove_data(sq, &m1))
196 while (sq_get_data(sq, &m2))
198 if (m1->type == m2->type && !strcmp(m1->name, m2->name))
200 sq_remove_last_data(sq);
204 m1->tag = merge_func(m1->tag, m2->tag);
210 sq_save_data(out, m1);
216 static struct imember *imember(char type, char *name, char *tag)
219 m = malloc(sizeof(struct imember));
222 m->tag = strdup(tag ? tag : "");
226 void freeimember(struct imember *m)
232 char *user_lookup(int users_id)
236 u = hash_lookup(users, users_id);
241 EXEC SQL BEGIN DECLARE SECTION;
242 char login[USERS_LOGIN_SIZE];
243 EXEC SQL END DECLARE SECTION;
245 EXEC SQL SELECT login INTO :login FROM users
246 WHERE users_id = :users_id;
250 u = strdup(strtrim(login));
251 hash_store(users, users_id, u);
256 char *string_lookup(int string_id)
260 s = hash_lookup(strings, string_id);
265 EXEC SQL BEGIN DECLARE SECTION;
266 char string[STRINGS_STRING_SIZE];
267 EXEC SQL END DECLARE SECTION;
269 EXEC SQL SELECT string INTO :string FROM strings
270 WHERE string_id = :string_id;
274 s = strdup(strtrim(string));
275 hash_store(strings, string_id, s);