Use passwd entry for ~ expansion if $HOME isn't set.
authorKen Raeburn <raeburn@mit.edu>
Fri, 10 Jan 2003 22:42:05 +0000 (22:42 +0000)
committerKen Raeburn <raeburn@mit.edu>
Fri, 10 Jan 2003 22:42:05 +0000 (22:42 +0000)
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

src/util/profile/ChangeLog
src/util/profile/configure.in
src/util/profile/prof_file.c

index 7a45fa9e413056f1cf9a04750e738d15621b3fca..ad592171aa5be73b2ef7ea705f522f7657b538d4 100644 (file)
@@ -1,5 +1,13 @@
 2003-01-10  Ken Raeburn  <raeburn@mit.edu>
 
+       * 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.
 
index 8a275d3efd6559823f5295b5236a52b8e9b93f10..142d23b37fae084412c152a0a386a4da28708de4 100644 (file)
@@ -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
index 22470cc6e6378178133f4af1fd4f1e416b3e98fa..a71c44fe43e093b6ad70383ff258230e021e9093 100644 (file)
@@ -17,6 +17,9 @@
 #include <sys/stat.h>
 #include <errno.h>
 
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
 
 #if defined(_WIN32)
 #include <io.h>
@@ -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) {