]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/gssd/krb5_util.c
gssd: don't krb5_free_context if krb5_init_context fails
[nfs-utils.git] / utils / gssd / krb5_util.c
index 887d118f3b19277196dcf75b5c532cb3c3376e92..4befa72a5b1f6fb9c496a705602e57a0e30e1f00 100644 (file)
@@ -139,7 +139,7 @@ int limit_to_legacy_enctypes = 0;
 
 static int select_krb5_ccache(const struct dirent *d);
 static int gssd_find_existing_krb5_ccache(uid_t uid, char *dirname,
-               struct dirent **d);
+               const char **cctype, struct dirent **d);
 static int gssd_get_single_krb5_cred(krb5_context context,
                krb5_keytab kt, struct gssd_k5_kt_princ *ple, int nocache);
 static int query_krb5_ccache(const char* cred_cache, char **ret_princname,
@@ -178,7 +178,8 @@ select_krb5_ccache(const struct dirent *d)
  * code otherwise.
  */
 static int
-gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
+gssd_find_existing_krb5_ccache(uid_t uid, char *dirname,
+                              const char **cctype, struct dirent **d)
 {
        struct dirent **namelist;
        int n;
@@ -192,6 +193,7 @@ gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
        int score, best_match_score = 0, err = -EACCES;
 
        memset(&best_match_stat, 0, sizeof(best_match_stat));
+       *cctype = NULL;
        *d = NULL;
        n = scandir(dirname, &namelist, select_krb5_ccache, 0);
        if (n < 0) {
@@ -203,41 +205,51 @@ gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
                for (i = 0; i < n; i++) {
                        snprintf(statname, sizeof(statname),
                                 "%s/%s", dirname, namelist[i]->d_name);
-                       printerr(3, "CC file '%s' being considered, "
+                       printerr(3, "CC '%s' being considered, "
                                 "with preferred realm '%s'\n",
                                 statname, preferred_realm ?
                                        preferred_realm : "<none selected>");
-                       snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname, 
-                                       namelist[i]->d_name);
                        if (lstat(statname, &tmp_stat)) {
-                               printerr(0, "Error doing stat on file '%s'\n",
+                               printerr(0, "Error doing stat on '%s'\n",
                                         statname);
                                free(namelist[i]);
                                continue;
                        }
                        /* Only pick caches owned by the user (uid) */
                        if (tmp_stat.st_uid != uid) {
-                               printerr(3, "CC file '%s' owned by %u, not %u\n",
+                               printerr(3, "CC '%s' owned by %u, not %u\n",
                                         statname, tmp_stat.st_uid, uid);
                                free(namelist[i]);
                                continue;
                        }
-                       if (!S_ISREG(tmp_stat.st_mode)) {
-                               printerr(3, "CC file '%s' is not a regular file\n",
+                       if (!S_ISREG(tmp_stat.st_mode) &&
+                           !S_ISDIR(tmp_stat.st_mode)) {
+                               printerr(3, "CC '%s' is not a regular "
+                                        "file or directory\n",
                                         statname);
                                free(namelist[i]);
                                continue;
                        }
                        if (uid == 0 && !root_uses_machine_creds && 
                                strstr(namelist[i]->d_name, "_machine_")) {
-                               printerr(3, "CC file '%s' not available to root\n",
+                               printerr(3, "CC '%s' not available to root\n",
                                         statname);
                                free(namelist[i]);
                                continue;
                        }
+                       if (S_ISDIR(tmp_stat.st_mode)) {
+                               *cctype = "DIR";
+                       } else
+                       if (S_ISREG(tmp_stat.st_mode)) {
+                               *cctype = "FILE";
+                       } else {
+                               continue;
+                       }
+                       snprintf(buf, sizeof(buf), "%s:%s/%s", *cctype,
+                                dirname, namelist[i]->d_name);
                        if (!query_krb5_ccache(buf, &princname, &realm)) {
-                               printerr(3, "CC file '%s' is expired or corrupt\n",
-                                        statname);
+                               printerr(3, "CC '%s' is expired or corrupt\n",
+                                        buf);
                                free(namelist[i]);
                                err = -EKEYEXPIRED;
                                continue;
@@ -248,9 +260,9 @@ gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
                                        strcmp(realm, preferred_realm) == 0) 
                                score++;
 
-                       printerr(3, "CC file '%s'(%s@%s) passed all checks and"
+                       printerr(3, "CC '%s'(%s@%s) passed all checks and"
                                    " has mtime of %u\n",
-                                statname, princname, realm, 
+                                buf, princname, realm, 
                                 tmp_stat.st_mtime);
                        /*
                         * if more than one match is found, return the most
@@ -284,10 +296,11 @@ gssd_find_existing_krb5_ccache(uid_t uid, char *dirname, struct dirent **d)
                                else {
                                        free(namelist[i]);
                                }
-                               printerr(3, "CC file '%s/%s' is our "
+                               printerr(3, "CC '%s:%s/%s' is our "
                                            "current best match "
                                            "with mtime of %u\n",
-                                        dirname, best_match_dir->d_name,
+                                        cctype, dirname,
+                                        best_match_dir->d_name,
                                         best_match_stat.st_mtime);
                        }
                        free(princname);
@@ -761,12 +774,16 @@ gssd_search_krb5_keytab(krb5_context context, krb5_keytab kt,
 }
 
 /*
- * Find a keytab entry to use for a given target hostname.
+ * Find a keytab entry to use for a given target realm.
  * Tries to find the most appropriate keytab to use given the
  * name of the host we are trying to connect with.
+ *
+ * Note: the tgtname contains a hostname in the realm that we
+ * are authenticating to. It may, or may not be the same as
+ * the server hostname.
  */
 static int
-find_keytab_entry(krb5_context context, krb5_keytab kt, const char *hostname,
+find_keytab_entry(krb5_context context, krb5_keytab kt, const char *tgtname,
                  krb5_keytab_entry *kte, const char **svcnames)
 {
        krb5_error_code code;
@@ -782,7 +799,7 @@ find_keytab_entry(krb5_context context, krb5_keytab kt, const char *hostname,
 
 
        /* Get full target hostname */
-       retval = get_full_hostname(hostname, targethostname,
+       retval = get_full_hostname(tgtname, targethostname,
                                   sizeof(targethostname));
        if (retval)
                goto out;
@@ -1023,20 +1040,43 @@ err_cache:
  * Returns 0 if a ccache was found, and a non-zero error code otherwise.
  */
 int
-gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername, char *dirname)
+gssd_setup_krb5_user_gss_ccache(uid_t uid, char *servername, char *dirpattern)
 {
-       char                    buf[MAX_NETOBJ_SZ];
+       char                    buf[MAX_NETOBJ_SZ], dirname[PATH_MAX];
+       const char              *cctype;
        struct dirent           *d;
-       int                     err;
+       int                     err, i, j;
 
        printerr(2, "getting credentials for client with uid %u for "
                    "server %s\n", uid, servername);
-       memset(buf, 0, sizeof(buf));
-       err = gssd_find_existing_krb5_ccache(uid, dirname, &d);
+
+       for (i = 0, j = 0; dirpattern[i] != '\0'; i++) {
+               switch (dirpattern[i]) {
+               case '%':
+                       switch (dirpattern[i + 1]) {
+                       case '%':
+                               dirname[j++] = dirpattern[i];
+                               i++;
+                               break;
+                       case 'U':
+                               j += sprintf(dirname + j, "%lu",
+                                            (unsigned long) uid);
+                               i++;
+                               break;
+                       }
+                       break;
+               default:
+                       dirname[j++] = dirpattern[i];
+                       break;
+               }
+       }
+       dirname[j] = '\0';
+
+       err = gssd_find_existing_krb5_ccache(uid, dirname, &cctype, &d);
        if (err)
                return err;
 
-       snprintf(buf, sizeof(buf), "FILE:%s/%s", dirname, d->d_name);
+       snprintf(buf, sizeof(buf), "%s:%s/%s", cctype, dirname, d->d_name);
        free(d);
 
        printerr(2, "using %s as credentials cache for client with "
@@ -1092,7 +1132,7 @@ gssd_get_krb5_machine_cred_list(char ***list)
                if (ple->ccname) {
                        /* Make sure cred is up-to-date before returning it */
                        retval = gssd_refresh_krb5_machine_credential(NULL, ple,
-                               NULL);
+                               NULL, NULL);
                        if (retval)
                                continue;
                        if (i + 1 > listsize) {
@@ -1172,9 +1212,9 @@ gssd_destroy_krb5_machine_creds(void)
                                    "cache '%s'\n", k5err, ple->ccname);
                }
        }
+       krb5_free_context(context);
   out:
        free(k5err);
-       krb5_free_context(context);
 }
 
 /*
@@ -1183,7 +1223,8 @@ gssd_destroy_krb5_machine_creds(void)
 int
 gssd_refresh_krb5_machine_credential(char *hostname,
                                     struct gssd_k5_kt_princ *ple, 
-                                        char *service)
+                                        char *service,
+                                        char *tgtname)
 {
        krb5_error_code code = 0;
        krb5_context context;
@@ -1216,19 +1257,22 @@ gssd_refresh_krb5_machine_credential(char *hostname,
                k5err = gssd_k5_err_msg(context, code);
                printerr(0, "ERROR: %s: %s while resolving keytab '%s'\n",
                         __func__, k5err, keytabfile);
-               goto out;
+               goto out_free_context;
        }
 
        if (ple == NULL) {
                krb5_keytab_entry kte;
 
-               code = find_keytab_entry(context, kt, hostname, &kte, svcnames);
+               if (tgtname == NULL)
+                       tgtname = hostname;
+
+               code = find_keytab_entry(context, kt, tgtname, &kte, svcnames);
                if (code) {
                        printerr(0, "ERROR: %s: no usable keytab entry found "
                                 "in keytab %s for connection with host %s\n",
                                 __FUNCTION__, keytabfile, hostname);
                        retval = code;
-                       goto out;
+                       goto out_free_kt;
                }
 
                ple = get_ple_by_princ(context, kte.principal);
@@ -1244,14 +1288,15 @@ gssd_refresh_krb5_machine_credential(char *hostname,
                                 __FUNCTION__, pname ? pname : "<unparsable>",
                                 hostname);
                        if (pname) k5_free_unparsed_name(context, pname);
-                       goto out;
+                       goto out_free_kt;
                }
        }
        retval = gssd_get_single_krb5_cred(context, kt, ple, 0);
-out:
-       if (kt)
-               krb5_kt_close(context, kt);
+out_free_kt:
+       krb5_kt_close(context, kt);
+out_free_context:
        krb5_free_context(context);
+out:
        free(k5err);
        return retval;
 }