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() */
37 #include <rx/rx_identity.h>
39 #include <afs/afsutil.h>
40 #include <afs/fileutil.h>
42 #ifdef AFS_ATHENA_STDENV
47 #include "cellconfig.h"
49 #include "afs/audit.h"
51 static int ParseLine(char *buffer, struct rx_identity *user);
54 UserListFileName(struct afsconf_dir *adir,
55 char *buffer, size_t len)
57 strcompose(buffer, len, adir->name, "/",
58 AFSDIR_ULIST_FILE, NULL);
63 afsconf_CheckAuth(void *arock, struct rx_call *acall)
65 struct afsconf_dir *adir = (struct afsconf_dir *) arock;
68 rc = ((afsconf_SuperUser(adir, acall, NULL) == 0) ? 10029 : 0);
72 #endif /* !defined(UKERNEL) */
75 GetNoAuthFlag(struct afsconf_dir *adir)
77 if (access(AFSDIR_SERVER_NOAUTH_FILEPATH, 0) == 0) {
78 osi_audit(NoAuthEvent, 0, AUD_END); /* some random server is running noauth */
79 return 1; /* if /usr/afs/local/NoAuth file exists, allow access */
86 afsconf_GetNoAuthFlag(struct afsconf_dir *adir)
91 rc = GetNoAuthFlag(adir);
97 afsconf_SetNoAuthFlag(struct afsconf_dir *adir, int aflag)
103 /* turn off noauth flag */
104 code = (unlink(AFSDIR_SERVER_NOAUTH_FILEPATH) ? errno : 0);
105 osi_audit(NoAuthDisableEvent, code, AUD_END);
107 /* try to create file */
109 open(AFSDIR_SERVER_NOAUTH_FILEPATH, O_CREAT | O_TRUNC | O_RDWR,
113 osi_audit(NoAuthEnableEvent, 0, AUD_END);
115 osi_audit(NoAuthEnableEvent, errno, AUD_END);
121 * Remove an identity from the UserList file
123 * This function removes the given identity from the user list file.
124 * For the purposes of identifying entries to remove, only the
125 * type and exportedName portions of the identity are used. Callers
126 * should remember that a given identity may be listed in the file in
127 * a number of different ways.
130 * A structure representing the configuration directory currently
133 * The RX identity to delete
136 * 0 on success, an error code on failure
140 afsconf_DeleteIdentity(struct afsconf_dir *adir, struct rx_identity *user)
151 struct rx_identity identity;
155 UserListFileName(adir, tbuffer, sizeof tbuffer);
159 * We attempt to fully resolve this pathname, so that the rename
160 * of the temporary file will work even if UserList is a symlink
161 * into a different filesystem.
163 char resolved_path[1024];
165 if (realpath(tbuffer, resolved_path)) {
166 strcpy(tbuffer, resolved_path);
169 #endif /* AFS_NT40_ENV */
170 tf = fopen(tbuffer, "r");
175 code = stat(tbuffer, &tstat);
180 strcpy(nbuffer, tbuffer);
181 strcat(nbuffer, ".NXX");
182 nf = fopen(nbuffer, "w+");
191 /* check for our user id */
192 tp = fgets(nbuffer, sizeof(nbuffer), tf);
196 copy = strdup(nbuffer);
201 code = ParseLine(copy, &identity);
202 if (code == 0 && rx_identity_match(user, &identity)) {
203 /* found the guy, don't copy to output file */
206 /* otherwise copy original line to output */
207 fprintf(nf, "%s", nbuffer);
210 rx_identity_freeContents(&identity);
215 if (fclose(nf) == EOF)
217 strcpy(nbuffer, tbuffer);
218 strcat(nbuffer, ".NXX"); /* generate new file name again */
221 flag = renamefile(nbuffer, tbuffer);
223 flag = chmod(tbuffer, tstat.st_mode);
227 /* finally, decide what to return to the caller */
230 return EIO; /* something mysterious went wrong */
232 return ENOENT; /* entry wasn't found, no changes made */
233 return 0; /* everything was fine */
237 * Remove a legacy Kerberos 4 name from the UserList file.
239 * This function removes a Kerberos 4 name from the super user list. It
240 * can only remove names which were added by the afsconf_AddUser interface,
241 * or with an explicit Kerberos v4 type.
244 * A structure representing the configuration directory
246 * The Kerberos v4 name to remove
249 * 0 on success, an error code upon failure.
251 * Note that this function is deprecated. New callers should use
252 * afsconf_DeleteIdentity instead.
256 afsconf_DeleteUser(struct afsconf_dir *adir, char *name)
258 struct rx_identity *user;
261 user = rx_identity_new(RX_ID_KRB4, name, name, strlen(name));
265 code = afsconf_DeleteIdentity(adir, user);
267 rx_identity_free(&user);
272 /* This is a multi-purpose funciton for use by either
273 * GetNthIdentity or GetNthUser. The parameter 'id' indicates
274 * whether we are counting all identities (if true), or just
275 * ones which can be represented by the old-style interfaces
278 GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
279 struct rx_identity **identity, int id)
283 struct rx_identity fileUser;
287 UserListFileName(dir, tbuffer, sizeof(tbuffer));
288 bp = BufioOpen(tbuffer, O_RDONLY, 0);
294 code = BufioGets(bp, tbuffer, sizeof(tbuffer));
298 code = ParseLine(tbuffer, &fileUser);
302 if (id || fileUser.kind == RX_ID_KRB4)
308 rx_identity_freeContents(&fileUser);
311 *identity = rx_identity_copy(&fileUser);
312 rx_identity_freeContents(&fileUser);
322 * Return the Nth super user identity from the UserList
325 * A structure representing the configuration directory
327 * A count (from zero) of the entries to return from the
329 * @param[out] identity
330 * A pointer to the Nth identity
332 * 0 on success, non-zero on failure
336 afsconf_GetNthIdentity(struct afsconf_dir *dir, int count,
337 struct rx_identity **identity)
339 return GetNthIdentityOrUser(dir, count, identity, 1);
343 * Return the Nth Kerberos v4 identity from the UserList
345 * This returns the Nth old, kerberos v4 style name from
346 * the UserList file. In counting entries it skips any other
347 * name types it encounters - so will hide any new-style
348 * identities from its callers.
351 * A structure representing the configuration directory
353 * A count (from zero) of the entries to return from the
356 * A string in which to write the name of the Nth identity
358 * The length of the buffer passed in abuffer
360 * 0 on success, non-zero on failure
362 * This function is deprecated, all new callers should use
363 * GetNthIdentity instead. This function is particularly dangerous
364 * as it will hide any new-style identities from callers.
368 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
369 afs_int32 abufferLen)
371 struct rx_identity *identity;
374 code = GetNthIdentityOrUser(adir, an, &identity, 0);
376 strlcpy(abuffer, identity->displayName, abufferLen);
377 rx_identity_free(&identity);
383 * Parse a UserList list
385 * Parse a line of data from a UserList file
387 * This parses a line of data in a UserList, and populates the passed
388 * rx_identity structure with the information about the user.
390 * @param buffer A string containing the line to be parsed
391 * @param user The user structure to be populated
393 * Note that the user->displayName, and user->exportedName.val fields
394 * must be freed with free() by the caller.
396 * This function damages the buffer thats passed to it. Callers are
397 * expected to make a copy if they want the buffer preserved.
400 * 0 on success, non-zero on failure.
404 ParseLine(char *buffer, struct rx_identity *user)
415 if (buffer[0] == ' ') { /* extended names have leading space */
417 code = sscanf(ptr, "%i", &kind);
421 strsep(&ptr, " "); /* skip the bit we just read with scanf */
422 ename = strsep(&ptr, " "); /* Pull out the ename */
423 displayName = strsep(&ptr, " "); /* Display name runs to the end */
424 if (ename == NULL || displayName == NULL)
427 decodedName = malloc(strlen(ename));
428 if (decodedName == NULL)
431 len = base64_decode(ename, decodedName);
437 rx_identity_populate(user, kind, displayName, decodedName, len);
440 return 0; /* Success ! */
443 /* No extended name, try for a legacy name */
444 code = sscanf(buffer, "%64s", name);
448 rx_identity_populate(user, RX_ID_KRB4, name, name, strlen(name));
453 * Check if a given identity is in the UserList file,
454 * and thus is a super user
457 * A structure representing the configuration directory to check
459 * The identity to check
461 * True if the user is listed in the UserList, otherwise false
465 afsconf_IsSuperIdentity(struct afsconf_dir *adir,
466 struct rx_identity *user)
470 struct rx_identity fileUser;
474 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
476 bp = BufioOpen(tbuffer, O_RDONLY, 0);
481 code = BufioGets(bp, tbuffer, sizeof(tbuffer));
485 code = ParseLine(tbuffer, &fileUser);
489 match = rx_identity_match(user, &fileUser);
491 rx_identity_freeContents(&fileUser);
497 /* add a user to the user list, checking for duplicates */
499 afsconf_AddIdentity(struct afsconf_dir *adir, struct rx_identity *user)
507 if (afsconf_IsSuperIdentity(adir, user)) {
509 return EEXIST; /* already in the list */
512 strcompose(tbuffer, sizeof tbuffer, adir->name, "/", AFSDIR_ULIST_FILE,
514 tf = fopen(tbuffer, "a+");
519 if (user->kind == RX_ID_KRB4) {
520 fprintf(tf, "%s\n", user->displayName);
522 base64_encode(user->exportedName.val, user->exportedName.len,
524 fprintf(tf, " %d %s %s\n", user->kind, ename, user->displayName);
537 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
539 struct rx_identity *user;
542 user = rx_identity_new(RX_ID_KRB4, aname, aname, strlen(aname));
546 code = afsconf_AddIdentity(adir, user);
548 rx_identity_free(&user);
553 /* special CompFindUser routine that builds up a princ and then
554 calls finduser on it. If found, returns char * to user string,
555 otherwise returns NULL. The resulting string should be immediately
556 copied to other storage prior to release of mutex. */
558 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
559 char *realm, struct rx_identity **identity)
561 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
562 struct rx_identity *testId;
564 /* always must have name */
565 if (!name || !name[0]) {
568 strcpy(fullname, name);
570 /* might have instance */
571 if (inst && inst[0]) {
572 if (!sep || !sep[0]) {
576 strcat(fullname, sep);
577 strcat(fullname, inst);
580 /* might have realm */
581 if (realm && realm[0]) {
582 strcat(fullname, "@");
583 strcat(fullname, realm);
586 testId = rx_identity_new(RX_ID_KRB4, fullname, fullname, strlen(fullname));
587 if (afsconf_IsSuperIdentity(adir, testId)) {
591 rx_identity_free(&testId);
595 rx_identity_free(&testId);
600 kerberosSuperUser(struct afsconf_dir *adir, char *tname, char *tinst,
601 char *tcell, struct rx_identity **identity)
603 char tcell_l[MAXKTCREALMLEN] = "";
606 /* keep track of which one actually authorized request */
607 char uname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
609 static char lcell[MAXCELLCHARS] = "";
610 static char lrealms[AFS_NUM_LREALMS][AFS_REALM_SZ];
611 static int num_lrealms = -1;
612 int lrealm_match = 0, i;
615 /* generate lowercased version of cell name */
617 strcpy(tcell_l, tcell);
620 *tmp = tolower(*tmp);
625 /* determine local cell name. It's static, so will only get
626 * calculated the first time through */
628 afsconf_GetLocalCell(adir, lcell, sizeof(lcell));
630 /* if running a krb environment, also get the local realm */
631 /* note - this assumes AFS_REALM_SZ <= MAXCELLCHARS */
632 /* just set it to lcell if it fails */
633 if (num_lrealms == -1) {
634 for (i=0; i<AFS_NUM_LREALMS; i++) {
635 if (afs_krb_get_lrealm(lrealms[i], i) != 0 /*KSUCCESS*/)
640 strncpy(lrealms[0], lcell, AFS_REALM_SZ);
647 /* See if the ticket cell matches one of the local realms */
649 for ( i=0;i<num_lrealms;i++ ) {
650 if (!strcasecmp(lrealms[i], tcell)) {
656 /* If yes, then make sure that the name is not present in
657 * an exclusion list */
659 if (tinst && tinst[0])
660 snprintf(uname,sizeof(uname),"%s.%s@%s",tname,tinst,tcell);
662 snprintf(uname,sizeof(uname),"%s@%s",tname,tcell);
664 if (afs_krb_exclusion(uname))
668 /* start with no uname and no authorization */
672 /* localauth special case */
673 if ((tinst == NULL || strlen(tinst) == 0) &&
674 (tcell == NULL || strlen(tcell) == 0)
675 && !strcmp(tname, AUTH_SUPERUSER)) {
676 strcpy(uname, "<LocalAuth>");
679 /* cell of connection matches local cell or one of the realms */
680 } else if (!strcasecmp(tcell, lcell) || lrealm_match) {
681 if (CompFindUser(adir, tname, ".", tinst, NULL, identity)) {
684 /* cell of conn doesn't match local cell or realm */
686 if (CompFindUser(adir, tname, ".", tinst, tcell, identity)) {
688 } else if (CompFindUser(adir, tname, ".", tinst, tcell_l, identity)) {
697 rxkadSuperUser(struct afsconf_dir *adir, struct rx_call *acall,
698 struct rx_identity **identity)
700 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
701 char tinst[MAXKTCNAMELEN];
702 char tcell[MAXKTCREALMLEN];
707 /* get auth details from server connection */
708 code = rxkad_GetServerInfo(acall->conn, NULL, &exp, tname, tinst, tcell,
711 return 0; /* bogus connection/other error */
713 return kerberosSuperUser(adir, tname, tinst, tcell, identity);
717 * Check whether the user authenticated on a given RX call is a super
718 * user or not. If they are, return a pointer to the identity of that
722 * The configuration directory currently in use
724 * The RX call whose authenticated identity is being checked
725 * @param[out] identity
726 * The RX identity of the user. Caller must free this structure.
728 * True if the user is a super user, or if the server is running
729 * in noauth mode. Otherwise, false.
732 afsconf_SuperIdentity(struct afsconf_dir *adir, struct rx_call *acall,
733 struct rx_identity **identity)
735 struct rx_connection *tconn;
745 if (afsconf_GetNoAuthFlag(adir)) {
747 *identity = rx_identity_new(RX_ID_KRB4, "<NoAuth>", "<NoAuth>", 8);
752 tconn = rx_ConnectionOf(acall);
753 code = rx_SecurityClassOf(tconn);
756 return 0; /* not authenticated at all, answer is no */
757 } else if (code == 1) {
760 return 0; /* not supported any longer */
761 } else if (code == 2) {
762 flag = rxkadSuperUser(adir, acall, identity);
765 } else { /* some other auth type */
767 return 0; /* mysterious, just say no */
772 * Check whether the user authenticated on a given RX call is a super
773 * user or not. If they are, return a pointer to the name of that
777 * The configuration directory currently in use
779 * The RX call whose authenticated identity is being checked
781 * A printable version of the name of the user
783 * True if the user is a super user, or if the server is running
784 * in noauth mode. Otherwise, false.
786 * This function is provided for backwards compatibility. New callers
787 * should use the afsconf_SuperIdentity function.
791 afsconf_SuperUser(struct afsconf_dir *adir, struct rx_call *acall,
794 struct rx_identity *identity;
797 code = afsconf_SuperIdentity(adir, acall, &identity);
799 if (identity->kind == RX_ID_KRB4) {
800 strlcpy(namep, identity->displayName, MAXKTCNAMELEN-1);
802 snprintf(namep, MAXKTCNAMELEN-1, "eName: %s",
803 identity->displayName);
806 rx_identity_free(&identity);