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>
24 #include <afs/afsutil.h>
25 #include <afs/fileutil.h>
29 #include "cellconfig.h"
31 #include "afs/audit.h"
33 /* The display names for localauth and noauth identities; they aren't used
34 * inside tickets or anything, but just serve as something to display in logs,
36 #define AFS_LOCALAUTH_NAME "<LocalAuth>"
37 #define AFS_LOCALAUTH_LEN (sizeof(AFS_LOCALAUTH_NAME)-1)
38 #define AFS_NOAUTH_NAME "<NoAuth>"
39 #define AFS_NOAUTH_LEN (sizeof(AFS_NOAUTH_NAME)-1)
41 static int ParseLine(char *buffer, struct rx_identity *user);
44 UserListFileName(struct afsconf_dir *adir,
45 char *buffer, size_t len)
47 strcompose(buffer, len, adir->name, "/",
48 AFSDIR_ULIST_FILE, NULL);
53 afsconf_CheckAuth(void *arock, struct rx_call *acall)
55 struct afsconf_dir *adir = (struct afsconf_dir *) arock;
58 rc = ((afsconf_SuperUser(adir, acall, NULL) == 0) ? 10029 : 0);
62 #endif /* !defined(UKERNEL) */
65 GetNoAuthFlag(struct afsconf_dir *adir)
67 if (access(AFSDIR_SERVER_NOAUTH_FILEPATH, 0) == 0) {
68 osi_audit(NoAuthEvent, 0, AUD_END); /* some random server is running noauth */
69 return 1; /* if /usr/afs/local/NoAuth file exists, allow access */
76 afsconf_GetNoAuthFlag(struct afsconf_dir *adir)
81 rc = GetNoAuthFlag(adir);
87 afsconf_SetNoAuthFlag(struct afsconf_dir *adir, int aflag)
93 /* turn off noauth flag */
94 code = (unlink(AFSDIR_SERVER_NOAUTH_FILEPATH) ? errno : 0);
95 osi_audit(NoAuthDisableEvent, code, AUD_END);
97 /* try to create file */
99 open(AFSDIR_SERVER_NOAUTH_FILEPATH, O_CREAT | O_TRUNC | O_RDWR,
103 osi_audit(NoAuthEnableEvent, 0, AUD_END);
105 osi_audit(NoAuthEnableEvent, errno, AUD_END);
111 * Remove an identity from the UserList file
113 * This function removes the given identity from the user list file.
114 * For the purposes of identifying entries to remove, only the
115 * type and exportedName portions of the identity are used. Callers
116 * should remember that a given identity may be listed in the file in
117 * a number of different ways.
120 * A structure representing the configuration directory currently
123 * The RX identity to delete
126 * 0 on success, an error code on failure
130 afsconf_DeleteIdentity(struct afsconf_dir *adir, struct rx_identity *user)
141 struct rx_identity identity;
144 memset(&identity, 0, sizeof(struct rx_identity));
147 UserListFileName(adir, tbuffer, sizeof tbuffer);
151 * We attempt to fully resolve this pathname, so that the rename
152 * of the temporary file will work even if UserList is a symlink
153 * into a different filesystem.
155 char resolved_path[1024];
157 if (realpath(tbuffer, resolved_path)) {
158 strcpy(tbuffer, resolved_path);
161 #endif /* AFS_NT40_ENV */
162 tf = fopen(tbuffer, "r");
167 code = stat(tbuffer, &tstat);
172 strcpy(nbuffer, tbuffer);
173 strcat(nbuffer, ".NXX");
174 nf = fopen(nbuffer, "w+");
183 /* check for our user id */
184 tp = fgets(nbuffer, sizeof(nbuffer), tf);
188 copy = strdup(nbuffer);
193 code = ParseLine(copy, &identity);
194 if (code == 0 && rx_identity_match(user, &identity)) {
195 /* found the guy, don't copy to output file */
198 /* otherwise copy original line to output */
199 fprintf(nf, "%s", nbuffer);
202 rx_identity_freeContents(&identity);
207 if (fclose(nf) == EOF)
209 strcpy(nbuffer, tbuffer);
210 strcat(nbuffer, ".NXX"); /* generate new file name again */
213 flag = renamefile(nbuffer, tbuffer);
215 flag = chmod(tbuffer, tstat.st_mode);
219 /* finally, decide what to return to the caller */
222 return EIO; /* something mysterious went wrong */
224 return ENOENT; /* entry wasn't found, no changes made */
225 return 0; /* everything was fine */
229 * Remove a legacy Kerberos 4 name from the UserList file.
231 * This function removes a Kerberos 4 name from the super user list. It
232 * can only remove names which were added by the afsconf_AddUser interface,
233 * or with an explicit Kerberos v4 type.
236 * A structure representing the configuration directory
238 * The Kerberos v4 name to remove
241 * 0 on success, an error code upon failure.
243 * Note that this function is deprecated. New callers should use
244 * afsconf_DeleteIdentity instead.
248 afsconf_DeleteUser(struct afsconf_dir *adir, char *name)
250 struct rx_identity *user;
253 user = rx_identity_new(RX_ID_KRB4, name, name, strlen(name));
257 code = afsconf_DeleteIdentity(adir, user);
259 rx_identity_free(&user);
264 /* This is a multi-purpose funciton for use by either
265 * GetNthIdentity or GetNthUser. The parameter 'id' indicates
266 * whether we are counting all identities (if true), or just
267 * ones which can be represented by the old-style interfaces
270 GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
271 struct rx_identity **identity, int id)
275 struct rx_identity fileUser;
279 UserListFileName(dir, tbuffer, sizeof(tbuffer));
280 bp = BufioOpen(tbuffer, O_RDONLY, 0);
286 code = BufioGets(bp, tbuffer, sizeof(tbuffer));
290 code = ParseLine(tbuffer, &fileUser);
294 if (id || fileUser.kind == RX_ID_KRB4)
300 rx_identity_freeContents(&fileUser);
303 *identity = rx_identity_copy(&fileUser);
304 rx_identity_freeContents(&fileUser);
314 * Return the Nth super user identity from the UserList
317 * A structure representing the configuration directory
319 * A count (from zero) of the entries to return from the
321 * @param[out] identity
322 * A pointer to the Nth identity
324 * 0 on success, non-zero on failure
328 afsconf_GetNthIdentity(struct afsconf_dir *dir, int count,
329 struct rx_identity **identity)
331 return GetNthIdentityOrUser(dir, count, identity, 1);
335 * Return the Nth Kerberos v4 identity from the UserList
337 * This returns the Nth old, kerberos v4 style name from
338 * the UserList file. In counting entries it skips any other
339 * name types it encounters - so will hide any new-style
340 * identities from its callers.
343 * A structure representing the configuration directory
345 * A count (from zero) of the entries to return from the
348 * A string in which to write the name of the Nth identity
350 * The length of the buffer passed in abuffer
352 * 0 on success, non-zero on failure
354 * This function is deprecated, all new callers should use
355 * GetNthIdentity instead. This function is particularly dangerous
356 * as it will hide any new-style identities from callers.
360 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
361 afs_int32 abufferLen)
363 struct rx_identity *identity;
366 code = GetNthIdentityOrUser(adir, an, &identity, 0);
368 strlcpy(abuffer, identity->displayName, abufferLen);
369 rx_identity_free(&identity);
375 * Parse a UserList list
377 * Parse a line of data from a UserList file
379 * This parses a line of data in a UserList, and populates the passed
380 * rx_identity structure with the information about the user.
382 * @param buffer A string containing the line to be parsed
383 * @param user The user structure to be populated
385 * Note that the user->displayName, and user->exportedName.val fields
386 * must be freed with free() by the caller.
388 * This function damages the buffer thats passed to it. Callers are
389 * expected to make a copy if they want the buffer preserved.
392 * 0 on success, non-zero on failure.
396 ParseLine(char *buffer, struct rx_identity *user)
407 if (buffer[0] == ' ') { /* extended names have leading space */
409 code = sscanf(ptr, "%i", &kind);
413 strsep(&ptr, " "); /* skip the bit we just read with scanf */
414 ename = strsep(&ptr, " "); /* Pull out the ename */
415 displayName = strsep(&ptr, " "); /* Display name runs to the end */
416 if (ename == NULL || displayName == NULL)
419 decodedName = malloc(strlen(ename));
420 if (decodedName == NULL)
423 len = base64_decode(ename, decodedName);
429 rx_identity_populate(user, kind, displayName, decodedName, len);
432 return 0; /* Success ! */
435 /* No extended name, try for a legacy name */
436 code = sscanf(buffer, "%64s", name);
440 rx_identity_populate(user, RX_ID_KRB4, name, name, strlen(name));
445 * Check if a given identity is in the UserList file,
446 * and thus is a super user
449 * A structure representing the configuration directory to check
451 * The identity to check
453 * True if the user is listed in the UserList, otherwise false
457 afsconf_IsSuperIdentity(struct afsconf_dir *adir,
458 struct rx_identity *user)
462 struct rx_identity fileUser;
466 UserListFileName(adir, tbuffer, sizeof tbuffer);
467 bp = BufioOpen(tbuffer, O_RDONLY, 0);
472 code = BufioGets(bp, tbuffer, sizeof(tbuffer));
476 code = ParseLine(tbuffer, &fileUser);
480 match = rx_identity_match(user, &fileUser);
482 rx_identity_freeContents(&fileUser);
488 /* add a user to the user list, checking for duplicates */
490 afsconf_AddIdentity(struct afsconf_dir *adir, struct rx_identity *user)
498 if (afsconf_IsSuperIdentity(adir, user)) {
500 return EEXIST; /* already in the list */
503 UserListFileName(adir, tbuffer, sizeof tbuffer);
504 tf = fopen(tbuffer, "a+");
509 if (user->kind == RX_ID_KRB4) {
510 fprintf(tf, "%s\n", user->displayName);
512 base64_encode(user->exportedName.val, user->exportedName.len,
514 fprintf(tf, " %d %s %s\n", user->kind, ename, user->displayName);
527 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
529 struct rx_identity *user;
532 user = rx_identity_new(RX_ID_KRB4, aname, aname, strlen(aname));
536 code = afsconf_AddIdentity(adir, user);
538 rx_identity_free(&user);
543 /* special CompFindUser routine that builds up a princ and then
544 calls finduser on it. If found, returns char * to user string,
545 otherwise returns NULL. The resulting string should be immediately
546 copied to other storage prior to release of mutex. */
548 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
549 char *realm, struct rx_identity **identity)
551 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
552 struct rx_identity *testId;
554 /* always must have name */
555 if (!name || !name[0]) {
558 strcpy(fullname, name);
560 /* might have instance */
561 if (inst && inst[0]) {
562 if (!sep || !sep[0]) {
566 strcat(fullname, sep);
567 strcat(fullname, inst);
570 /* might have realm */
571 if (realm && realm[0]) {
572 strcat(fullname, "@");
573 strcat(fullname, realm);
576 testId = rx_identity_new(RX_ID_KRB4, fullname, fullname, strlen(fullname));
577 if (afsconf_IsSuperIdentity(adir, testId)) {
581 rx_identity_free(&testId);
585 rx_identity_free(&testId);
590 kerberosSuperUser(struct afsconf_dir *adir, char *tname, char *tinst,
591 char *tcell, struct rx_identity **identity)
593 char tcell_l[MAXKTCREALMLEN] = "";
595 static char lcell[MAXCELLCHARS] = "";
596 static char lrealms[AFS_NUM_LREALMS][AFS_REALM_SZ];
597 static int num_lrealms = -1;
598 int lrealm_match = 0, i;
601 /* generate lowercased version of cell name */
603 strcpy(tcell_l, tcell);
606 *tmp = tolower(*tmp);
611 /* determine local cell name. It's static, so will only get
612 * calculated the first time through */
614 afsconf_GetLocalCell(adir, lcell, sizeof(lcell));
616 /* if running a krb environment, also get the local realm */
617 /* note - this assumes AFS_REALM_SZ <= MAXCELLCHARS */
618 /* just set it to lcell if it fails */
619 if (num_lrealms == -1) {
620 for (i=0; i<AFS_NUM_LREALMS; i++) {
621 if (afs_krb_get_lrealm(lrealms[i], i) != 0 /*KSUCCESS*/)
626 strncpy(lrealms[0], lcell, AFS_REALM_SZ);
633 /* See if the ticket cell matches one of the local realms */
635 for ( i=0;i<num_lrealms;i++ ) {
636 if (!strcasecmp(lrealms[i], tcell)) {
642 /* If yes, then make sure that the name is not present in
643 * an exclusion list */
645 char uname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
646 if (tinst && tinst[0])
647 snprintf(uname,sizeof(uname),"%s.%s@%s",tname,tinst,tcell);
649 snprintf(uname,sizeof(uname),"%s@%s",tname,tcell);
651 if (afs_krb_exclusion(uname))
655 /* start with no authorization */
658 /* localauth special case */
659 if ((tinst == NULL || strlen(tinst) == 0) &&
660 (tcell == NULL || strlen(tcell) == 0)
661 && !strcmp(tname, AUTH_SUPERUSER)) {
663 *identity = rx_identity_new(RX_ID_KRB4, AFS_LOCALAUTH_NAME,
664 AFS_LOCALAUTH_NAME, AFS_LOCALAUTH_LEN);
667 /* cell of connection matches local cell or one of the realms */
668 } else if (!strcasecmp(tcell, lcell) || lrealm_match) {
669 if (CompFindUser(adir, tname, ".", tinst, NULL, identity)) {
672 /* cell of conn doesn't match local cell or realm */
674 if (CompFindUser(adir, tname, ".", tinst, tcell, identity)) {
676 } else if (CompFindUser(adir, tname, ".", tinst, tcell_l, identity)) {
685 rxkadSuperUser(struct afsconf_dir *adir, struct rx_call *acall,
686 struct rx_identity **identity)
688 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
689 char tinst[MAXKTCNAMELEN];
690 char tcell[MAXKTCREALMLEN];
695 /* get auth details from server connection */
696 code = rxkad_GetServerInfo(rx_ConnectionOf(acall), NULL, &exp, tname,
699 return 0; /* bogus connection/other error */
701 return kerberosSuperUser(adir, tname, tinst, tcell, identity);
705 * Check whether the user authenticated on a given RX call is a super
706 * user or not. If they are, return a pointer to the identity of that
710 * The configuration directory currently in use
712 * The RX call whose authenticated identity is being checked
713 * @param[out] identity
714 * The RX identity of the user. Caller must free this structure.
716 * True if the user is a super user, or if the server is running
717 * in noauth mode. Otherwise, false.
720 afsconf_SuperIdentity(struct afsconf_dir *adir, struct rx_call *acall,
721 struct rx_identity **identity)
723 struct rx_connection *tconn;
733 if (afsconf_GetNoAuthFlag(adir)) {
735 *identity = rx_identity_new(RX_ID_KRB4, AFS_NOAUTH_NAME,
736 AFS_NOAUTH_NAME, AFS_NOAUTH_LEN);
741 tconn = rx_ConnectionOf(acall);
742 code = rx_SecurityClassOf(tconn);
745 return 0; /* not authenticated at all, answer is no */
746 } else if (code == 1) {
749 return 0; /* not supported any longer */
750 } else if (code == 2) {
751 flag = rxkadSuperUser(adir, acall, identity);
754 } else { /* some other auth type */
756 return 0; /* mysterious, just say no */
761 * Check whether the user authenticated on a given RX call is a super
762 * user or not. If they are, return a pointer to the name of that
766 * The configuration directory currently in use
768 * The RX call whose authenticated identity is being checked
770 * A printable version of the name of the user
772 * True if the user is a super user, or if the server is running
773 * in noauth mode. Otherwise, false.
775 * This function is provided for backwards compatibility. New callers
776 * should use the afsconf_SuperIdentity function.
780 afsconf_SuperUser(struct afsconf_dir *adir, struct rx_call *acall,
783 struct rx_identity *identity;
787 ret = afsconf_SuperIdentity(adir, acall, &identity);
789 if (identity->kind == RX_ID_KRB4) {
790 strlcpy(namep, identity->displayName, MAXKTCNAMELEN-1);
792 snprintf(namep, MAXKTCNAMELEN-1, "eName: %s",
793 identity->displayName);
795 rx_identity_free(&identity);
798 ret = afsconf_SuperIdentity(adir, acall, NULL);