2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #include <afs/pthread_glock.h>
18 #include <sys/types.h>
25 #include <netinet/in.h>
29 #include <stdlib.h> /* for realpath() */
37 #include <afs/afsutil.h>
38 #include <afs/fileutil.h>
40 #ifdef AFS_ATHENA_STDENV
45 #include "cellconfig.h"
47 #include "afs/audit.h"
49 afs_int32 afsconf_SuperUser();
53 afsconf_CheckAuth(adir, acall)
54 register struct rx_call *acall;
55 register struct afsconf_dir *adir;
57 LOCK_GLOBAL_MUTEX return ((afsconf_SuperUser(adir, acall, NULL) == 0) ?
60 #endif /* !defined(UKERNEL) */
64 struct afsconf_dir *adir;
66 if (access(AFSDIR_SERVER_NOAUTH_FILEPATH, 0) == 0) {
67 osi_audit(NoAuthEvent, 0, AUD_END); /* some random server is running noauth */
68 return 1; /* if /usr/afs/local/NoAuth file exists, allow access */
75 afsconf_GetNoAuthFlag(adir)
76 struct afsconf_dir *adir;
80 LOCK_GLOBAL_MUTEX rc = GetNoAuthFlag(adir);
81 UNLOCK_GLOBAL_MUTEX return rc;
85 afsconf_SetNoAuthFlag(adir, aflag)
86 struct afsconf_dir *adir;
89 register afs_int32 code;
91 LOCK_GLOBAL_MUTEX if (aflag == 0) {
92 /* turn off noauth flag */
93 code = (unlink(AFSDIR_SERVER_NOAUTH_FILEPATH) ? errno : 0);
94 osi_audit(NoAuthDisableEvent, code, AUD_END);
96 /* try to create file */
98 open(AFSDIR_SERVER_NOAUTH_FILEPATH, O_CREAT | O_TRUNC | O_RDWR,
102 osi_audit(NoAuthEnableEvent, 0, AUD_END);
104 osi_audit(NoAuthEnableEvent, errno, AUD_END);
108 /* deletes a user from the UserList file */
110 afsconf_DeleteUser(adir, auser)
111 struct afsconf_dir *adir;
112 register char *auser;
123 register afs_int32 code;
125 LOCK_GLOBAL_MUTEX strcompose(tbuffer, sizeof tbuffer, adir->name, "/",
126 AFSDIR_ULIST_FILE, NULL);
130 * We attempt to fully resolve this pathname, so that the rename
131 * of the temporary file will work even if UserList is a symlink
132 * into a different filesystem.
134 char resolved_path[1024];
136 if (realpath(tbuffer, resolved_path)) {
137 strcpy(tbuffer, resolved_path);
140 #endif /* AFS_NT40_ENV */
141 tf = fopen(tbuffer, "r");
143 UNLOCK_GLOBAL_MUTEX return -1;
145 code = stat(tbuffer, &tstat);
147 UNLOCK_GLOBAL_MUTEX return code;
149 strcpy(nbuffer, tbuffer);
150 strcat(nbuffer, ".NXX");
151 nf = fopen(nbuffer, "w+");
154 UNLOCK_GLOBAL_MUTEX return EIO;
159 /* check for our user id */
160 tp = fgets(nbuffer, sizeof(nbuffer), tf);
163 code = sscanf(nbuffer, "%64s", tname);
164 if (code == 1 && strcmp(tname, auser) == 0) {
165 /* found the guy, don't copy to output file */
168 /* otherwise copy original line to output */
169 fprintf(nf, "%s", nbuffer);
175 if (fclose(nf) == EOF)
177 strcpy(nbuffer, tbuffer);
178 strcat(nbuffer, ".NXX"); /* generate new file name again */
181 flag = renamefile(nbuffer, tbuffer);
183 flag = chmod(tbuffer, tstat.st_mode);
187 /* finally, decide what to return to the caller */
188 UNLOCK_GLOBAL_MUTEX if (flag)
189 return EIO; /* something mysterious went wrong */
191 return ENOENT; /* entry wasn't found, no changes made */
192 return 0; /* everything was fine */
195 /* returns nth super user from the UserList file */
197 afsconf_GetNthUser(adir, an, abuffer, abufferLen)
198 struct afsconf_dir *adir;
201 afs_int32 abufferLen;
208 register afs_int32 code;
210 LOCK_GLOBAL_MUTEX strcompose(tbuffer, sizeof tbuffer, adir->name, "/",
211 AFSDIR_ULIST_FILE, NULL);
212 tf = fopen(tbuffer, "r");
214 UNLOCK_GLOBAL_MUTEX return 1;
218 /* check for our user id */
219 tp = fgets(tbuffer, sizeof(tbuffer), tf);
222 code = sscanf(tbuffer, "%64s", tname);
223 if (code == 1 && an-- == 0) {
229 strcpy(abuffer, tname);
231 UNLOCK_GLOBAL_MUTEX return flag;
234 /* returns true iff user is in the UserList file */
236 FindUser(adir, auser)
237 struct afsconf_dir *adir;
238 register char *auser;
244 register afs_int32 code;
247 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
249 bp = BufioOpen(tbuffer, O_RDONLY, 0);
254 /* check for our user id */
255 rc = BufioGets(bp, tbuffer, sizeof(tbuffer));
258 code = sscanf(tbuffer, "%64s", tname);
259 if (code == 1 && strcmp(tname, auser) == 0) {
268 /* add a user to the user list, checking for duplicates */
270 afsconf_AddUser(adir, aname)
271 struct afsconf_dir *adir;
275 register afs_int32 code;
278 LOCK_GLOBAL_MUTEX if (FindUser(adir, aname)) {
279 UNLOCK_GLOBAL_MUTEX return EEXIST; /* already in the list */
282 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
284 tf = fopen(tbuffer, "a+");
286 UNLOCK_GLOBAL_MUTEX return EIO;
288 fprintf(tf, "%s\n", aname);
294 UNLOCK_GLOBAL_MUTEX return code;
297 /* special CompFindUser routine that builds up a princ and then
298 calls finduser on it. If found, returns char * to user string,
299 otherwise returns NULL. The resulting string should be immediately
300 copied to other storage prior to release of mutex. */
302 CompFindUser(adir, name, sep, inst, realm)
303 struct afsconf_dir *adir;
309 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
311 /* always must have name */
312 if (!name || !name[0]) {
315 strcpy(fullname, name);
317 /* might have instance */
318 if (inst && inst[0]) {
319 if (!sep || !sep[0]) {
323 strcat(fullname, sep);
324 strcat(fullname, inst);
327 /* might have realm */
328 if (realm && realm[0]) {
329 strcat(fullname, "@");
330 strcat(fullname, realm);
333 if (FindUser(adir, fullname)) {
341 /* make sure user authenticated on rx call acall is in list of valid
342 users. Copy the "real name" of the authenticated user into namep
343 if a pointer is passed.
346 afsconf_SuperUser(adir, acall, namep)
347 struct afsconf_dir *adir;
348 struct rx_call *acall;
351 register struct rx_connection *tconn;
352 register afs_int32 code;
355 LOCK_GLOBAL_MUTEX if (!adir) {
356 UNLOCK_GLOBAL_MUTEX return 0;
359 if (afsconf_GetNoAuthFlag(adir)) {
361 strcpy(namep, "<NoAuth>");
362 UNLOCK_GLOBAL_MUTEX return 1;
365 tconn = rx_ConnectionOf(acall);
366 code = rx_SecurityClassOf(tconn);
368 UNLOCK_GLOBAL_MUTEX return 0; /* not authenticated at all, answer is no */
369 } else if (code == 1) {
371 UNLOCK_GLOBAL_MUTEX return 0; /* not supported any longer */
372 } else if (code == 2) {
373 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
374 char tinst[MAXKTCNAMELEN];
375 char tcell[MAXKTCREALMLEN];
376 char tcell_l[MAXKTCREALMLEN];
379 /* keep track of which one actually authorized request */
380 char uname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
383 static char lcell[MAXCELLCHARS] = "";
384 static char lrealm[AFS_REALM_SZ] = "";
386 /* get auth details from server connection */
388 rxkad_GetServerInfo(acall->conn, NULL, &exp, tname, tinst, tcell,
391 UNLOCK_GLOBAL_MUTEX return 0; /* bogus connection/other error */
394 /* don't bother checking anything else if tix have expired */
395 #ifdef AFS_PTHREAD_ENV
396 if (exp < clock_Sec()) {
398 if (exp < FT_ApproxTime()) {
400 UNLOCK_GLOBAL_MUTEX return 0; /* expired tix */
403 /* generate lowercased version of cell name */
404 strcpy(tcell_l, tcell);
407 *tmp = tolower(*tmp);
411 /* determine local cell name. It's static, so will only get
412 * calculated the first time through */
414 afsconf_GetLocalCell(adir, lcell, sizeof(lcell));
416 /* if running a krb environment, also get the local realm */
417 /* note - this assumes AFS_REALM_SZ <= MAXCELLCHARS */
418 /* just set it to lcell if it fails */
420 if (afs_krb_get_lrealm(lrealm, 0) != 0) /* KSUCCESS */
421 strncpy(lrealm, lcell, AFS_REALM_SZ);
425 /* start with no uname and no authorization */
429 /* localauth special case */
430 if (strlen(tinst) == 0 && strlen(tcell) == 0
431 && !strcmp(tname, AUTH_SUPERUSER)) {
432 strcpy(uname, "<LocalAuth>");
435 /* cell of connection matches local cell or krb4 realm */
436 } else if (!strcasecmp(tcell, lcell) || !strcasecmp(tcell, lrealm)) {
437 if ((tmp = CompFindUser(adir, tname, ".", tinst, NULL))) {
441 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, NULL))) {
447 /* cell of conn doesn't match local cell or realm */
449 if ((tmp = CompFindUser(adir, tname, ".", tinst, tcell))) {
453 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, tcell))) {
457 } else if ((tmp = CompFindUser(adir, tname, ".", tinst, tcell_l))) {
461 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, tcell_l))) {
469 strcpy(namep, uname);
470 UNLOCK_GLOBAL_MUTEX return flag;
471 } else { /* some other auth type */
472 UNLOCK_GLOBAL_MUTEX return 0; /* mysterious, just say no */