4 Copyright (c) 2000-2004 The Regents of the University of Michigan.
7 Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
8 Copyright (c) 2001 Andy Adamson <andros@UMICH.EDU>.
9 Copyright (c) 2002 Marius Aamodt Eriksen <marius@UMICH.EDU>.
10 Copyright (c) 2002 Bruce Fields <bfields@UMICH.EDU>
11 Copyright (c) 2004 Kevin Coffman <kwc@umich.edu>
12 All rights reserved, all wrongs reversed.
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23 3. Neither the name of the University nor the names of its
24 contributors may be used to endorse or promote products derived
25 from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <arpa/inet.h>
62 #include <gssapi/gssapi.h>
69 #include "krb5_util.h"
74 * array of struct pollfd suitable to pass to poll. initialized to
75 * zero - a zero struct is ignored by poll() because the events mask is 0.
78 * linked list of struct clnt_info which associates a clntXXX directory
79 * with an index into pollarray[], and other basic data about that client.
81 * Directory structure: created by the kernel nfs client
82 * /pipefsdir/clntXX : one per rpc_clnt struct in the kernel
83 * /pipefsdir/clntXX/krb5 : read uid for which kernel wants
84 * a context, write the resulting context
85 * /pipefsdir/clntXX/info : stores info such as server name
88 * Poll all /pipefsdir/clntXX/krb5 files. When ready, data read
89 * is a uid; performs rpcsec_gss context initialization protocol to
90 * get a cred for that user. Writes result to corresponding krb5 file
91 * in a form the kernel code will understand.
92 * In addition, we make sure we are notified whenever anything is
93 * created or destroyed in pipefsdir/ or in an of the clntXX directories,
94 * and rescan the whole pipefsdir when this happens.
97 struct pollfd * pollarray;
99 int pollsize; /* the size of pollaray (in pollfd's) */
101 /* XXX buffer problems: */
103 read_service_info(char *info_file_name, char **servicename, char **servername,
104 int *prog, int *vers, char **protocol) {
105 #define INFOBUFLEN 256
106 char buf[INFOBUFLEN];
107 static char dummy[128];
109 static char service[128];
110 static char address[128];
116 struct hostent *ent = NULL;
119 *servicename = *servername = *protocol = NULL;
121 if ((fd = open(info_file_name, O_RDONLY)) == -1) {
122 printerr(0, "ERROR: can't open %s: %s\n", info_file_name,
126 if ((nbytes = read(fd, buf, INFOBUFLEN)) == -1)
130 numfields = sscanf(buf,"RPC server: %s\n"
131 "service: %s %s version %s\n"
135 service, program, version,
139 if (numfields == 5) {
140 strcpy(protoname, "tcp");
141 } else if (numfields != 6) {
145 /* check service, program, and version */
146 if(memcmp(service, "nfs", 3)) return -1;
147 *prog = atoi(program + 1); /* skip open paren */
148 *vers = atoi(version);
149 if((*prog != 100003) || ((*vers != 2) && (*vers != 3) && (*vers != 4)))
152 /* create service name */
153 inaddr = inet_addr(address);
154 if (!(ent = gethostbyaddr(&inaddr, sizeof(inaddr), AF_INET))) {
155 printerr(0, "ERROR: can't resolve server %s name\n", address);
158 if (!(*servername = calloc(strlen(ent->h_name) + 1, 1)))
160 memcpy(*servername, ent->h_name, strlen(ent->h_name));
161 snprintf(buf, INFOBUFLEN, "%s@%s", service, ent->h_name);
162 if (!(*servicename = calloc(strlen(buf) + 1, 1)))
164 memcpy(*servicename, buf, strlen(buf));
166 if (!(*protocol = strdup(protoname)))
170 printerr(0, "ERROR: failed to read service info\n");
171 if (fd != -1) close(fd);
172 if (*servername) free(*servername);
173 if (*servicename) free(*servicename);
174 if (*protocol) free(*protocol);
179 destroy_client(struct clnt_info *clp)
181 if (clp->dir_fd != -1) close(clp->dir_fd);
182 if (clp->krb5_fd != -1) close(clp->krb5_fd);
183 if (clp->spkm3_fd != -1) close(clp->spkm3_fd);
184 if (clp->dirname) free(clp->dirname);
185 if (clp->servicename) free(clp->servicename);
186 if (clp->servername) free(clp->servername);
187 if (clp->protocol) free(clp->protocol);
191 static struct clnt_info *
192 insert_new_clnt(void)
194 struct clnt_info *clp = NULL;
196 if (!(clp = (struct clnt_info *)calloc(1,sizeof(struct clnt_info)))) {
197 printerr(0, "ERROR: can't malloc clnt_info: %s\n",
201 clp->krb5_poll_index = -1;
202 clp->spkm3_poll_index = -1;
207 TAILQ_INSERT_HEAD(&clnt_list, clp, list);
213 process_clnt_dir_files(struct clnt_info * clp)
217 char info_file_name[32];
219 snprintf(kname, sizeof(kname), "%s/krb5", clp->dirname);
220 clp->krb5_fd = open(kname, O_RDWR);
221 snprintf(sname, sizeof(sname), "%s/spkm3", clp->dirname);
222 clp->spkm3_fd = open(sname, O_RDWR);
223 if((clp->krb5_fd == -1) && (clp->spkm3_fd == -1))
225 snprintf(info_file_name, sizeof(info_file_name), "%s/info",
227 if (read_service_info(info_file_name, &clp->servicename,
228 &clp->servername, &clp->prog, &clp->vers,
235 get_poll_index(int *ind)
240 for (i=0; i<FD_ALLOC_BLOCK; i++) {
241 if (pollarray[i].events == 0) {
247 printerr(0, "ERROR: No pollarray slots open\n");
254 process_clnt_dir(char *dir)
256 struct clnt_info * clp;
258 if (!(clp = insert_new_clnt()))
259 goto fail_destroy_client;
261 if (!(clp->dirname = calloc(strlen(dir) + 1, 1))) {
262 goto fail_destroy_client;
264 memcpy(clp->dirname, dir, strlen(dir));
265 if ((clp->dir_fd = open(clp->dirname, O_RDONLY)) == -1) {
266 printerr(0, "ERROR: can't open %s: %s\n",
267 clp->dirname, strerror(errno));
268 goto fail_destroy_client;
270 fcntl(clp->dir_fd, F_SETSIG, DNOTIFY_SIGNAL);
271 fcntl(clp->dir_fd, F_NOTIFY, DN_CREATE | DN_DELETE | DN_MULTISHOT);
273 if (process_clnt_dir_files(clp))
274 goto fail_keep_client;
276 if(clp->krb5_fd != -1) {
277 if (get_poll_index(&clp->krb5_poll_index)) {
278 printerr(0, "ERROR: Too many krb5 clients\n");
279 goto fail_destroy_client;
281 pollarray[clp->krb5_poll_index].fd = clp->krb5_fd;
282 pollarray[clp->krb5_poll_index].events |= POLLIN;
285 if(clp->spkm3_fd != -1) {
286 if (get_poll_index(&clp->spkm3_poll_index)) {
287 printerr(0, "ERROR: Too many spkm3 clients\n");
288 goto fail_destroy_client;
290 pollarray[clp->spkm3_poll_index].fd = clp->spkm3_fd;
291 pollarray[clp->spkm3_poll_index].events |= POLLIN;
298 TAILQ_REMOVE(&clnt_list, clp, list);
302 /* We couldn't find some subdirectories, but we keep the client
303 * around in case we get a notification on the directory when the
304 * subdirectories are created. */
309 init_client_list(void)
311 TAILQ_INIT(&clnt_list);
312 /* Eventually plan to grow/shrink poll array: */
313 pollsize = FD_ALLOC_BLOCK;
314 pollarray = calloc(pollsize, sizeof(struct pollfd));
318 destroy_client_list(void)
320 struct clnt_info *clp;
322 printerr(1, "processing client list\n");
324 while (clnt_list.tqh_first != NULL) {
325 clp = clnt_list.tqh_first;
326 TAILQ_REMOVE(&clnt_list, clp, list);
331 /* Used to read (and re-read) list of clients, set up poll array. */
333 update_client_list(void)
335 struct dirent **namelist;
338 destroy_client_list();
340 if (chdir(pipefsdir) < 0) {
341 printerr(0, "ERROR: can't chdir to %s: %s\n",
342 pipefsdir, strerror(errno));
346 memset(pollarray, 0, pollsize * sizeof(struct pollfd));
348 j = scandir(pipefsdir, &namelist, NULL, alphasort);
350 printerr(0, "ERROR: can't scandir %s: %s\n",
351 pipefsdir, strerror(errno));
354 for (i=0; i < j; i++) {
355 if (i < FD_ALLOC_BLOCK
356 && !strncmp(namelist[i]->d_name, "clnt", 4))
357 process_clnt_dir(namelist[i]->d_name);
366 do_downcall(int k5_fd, uid_t uid, struct authgss_private_data *pd,
367 gss_buffer_desc *context_token)
370 char *p = buf, *end = buf + 2048;
371 unsigned int timeout = 0; /* XXX decide on a reasonable value */
373 printerr(1, "doing downcall\n");
375 if (WRITE_BYTES(&p, end, uid)) return -1;
376 /* Not setting any timeout for now: */
377 if (WRITE_BYTES(&p, end, timeout)) return -1;
378 if (WRITE_BYTES(&p, end, pd->pd_seq_win)) return -1;
379 if (write_buffer(&p, end, &pd->pd_ctx_hndl)) return -1;
380 if (write_buffer(&p, end, context_token)) return -1;
382 if (write(k5_fd, buf, p - buf) < p - buf) return -1;
387 do_error_downcall(int k5_fd, uid_t uid, int err)
390 char *p = buf, *end = buf + 1024;
391 unsigned int timeout = 0;
394 printerr(1, "doing error downcall\n");
396 if (WRITE_BYTES(&p, end, uid)) return -1;
397 if (WRITE_BYTES(&p, end, timeout)) return -1;
398 /* use seq_win = 0 to indicate an error: */
399 if (WRITE_BYTES(&p, end, zero)) return -1;
400 if (WRITE_BYTES(&p, end, err)) return -1;
402 if (write(k5_fd, buf, p - buf) < p - buf) return -1;
408 * Create an RPC connection and establish an authenticated
409 * gss context with a server.
411 int create_auth_rpc_client(struct clnt_info *clp,
416 CLIENT *rpc_clnt = NULL;
417 struct rpc_gss_sec sec;
423 sec.qop = GSS_C_QOP_DEFAULT;
424 sec.svc = RPCSEC_GSS_SVC_NONE;
425 sec.cred = GSS_C_NO_CREDENTIAL;
427 if (authtype == AUTHTYPE_KRB5) {
428 sec.mech = (gss_OID)&krb5oid;
429 sec.req_flags = GSS_C_MUTUAL_FLAG;
431 else if (authtype == AUTHTYPE_SPKM3) {
432 sec.mech = (gss_OID)&spkm3oid;
433 sec.req_flags = GSS_C_ANON_FLAG;
436 printerr(0, "ERROR: Invalid authentication type (%d) "
437 "in create_auth_rpc_client\n", authtype);
442 if (authtype == AUTHTYPE_KRB5) {
443 #ifdef HAVE_SET_ALLOWABLE_ENCTYPES
445 * Do this before creating rpc connection since we won't need
446 * rpc connection if it fails!
448 if (limit_krb5_enctypes(&sec, uid)) {
449 printerr(1, "WARNING: Failed while limiting krb5 "
450 "encryption types for user with uid %d\n",
457 /* Create the context as the user (not as root) */
458 save_uid = geteuid();
459 if (seteuid(uid) != 0) {
460 printerr(0, "WARNING: Failed to seteuid for "
461 "user with uid %d\n", uid);
464 printerr(2, "creating context using euid %d (save_uid %d)\n",
465 geteuid(), save_uid);
467 /* create an rpc connection to the nfs server */
469 printerr(2, "creating %s client for server %s\n", clp->protocol,
471 if ((rpc_clnt = clnt_create(clp->servername, clp->prog, clp->vers,
472 clp->protocol)) == NULL) {
473 printerr(0, "WARNING: can't create rpc_clnt for server "
474 "%s for user with uid %d\n",
475 clp->servername, uid);
479 printerr(2, "creating context with server %s\n", clp->servicename);
480 auth = authgss_create_default(rpc_clnt, clp->servicename, &sec);
482 /* Our caller should print appropriate message */
483 printerr(2, "WARNING: Failed to create krb5 context for "
484 "user with uid %d for server %s\n",
485 uid, clp->servername);
489 /* Restore euid to original value */
490 if (seteuid(save_uid) != 0) {
491 printerr(0, "WARNING: Failed to restore euid"
492 " to uid %d\n", save_uid);
502 if (sec.cred != GSS_C_NO_CREDENTIAL)
503 gss_release_cred(&min_stat, &sec.cred);
504 if (rpc_clnt) clnt_destroy(rpc_clnt);
511 * this code uses the userland rpcsec gss library to create a krb5
512 * context on behalf of the kernel
515 handle_krb5_upcall(struct clnt_info *clp)
519 struct authgss_private_data pd;
520 gss_buffer_desc token;
521 char **credlist = NULL;
524 printerr(1, "handling krb5 upcall\n");
529 if (read(clp->krb5_fd, &uid, sizeof(uid)) < sizeof(uid)) {
530 printerr(0, "WARNING: failed reading uid from krb5 "
531 "upcall pipe: %s\n", strerror(errno));
539 * Get a list of credential cache names and try each
540 * of them until one works or we've tried them all
542 if (gssd_get_krb5_machine_cred_list(&credlist)) {
543 printerr(0, "WARNING: Failed to obtain machine "
544 "credentials for connection to "
545 "server %s\n", clp->servername);
546 goto out_return_error;
548 for (ccname = credlist; ccname && *ccname; ccname++) {
549 gssd_setup_krb5_machine_gss_ccache(*ccname);
550 if ((create_auth_rpc_client(clp, &auth, uid,
551 AUTHTYPE_KRB5)) == 0) {
556 printerr(2, "WARNING: Failed to create krb5 context "
557 "for user with uid %d with credentials "
558 "cache %s for server %s\n",
559 uid, *ccname, clp->servername);
561 gssd_free_krb5_machine_cred_list(credlist);
563 printerr(0, "WARNING: Failed to create krb5 context "
564 "for user with uid %d with any "
565 "credentials cache for server %s\n",
566 uid, clp->servername);
567 goto out_return_error;
571 /* Tell krb5 gss which credentials cache to use */
572 gssd_setup_krb5_user_gss_ccache(uid, clp->servername);
574 if (create_auth_rpc_client(clp, &auth, uid, AUTHTYPE_KRB5)) {
575 printerr(0, "WARNING: Failed to create krb5 context "
576 "for user with uid %d for server %s\n",
577 uid, clp->servername);
578 goto out_return_error;
582 if (!authgss_get_private_data(auth, &pd)) {
583 printerr(0, "WARNING: Failed to obtain authentication "
584 "data for user with uid %d for server %s\n",
585 uid, clp->servername);
586 goto out_return_error;
589 if (serialize_context_for_kernel(pd.pd_ctx, &token)) {
590 printerr(0, "WARNING: Failed to serialize krb5 context for "
591 "user with uid %d for server %s\n",
592 uid, clp->servername);
593 goto out_return_error;
596 do_downcall(clp->krb5_fd, uid, &pd, &token);
604 do_error_downcall(clp->krb5_fd, uid, -1);
609 * this code uses the userland rpcsec gss library to create an spkm3
610 * context on behalf of the kernel
613 handle_spkm3_upcall(struct clnt_info *clp)
617 struct authgss_private_data pd;
618 gss_buffer_desc token;
620 printerr(2, "handling spkm3 upcall\n");
625 if (read(clp->spkm3_fd, &uid, sizeof(uid)) < sizeof(uid)) {
626 printerr(0, "WARNING: failed reading uid from spkm3 "
627 "upcall pipe: %s\n", strerror(errno));
631 if (create_auth_rpc_client(clp, &auth, uid, AUTHTYPE_SPKM3)) {
632 printerr(0, "WARNING: Failed to create spkm3 context for "
633 "user with uid %d\n", uid);
634 goto out_return_error;
637 if (!authgss_get_private_data(auth, &pd)) {
638 printerr(0, "WARNING: Failed to obtain authentication "
639 "data for user with uid %d for server %s\n",
640 uid, clp->servername);
641 goto out_return_error;
644 if (serialize_context_for_kernel(pd.pd_ctx, &token)) {
645 printerr(0, "WARNING: Failed to serialize spkm3 context for "
646 "user with uid %d for server\n",
647 uid, clp->servername);
648 goto out_return_error;
651 do_downcall(clp->spkm3_fd, uid, &pd, &token);
659 do_error_downcall(clp->spkm3_fd, uid, -1);