]>
Commit | Line | Data |
---|---|---|
fb8809f4 | 1 | /* $Header$ |
2 | * | |
3 | * Do AFS incremental updates | |
4 | * | |
5 | * Copyright (C) 1989 by the Massachusetts Institute of Technology | |
6 | * for copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <sms.h> | |
11 | #include <sms_app.h> | |
12 | #include <sys/param.h> | |
13 | #include <krb.h> | |
14 | #include <krb_et.h> | |
15 | #include <afs/auth.h> | |
16 | #include <pwd.h> | |
f633445d | 17 | #include <sys/file.h> |
18 | ||
19 | #define file_exists(file) (access((file), F_OK) == 0) | |
fb8809f4 | 20 | |
a6be2cf4 | 21 | char prs[64], fs[64]; |
fb8809f4 | 22 | |
23 | char *whoami; | |
24 | ||
25 | main(argc, argv) | |
26 | char **argv; | |
27 | int argc; | |
28 | { | |
29 | int beforec, afterc; | |
30 | char *table, **before, **after; | |
810d12a7 | 31 | #ifdef DEBUG |
32 | char buf[1024]; | |
33 | int i; | |
34 | #endif | |
fb8809f4 | 35 | |
36 | table = argv[1]; | |
37 | beforec = atoi(argv[2]); | |
38 | before = &argv[4]; | |
39 | afterc = atoi(argv[3]); | |
40 | after = &argv[4 + beforec]; | |
41 | whoami = argv[0]; | |
42 | ||
810d12a7 | 43 | #ifdef DEBUG |
44 | sprintf(buf, "%s (", table); | |
45 | for (i = 0; i < beforec; i++) { | |
46 | if (i > 0) | |
47 | strcat(buf, ","); | |
48 | strcat(buf, before[i]); | |
49 | } | |
50 | strcat(buf, ")->("); | |
51 | for (i = 0; i < afterc; i++) { | |
52 | if (i > 0) | |
53 | strcat(buf, ","); | |
54 | strcat(buf, after[i]); | |
55 | } | |
56 | strcat(buf, ")\n"); | |
57 | write(1,buf,strlen(buf)); | |
58 | #endif | |
59 | ||
fb8809f4 | 60 | initialize_sms_error_table(); |
61 | initialize_krb_error_table(); | |
a6be2cf4 | 62 | sprintf(prs, "%s/prs", BIN_DIR); |
63 | sprintf(fs, "%s/fs", BIN_DIR); | |
fb8809f4 | 64 | |
65 | if (!strcmp(table, "users")) { | |
66 | do_user(before, beforec, after, afterc); | |
67 | } else if (!strcmp(table, "list")) { | |
68 | do_list(before, beforec, after, afterc); | |
69 | } else if (!strcmp(table, "members")) { | |
70 | do_member(before, beforec, after, afterc); | |
71 | } else if (!strcmp(table, "filesys")) { | |
72 | do_filesys(before, beforec, after, afterc); | |
73 | } else if (!strcmp(table, "nfsquota")) { | |
74 | do_quota(before, beforec, after, afterc); | |
75 | } | |
76 | unlog(); | |
77 | exit(0); | |
78 | } | |
79 | ||
80 | ||
81 | do_cmd(cmd) | |
82 | char *cmd; | |
83 | { | |
84 | char realm[REALM_SZ + 1]; | |
85 | static int inited = 0; | |
810d12a7 | 86 | int success = 0, tries = 0, fd, cc; |
fb8809f4 | 87 | CREDENTIALS *c, *get_ticket(); |
88 | struct passwd *pw; | |
810d12a7 | 89 | char buf[128], localcell[128], *p, *index(); |
fb8809f4 | 90 | |
91 | while (success == 0 && tries < 3) { | |
92 | if (!inited) { | |
93 | if (krb_get_lrealm(realm) != KSUCCESS) | |
94 | (void) strcpy(realm, KRB_REALM); | |
95 | sprintf(buf, "/tmp/tkt_%d_afsinc", getpid()); | |
96 | krb_set_tkt_string(buf); | |
810d12a7 | 97 | |
98 | if ((fd = open("/usr/vice/etc/ThisCell", O_RDONLY, 0)) < 0) { | |
99 | critical_alert("incremental", "unable to find AFS cell"); | |
100 | exit(1); | |
101 | } | |
102 | if ((cc = read(fd, localcell, sizeof(localcell))) < 0) { | |
103 | critical_alert("incremental", "unable to read AFS cell"); | |
104 | exit(1); | |
105 | } | |
106 | close(fd); | |
107 | p = index(localcell, '\n'); | |
108 | if (p) *p = 0; | |
109 | ||
fb8809f4 | 110 | if (((pw = getpwnam("smsdba")) == NULL) || |
810d12a7 | 111 | ((c = get_ticket("sms", "", realm, localcell)) == NULL) || |
fb8809f4 | 112 | (setpag() < 0) || |
113 | (setreuid(pw->pw_uid, pw->pw_uid) < 0) || | |
810d12a7 | 114 | aklog(c, localcell)) { |
fb8809f4 | 115 | com_err(whoami, 0, "failed to authenticate"); |
116 | } else | |
117 | inited++; | |
118 | } | |
119 | ||
120 | if (inited) { | |
121 | com_err(whoami, 0, "Executing command: %s", cmd); | |
122 | if (system(cmd) == 0) | |
123 | success++; | |
124 | } | |
125 | if (!success) { | |
126 | tries++; | |
127 | sleep(5 * 60); | |
128 | } | |
129 | } | |
130 | if (!success) | |
131 | critical_alert("incremental", "failed command: %s", cmd); | |
132 | } | |
133 | ||
134 | ||
135 | do_user(before, beforec, after, afterc) | |
136 | char **before; | |
137 | int beforec; | |
138 | char **after; | |
139 | int afterc; | |
140 | { | |
141 | int bstate, astate; | |
142 | char cmd[512]; | |
143 | ||
144 | cmd[0] = bstate = astate = 0; | |
145 | if (afterc > U_STATE) | |
146 | astate = atoi(after[U_STATE]); | |
147 | if (beforec > U_STATE) | |
148 | bstate = atoi(before[U_STATE]); | |
810d12a7 | 149 | if (astate == 2) astate = 1; |
150 | if (bstate == 2) bstate = 1; | |
fb8809f4 | 151 | |
152 | if (astate != 1 && bstate != 1) | |
153 | return; | |
154 | if (astate == 1 && bstate != 1) { | |
810d12a7 | 155 | sprintf(cmd, "%s newuser -name %s -id %s", |
a6be2cf4 | 156 | prs, after[U_NAME], after[U_UID]); |
fb8809f4 | 157 | do_cmd(cmd); |
158 | return; | |
159 | } else if (astate != 1 && bstate == 1) { | |
a6be2cf4 | 160 | sprintf(cmd, "%s delete %s", prs, before[U_NAME]); |
fb8809f4 | 161 | do_cmd(cmd); |
162 | return; | |
163 | } | |
164 | ||
165 | if (beforec > U_UID && afterc > U_UID && | |
166 | strcmp(before[U_UID], after[U_UID])) { | |
167 | /* change UID, & possibly user name here */ | |
168 | exit(1); | |
169 | } | |
170 | ||
171 | if (beforec > U_NAME && afterc > U_NAME && | |
172 | strcmp(before[U_NAME], after[U_NAME])) { | |
810d12a7 | 173 | sprintf(cmd, "%s chname -oldname %s -newname %s", |
a6be2cf4 | 174 | prs, before[U_NAME], after[U_NAME]); |
fb8809f4 | 175 | do_cmd(cmd); |
176 | } | |
177 | } | |
178 | ||
179 | ||
180 | ||
181 | do_list(before, beforec, after, afterc) | |
182 | char **before; | |
183 | int beforec; | |
184 | char **after; | |
185 | int afterc; | |
186 | { | |
187 | char cmd[512]; | |
188 | int agid, bgid; | |
189 | ||
190 | cmd[0] = agid = bgid = 0; | |
191 | if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) | |
192 | bgid = atoi(before[L_GID]); | |
193 | if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) | |
194 | agid = atoi(after[L_GID]); | |
195 | ||
196 | if (bgid == 0 && agid != 0) { | |
810d12a7 | 197 | sprintf(cmd, |
198 | "%s create -name system:%s -id %s -owner system:administrators", | |
a6be2cf4 | 199 | prs, after[L_NAME], after[L_GID]); |
fb8809f4 | 200 | do_cmd(cmd); |
201 | return; | |
202 | } | |
203 | if (agid == 0 && bgid != 0) { | |
a6be2cf4 | 204 | sprintf(cmd, "%s delete -name system:%s", prs, before[L_NAME]); |
fb8809f4 | 205 | do_cmd(cmd); |
206 | return; | |
207 | } | |
208 | if (agid == 0 && bgid == 0) | |
209 | return; | |
210 | if (strcmp(before[L_NAME], after[L_NAME])) { | |
211 | sprintf(cmd, | |
810d12a7 | 212 | "%s chname -oldname system:%s -newname system:%s", |
a6be2cf4 | 213 | prs, before[L_NAME], after[L_NAME]); |
fb8809f4 | 214 | do_cmd(cmd); |
215 | return; | |
216 | } | |
217 | } | |
218 | ||
219 | ||
220 | do_member(before, beforec, after, afterc) | |
221 | char **before; | |
222 | int beforec; | |
223 | char **after; | |
224 | int afterc; | |
225 | { | |
226 | char cmd[512]; | |
227 | ||
228 | if (beforec == 0 && !strcmp(after[LM_TYPE], "USER")) { | |
810d12a7 | 229 | sprintf(cmd, "%s add -user %s -group system:%s", |
a6be2cf4 | 230 | prs, after[LM_MEMBER], after[LM_LIST]); |
fb8809f4 | 231 | do_cmd(cmd); |
232 | return; | |
233 | } | |
234 | if (afterc == 0 && !strcmp(before[LM_TYPE], "USER")) { | |
810d12a7 | 235 | sprintf(cmd, "%s remove -user %s -group system:%s", |
a6be2cf4 | 236 | prs, before[LM_MEMBER], before[LM_LIST]); |
fb8809f4 | 237 | do_cmd(cmd); |
238 | return; | |
239 | } | |
240 | } | |
241 | ||
242 | ||
243 | do_filesys(before, beforec, after, afterc) | |
244 | char **before; | |
245 | int beforec; | |
246 | char **after; | |
247 | int afterc; | |
248 | { | |
f633445d | 249 | if (afterc < FS_CREATE) |
250 | return; | |
251 | if (!strcmp("AFS", after[FS_TYPE]) && !strncmp("/afs", after[FS_PACK]) && | |
252 | !file_exists(after[FS_PACK])) { | |
253 | critical_alert("incremental", "unable to create locker %s", | |
254 | after[FS_PACK]); | |
255 | } | |
fb8809f4 | 256 | } |
257 | ||
258 | ||
259 | do_quota(before, beforec, after, afterc) | |
260 | char **before; | |
261 | int beforec; | |
262 | char **after; | |
263 | int afterc; | |
264 | { | |
265 | char cmd[512]; | |
266 | ||
267 | if (!(afterc >= Q_DIRECTORY && !strncmp("/afs", after[Q_DIRECTORY], 4)) && | |
268 | !(beforec >= Q_DIRECTORY && !strncmp("/afs", before[Q_DIRECTORY], 4))) | |
269 | return; | |
ac3e92fb | 270 | if (afterc >= Q_LOGIN && strcmp("[nobody]", after[Q_LOGIN])) |
271 | return; | |
fb8809f4 | 272 | if (afterc != 0) { |
ac3e92fb | 273 | sprintf(cmd, "%s setquota -dir %s -quota %s", |
a6be2cf4 | 274 | fs, after[Q_DIRECTORY], after[Q_QUOTA]); |
fb8809f4 | 275 | do_cmd(cmd); |
276 | return; | |
277 | } | |
278 | } | |
279 | ||
280 | ||
281 | CREDENTIALS *get_ticket(name, instance, realm, cell) | |
282 | char *name; | |
283 | char *instance; | |
284 | char *realm; | |
285 | char *cell; | |
286 | { | |
287 | static CREDENTIALS c; | |
288 | int status; | |
289 | ||
290 | status = krb_get_svc_in_tkt(name, instance, realm, | |
291 | "krbtgt", realm, 1, KEYFILE); | |
292 | if (status != 0) { | |
293 | com_err(whoami, status+ERROR_TABLE_BASE_krb, "getting initial ticket from srvtab"); | |
294 | return(NULL); | |
295 | } | |
296 | status = krb_get_cred("afs", cell, realm, &c); | |
297 | if (status != 0) { | |
298 | status = get_ad_tkt("afs", cell, realm, 255); | |
299 | if (status == 0) | |
300 | status = krb_get_cred("afs", cell, realm, &c); | |
301 | } | |
302 | if (status != 0) { | |
303 | com_err(whoami, status+ERROR_TABLE_BASE_krb, "getting service ticket"); | |
304 | return(NULL); | |
305 | } | |
306 | return(&c); | |
307 | } | |
308 | ||
309 | ||
310 | aklog(c, cell) | |
311 | CREDENTIALS *c; | |
312 | char *cell; | |
313 | { | |
314 | struct ktc_principal aserver; | |
315 | struct ktc_token atoken; | |
316 | ||
317 | atoken.kvno = c->kvno; | |
318 | strcpy(aserver.name, "afs"); | |
319 | strcpy(aserver.instance, ""); | |
320 | strcpy(aserver.cell, cell); | |
321 | ||
322 | atoken.startTime = c->issue_date; | |
323 | atoken.endTime = c->issue_date + (c->lifetime * 5 * 60); | |
324 | bcopy (c->session, &atoken.sessionKey, 8); | |
325 | atoken.ticketLen = c->ticket_st.length; | |
326 | bcopy (c->ticket_st.dat, atoken.ticket, atoken.ticketLen); | |
327 | ||
328 | return(ktc_SetToken(&aserver, &atoken, NULL)); | |
329 | } | |
330 | ||
331 | ||
332 | unlog() | |
333 | { | |
334 | ktc_ForgetToken("afs"); | |
ac3e92fb | 335 | dest_tkt(); |
fb8809f4 | 336 | } |