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
297 * We return -1 for EOF, 0 for success, and >0 for all errors.
300 GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
301 struct rx_identity **identity, int id)
305 struct rx_identity fileUser;
308 tbuffer = malloc(AFSDIR_PATH_MAX);
313 UserListFileName(dir, tbuffer, AFSDIR_PATH_MAX);
314 bp = BufioOpen(tbuffer, O_RDONLY, 0);
321 code = BufioGets(bp, tbuffer, AFSDIR_PATH_MAX);
327 code = ParseLine(tbuffer, &fileUser);
331 if (id || fileUser.kind == RX_ID_KRB4)
337 rx_identity_freeContents(&fileUser);
340 *identity = rx_identity_copy(&fileUser);
341 rx_identity_freeContents(&fileUser);
352 * Return the Nth super user identity from the UserList
355 * A structure representing the configuration directory
357 * A count (from zero) of the entries to return from the
359 * @param[out] identity
360 * A pointer to the Nth identity
364 * @retval -1 We have searched beyond the end of the list.
369 afsconf_GetNthIdentity(struct afsconf_dir *dir, int count,
370 struct rx_identity **identity)
372 return GetNthIdentityOrUser(dir, count, identity, 1);
376 * Return the Nth Kerberos v4 identity from the UserList
378 * This returns the Nth old, kerberos v4 style name from
379 * the UserList file. In counting entries it skips any other
380 * name types it encounters - so will hide any new-style
381 * identities from its callers.
384 * A structure representing the configuration directory
386 * A count (from zero) of the entries to return from the
389 * A string in which to write the name of the Nth identity
391 * The length of the buffer passed in abuffer
395 * @retval 1 Either an EPERM error, or we have searched beyond the end of the
397 * @retval >1 All other errors.
399 * This function is deprecated, all new callers should use
400 * GetNthIdentity instead. This function is particularly dangerous
401 * as it will hide any new-style identities from callers. It is also
402 * impossible to distinguish an EPERM error from a normal end-of-file
403 * condition with this function.
407 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
408 afs_int32 abufferLen)
410 struct rx_identity *identity;
413 code = GetNthIdentityOrUser(adir, an, &identity, 0);
415 strlcpy(abuffer, identity->displayName, abufferLen);
416 rx_identity_free(&identity);
419 /* The new functions use -1 to indicate EOF, but the old interface
420 * uses 1 to indicate EOF. */
427 * Parse a UserList list
429 * Parse a line of data from a UserList file
431 * This parses a line of data in a UserList, and populates the passed
432 * rx_identity structure with the information about the user.
434 * @param buffer A string containing the line to be parsed
435 * @param user The user structure to be populated
437 * Note that the user->displayName, and user->exportedName.val fields
438 * must be freed with free() by the caller.
440 * This function damages the buffer thats passed to it. Callers are
441 * expected to make a copy if they want the buffer preserved.
444 * 0 on success, non-zero on failure.
448 ParseLine(char *buffer, struct rx_identity *user)
459 if (buffer[0] == ' ') { /* extended names have leading space */
461 code = sscanf(ptr, "%i", &kind);
465 strsep(&ptr, " "); /* skip the bit we just read with scanf */
466 ename = strsep(&ptr, " "); /* Pull out the ename */
467 displayName = strsep(&ptr, " "); /* Display name runs to the end */
468 if (ename == NULL || displayName == NULL)
471 decodedName = malloc(strlen(ename));
472 if (decodedName == NULL)
475 len = base64_decode(ename, decodedName);
481 rx_identity_populate(user, kind, displayName, decodedName, len);
484 return 0; /* Success ! */
487 /* No extended name, try for a legacy name */
488 code = sscanf(buffer, "%64s", name);
492 rx_identity_populate(user, RX_ID_KRB4, name, name, strlen(name));
497 * Check if a given identity is in the UserList file,
498 * and thus is a super user
501 * A structure representing the configuration directory to check
503 * The identity to check
505 * True if the user is listed in the UserList, otherwise false
509 afsconf_IsSuperIdentity(struct afsconf_dir *adir,
510 struct rx_identity *user)
514 struct rx_identity fileUser;
518 tbuffer = malloc(AFSDIR_PATH_MAX);
522 UserListFileName(adir, tbuffer, AFSDIR_PATH_MAX);
523 bp = BufioOpen(tbuffer, O_RDONLY, 0);
530 code = BufioGets(bp, tbuffer, AFSDIR_PATH_MAX);
534 code = ParseLine(tbuffer, &fileUser);
538 match = rx_identity_match(user, &fileUser);
540 rx_identity_freeContents(&fileUser);
547 /* add a user to the user list, checking for duplicates */
549 afsconf_AddIdentity(struct afsconf_dir *adir, struct rx_identity *user)
557 if (afsconf_IsSuperIdentity(adir, user)) {
559 return EEXIST; /* already in the list */
562 tbuffer = malloc(AFSDIR_PATH_MAX);
563 UserListFileName(adir, tbuffer, AFSDIR_PATH_MAX);
564 tf = fopen(tbuffer, "a+");
570 if (user->kind == RX_ID_KRB4) {
571 fprintf(tf, "%s\n", user->displayName);
573 base64_encode(user->exportedName.val, user->exportedName.len,
575 fprintf(tf, " %d %s %s\n", user->kind, ename, user->displayName);
588 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
590 struct rx_identity *user;
593 user = rx_identity_new(RX_ID_KRB4, aname, aname, strlen(aname));
597 code = afsconf_AddIdentity(adir, user);
599 rx_identity_free(&user);
604 /* special CompFindUser routine that builds up a princ and then
605 calls finduser on it. If found, returns char * to user string,
606 otherwise returns NULL. The resulting string should be immediately
607 copied to other storage prior to release of mutex. */
609 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
610 char *realm, struct rx_identity **identity)
612 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
613 struct rx_identity *testId;
615 /* always must have name */
616 if (!name || !name[0]) {
620 if (strlcpy(fullname, name, sizeof(fullname)) >= sizeof(fullname))
623 /* might have instance */
624 if (inst && inst[0]) {
625 if (!sep || !sep[0]) {
629 if (strlcat(fullname, sep, sizeof(fullname)) >= sizeof(fullname))
632 if (strlcat(fullname, inst, sizeof(fullname)) >= sizeof(fullname))
636 /* might have realm */
637 if (realm && realm[0]) {
638 if (strlcat(fullname, "@", sizeof(fullname)) >= sizeof(fullname))
641 if (strlcat(fullname, realm, sizeof(fullname)) >= sizeof(fullname))
645 testId = rx_identity_new(RX_ID_KRB4, fullname, fullname, strlen(fullname));
646 if (afsconf_IsSuperIdentity(adir, testId)) {
650 rx_identity_free(&testId);
654 rx_identity_free(&testId);
659 kerberosSuperUser(struct afsconf_dir *adir, char *tname, char *tinst,
660 char *tcell, struct rx_identity **identity)
662 char tcell_l[MAXKTCREALMLEN] = "";
667 /* generate lowercased version of cell name */
669 opr_lcstring(tcell_l, tcell, sizeof(tcell_l));
671 code = afsconf_IsLocalRealmMatch(adir, &islocal, tname, tinst, tcell);
676 /* start with no authorization */
679 /* localauth special case */
680 if ((tinst == NULL || strlen(tinst) == 0) &&
681 (tcell == NULL || strlen(tcell) == 0)
682 && !strcmp(tname, AUTH_SUPERUSER)) {
684 *identity = rx_identity_new(RX_ID_KRB4, AFS_LOCALAUTH_NAME,
685 AFS_LOCALAUTH_NAME, AFS_LOCALAUTH_LEN);
688 /* cell of connection matches local cell or one of the realms */
689 } else if (islocal) {
690 if (CompFindUser(adir, tname, ".", tinst, NULL, identity)) {
693 /* cell of conn doesn't match local cell or realm */
695 if (CompFindUser(adir, tname, ".", tinst, tcell, identity)) {
697 } else if (CompFindUser(adir, tname, ".", tinst, tcell_l, identity)) {
706 rxkadSuperUser(struct afsconf_dir *adir, struct rx_call *acall,
707 struct rx_identity **identity)
709 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
710 char tinst[MAXKTCNAMELEN];
711 char tcell[MAXKTCREALMLEN];
716 /* get auth details from server connection */
717 code = rxkad_GetServerInfo(rx_ConnectionOf(acall), NULL, &exp, tname,
720 return 0; /* bogus connection/other error */
722 return kerberosSuperUser(adir, tname, tinst, tcell, identity);
726 * Check whether the user authenticated on a given RX call is a super
727 * user or not. If they are, return a pointer to the identity of that
731 * The configuration directory currently in use
733 * The RX call whose authenticated identity is being checked
734 * @param[out] identity
735 * The RX identity of the user. Caller must free this structure.
737 * True if the user is a super user, or if the server is running
738 * in noauth mode. Otherwise, false.
741 afsconf_SuperIdentity(struct afsconf_dir *adir, struct rx_call *acall,
742 struct rx_identity **identity)
744 struct rx_connection *tconn;
754 if (afsconf_GetNoAuthFlag(adir)) {
756 *identity = rx_identity_new(RX_ID_KRB4, AFS_NOAUTH_NAME,
757 AFS_NOAUTH_NAME, AFS_NOAUTH_LEN);
762 tconn = rx_ConnectionOf(acall);
763 code = rx_SecurityClassOf(tconn);
764 if (code == RX_SECIDX_NULL) {
766 return 0; /* not authenticated at all, answer is no */
767 } else if (code == RX_SECIDX_VAB) {
770 return 0; /* not supported any longer */
771 } else if (code == RX_SECIDX_KAD) {
772 flag = rxkadSuperUser(adir, acall, identity);
775 } else { /* some other auth type */
777 return 0; /* mysterious, just say no */
782 * Check whether the user authenticated on a given RX call is a super
783 * user or not. If they are, return a pointer to the name of that
787 * The configuration directory currently in use
789 * The RX call whose authenticated identity is being checked
791 * A printable version of the name of the user
793 * True if the user is a super user, or if the server is running
794 * in noauth mode. Otherwise, false.
796 * This function is provided for backwards compatibility. New callers
797 * should use the afsconf_SuperIdentity function.
801 afsconf_SuperUser(struct afsconf_dir *adir, struct rx_call *acall,
804 struct rx_identity *identity;
808 ret = afsconf_SuperIdentity(adir, acall, &identity);
810 if (identity->kind == RX_ID_KRB4) {
811 strlcpy(namep, identity->displayName, MAXKTCNAMELEN-1);
813 snprintf(namep, MAXKTCNAMELEN-1, "eName: %s",
814 identity->displayName);
816 rx_identity_free(&identity);
819 ret = afsconf_SuperIdentity(adir, acall, NULL);
826 * Check whether the user authenticated on a given RX call is
827 * compatible with the access specified by needed_level.
830 * The configuration directory currently in use
832 * The RX call whose authenticated identity is being checked
833 * @param[in] needed_level
834 * Either RESTRICTED_QUERY_ANYUSER for allowing any access or
835 * RESTRICTED_QUERY_ADMIN for allowing super user only.
837 * True if the user is compatible with needed_level.
842 afsconf_CheckRestrictedQuery(struct afsconf_dir *adir,
843 struct rx_call *acall,
846 if (needed_level == RESTRICTED_QUERY_ANYUSER)
849 return afsconf_SuperIdentity(adir, acall, NULL);