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>
16 #include <afs/pthread_glock.h>
17 #include <sys/types.h>
24 #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"
51 afsconf_CheckAuth(void *arock, struct rx_call *acall)
53 struct afsconf_dir *adir = (struct afsconf_dir *) arock;
56 rc = ((afsconf_SuperUser(adir, acall, NULL) == 0) ? 10029 : 0);
60 #endif /* !defined(UKERNEL) */
63 GetNoAuthFlag(struct afsconf_dir *adir)
65 if (access(AFSDIR_SERVER_NOAUTH_FILEPATH, 0) == 0) {
66 osi_audit(NoAuthEvent, 0, AUD_END); /* some random server is running noauth */
67 return 1; /* if /usr/afs/local/NoAuth file exists, allow access */
74 afsconf_GetNoAuthFlag(struct afsconf_dir *adir)
79 rc = GetNoAuthFlag(adir);
85 afsconf_SetNoAuthFlag(struct afsconf_dir *adir, int aflag)
91 /* turn off noauth flag */
92 code = (unlink(AFSDIR_SERVER_NOAUTH_FILEPATH) ? errno : 0);
93 osi_audit(NoAuthDisableEvent, code, AUD_END);
95 /* try to create file */
97 open(AFSDIR_SERVER_NOAUTH_FILEPATH, O_CREAT | O_TRUNC | O_RDWR,
101 osi_audit(NoAuthEnableEvent, 0, AUD_END);
103 osi_audit(NoAuthEnableEvent, errno, AUD_END);
108 /* deletes a user from the UserList file */
110 afsconf_DeleteUser(struct afsconf_dir *adir, char *auser)
124 strcompose(tbuffer, sizeof tbuffer, adir->name, "/",
125 AFSDIR_ULIST_FILE, NULL);
129 * We attempt to fully resolve this pathname, so that the rename
130 * of the temporary file will work even if UserList is a symlink
131 * into a different filesystem.
133 char resolved_path[1024];
135 if (realpath(tbuffer, resolved_path)) {
136 strcpy(tbuffer, resolved_path);
139 #endif /* AFS_NT40_ENV */
140 tf = fopen(tbuffer, "r");
145 code = stat(tbuffer, &tstat);
150 strcpy(nbuffer, tbuffer);
151 strcat(nbuffer, ".NXX");
152 nf = fopen(nbuffer, "w+");
161 /* check for our user id */
162 tp = fgets(nbuffer, sizeof(nbuffer), tf);
165 code = sscanf(nbuffer, "%64s", tname);
166 if (code == 1 && strcmp(tname, auser) == 0) {
167 /* found the guy, don't copy to output file */
170 /* otherwise copy original line to output */
171 fprintf(nf, "%s", nbuffer);
177 if (fclose(nf) == EOF)
179 strcpy(nbuffer, tbuffer);
180 strcat(nbuffer, ".NXX"); /* generate new file name again */
183 flag = renamefile(nbuffer, tbuffer);
185 flag = chmod(tbuffer, tstat.st_mode);
189 /* finally, decide what to return to the caller */
192 return EIO; /* something mysterious went wrong */
194 return ENOENT; /* entry wasn't found, no changes made */
195 return 0; /* everything was fine */
198 /* returns nth super user from the UserList file */
200 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
201 afs_int32 abufferLen)
211 strcompose(tbuffer, sizeof tbuffer, adir->name, "/",
212 AFSDIR_ULIST_FILE, NULL);
213 tf = fopen(tbuffer, "r");
220 /* check for our user id */
221 tp = fgets(tbuffer, sizeof(tbuffer), tf);
224 code = sscanf(tbuffer, "%64s", tname);
225 if (code == 1 && an-- == 0) {
231 strcpy(abuffer, tname);
237 /* returns true iff user is in the UserList file */
239 FindUser(struct afsconf_dir *adir, char *auser)
248 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
250 bp = BufioOpen(tbuffer, O_RDONLY, 0);
255 /* check for our user id */
256 rc = BufioGets(bp, tbuffer, sizeof(tbuffer));
259 code = sscanf(tbuffer, "%64s", tname);
260 if (code == 1 && strcmp(tname, auser) == 0) {
269 /* add a user to the user list, checking for duplicates */
271 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
278 if (FindUser(adir, aname)) {
280 return EEXIST; /* already in the list */
283 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
285 tf = fopen(tbuffer, "a+");
290 fprintf(tf, "%s\n", aname);
300 /* special CompFindUser routine that builds up a princ and then
301 calls finduser on it. If found, returns char * to user string,
302 otherwise returns NULL. The resulting string should be immediately
303 copied to other storage prior to release of mutex. */
305 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
308 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
310 /* always must have name */
311 if (!name || !name[0]) {
314 strcpy(fullname, name);
316 /* might have instance */
317 if (inst && inst[0]) {
318 if (!sep || !sep[0]) {
322 strcat(fullname, sep);
323 strcat(fullname, inst);
326 /* might have realm */
327 if (realm && realm[0]) {
328 strcat(fullname, "@");
329 strcat(fullname, realm);
332 if (FindUser(adir, fullname)) {
340 kerberosSuperUser(struct afsconf_dir *adir, char *tname, char *tinst,
341 char *tcell, char *namep)
343 char tcell_l[MAXKTCREALMLEN] = "";
346 /* keep track of which one actually authorized request */
347 char uname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
349 static char lcell[MAXCELLCHARS] = "";
350 static char lrealms[AFS_NUM_LREALMS][AFS_REALM_SZ];
351 static int num_lrealms = -1;
352 int lrealm_match = 0, i;
355 /* generate lowercased version of cell name */
357 strcpy(tcell_l, tcell);
360 *tmp = tolower(*tmp);
365 /* determine local cell name. It's static, so will only get
366 * calculated the first time through */
368 afsconf_GetLocalCell(adir, lcell, sizeof(lcell));
370 /* if running a krb environment, also get the local realm */
371 /* note - this assumes AFS_REALM_SZ <= MAXCELLCHARS */
372 /* just set it to lcell if it fails */
373 if (num_lrealms == -1) {
374 for (i=0; i<AFS_NUM_LREALMS; i++) {
375 if (afs_krb_get_lrealm(lrealms[i], i) != 0 /*KSUCCESS*/)
380 strncpy(lrealms[0], lcell, AFS_REALM_SZ);
387 /* See if the ticket cell matches one of the local realms */
389 for ( i=0;i<num_lrealms;i++ ) {
390 if (!strcasecmp(lrealms[i], tcell)) {
396 /* If yes, then make sure that the name is not present in
397 * an exclusion list */
399 if (tinst && tinst[0])
400 snprintf(uname,sizeof(uname),"%s.%s@%s",tname,tinst,tcell);
402 snprintf(uname,sizeof(uname),"%s@%s",tname,tcell);
404 if (afs_krb_exclusion(uname))
408 /* start with no uname and no authorization */
412 /* localauth special case */
413 if ((tinst == NULL || strlen(tinst) == 0) &&
414 (tcell == NULL || strlen(tcell) == 0)
415 && !strcmp(tname, AUTH_SUPERUSER)) {
416 strcpy(uname, "<LocalAuth>");
419 /* cell of connection matches local cell or one of the realms */
420 } else if (!strcasecmp(tcell, lcell) || lrealm_match) {
421 if ((tmp = CompFindUser(adir, tname, ".", tinst, NULL))) {
425 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, NULL))) {
430 /* cell of conn doesn't match local cell or realm */
432 if ((tmp = CompFindUser(adir, tname, ".", tinst, tcell))) {
436 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, tcell))) {
440 } else if ((tmp = CompFindUser(adir, tname, ".", tinst, tcell_l))) {
444 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, tcell_l))) {
452 strcpy(namep, uname);
458 rxkadSuperUser(struct afsconf_dir *adir, struct rx_call *acall, char *namep)
460 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
461 char tinst[MAXKTCNAMELEN];
462 char tcell[MAXKTCREALMLEN];
467 /* get auth details from server connection */
468 code = rxkad_GetServerInfo(acall->conn, NULL, &exp, tname, tinst, tcell,
471 return 0; /* bogus connection/other error */
473 return kerberosSuperUser(adir, tname, tinst, tcell, namep);
476 /* make sure user authenticated on rx call acall is in list of valid
477 users. Copy the "real name" of the authenticated user into namep
478 if a pointer is passed.
481 afsconf_SuperUser(struct afsconf_dir *adir, struct rx_call *acall, char *namep)
483 struct rx_connection *tconn;
493 if (afsconf_GetNoAuthFlag(adir)) {
495 strcpy(namep, "<NoAuth>");
500 tconn = rx_ConnectionOf(acall);
501 code = rx_SecurityClassOf(tconn);
504 return 0; /* not authenticated at all, answer is no */
505 } else if (code == 1) {
508 return 0; /* not supported any longer */
509 } else if (code == 2) {
510 flag = rxkadSuperUser(adir, acall, namep);
513 } else { /* some other auth type */
515 return 0; /* mysterious, just say no */