]>
Commit | Line | Data |
---|---|---|
883e2e2b | 1 | /* $Id$ |
2 | * | |
3 | * Utility functions for outputting ACLs | |
4 | * | |
5 | * Copyright (C) 1999 by the Massachusetts Institute of Technology. | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <mit-copyright.h> | |
11 | #include <moira.h> | |
12 | #include <moira_site.h> | |
13 | #include "util.h" | |
14 | ||
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | ||
19 | #include <krb.h> | |
20 | ||
21 | EXEC SQL INCLUDE sqlca; | |
22 | ||
23 | RCSID("$Header$"); | |
24 | ||
25 | static char defaultrealm[REALM_SZ]; | |
26 | ||
27 | static struct hash *users, *strings; | |
28 | ||
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 *)); | |
33 | ||
34 | void init_acls(void) | |
35 | { | |
36 | users = create_hash(2000); | |
37 | strings = create_hash(2000); | |
38 | krb_get_lrealm(defaultrealm, 1); | |
39 | } | |
40 | ||
41 | void dump_krb_acl(FILE *out, char *type, int id, int vers) | |
42 | { | |
43 | struct save_queue *sq; | |
44 | struct imember *m; | |
45 | char kbuf[MAX_K_NAME_SZ]; | |
46 | ||
47 | sq = get_acl(type, id, NULL); | |
48 | while (sq_remove_data(sq, &m)) | |
49 | { | |
1bc309fb | 50 | if (m->name == NULL) |
51 | { | |
52 | fprintf(stderr, "Found string_id with no associated string. Exiting.\n"); | |
53 | exit(MR_DBMS_ERR); | |
54 | } | |
55 | if (m->type != 'S') | |
883e2e2b | 56 | { |
57 | canon_krb(m, vers, kbuf, sizeof(kbuf)); | |
58 | fprintf(out, "%s\n", kbuf); | |
59 | } | |
60 | freeimember(m); | |
61 | } | |
62 | sq_destroy(sq); | |
63 | } | |
64 | ||
65 | void canon_krb(struct imember *m, int vers, char *buf, int len) | |
66 | { | |
67 | char *at; | |
68 | ||
69 | switch (m->type) | |
70 | { | |
71 | case 'U': | |
72 | snprintf(buf, len, "%s@%s", m->name, defaultrealm); | |
73 | break; | |
74 | ||
75 | case 'K': | |
76 | at = strchr(m->name, '@'); | |
77 | if (!at) | |
78 | at = strchr(m->name, '\0'); | |
79 | if (vers == 5) | |
80 | { | |
81 | char *dot = strchr(m->name, '.'); | |
82 | if (dot && dot < at) | |
83 | snprintf(buf, len, "%.*s/%s", dot - m->name, m->name, dot + 1); | |
84 | else | |
85 | snprintf(buf, len, "%s", m->name); | |
86 | } | |
87 | else | |
88 | { | |
89 | char *slash = strchr(m->name, '/'); | |
90 | if (slash && slash < at) | |
91 | snprintf(buf, len, "%.*s.%s", slash - m->name, m->name, slash + 1); | |
92 | else | |
93 | snprintf(buf, len, "%s", m->name); | |
94 | } | |
95 | if (!*at) | |
96 | { | |
97 | int plen = strlen(buf); | |
98 | snprintf(buf + plen, len - plen, "@%s", defaultrealm); | |
99 | } | |
100 | break; | |
101 | } | |
102 | } | |
103 | ||
104 | void dump_user_list(FILE *out, char *type, int id) | |
105 | { | |
106 | struct save_queue *sq; | |
107 | struct imember *m; | |
108 | ||
109 | sq = get_acl(type, id, NULL); | |
110 | while (sq_remove_data(sq, &m)) | |
111 | { | |
112 | if (m->type == 'U' || (m->type == 'S' && !strchr(m->name, '@'))) | |
113 | fprintf(out, "%s\n", m->name); | |
114 | freeimember(m); | |
115 | } | |
116 | sq_destroy(sq); | |
117 | } | |
118 | ||
119 | struct save_queue *get_acl(char *type, int id, | |
120 | char *(merge_func)(char *, char *)) | |
121 | { | |
122 | struct save_queue *sq; | |
123 | ||
124 | sq = sq_create(); | |
125 | save_imember(sq, type, id, NULL); | |
126 | return merge_imembers(sq, merge_func); | |
127 | } | |
128 | ||
129 | static void save_imember(struct save_queue *sq, char *type, int id, char *tag) | |
130 | { | |
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; | |
135 | char *mtag; | |
136 | ||
137 | switch (*type) | |
138 | { | |
139 | case 'U': | |
140 | sq_save_data(sq, imember('U', user_lookup(id), tag)); | |
141 | break; | |
142 | ||
143 | case 'K': | |
144 | case 'S': | |
145 | sq_save_data(sq, imember(*type, string_lookup(id), tag)); | |
146 | break; | |
147 | ||
148 | case 'L': | |
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; | |
153 | while (1) | |
154 | { | |
155 | EXEC SQL FETCH csr_acl_mem INTO :mtype, :mid, :tagid; | |
156 | if (sqlca.sqlcode) | |
157 | break; | |
158 | ||
159 | if (tag) | |
160 | mtag = tag; | |
161 | else | |
162 | mtag = string_lookup(tagid); | |
163 | if (mtype[0] == 'L') | |
164 | { | |
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; | |
169 | while (1) | |
170 | { | |
171 | EXEC SQL FETCH csr_list INTO :mtype, :mid; | |
172 | if (sqlca.sqlcode) | |
173 | break; | |
174 | ||
175 | save_imember(sq, mtype, mid, mtag); | |
176 | } | |
177 | EXEC SQL CLOSE csr_list; | |
178 | } | |
179 | else | |
180 | save_imember(sq, mtype, mid, mtag); | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | static struct save_queue *merge_imembers(struct save_queue *sq, | |
186 | char *(merge_func)(char *, char *)) | |
187 | { | |
188 | int n; | |
189 | struct imember *m1, *m2; | |
190 | struct save_queue *out; | |
191 | char *t1; | |
192 | ||
193 | out = sq_create(); | |
194 | while (sq_remove_data(sq, &m1)) | |
195 | { | |
196 | while (sq_get_data(sq, &m2)) | |
197 | { | |
198 | if (m1->type == m2->type && !strcmp(m1->name, m2->name)) | |
199 | { | |
200 | sq_remove_last_data(sq); | |
201 | if (merge_func) | |
202 | { | |
203 | t1 = m1->tag; | |
204 | m1->tag = merge_func(m1->tag, m2->tag); | |
205 | free(t1); | |
206 | } | |
207 | freeimember(m2); | |
208 | } | |
209 | } | |
210 | sq_save_data(out, m1); | |
211 | } | |
212 | sq_destroy(sq); | |
213 | return out; | |
214 | } | |
215 | ||
216 | static struct imember *imember(char type, char *name, char *tag) | |
217 | { | |
218 | struct imember *m; | |
219 | m = malloc(sizeof(struct imember)); | |
220 | m->type = type; | |
221 | m->name = name; | |
222 | m->tag = strdup(tag ? tag : ""); | |
223 | return m; | |
224 | } | |
225 | ||
226 | void freeimember(struct imember *m) | |
227 | { | |
228 | free(m->tag); | |
229 | free(m); | |
230 | } | |
231 | ||
232 | char *user_lookup(int users_id) | |
233 | { | |
234 | char *u; | |
235 | ||
236 | u = hash_lookup(users, users_id); | |
237 | if (u) | |
238 | return u; | |
239 | else | |
240 | { | |
241 | EXEC SQL BEGIN DECLARE SECTION; | |
242 | char login[USERS_LOGIN_SIZE]; | |
243 | EXEC SQL END DECLARE SECTION; | |
244 | ||
245 | EXEC SQL SELECT login INTO :login FROM users | |
246 | WHERE users_id = :users_id; | |
247 | if (sqlca.sqlcode) | |
248 | return NULL; | |
249 | ||
250 | u = strdup(strtrim(login)); | |
251 | hash_store(users, users_id, u); | |
252 | return u; | |
253 | } | |
254 | } | |
255 | ||
256 | char *string_lookup(int string_id) | |
257 | { | |
258 | char *s; | |
259 | ||
260 | s = hash_lookup(strings, string_id); | |
261 | if (s) | |
262 | return s; | |
263 | else | |
264 | { | |
265 | EXEC SQL BEGIN DECLARE SECTION; | |
266 | char string[STRINGS_STRING_SIZE]; | |
267 | EXEC SQL END DECLARE SECTION; | |
268 | ||
269 | EXEC SQL SELECT string INTO :string FROM strings | |
270 | WHERE string_id = :string_id; | |
271 | if (sqlca.sqlcode) | |
272 | return NULL; | |
273 | ||
274 | s = strdup(strtrim(string)); | |
275 | hash_store(strings, string_id, s); | |
276 | return s; | |
277 | } | |
278 | } |