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>
19 #include <afs/pthread_glock.h>
22 #include <rx/rx_identity.h>
23 #include <afs/afsutil.h>
24 #include <afs/fileutil.h>
28 #include "cellconfig.h"
30 #include "afs/audit.h"
32 /* The display names for localauth and noauth identities; they aren't used
33 * inside tickets or anything, but just serve as something to display in logs,
35 #define AFS_LOCALAUTH_NAME "<LocalAuth>"
36 #define AFS_LOCALAUTH_LEN (sizeof(AFS_LOCALAUTH_NAME)-1)
37 #define AFS_NOAUTH_NAME "<NoAuth>"
38 #define AFS_NOAUTH_LEN (sizeof(AFS_NOAUTH_NAME)-1)
40 static int ParseLine(char *buffer, struct rx_identity *user);
43 UserListFileName(struct afsconf_dir *adir,
44 char *buffer, size_t len)
46 strcompose(buffer, len, adir->name, "/", AFSDIR_ULIST_FILE, (char *)NULL);
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);
109 * Remove an identity from the UserList file
111 * This function removes the given identity from the user list file.
112 * For the purposes of identifying entries to remove, only the
113 * type and exportedName portions of the identity are used. Callers
114 * should remember that a given identity may be listed in the file in
115 * a number of different ways.
118 * A structure representing the configuration directory currently
121 * The RX identity to delete
124 * 0 on success, an error code on failure
128 afsconf_DeleteIdentity(struct afsconf_dir *adir, struct rx_identity *user)
130 char *filename, *nfilename;
139 struct rx_identity identity;
142 memset(&identity, 0, sizeof(struct rx_identity));
144 buffer = malloc(AFSDIR_PATH_MAX);
147 filename = malloc(AFSDIR_PATH_MAX);
148 if (filename == NULL) {
154 UserListFileName(adir, filename, AFSDIR_PATH_MAX);
158 * We attempt to fully resolve this pathname, so that the rename
159 * of the temporary file will work even if UserList is a symlink
160 * into a different filesystem.
162 nfilename = malloc(AFSDIR_PATH_MAX);
163 if (nfilename == NULL) {
169 if (realpath(filename, nfilename)) {
171 filename = nfilename;
176 #endif /* AFS_NT40_ENV */
177 if (asprintf(&nfilename, "%s.NXX", filename) < 0) {
183 tf = fopen(filename, "r");
191 code = stat(filename, &tstat);
199 nf = fopen(nfilename, "w+");
211 /* check for our user id */
212 tp = fgets(buffer, AFSDIR_PATH_MAX, tf);
216 copy = strdup(buffer);
221 code = ParseLine(copy, &identity);
222 if (code == 0 && rx_identity_match(user, &identity)) {
223 /* found the guy, don't copy to output file */
226 /* otherwise copy original line to output */
227 fprintf(nf, "%s", buffer);
230 rx_identity_freeContents(&identity);
236 if (fclose(nf) == EOF)
240 flag = rk_rename(nfilename, filename);
242 flag = chmod(filename, tstat.st_mode);
246 /* finally, decide what to return to the caller */
251 return EIO; /* something mysterious went wrong */
253 return ENOENT; /* entry wasn't found, no changes made */
254 return 0; /* everything was fine */
258 * Remove a legacy Kerberos 4 name from the UserList file.
260 * This function removes a Kerberos 4 name from the super user list. It
261 * can only remove names which were added by the afsconf_AddUser interface,
262 * or with an explicit Kerberos v4 type.
265 * A structure representing the configuration directory
267 * The Kerberos v4 name to remove
270 * 0 on success, an error code upon failure.
272 * Note that this function is deprecated. New callers should use
273 * afsconf_DeleteIdentity instead.
277 afsconf_DeleteUser(struct afsconf_dir *adir, char *name)
279 struct rx_identity *user;
282 user = rx_identity_new(RX_ID_KRB4, name, name, strlen(name));
286 code = afsconf_DeleteIdentity(adir, user);
288 rx_identity_free(&user);
293 /* This is a multi-purpose funciton for use by either
294 * GetNthIdentity or GetNthUser. The parameter 'id' indicates
295 * whether we are counting all identities (if true), or just
296 * ones which can be represented by the old-style interfaces
299 GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
300 struct rx_identity **identity, int id)
304 struct rx_identity fileUser;
307 tbuffer = malloc(AFSDIR_PATH_MAX);
312 UserListFileName(dir, tbuffer, AFSDIR_PATH_MAX);
313 bp = BufioOpen(tbuffer, O_RDONLY, 0);
320 code = BufioGets(bp, tbuffer, AFSDIR_PATH_MAX);
324 code = ParseLine(tbuffer, &fileUser);
328 if (id || fileUser.kind == RX_ID_KRB4)
334 rx_identity_freeContents(&fileUser);
337 *identity = rx_identity_copy(&fileUser);
338 rx_identity_freeContents(&fileUser);
349 * Return the Nth super user identity from the UserList
352 * A structure representing the configuration directory
354 * A count (from zero) of the entries to return from the
356 * @param[out] identity
357 * A pointer to the Nth identity
359 * 0 on success, non-zero on failure
363 afsconf_GetNthIdentity(struct afsconf_dir *dir, int count,
364 struct rx_identity **identity)
366 return GetNthIdentityOrUser(dir, count, identity, 1);
370 * Return the Nth Kerberos v4 identity from the UserList
372 * This returns the Nth old, kerberos v4 style name from
373 * the UserList file. In counting entries it skips any other
374 * name types it encounters - so will hide any new-style
375 * identities from its callers.
378 * A structure representing the configuration directory
380 * A count (from zero) of the entries to return from the
383 * A string in which to write the name of the Nth identity
385 * The length of the buffer passed in abuffer
387 * 0 on success, non-zero on failure
389 * This function is deprecated, all new callers should use
390 * GetNthIdentity instead. This function is particularly dangerous
391 * as it will hide any new-style identities from callers.
395 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
396 afs_int32 abufferLen)
398 struct rx_identity *identity;
401 code = GetNthIdentityOrUser(adir, an, &identity, 0);
403 strlcpy(abuffer, identity->displayName, abufferLen);
404 rx_identity_free(&identity);
410 * Parse a UserList list
412 * Parse a line of data from a UserList file
414 * This parses a line of data in a UserList, and populates the passed
415 * rx_identity structure with the information about the user.
417 * @param buffer A string containing the line to be parsed
418 * @param user The user structure to be populated
420 * Note that the user->displayName, and user->exportedName.val fields
421 * must be freed with free() by the caller.
423 * This function damages the buffer thats passed to it. Callers are
424 * expected to make a copy if they want the buffer preserved.
427 * 0 on success, non-zero on failure.
431 ParseLine(char *buffer, struct rx_identity *user)
442 if (buffer[0] == ' ') { /* extended names have leading space */
444 code = sscanf(ptr, "%i", &kind);
448 strsep(&ptr, " "); /* skip the bit we just read with scanf */
449 ename = strsep(&ptr, " "); /* Pull out the ename */
450 displayName = strsep(&ptr, " "); /* Display name runs to the end */
451 if (ename == NULL || displayName == NULL)
454 decodedName = malloc(strlen(ename));
455 if (decodedName == NULL)
458 len = base64_decode(ename, decodedName);
464 rx_identity_populate(user, kind, displayName, decodedName, len);
467 return 0; /* Success ! */
470 /* No extended name, try for a legacy name */
471 code = sscanf(buffer, "%64s", name);
475 rx_identity_populate(user, RX_ID_KRB4, name, name, strlen(name));
480 * Check if a given identity is in the UserList file,
481 * and thus is a super user
484 * A structure representing the configuration directory to check
486 * The identity to check
488 * True if the user is listed in the UserList, otherwise false
492 afsconf_IsSuperIdentity(struct afsconf_dir *adir,
493 struct rx_identity *user)
497 struct rx_identity fileUser;
501 tbuffer = malloc(AFSDIR_PATH_MAX);
505 UserListFileName(adir, tbuffer, AFSDIR_PATH_MAX);
506 bp = BufioOpen(tbuffer, O_RDONLY, 0);
513 code = BufioGets(bp, tbuffer, AFSDIR_PATH_MAX);
517 code = ParseLine(tbuffer, &fileUser);
521 match = rx_identity_match(user, &fileUser);
523 rx_identity_freeContents(&fileUser);
530 /* add a user to the user list, checking for duplicates */
532 afsconf_AddIdentity(struct afsconf_dir *adir, struct rx_identity *user)
540 if (afsconf_IsSuperIdentity(adir, user)) {
542 return EEXIST; /* already in the list */
545 tbuffer = malloc(AFSDIR_PATH_MAX);
546 UserListFileName(adir, tbuffer, AFSDIR_PATH_MAX);
547 tf = fopen(tbuffer, "a+");
553 if (user->kind == RX_ID_KRB4) {
554 fprintf(tf, "%s\n", user->displayName);
556 base64_encode(user->exportedName.val, user->exportedName.len,
558 fprintf(tf, " %d %s %s\n", user->kind, ename, user->displayName);
571 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
573 struct rx_identity *user;
576 user = rx_identity_new(RX_ID_KRB4, aname, aname, strlen(aname));
580 code = afsconf_AddIdentity(adir, user);
582 rx_identity_free(&user);
587 /* special CompFindUser routine that builds up a princ and then
588 calls finduser on it. If found, returns char * to user string,
589 otherwise returns NULL. The resulting string should be immediately
590 copied to other storage prior to release of mutex. */
592 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
593 char *realm, struct rx_identity **identity)
595 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
596 struct rx_identity *testId;
598 /* always must have name */
599 if (!name || !name[0]) {
603 if (strlcpy(fullname, name, sizeof(fullname)) >= sizeof(fullname))
606 /* might have instance */
607 if (inst && inst[0]) {
608 if (!sep || !sep[0]) {
612 if (strlcat(fullname, sep, sizeof(fullname)) >= sizeof(fullname))
615 if (strlcat(fullname, inst, sizeof(fullname)) >= sizeof(fullname))
619 /* might have realm */
620 if (realm && realm[0]) {
621 if (strlcat(fullname, "@", sizeof(fullname)) >= sizeof(fullname))
624 if (strlcat(fullname, realm, sizeof(fullname)) >= sizeof(fullname))
628 testId = rx_identity_new(RX_ID_KRB4, fullname, fullname, strlen(fullname));
629 if (afsconf_IsSuperIdentity(adir, testId)) {
633 rx_identity_free(&testId);
637 rx_identity_free(&testId);
642 kerberosSuperUser(struct afsconf_dir *adir, char *tname, char *tinst,
643 char *tcell, struct rx_identity **identity)
645 char tcell_l[MAXKTCREALMLEN] = "";
650 /* generate lowercased version of cell name */
652 opr_lcstring(tcell_l, tcell, sizeof(tcell_l));
654 code = afsconf_IsLocalRealmMatch(adir, &islocal, tname, tinst, tcell);
659 /* start with no authorization */
662 /* localauth special case */
663 if ((tinst == NULL || strlen(tinst) == 0) &&
664 (tcell == NULL || strlen(tcell) == 0)
665 && !strcmp(tname, AUTH_SUPERUSER)) {
667 *identity = rx_identity_new(RX_ID_KRB4, AFS_LOCALAUTH_NAME,
668 AFS_LOCALAUTH_NAME, AFS_LOCALAUTH_LEN);
671 /* cell of connection matches local cell or one of the realms */
672 } else if (islocal) {
673 if (CompFindUser(adir, tname, ".", tinst, NULL, identity)) {
676 /* cell of conn doesn't match local cell or realm */
678 if (CompFindUser(adir, tname, ".", tinst, tcell, identity)) {
680 } else if (CompFindUser(adir, tname, ".", tinst, tcell_l, identity)) {
689 rxkadSuperUser(struct afsconf_dir *adir, struct rx_call *acall,
690 struct rx_identity **identity)
692 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
693 char tinst[MAXKTCNAMELEN];
694 char tcell[MAXKTCREALMLEN];
699 /* get auth details from server connection */
700 code = rxkad_GetServerInfo(rx_ConnectionOf(acall), NULL, &exp, tname,
703 return 0; /* bogus connection/other error */
705 return kerberosSuperUser(adir, tname, tinst, tcell, identity);
709 * Check whether the user authenticated on a given RX call is a super
710 * user or not. If they are, return a pointer to the identity of that
714 * The configuration directory currently in use
716 * The RX call whose authenticated identity is being checked
717 * @param[out] identity
718 * The RX identity of the user. Caller must free this structure.
720 * True if the user is a super user, or if the server is running
721 * in noauth mode. Otherwise, false.
724 afsconf_SuperIdentity(struct afsconf_dir *adir, struct rx_call *acall,
725 struct rx_identity **identity)
727 struct rx_connection *tconn;
737 if (afsconf_GetNoAuthFlag(adir)) {
739 *identity = rx_identity_new(RX_ID_KRB4, AFS_NOAUTH_NAME,
740 AFS_NOAUTH_NAME, AFS_NOAUTH_LEN);
745 tconn = rx_ConnectionOf(acall);
746 code = rx_SecurityClassOf(tconn);
749 return 0; /* not authenticated at all, answer is no */
750 } else if (code == 1) {
753 return 0; /* not supported any longer */
754 } else if (code == 2) {
755 flag = rxkadSuperUser(adir, acall, identity);
758 } else { /* some other auth type */
760 return 0; /* mysterious, just say no */
765 * Check whether the user authenticated on a given RX call is a super
766 * user or not. If they are, return a pointer to the name of that
770 * The configuration directory currently in use
772 * The RX call whose authenticated identity is being checked
774 * A printable version of the name of the user
776 * True if the user is a super user, or if the server is running
777 * in noauth mode. Otherwise, false.
779 * This function is provided for backwards compatibility. New callers
780 * should use the afsconf_SuperIdentity function.
784 afsconf_SuperUser(struct afsconf_dir *adir, struct rx_call *acall,
787 struct rx_identity *identity;
791 ret = afsconf_SuperIdentity(adir, acall, &identity);
793 if (identity->kind == RX_ID_KRB4) {
794 strlcpy(namep, identity->displayName, MAXKTCNAMELEN-1);
796 snprintf(namep, MAXKTCNAMELEN-1, "eName: %s",
797 identity->displayName);
799 rx_identity_free(&identity);
802 ret = afsconf_SuperIdentity(adir, acall, NULL);
809 * Check whether the user authenticated on a given RX call is
810 * compatible with the access specified by needed_level.
813 * The configuration directory currently in use
815 * The RX call whose authenticated identity is being checked
816 * @param[in] needed_level
817 * Either RESTRICTED_QUERY_ANYUSER for allowing any access or
818 * RESTRICTED_QUERY_ADMIN for allowing super user only.
820 * True if the user is compatible with needed_level.
825 afsconf_CheckRestrictedQuery(struct afsconf_dir *adir,
826 struct rx_call *acall,
829 if (needed_level == RESTRICTED_QUERY_ANYUSER)
832 return afsconf_SuperIdentity(adir, acall, NULL);