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>
30 #include <stdlib.h> /* for realpath() */
38 #include <afs/afsutil.h>
39 #include <afs/fileutil.h>
41 #ifdef AFS_ATHENA_STDENV
46 #include "cellconfig.h"
48 #include "afs/audit.h"
52 afsconf_CheckAuth(void *arock, struct rx_call *acall)
54 struct afsconf_dir *adir = (struct afsconf_dir *) arock;
57 rc = ((afsconf_SuperUser(adir, acall, NULL) == 0) ? 10029 : 0);
61 #endif /* !defined(UKERNEL) */
64 GetNoAuthFlag(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(struct afsconf_dir *adir)
80 rc = GetNoAuthFlag(adir);
86 afsconf_SetNoAuthFlag(struct afsconf_dir *adir, int aflag)
88 register afs_int32 code;
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);
109 /* deletes a user from the UserList file */
111 afsconf_DeleteUser(struct afsconf_dir *adir, register char *auser)
122 register afs_int32 code;
125 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");
146 code = stat(tbuffer, &tstat);
151 strcpy(nbuffer, tbuffer);
152 strcat(nbuffer, ".NXX");
153 nf = fopen(nbuffer, "w+");
162 /* check for our user id */
163 tp = fgets(nbuffer, sizeof(nbuffer), tf);
166 code = sscanf(nbuffer, "%64s", tname);
167 if (code == 1 && strcmp(tname, auser) == 0) {
168 /* found the guy, don't copy to output file */
171 /* otherwise copy original line to output */
172 fprintf(nf, "%s", nbuffer);
178 if (fclose(nf) == EOF)
180 strcpy(nbuffer, tbuffer);
181 strcat(nbuffer, ".NXX"); /* generate new file name again */
184 flag = renamefile(nbuffer, tbuffer);
186 flag = chmod(tbuffer, tstat.st_mode);
190 /* finally, decide what to return to the caller */
193 return EIO; /* something mysterious went wrong */
195 return ENOENT; /* entry wasn't found, no changes made */
196 return 0; /* everything was fine */
199 /* returns nth super user from the UserList file */
201 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
202 afs_int32 abufferLen)
209 register afs_int32 code;
212 strcompose(tbuffer, sizeof tbuffer, adir->name, "/",
213 AFSDIR_ULIST_FILE, NULL);
214 tf = fopen(tbuffer, "r");
221 /* check for our user id */
222 tp = fgets(tbuffer, sizeof(tbuffer), tf);
225 code = sscanf(tbuffer, "%64s", tname);
226 if (code == 1 && an-- == 0) {
232 strcpy(abuffer, tname);
238 /* returns true iff user is in the UserList file */
240 FindUser(struct afsconf_dir *adir, register char *auser)
246 register afs_int32 code;
249 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
251 bp = BufioOpen(tbuffer, O_RDONLY, 0);
256 /* check for our user id */
257 rc = BufioGets(bp, tbuffer, sizeof(tbuffer));
260 code = sscanf(tbuffer, "%64s", tname);
261 if (code == 1 && strcmp(tname, auser) == 0) {
270 /* add a user to the user list, checking for duplicates */
272 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
275 register afs_int32 code;
279 if (FindUser(adir, aname)) {
281 return EEXIST; /* already in the list */
284 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
286 tf = fopen(tbuffer, "a+");
291 fprintf(tf, "%s\n", aname);
301 /* special CompFindUser routine that builds up a princ and then
302 calls finduser on it. If found, returns char * to user string,
303 otherwise returns NULL. The resulting string should be immediately
304 copied to other storage prior to release of mutex. */
306 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
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(struct afsconf_dir *adir, struct rx_call *acall, char *namep)
348 register struct rx_connection *tconn;
349 register afs_int32 code;
358 if (afsconf_GetNoAuthFlag(adir)) {
360 strcpy(namep, "<NoAuth>");
365 tconn = rx_ConnectionOf(acall);
366 code = rx_SecurityClassOf(tconn);
369 return 0; /* not authenticated at all, answer is no */
370 } else if (code == 1) {
373 return 0; /* not supported any longer */
374 } else if (code == 2) {
375 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
376 char tinst[MAXKTCNAMELEN];
377 char tcell[MAXKTCREALMLEN];
378 char tcell_l[MAXKTCREALMLEN];
381 /* keep track of which one actually authorized request */
382 char uname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
385 static char lcell[MAXCELLCHARS] = "";
386 static char lrealms[AFS_NUM_LREALMS][AFS_REALM_SZ];
387 static int num_lrealms = -1;
388 int lrealm_match = 0, i;
390 /* get auth details from server connection */
392 rxkad_GetServerInfo(acall->conn, NULL, &exp, tname, tinst, tcell,
396 return 0; /* bogus connection/other error */
399 /* don't bother checking anything else if tix have expired */
400 #ifdef AFS_PTHREAD_ENV
401 if (exp < clock_Sec()) {
403 if (exp < FT_ApproxTime()) {
406 return 0; /* expired tix */
409 /* generate lowercased version of cell name */
410 strcpy(tcell_l, tcell);
413 *tmp = tolower(*tmp);
417 /* determine local cell name. It's static, so will only get
418 * calculated the first time through */
420 afsconf_GetLocalCell(adir, lcell, sizeof(lcell));
422 /* if running a krb environment, also get the local realm */
423 /* note - this assumes AFS_REALM_SZ <= MAXCELLCHARS */
424 /* just set it to lcell if it fails */
425 if (num_lrealms == -1) {
426 for (i=0; i<AFS_NUM_LREALMS; i++) {
427 if (afs_krb_get_lrealm(lrealms[i], i) != 0 /*KSUCCESS*/)
432 strncpy(lrealms[0], lcell, AFS_REALM_SZ);
439 /* See if the ticket cell matches one of the local realms */
441 for ( i=0;i<num_lrealms;i++ ) {
442 if (!strcasecmp(lrealms[i], tcell)) {
448 /* If yes, then make sure that the name is not present in
449 * an exclusion list */
452 snprintf(uname,sizeof(uname),"%s.%s@%s",tname,tinst,tcell);
454 snprintf(uname,sizeof(uname),"%s@%s",tname,tcell);
456 if (afs_krb_exclusion(uname))
460 /* start with no uname and no authorization */
464 /* localauth special case */
465 if (strlen(tinst) == 0 && strlen(tcell) == 0
466 && !strcmp(tname, AUTH_SUPERUSER)) {
467 strcpy(uname, "<LocalAuth>");
470 /* cell of connection matches local cell or one of the realms */
471 } else if (!strcasecmp(tcell, lcell) || lrealm_match) {
472 if ((tmp = CompFindUser(adir, tname, ".", tinst, NULL))) {
476 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, NULL))) {
481 /* cell of conn doesn't match local cell or realm */
483 if ((tmp = CompFindUser(adir, tname, ".", tinst, tcell))) {
487 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, tcell))) {
491 } else if ((tmp = CompFindUser(adir, tname, ".", tinst, tcell_l))) {
495 } else if ((tmp = CompFindUser(adir, tname, "/", tinst, tcell_l))) {
503 strcpy(namep, uname);
506 } else { /* some other auth type */
508 return 0; /* mysterious, just say no */