From: Ken Raeburn Date: Fri, 10 Jan 2003 22:42:05 +0000 (+0000) Subject: Use passwd entry for ~ expansion if $HOME isn't set. X-Git-Tag: krb5-1.3-alpha1~167 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1f55e6f6211626f3c3dd099dc2a33d9eb4da146b;p=krb5.git Use passwd entry for ~ expansion if $HOME isn't set. Also fix a minor logic bug in checking file access. ticket: 1237 status: open git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@15110 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/util/profile/ChangeLog b/src/util/profile/ChangeLog index 7a45fa9e4..ad592171a 100644 --- a/src/util/profile/ChangeLog +++ b/src/util/profile/ChangeLog @@ -1,5 +1,13 @@ 2003-01-10 Ken Raeburn + * configure.in: Check for pwd.h. + * prof_file.c: Include pwd.h if available. + (profile_open_file) [HAVE_PWD_H]: If $HOME isn't set, look up the + home directory in the passwd file. Expand the filename before + checking against the cache. + (profile_open_file) [SHARE_TREE_DATA]: Fix the sense of the test + for read access. + * configure.in: Use V5_AC_OUTPUT_MAKEFILE instead of K5_GEN_MAKEFILE and K5_AC_OUTPUT. diff --git a/src/util/profile/configure.in b/src/util/profile/configure.in index 8a275d3ef..142d23b37 100644 --- a/src/util/profile/configure.in +++ b/src/util/profile/configure.in @@ -4,7 +4,7 @@ AC_C_CONST AC_CHECK_SIZEOF(short) AC_CHECK_SIZEOF(int) AC_CHECK_SIZEOF(long) -AC_CHECK_HEADERS(unistd.h stdlib.h) +AC_CHECK_HEADERS(unistd.h stdlib.h pwd.h) AC_CHECK_FUNCS(stat access strdup) AC_PROG_AWK KRB5_BUILD_LIBOBJS diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index 22470cc6e..a71c44fe4 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -17,6 +17,9 @@ #include #include +#ifdef HAVE_PWD_H +#include +#endif #if defined(_WIN32) #include @@ -91,6 +94,7 @@ errcode_t profile_open_file(filespec, ret_prof) char *home_env = 0; unsigned int len; prf_data_t data; + char *expanded_filename; prf = malloc(sizeof(struct _prf_file_t)); if (!prf) @@ -98,18 +102,45 @@ errcode_t profile_open_file(filespec, ret_prof) memset(prf, 0, sizeof(struct _prf_file_t)); prf->magic = PROF_MAGIC_FILE; + len = strlen(filespec)+1; + if (filespec[0] == '~' && filespec[1] == '/') { + home_env = getenv("HOME"); +#ifdef HAVE_PWD_H + if (home_env == NULL) { + uid_t uid; + struct passwd *pw; + + uid = getuid(); + pw = getpwuid(uid); + if (pw != NULL && pw->pw_dir[0] != 0) + home_env = pw->pw_dir; + } +#endif + if (home_env) + len += strlen(home_env); + } + expanded_filename = malloc(len); + if (expanded_filename == 0) + return errno; + if (home_env) { + strcpy(expanded_filename, home_env); + strcat(expanded_filename, filespec+1); + } else + memcpy(expanded_filename, filespec, len); + #ifdef SHARE_TREE_DATA (void) prof_mutex_lock(&g_shared_trees_mutex); for (data = g_shared_trees; data; data = data->next) { - if (!strcmp(data->filespec, filespec) + if (!strcmp(data->filespec, expanded_filename) /* Check that current uid has read access. */ - && r_access(data->filespec) == 0) + && r_access(data->filespec)) break; } if (data) { retval = profile_update_file_data(data); data->refcount++; (void) prof_mutex_unlock(&g_shared_trees_mutex); + free(expanded_filename); prf->data = data; *ret_prof = prf; return retval; @@ -129,23 +160,7 @@ errcode_t profile_open_file(filespec, ret_prof) data->magic = PROF_MAGIC_FILE_DATA; data->refcount = 1; data->comment = 0; - - len = strlen(filespec)+1; - if (filespec[0] == '~' && filespec[1] == '/') { - home_env = getenv("HOME"); - if (home_env) - len += strlen(home_env); - } - data->filespec = malloc(len); - if (!data->filespec) { - free(prf); - return ENOMEM; - } - if (home_env) { - strcpy(data->filespec, home_env); - strcat(data->filespec, filespec+1); - } else - strcpy(data->filespec, filespec); + data->filespec = expanded_filename; retval = profile_update_file(prf); if (retval) {