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, "/",
47 AFSDIR_ULIST_FILE, NULL);
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)
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);
110 * Remove an identity from the UserList file
112 * This function removes the given identity from the user list file.
113 * For the purposes of identifying entries to remove, only the
114 * type and exportedName portions of the identity are used. Callers
115 * should remember that a given identity may be listed in the file in
116 * a number of different ways.
119 * A structure representing the configuration directory currently
122 * The RX identity to delete
125 * 0 on success, an error code on failure
129 afsconf_DeleteIdentity(struct afsconf_dir *adir, struct rx_identity *user)
140 struct rx_identity identity;
143 memset(&identity, 0, sizeof(struct rx_identity));
146 UserListFileName(adir, tbuffer, sizeof tbuffer);
150 * We attempt to fully resolve this pathname, so that the rename
151 * of the temporary file will work even if UserList is a symlink
152 * into a different filesystem.
154 char resolved_path[1024];
156 if (realpath(tbuffer, resolved_path)) {
157 strcpy(tbuffer, resolved_path);
160 #endif /* AFS_NT40_ENV */
161 tf = fopen(tbuffer, "r");
166 code = stat(tbuffer, &tstat);
171 strcpy(nbuffer, tbuffer);
172 strcat(nbuffer, ".NXX");
173 nf = fopen(nbuffer, "w+");
182 /* check for our user id */
183 tp = fgets(nbuffer, sizeof(nbuffer), tf);
187 copy = strdup(nbuffer);
192 code = ParseLine(copy, &identity);
193 if (code == 0 && rx_identity_match(user, &identity)) {
194 /* found the guy, don't copy to output file */
197 /* otherwise copy original line to output */
198 fprintf(nf, "%s", nbuffer);
201 rx_identity_freeContents(&identity);
206 if (fclose(nf) == EOF)
208 strcpy(nbuffer, tbuffer);
209 strcat(nbuffer, ".NXX"); /* generate new file name again */
212 flag = renamefile(nbuffer, tbuffer);
214 flag = chmod(tbuffer, tstat.st_mode);
218 /* finally, decide what to return to the caller */
221 return EIO; /* something mysterious went wrong */
223 return ENOENT; /* entry wasn't found, no changes made */
224 return 0; /* everything was fine */
228 * Remove a legacy Kerberos 4 name from the UserList file.
230 * This function removes a Kerberos 4 name from the super user list. It
231 * can only remove names which were added by the afsconf_AddUser interface,
232 * or with an explicit Kerberos v4 type.
235 * A structure representing the configuration directory
237 * The Kerberos v4 name to remove
240 * 0 on success, an error code upon failure.
242 * Note that this function is deprecated. New callers should use
243 * afsconf_DeleteIdentity instead.
247 afsconf_DeleteUser(struct afsconf_dir *adir, char *name)
249 struct rx_identity *user;
252 user = rx_identity_new(RX_ID_KRB4, name, name, strlen(name));
256 code = afsconf_DeleteIdentity(adir, user);
258 rx_identity_free(&user);
263 /* This is a multi-purpose funciton for use by either
264 * GetNthIdentity or GetNthUser. The parameter 'id' indicates
265 * whether we are counting all identities (if true), or just
266 * ones which can be represented by the old-style interfaces
269 GetNthIdentityOrUser(struct afsconf_dir *dir, int count,
270 struct rx_identity **identity, int id)
274 struct rx_identity fileUser;
278 UserListFileName(dir, tbuffer, sizeof(tbuffer));
279 bp = BufioOpen(tbuffer, O_RDONLY, 0);
285 code = BufioGets(bp, tbuffer, sizeof(tbuffer));
289 code = ParseLine(tbuffer, &fileUser);
293 if (id || fileUser.kind == RX_ID_KRB4)
299 rx_identity_freeContents(&fileUser);
302 *identity = rx_identity_copy(&fileUser);
303 rx_identity_freeContents(&fileUser);
313 * Return the Nth super user identity from the UserList
316 * A structure representing the configuration directory
318 * A count (from zero) of the entries to return from the
320 * @param[out] identity
321 * A pointer to the Nth identity
323 * 0 on success, non-zero on failure
327 afsconf_GetNthIdentity(struct afsconf_dir *dir, int count,
328 struct rx_identity **identity)
330 return GetNthIdentityOrUser(dir, count, identity, 1);
334 * Return the Nth Kerberos v4 identity from the UserList
336 * This returns the Nth old, kerberos v4 style name from
337 * the UserList file. In counting entries it skips any other
338 * name types it encounters - so will hide any new-style
339 * identities from its callers.
342 * A structure representing the configuration directory
344 * A count (from zero) of the entries to return from the
347 * A string in which to write the name of the Nth identity
349 * The length of the buffer passed in abuffer
351 * 0 on success, non-zero on failure
353 * This function is deprecated, all new callers should use
354 * GetNthIdentity instead. This function is particularly dangerous
355 * as it will hide any new-style identities from callers.
359 afsconf_GetNthUser(struct afsconf_dir *adir, afs_int32 an, char *abuffer,
360 afs_int32 abufferLen)
362 struct rx_identity *identity;
365 code = GetNthIdentityOrUser(adir, an, &identity, 0);
367 strlcpy(abuffer, identity->displayName, abufferLen);
368 rx_identity_free(&identity);
374 * Parse a UserList list
376 * Parse a line of data from a UserList file
378 * This parses a line of data in a UserList, and populates the passed
379 * rx_identity structure with the information about the user.
381 * @param buffer A string containing the line to be parsed
382 * @param user The user structure to be populated
384 * Note that the user->displayName, and user->exportedName.val fields
385 * must be freed with free() by the caller.
387 * This function damages the buffer thats passed to it. Callers are
388 * expected to make a copy if they want the buffer preserved.
391 * 0 on success, non-zero on failure.
395 ParseLine(char *buffer, struct rx_identity *user)
406 if (buffer[0] == ' ') { /* extended names have leading space */
408 code = sscanf(ptr, "%i", &kind);
412 strsep(&ptr, " "); /* skip the bit we just read with scanf */
413 ename = strsep(&ptr, " "); /* Pull out the ename */
414 displayName = strsep(&ptr, " "); /* Display name runs to the end */
415 if (ename == NULL || displayName == NULL)
418 decodedName = malloc(strlen(ename));
419 if (decodedName == NULL)
422 len = base64_decode(ename, decodedName);
428 rx_identity_populate(user, kind, displayName, decodedName, len);
431 return 0; /* Success ! */
434 /* No extended name, try for a legacy name */
435 code = sscanf(buffer, "%64s", name);
439 rx_identity_populate(user, RX_ID_KRB4, name, name, strlen(name));
444 * Check if a given identity is in the UserList file,
445 * and thus is a super user
448 * A structure representing the configuration directory to check
450 * The identity to check
452 * True if the user is listed in the UserList, otherwise false
456 afsconf_IsSuperIdentity(struct afsconf_dir *adir,
457 struct rx_identity *user)
461 struct rx_identity fileUser;
465 UserListFileName(adir, tbuffer, sizeof tbuffer);
466 bp = BufioOpen(tbuffer, O_RDONLY, 0);
471 code = BufioGets(bp, tbuffer, sizeof(tbuffer));
475 code = ParseLine(tbuffer, &fileUser);
479 match = rx_identity_match(user, &fileUser);
481 rx_identity_freeContents(&fileUser);
487 /* add a user to the user list, checking for duplicates */
489 afsconf_AddIdentity(struct afsconf_dir *adir, struct rx_identity *user)
497 if (afsconf_IsSuperIdentity(adir, user)) {
499 return EEXIST; /* already in the list */
502 UserListFileName(adir, tbuffer, sizeof tbuffer);
503 tf = fopen(tbuffer, "a+");
508 if (user->kind == RX_ID_KRB4) {
509 fprintf(tf, "%s\n", user->displayName);
511 base64_encode(user->exportedName.val, user->exportedName.len,
513 fprintf(tf, " %d %s %s\n", user->kind, ename, user->displayName);
526 afsconf_AddUser(struct afsconf_dir *adir, char *aname)
528 struct rx_identity *user;
531 user = rx_identity_new(RX_ID_KRB4, aname, aname, strlen(aname));
535 code = afsconf_AddIdentity(adir, user);
537 rx_identity_free(&user);
542 /* special CompFindUser routine that builds up a princ and then
543 calls finduser on it. If found, returns char * to user string,
544 otherwise returns NULL. The resulting string should be immediately
545 copied to other storage prior to release of mutex. */
547 CompFindUser(struct afsconf_dir *adir, char *name, char *sep, char *inst,
548 char *realm, struct rx_identity **identity)
550 static char fullname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
551 struct rx_identity *testId;
553 /* always must have name */
554 if (!name || !name[0]) {
557 strcpy(fullname, name);
559 /* might have instance */
560 if (inst && inst[0]) {
561 if (!sep || !sep[0]) {
565 strcat(fullname, sep);
566 strcat(fullname, inst);
569 /* might have realm */
570 if (realm && realm[0]) {
571 strcat(fullname, "@");
572 strcat(fullname, realm);
575 testId = rx_identity_new(RX_ID_KRB4, fullname, fullname, strlen(fullname));
576 if (afsconf_IsSuperIdentity(adir, testId)) {
580 rx_identity_free(&testId);
584 rx_identity_free(&testId);
589 kerberosSuperUser(struct afsconf_dir *adir, char *tname, char *tinst,
590 char *tcell, struct rx_identity **identity)
592 char tcell_l[MAXKTCREALMLEN] = "";
594 static char lcell[MAXCELLCHARS] = "";
595 static char lrealms[AFS_NUM_LREALMS][AFS_REALM_SZ];
596 static int num_lrealms = -1;
597 int lrealm_match = 0, i;
600 /* generate lowercased version of cell name */
602 strcpy(tcell_l, tcell);
605 *tmp = tolower(*tmp);
610 /* determine local cell name. It's static, so will only get
611 * calculated the first time through */
613 afsconf_GetLocalCell(adir, lcell, sizeof(lcell));
615 /* if running a krb environment, also get the local realm */
616 /* note - this assumes AFS_REALM_SZ <= MAXCELLCHARS */
617 /* just set it to lcell if it fails */
618 if (num_lrealms == -1) {
619 for (i=0; i<AFS_NUM_LREALMS; i++) {
620 if (afs_krb_get_lrealm(lrealms[i], i) != 0 /*KSUCCESS*/)
625 strncpy(lrealms[0], lcell, AFS_REALM_SZ);
632 /* See if the ticket cell matches one of the local realms */
634 for ( i=0;i<num_lrealms;i++ ) {
635 if (!strcasecmp(lrealms[i], tcell)) {
641 /* If yes, then make sure that the name is not present in
642 * an exclusion list */
644 char uname[MAXKTCNAMELEN + MAXKTCNAMELEN + MAXKTCREALMLEN + 3];
645 if (tinst && tinst[0])
646 snprintf(uname,sizeof(uname),"%s.%s@%s",tname,tinst,tcell);
648 snprintf(uname,sizeof(uname),"%s@%s",tname,tcell);
650 if (afs_krb_exclusion(uname))
654 /* start with no authorization */
657 /* localauth special case */
658 if ((tinst == NULL || strlen(tinst) == 0) &&
659 (tcell == NULL || strlen(tcell) == 0)
660 && !strcmp(tname, AUTH_SUPERUSER)) {
662 *identity = rx_identity_new(RX_ID_KRB4, AFS_LOCALAUTH_NAME,
663 AFS_LOCALAUTH_NAME, AFS_LOCALAUTH_LEN);
666 /* cell of connection matches local cell or one of the realms */
667 } else if (!strcasecmp(tcell, lcell) || lrealm_match) {
668 if (CompFindUser(adir, tname, ".", tinst, NULL, identity)) {
671 /* cell of conn doesn't match local cell or realm */
673 if (CompFindUser(adir, tname, ".", tinst, tcell, identity)) {
675 } else if (CompFindUser(adir, tname, ".", tinst, tcell_l, identity)) {
684 rxkadSuperUser(struct afsconf_dir *adir, struct rx_call *acall,
685 struct rx_identity **identity)
687 char tname[MAXKTCNAMELEN]; /* authentication from ticket */
688 char tinst[MAXKTCNAMELEN];
689 char tcell[MAXKTCREALMLEN];
694 /* get auth details from server connection */
695 code = rxkad_GetServerInfo(rx_ConnectionOf(acall), NULL, &exp, tname,
698 return 0; /* bogus connection/other error */
700 return kerberosSuperUser(adir, tname, tinst, tcell, identity);
704 * Check whether the user authenticated on a given RX call is a super
705 * user or not. If they are, return a pointer to the identity of that
709 * The configuration directory currently in use
711 * The RX call whose authenticated identity is being checked
712 * @param[out] identity
713 * The RX identity of the user. Caller must free this structure.
715 * True if the user is a super user, or if the server is running
716 * in noauth mode. Otherwise, false.
719 afsconf_SuperIdentity(struct afsconf_dir *adir, struct rx_call *acall,
720 struct rx_identity **identity)
722 struct rx_connection *tconn;
732 if (afsconf_GetNoAuthFlag(adir)) {
734 *identity = rx_identity_new(RX_ID_KRB4, AFS_NOAUTH_NAME,
735 AFS_NOAUTH_NAME, AFS_NOAUTH_LEN);
740 tconn = rx_ConnectionOf(acall);
741 code = rx_SecurityClassOf(tconn);
744 return 0; /* not authenticated at all, answer is no */
745 } else if (code == 1) {
748 return 0; /* not supported any longer */
749 } else if (code == 2) {
750 flag = rxkadSuperUser(adir, acall, identity);
753 } else { /* some other auth type */
755 return 0; /* mysterious, just say no */
760 * Check whether the user authenticated on a given RX call is a super
761 * user or not. If they are, return a pointer to the name of that
765 * The configuration directory currently in use
767 * The RX call whose authenticated identity is being checked
769 * A printable version of the name of the user
771 * True if the user is a super user, or if the server is running
772 * in noauth mode. Otherwise, false.
774 * This function is provided for backwards compatibility. New callers
775 * should use the afsconf_SuperIdentity function.
779 afsconf_SuperUser(struct afsconf_dir *adir, struct rx_call *acall,
782 struct rx_identity *identity;
786 ret = afsconf_SuperIdentity(adir, acall, &identity);
788 if (identity->kind == RX_ID_KRB4) {
789 strlcpy(namep, identity->displayName, MAXKTCNAMELEN-1);
791 snprintf(namep, MAXKTCNAMELEN-1, "eName: %s",
792 identity->displayName);
794 rx_identity_free(&identity);
797 ret = afsconf_SuperIdentity(adir, acall, NULL);