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 canon_krb(m, vers, kbuf, sizeof(kbuf));
53 fprintf(out, "%s\n", kbuf);
60 void canon_krb(struct imember *m, int vers, char *buf, int len)
67 snprintf(buf, len, "%s@%s", m->name, defaultrealm);
71 at = strchr(m->name, '@');
73 at = strchr(m->name, '\0');
76 char *dot = strchr(m->name, '.');
78 snprintf(buf, len, "%.*s/%s", dot - m->name, m->name, dot + 1);
80 snprintf(buf, len, "%s", m->name);
84 char *slash = strchr(m->name, '/');
85 if (slash && slash < at)
86 snprintf(buf, len, "%.*s.%s", slash - m->name, m->name, slash + 1);
88 snprintf(buf, len, "%s", m->name);
92 int plen = strlen(buf);
93 snprintf(buf + plen, len - plen, "@%s", defaultrealm);
99 void dump_user_list(FILE *out, char *type, int id)
101 struct save_queue *sq;
104 sq = get_acl(type, id, NULL);
105 while (sq_remove_data(sq, &m))
107 if (m->type == 'U' || (m->type == 'S' && !strchr(m->name, '@')))
108 fprintf(out, "%s\n", m->name);
114 struct save_queue *get_acl(char *type, int id,
115 char *(merge_func)(char *, char *))
117 struct save_queue *sq;
120 save_imember(sq, type, id, NULL);
121 return merge_imembers(sq, merge_func);
124 static void save_imember(struct save_queue *sq, char *type, int id, char *tag)
126 EXEC SQL BEGIN DECLARE SECTION;
127 int lid = id, mid, mid2, tagid;
128 char mtype[IMEMBERS_MEMBER_TYPE_SIZE];
129 EXEC SQL END DECLARE SECTION;
135 sq_save_data(sq, imember('U', user_lookup(id), tag));
140 sq_save_data(sq, imember(*type, string_lookup(id), tag));
144 EXEC SQL DECLARE csr_acl_mem CURSOR FOR
145 SELECT member_type, member_id, tag FROM imembers
146 WHERE list_id = :lid AND direct = 1;
147 EXEC SQL OPEN csr_acl_mem;
150 EXEC SQL FETCH csr_acl_mem INTO :mtype, :mid, :tagid;
157 mtag = string_lookup(tagid);
160 EXEC SQL DECLARE csr_list CURSOR FOR
161 SELECT member_type, member_id FROM imembers
162 WHERE list_id = :mid AND member_type != 'LIST';
163 EXEC SQL OPEN csr_list;
166 EXEC SQL FETCH csr_list INTO :mtype, :mid;
170 save_imember(sq, mtype, mid, mtag);
172 EXEC SQL CLOSE csr_list;
175 save_imember(sq, mtype, mid, mtag);
180 static struct save_queue *merge_imembers(struct save_queue *sq,
181 char *(merge_func)(char *, char *))
184 struct imember *m1, *m2;
185 struct save_queue *out;
189 while (sq_remove_data(sq, &m1))
191 while (sq_get_data(sq, &m2))
193 if (m1->type == m2->type && !strcmp(m1->name, m2->name))
195 sq_remove_last_data(sq);
199 m1->tag = merge_func(m1->tag, m2->tag);
205 sq_save_data(out, m1);
211 static struct imember *imember(char type, char *name, char *tag)
214 m = malloc(sizeof(struct imember));
217 m->tag = strdup(tag ? tag : "");
221 void freeimember(struct imember *m)
227 char *user_lookup(int users_id)
231 u = hash_lookup(users, users_id);
236 EXEC SQL BEGIN DECLARE SECTION;
237 char login[USERS_LOGIN_SIZE];
238 EXEC SQL END DECLARE SECTION;
240 EXEC SQL SELECT login INTO :login FROM users
241 WHERE users_id = :users_id;
245 u = strdup(strtrim(login));
246 hash_store(users, users_id, u);
251 char *string_lookup(int string_id)
255 s = hash_lookup(strings, string_id);
260 EXEC SQL BEGIN DECLARE SECTION;
261 char string[STRINGS_STRING_SIZE];
262 EXEC SQL END DECLARE SECTION;
264 EXEC SQL SELECT string INTO :string FROM strings
265 WHERE string_id = :string_id;
269 s = strdup(strtrim(string));
270 hash_store(strings, string_id, s);