]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/gssd/gssd_proc.c
statd: Replace note() with xlog() in rpc.statd
[nfs-utils.git] / utils / gssd / gssd_proc.c
index 3f81462a5f552cd89f633783381d75af3a353300..33a31c3c3c5b1c018ea8a7408bba34863569053b 100644 (file)
@@ -73,6 +73,7 @@
 #include "krb5_util.h"
 #include "context.h"
 #include "nfsrpc.h"
+#include "nfslib.h"
 
 /*
  * pollarray:
  *      {rpc_pipefs}/{dir}/clntXX/krb5    : read uid for which kernel wants
  *                                         a context, write the resulting context
  *      {rpc_pipefs}/{dir}/clntXX/info    : stores info such as server name
+ *      {rpc_pipefs}/{dir}/clntXX/gssd    : pipe for all gss mechanisms using
+ *                                         a text-based string of parameters
  *
  * Algorithm:
- *      Poll all {rpc_pipefs}/{dir}/clntXX/krb5 files.  When data is ready,
+ *      Poll all {rpc_pipefs}/{dir}/clntXX/YYYY files.  When data is ready,
  *      read and process; performs rpcsec_gss context initialization protocol to
  *      get a cred for that user.  Writes result to corresponding krb5 file
  *      in a form the kernel code will understand.
@@ -289,9 +292,13 @@ destroy_client(struct clnt_info *clp)
        if (clp->spkm3_poll_index != -1)
                memset(&pollarray[clp->spkm3_poll_index], 0,
                                        sizeof(struct pollfd));
+       if (clp->gssd_poll_index != -1)
+               memset(&pollarray[clp->gssd_poll_index], 0,
+                                       sizeof(struct pollfd));
        if (clp->dir_fd != -1) close(clp->dir_fd);
        if (clp->krb5_fd != -1) close(clp->krb5_fd);
        if (clp->spkm3_fd != -1) close(clp->spkm3_fd);
+       if (clp->gssd_fd != -1) close(clp->gssd_fd);
        free(clp->dirname);
        free(clp->servicename);
        free(clp->servername);
@@ -311,8 +318,10 @@ insert_new_clnt(void)
        }
        clp->krb5_poll_index = -1;
        clp->spkm3_poll_index = -1;
+       clp->gssd_poll_index = -1;
        clp->krb5_fd = -1;
        clp->spkm3_fd = -1;
+       clp->gssd_fd = -1;
        clp->dir_fd = -1;
 
        TAILQ_INSERT_HEAD(&clnt_list, clp, list);
@@ -324,17 +333,42 @@ static int
 process_clnt_dir_files(struct clnt_info * clp)
 {
        char    name[PATH_MAX];
+       char    gname[PATH_MAX];
        char    info_file_name[PATH_MAX];
 
-       if (clp->krb5_fd == -1) {
-               snprintf(name, sizeof(name), "%s/krb5", clp->dirname);
-               clp->krb5_fd = open(name, O_RDWR);
+       if (clp->gssd_fd == -1) {
+               snprintf(gname, sizeof(gname), "%s/gssd", clp->dirname);
+               clp->gssd_fd = open(gname, O_RDWR);
        }
-       if (clp->spkm3_fd == -1) {
-               snprintf(name, sizeof(name), "%s/spkm3", clp->dirname);
-               clp->spkm3_fd = open(name, O_RDWR);
+       if (clp->gssd_fd == -1) {
+               if (clp->krb5_fd == -1) {
+                       snprintf(name, sizeof(name), "%s/krb5", clp->dirname);
+                       clp->krb5_fd = open(name, O_RDWR);
+               }
+               if (clp->spkm3_fd == -1) {
+                       snprintf(name, sizeof(name), "%s/spkm3", clp->dirname);
+                       clp->spkm3_fd = open(name, O_RDWR);
+               }
+
+               /* If we opened a gss-specific pipe, let's try opening
+                * the new upcall pipe again. If we succeed, close
+                * gss-specific pipe(s).
+                */
+               if (clp->krb5_fd != -1 || clp->spkm3_fd != -1) {
+                       clp->gssd_fd = open(gname, O_RDWR);
+                       if (clp->gssd_fd != -1) {
+                               if (clp->krb5_fd != -1)
+                                       close(clp->krb5_fd);
+                               clp->krb5_fd = -1;
+                               if (clp->spkm3_fd != -1)
+                                       close(clp->spkm3_fd);
+                               clp->spkm3_fd = -1;
+                       }
+               }
        }
-       if ((clp->krb5_fd == -1) && (clp->spkm3_fd == -1))
+
+       if ((clp->krb5_fd == -1) && (clp->spkm3_fd == -1) &&
+                       (clp->gssd_fd == -1))
                return -1;
        snprintf(info_file_name, sizeof(info_file_name), "%s/info",
                        clp->dirname);
@@ -369,6 +403,15 @@ get_poll_index(int *ind)
 static int
 insert_clnt_poll(struct clnt_info *clp)
 {
+       if ((clp->gssd_fd != -1) && (clp->gssd_poll_index == -1)) {
+               if (get_poll_index(&clp->gssd_poll_index)) {
+                       printerr(0, "ERROR: Too many gssd clients\n");
+                       return -1;
+               }
+               pollarray[clp->gssd_poll_index].fd = clp->gssd_fd;
+               pollarray[clp->gssd_poll_index].events |= POLLIN;
+       }
+
        if ((clp->krb5_fd != -1) && (clp->krb5_poll_index == -1)) {
                if (get_poll_index(&clp->krb5_poll_index)) {
                        printerr(0, "ERROR: Too many krb5 clients\n");
@@ -835,15 +878,14 @@ int create_auth_rpc_client(struct clnt_info *clp,
        goto out;
 }
 
-
 /*
  * this code uses the userland rpcsec gss library to create a krb5
  * context on behalf of the kernel
  */
-void
-handle_krb5_upcall(struct clnt_info *clp)
+static void
+process_krb5_upcall(struct clnt_info *clp, uid_t uid, int fd, char *tgtname,
+                   char *service)
 {
-       uid_t                   uid;
        CLIENT                  *rpc_clnt = NULL;
        AUTH                    *auth = NULL;
        struct authgss_private_data pd;
@@ -853,19 +895,43 @@ handle_krb5_upcall(struct clnt_info *clp)
        char                    **dirname;
        int                     create_resp = -1;
 
-       printerr(1, "handling krb5 upcall\n");
+       printerr(1, "handling krb5 upcall (%s)\n", clp->dirname);
 
+       if (tgtname) {
+               if (clp->servicename) {
+                       free(clp->servicename);
+                       clp->servicename = strdup(tgtname);
+               }
+       }
        token.length = 0;
        token.value = NULL;
        memset(&pd, 0, sizeof(struct authgss_private_data));
 
-       if (read(clp->krb5_fd, &uid, sizeof(uid)) < sizeof(uid)) {
-               printerr(0, "WARNING: failed reading uid from krb5 "
-                           "upcall pipe: %s\n", strerror(errno));
-               goto out;
-       }
-
-       if (uid != 0 || (uid == 0 && root_uses_machine_creds == 0)) {
+       /*
+        * If "service" is specified, then the kernel is indicating that
+        * we must use machine credentials for this request.  (Regardless
+        * of the uid value or the setting of root_uses_machine_creds.)
+        * If the service value is "*", then any service name can be used.
+        * Otherwise, it specifies the service name that should be used.
+        * (For now, the values of service will only be "*" or "nfs".)
+        *
+        * Restricting gssd to use "nfs" service name is needed for when
+        * the NFS server is doing a callback to the NFS client.  In this
+        * case, the NFS server has to authenticate itself as "nfs" --
+        * even if there are other service keys such as "host" or "root"
+        * in the keytab.
+        *
+        * Another case when the kernel may specify the service attribute
+        * is when gssd is being asked to create the context for a
+        * SETCLIENT_ID operation.  In this case, machine credentials
+        * must be used for the authentication.  However, the service name
+        * used for this case is not important.
+        *
+        */
+       printerr(2, "%s: service is '%s'\n", __func__,
+                service ? service : "<null>");
+       if (uid != 0 || (uid == 0 && root_uses_machine_creds == 0 &&
+                               service == NULL)) {
                /* Tell krb5 gss which credentials cache to use */
                for (dirname = ccachesearch; *dirname != NULL; dirname++) {
                        if (gssd_setup_krb5_user_gss_ccache(uid, clp->servername, *dirname) == 0)
@@ -876,12 +942,13 @@ handle_krb5_upcall(struct clnt_info *clp)
                }
        }
        if (create_resp != 0) {
-               if (uid == 0 && root_uses_machine_creds == 1) {
+               if (uid == 0 && (root_uses_machine_creds == 1 ||
+                               service != NULL)) {
                        int nocache = 0;
                        int success = 0;
                        do {
                                gssd_refresh_krb5_machine_credential(clp->servername,
-                                                                    NULL, nocache);
+                                                                    NULL, service);
                                /*
                                 * Get a list of credential cache names and try each
                                 * of them until one works or we've tried them all
@@ -941,7 +1008,7 @@ handle_krb5_upcall(struct clnt_info *clp)
                goto out_return_error;
        }
 
-       do_downcall(clp->krb5_fd, uid, &pd, &token);
+       do_downcall(fd, uid, &pd, &token);
 
 out:
        if (token.value)
@@ -957,7 +1024,7 @@ out:
        return;
 
 out_return_error:
-       do_error_downcall(clp->krb5_fd, uid, -1);
+       do_error_downcall(fd, uid, -1);
        goto out;
 }
 
@@ -965,26 +1032,19 @@ out_return_error:
  * this code uses the userland rpcsec gss library to create an spkm3
  * context on behalf of the kernel
  */
-void
-handle_spkm3_upcall(struct clnt_info *clp)
+static void
+process_spkm3_upcall(struct clnt_info *clp, uid_t uid, int fd)
 {
-       uid_t                   uid;
        CLIENT                  *rpc_clnt = NULL;
        AUTH                    *auth = NULL;
        struct authgss_private_data pd;
        gss_buffer_desc         token;
 
-       printerr(2, "handling spkm3 upcall\n");
+       printerr(2, "handling spkm3 upcall (%s)\n", clp->dirname);
 
        token.length = 0;
        token.value = NULL;
 
-       if (read(clp->spkm3_fd, &uid, sizeof(uid)) < sizeof(uid)) {
-               printerr(0, "WARNING: failed reading uid from spkm3 "
-                        "upcall pipe: %s\n", strerror(errno));
-               goto out;
-       }
-
        if (create_auth_rpc_client(clp, &rpc_clnt, &auth, uid, AUTHTYPE_SPKM3)) {
                printerr(0, "WARNING: Failed to create spkm3 context for "
                            "user with uid %d\n", uid);
@@ -1005,7 +1065,7 @@ handle_spkm3_upcall(struct clnt_info *clp)
                goto out_return_error;
        }
 
-       do_downcall(clp->spkm3_fd, uid, &pd, &token);
+       do_downcall(fd, uid, &pd, &token);
 
 out:
        if (token.value)
@@ -1017,6 +1077,139 @@ out:
        return;
 
 out_return_error:
-       do_error_downcall(clp->spkm3_fd, uid, -1);
+       do_error_downcall(fd, uid, -1);
        goto out;
 }
+
+void
+handle_krb5_upcall(struct clnt_info *clp)
+{
+       uid_t                   uid;
+
+       if (read(clp->krb5_fd, &uid, sizeof(uid)) < sizeof(uid)) {
+               printerr(0, "WARNING: failed reading uid from krb5 "
+                           "upcall pipe: %s\n", strerror(errno));
+               return;
+       }
+
+       return process_krb5_upcall(clp, uid, clp->krb5_fd, NULL, NULL);
+}
+
+void
+handle_spkm3_upcall(struct clnt_info *clp)
+{
+       uid_t                   uid;
+
+       if (read(clp->spkm3_fd, &uid, sizeof(uid)) < sizeof(uid)) {
+               printerr(0, "WARNING: failed reading uid from spkm3 "
+                        "upcall pipe: %s\n", strerror(errno));
+               return;
+       }
+
+       return process_spkm3_upcall(clp, uid, clp->spkm3_fd);
+}
+
+void
+handle_gssd_upcall(struct clnt_info *clp)
+{
+       uid_t                   uid;
+       char                    *lbuf = NULL;
+       int                     lbuflen = 0;
+       char                    *p;
+       char                    *mech = NULL;
+       char                    *target = NULL;
+       char                    *service = NULL;
+
+       printerr(1, "handling gssd upcall (%s)\n", clp->dirname);
+
+       if (readline(clp->gssd_fd, &lbuf, &lbuflen) != 1) {
+               printerr(0, "WARNING: handle_gssd_upcall: "
+                           "failed reading request\n");
+               return;
+       }
+       printerr(2, "%s: '%s'\n", __func__, lbuf);
+
+       /* find the mechanism name */
+       if ((p = strstr(lbuf, "mech=")) != NULL) {
+               mech = malloc(lbuflen);
+               if (!mech)
+                       goto out;
+               if (sscanf(p, "mech=%s", mech) != 1) {
+                       printerr(0, "WARNING: handle_gssd_upcall: "
+                                   "failed to parse gss mechanism name "
+                                   "in upcall string '%s'\n", lbuf);
+                       goto out;
+               }
+       } else {
+               printerr(0, "WARNING: handle_gssd_upcall: "
+                           "failed to find gss mechanism name "
+                           "in upcall string '%s'\n", lbuf);
+               goto out;
+       }
+
+       /* read uid */
+       if ((p = strstr(lbuf, "uid=")) != NULL) {
+               if (sscanf(p, "uid=%d", &uid) != 1) {
+                       printerr(0, "WARNING: handle_gssd_upcall: "
+                                   "failed to parse uid "
+                                   "in upcall string '%s'\n", lbuf);
+                       goto out;
+               }
+       } else {
+               printerr(0, "WARNING: handle_gssd_upcall: "
+                           "failed to find uid "
+                           "in upcall string '%s'\n", lbuf);
+               goto out;
+       }
+
+       /* read target name */
+       if ((p = strstr(lbuf, "target=")) != NULL) {
+               target = malloc(lbuflen);
+               if (!target)
+                       goto out;
+               if (sscanf(p, "target=%s", target) != 1) {
+                       printerr(0, "WARNING: handle_gssd_upcall: "
+                                   "failed to parse target name "
+                                   "in upcall string '%s'\n", lbuf);
+                       goto out;
+               }
+       }
+
+       /*
+        * read the service name
+        *
+        * The presence of attribute "service=" indicates that machine
+        * credentials should be used for this request.  If the value
+        * is "*", then any machine credentials available can be used.
+        * If the value is anything else, then machine credentials for
+        * the specified service name (always "nfs" for now) should be
+        * used.
+        */
+       if ((p = strstr(lbuf, "service=")) != NULL) {
+               service = malloc(lbuflen);
+               if (!service)
+                       goto out;
+               if (sscanf(p, "service=%s", service) != 1) {
+                       printerr(0, "WARNING: handle_gssd_upcall: "
+                                   "failed to parse service type "
+                                   "in upcall string '%s'\n", lbuf);
+                       goto out;
+               }
+       }
+
+       if (strcmp(mech, "krb5") == 0)
+               process_krb5_upcall(clp, uid, clp->gssd_fd, target, service);
+       else if (strcmp(mech, "spkm3") == 0)
+               process_spkm3_upcall(clp, uid, clp->gssd_fd);
+       else
+               printerr(0, "WARNING: handle_gssd_upcall: "
+                           "received unknown gss mech '%s'\n", mech);
+
+out:
+       free(lbuf);
+       free(mech);
+       free(target);
+       free(service);
+       return; 
+}
+