Whamcloud - gitweb
b=14528
[fs/lustre-release.git] / lustre / utils / gss / svcgssd_proc.c
1 /*
2   svc_in_gssd_proc.c
3
4   Copyright (c) 2000 The Regents of the University of Michigan.
5   All rights reserved.
6
7   Copyright (c) 2002 Bruce Fields <bfields@UMICH.EDU>
8
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions
11   are met:
12
13   1. Redistributions of source code must retain the above copyright
14      notice, this list of conditions and the following disclaimer.
15   2. Redistributions in binary form must reproduce the above copyright
16      notice, this list of conditions and the following disclaimer in the
17      documentation and/or other materials provided with the distribution.
18   3. Neither the name of the University nor the names of its
19      contributors may be used to endorse or promote products derived
20      from this software without specific prior written permission.
21
22   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 */
35
36 #include <sys/param.h>
37 #include <sys/stat.h>
38
39 #include <pwd.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <netdb.h>
47
48 #include "svcgssd.h"
49 #include "gss_util.h"
50 #include "err_util.h"
51 #include "context.h"
52 #include "cacheio.h"
53 #include "lsupport.h"
54
55 extern char * mech2file(gss_OID mech);
56 #define SVCGSSD_CONTEXT_CHANNEL "/proc/net/rpc/auth.sptlrpc.context/channel"
57 #define SVCGSSD_INIT_CHANNEL    "/proc/net/rpc/auth.sptlrpc.init/channel"
58
59 #define TOKEN_BUF_SIZE          8192
60
61 struct svc_cred {
62         uint32_t cr_remote;
63         uint32_t cr_usr_root;
64         uint32_t cr_usr_mds;
65         uid_t    cr_uid;
66         uid_t    cr_mapped_uid;
67         uid_t    cr_gid;
68 };
69
70 static int
71 do_svc_downcall(gss_buffer_desc *out_handle, struct svc_cred *cred,
72                 gss_OID mech, gss_buffer_desc *context_token)
73 {
74         FILE *f;
75         char *fname = NULL;
76         int err;
77
78         printerr(2, "doing downcall\n");
79         if ((fname = mech2file(mech)) == NULL)
80                 goto out_err;
81         f = fopen(SVCGSSD_CONTEXT_CHANNEL, "w");
82         if (f == NULL) {
83                 printerr(0, "WARNING: unable to open downcall channel "
84                              "%s: %s\n",
85                              SVCGSSD_CONTEXT_CHANNEL, strerror(errno));
86                 goto out_err;
87         }
88         qword_printhex(f, out_handle->value, out_handle->length);
89         /* XXX are types OK for the rest of this? */
90         qword_printint(f, 0x7fffffff); /*XXX need a better timeout */
91         qword_printint(f, cred->cr_remote);
92         qword_printint(f, cred->cr_usr_root);
93         qword_printint(f, cred->cr_usr_mds);
94         qword_printint(f, cred->cr_mapped_uid);
95         qword_printint(f, cred->cr_uid);
96         qword_printint(f, cred->cr_gid);
97         qword_print(f, fname);
98         qword_printhex(f, context_token->value, context_token->length);
99         err = qword_eol(f);
100         fclose(f);
101         return err;
102 out_err:
103         printerr(0, "WARNING: downcall failed\n");
104         return -1;
105 }
106
107 struct gss_verifier {
108         u_int32_t       flav;
109         gss_buffer_desc body;
110 };
111
112 #define RPCSEC_GSS_SEQ_WIN      5
113
114 static int
115 send_response(FILE *f, gss_buffer_desc *in_handle, gss_buffer_desc *in_token,
116               u_int32_t maj_stat, u_int32_t min_stat,
117               gss_buffer_desc *out_handle, gss_buffer_desc *out_token)
118 {
119         char buf[2 * TOKEN_BUF_SIZE];
120         char *bp = buf;
121         int blen = sizeof(buf);
122         /* XXXARG: */
123         int g;
124
125         printerr(2, "sending null reply\n");
126
127         qword_addhex(&bp, &blen, in_handle->value, in_handle->length);
128         qword_addhex(&bp, &blen, in_token->value, in_token->length);
129         qword_addint(&bp, &blen, 0x7fffffff); /*XXX need a better timeout */
130         qword_adduint(&bp, &blen, maj_stat);
131         qword_adduint(&bp, &blen, min_stat);
132         qword_addhex(&bp, &blen, out_handle->value, out_handle->length);
133         qword_addhex(&bp, &blen, out_token->value, out_token->length);
134         qword_addeol(&bp, &blen);
135         if (blen <= 0) {
136                 printerr(0, "WARNING: send_respsonse: message too long\n");
137                 return -1;
138         }
139         g = open(SVCGSSD_INIT_CHANNEL, O_WRONLY);
140         if (g == -1) {
141                 printerr(0, "WARNING: open %s failed: %s\n",
142                                 SVCGSSD_INIT_CHANNEL, strerror(errno));
143                 return -1;
144         }
145         *bp = '\0';
146         printerr(3, "writing message: %s", buf);
147         if (write(g, buf, bp - buf) == -1) {
148                 printerr(0, "WARNING: failed to write message\n");
149                 close(g);
150                 return -1;
151         }
152         close(g);
153         return 0;
154 }
155
156 #define rpc_auth_ok                     0
157 #define rpc_autherr_badcred             1
158 #define rpc_autherr_rejectedcred        2
159 #define rpc_autherr_badverf             3
160 #define rpc_autherr_rejectedverf        4
161 #define rpc_autherr_tooweak             5
162 #define rpcsec_gsserr_credproblem       13
163 #define rpcsec_gsserr_ctxproblem        14
164
165 #if 0
166 static void
167 add_supplementary_groups(char *secname, char *name, struct svc_cred *cred)
168 {
169         int ret;
170         static gid_t *groups = NULL;
171
172         cred->cr_ngroups = NGROUPS;
173         ret = nfs4_gss_princ_to_grouplist(secname, name,
174                         cred->cr_groups, &cred->cr_ngroups);
175         if (ret < 0) {
176                 groups = realloc(groups, cred->cr_ngroups*sizeof(gid_t));
177                 ret = nfs4_gss_princ_to_grouplist(secname, name,
178                                 groups, &cred->cr_ngroups);
179                 if (ret < 0)
180                         cred->cr_ngroups = 0;
181                 else {
182                         if (cred->cr_ngroups > NGROUPS)
183                                 cred->cr_ngroups = NGROUPS;
184                         memcpy(cred->cr_groups, groups,
185                                         cred->cr_ngroups*sizeof(gid_t));
186                 }
187         }
188 }
189 #endif
190
191 #if 0
192 static int
193 get_ids(gss_name_t client_name, gss_OID mech, struct svc_cred *cred)
194 {
195         u_int32_t       maj_stat, min_stat;
196         gss_buffer_desc name;
197         char            *sname;
198         int             res = -1;
199         uid_t           uid, gid;
200         gss_OID         name_type = GSS_C_NO_OID;
201         char            *secname;
202
203         maj_stat = gss_display_name(&min_stat, client_name, &name, &name_type);
204         if (maj_stat != GSS_S_COMPLETE) {
205                 pgsserr("get_ids: gss_display_name",
206                         maj_stat, min_stat, mech);
207                 goto out;
208         }
209         if (name.length >= 0xffff || /* be certain name.length+1 doesn't overflow */
210             !(sname = calloc(name.length + 1, 1))) {
211                 printerr(0, "WARNING: get_ids: error allocating %d bytes "
212                         "for sname\n", name.length + 1);
213                 gss_release_buffer(&min_stat, &name);
214                 goto out;
215         }
216         memcpy(sname, name.value, name.length);
217         printerr(1, "sname = %s\n", sname);
218         gss_release_buffer(&min_stat, &name);
219
220         res = -EINVAL;
221         if ((secname = mech2file(mech)) == NULL) {
222                 printerr(0, "WARNING: get_ids: error mapping mech to "
223                         "file for name '%s'\n", sname);
224                 goto out_free;
225         }
226         nfs4_init_name_mapping(NULL); /* XXX: should only do this once */
227         res = nfs4_gss_princ_to_ids(secname, sname, &uid, &gid);
228         if (res < 0) {
229                 /*
230                  * -ENOENT means there was no mapping, any other error
231                  * value means there was an error trying to do the
232                  * mapping.
233                  * If there was no mapping, we send down the value -1
234                  * to indicate that the anonuid/anongid for the export
235                  * should be used.
236                  */
237                 if (res == -ENOENT) {
238                         cred->cr_uid = -1;
239                         cred->cr_gid = -1;
240                         cred->cr_ngroups = 0;
241                         res = 0;
242                         goto out_free;
243                 }
244                 printerr(0, "WARNING: get_ids: failed to map name '%s' "
245                         "to uid/gid: %s\n", sname, strerror(-res));
246                 goto out_free;
247         }
248         cred->cr_uid = uid;
249         cred->cr_gid = gid;
250         add_supplementary_groups(secname, sname, cred);
251         res = 0;
252 out_free:
253         free(sname);
254 out:
255         return res;
256 }
257 #endif
258
259 #if 0
260 void
261 print_hexl(int pri, unsigned char *cp, int length)
262 {
263         int i, j, jm;
264         unsigned char c;
265
266         printerr(pri, "length %d\n",length);
267         printerr(pri, "\n");
268
269         for (i = 0; i < length; i += 0x10) {
270                 printerr(pri, "  %04x: ", (u_int)i);
271                 jm = length - i;
272                 jm = jm > 16 ? 16 : jm;
273
274                 for (j = 0; j < jm; j++) {
275                         if ((j % 2) == 1)
276                                 printerr(pri,"%02x ", (u_int)cp[i+j]);
277                         else
278                                 printerr(pri,"%02x", (u_int)cp[i+j]);
279                 }
280                 for (; j < 16; j++) {
281                         if ((j % 2) == 1)
282                                 printerr(pri,"   ");
283                         else
284                                 printerr(pri,"  ");
285                 }
286                 printerr(pri," ");
287
288                 for (j = 0; j < jm; j++) {
289                         c = cp[i+j];
290                         c = isprint(c) ? c : '.';
291                         printerr(pri,"%c", c);
292                 }
293                 printerr(pri,"\n");
294         }
295 }
296 #endif
297
298 static int
299 get_ids(gss_name_t client_name, gss_OID mech, struct svc_cred *cred,
300         lnet_nid_t nid, uint32_t lustre_svc)
301 {
302         u_int32_t       maj_stat, min_stat;
303         gss_buffer_desc name;
304         char            *sname, *host, *realm;
305         const int       namebuf_size = 512;
306         char            namebuf[namebuf_size];
307         int             res = -1;
308         gss_OID         name_type = GSS_C_NO_OID;
309         struct passwd   *pw;
310
311         cred->cr_remote = cred->cr_usr_root = cred->cr_usr_mds = 0;
312         cred->cr_uid = cred->cr_mapped_uid = cred->cr_gid = -1;
313
314         maj_stat = gss_display_name(&min_stat, client_name, &name, &name_type);
315         if (maj_stat != GSS_S_COMPLETE) {
316                 pgsserr("get_ids: gss_display_name",
317                         maj_stat, min_stat, mech);
318                 return -1;
319         }
320         if (name.length >= 0xffff || /* be certain name.length+1 doesn't overflow */
321             !(sname = calloc(name.length + 1, 1))) {
322                 printerr(0, "WARNING: get_ids: error allocating %d bytes "
323                         "for sname\n", name.length + 1);
324                 gss_release_buffer(&min_stat, &name);
325                 return -1;
326         }
327         memcpy(sname, name.value, name.length);
328         gss_release_buffer(&min_stat, &name);
329
330         if (lustre_svc == LUSTRE_GSS_SVC_MDS)
331                 lookup_mapping(sname, nid, &cred->cr_mapped_uid);
332         else
333                 cred->cr_mapped_uid = -1;
334
335         realm = strchr(sname, '@');
336         if (realm)
337                 *realm++ = '\0';
338
339         host = strchr(sname, '/');
340         if (host)
341                 *host++ = '\0';
342
343         if (strcmp(sname, GSSD_SERVICE_OSS) == 0) {
344                 printerr(0, "forbid "GSSD_SERVICE_OSS" as user name\n");
345                 goto out_free;
346         }
347
348         /* 1. check host part */
349         if (host) {
350                 if (lnet_nid2hostname(nid, namebuf, namebuf_size)) {
351                         printerr(0, "ERROR: failed to resolve hostname for "
352                                  "%s/%s@%s from %016llx\n",
353                                  sname, host, realm, nid);
354                         goto out_free;
355                 }
356
357                 if (strcasecmp(host, namebuf)) {
358                         printerr(0, "ERROR: %s/%s@s claimed hostname doesn't "
359                                  "match %s, nid %016llx\n", sname, host, realm,
360                                  namebuf, nid);
361                         goto out_free;
362                 }
363         } else {
364                 if (!strcmp(sname, GSSD_SERVICE_MDS)) {
365                         printerr(0, "ERROR: "GSSD_SERVICE_MDS"@%s from %016llx "
366                                  "doesn't bind with hostname\n",
367                                  realm ? realm : "", nid);
368                         goto out_free;
369                 }
370         }
371
372         /* 2. check realm */
373         if (!realm) {
374                 /* just deny it
375                 cred->cr_remote = (mds_local_realm != NULL);
376                 */
377                 printerr(0, "ERROR: %s%s%s have no realm name\n",
378                          sname, host ? "/" : "", host ? "host" : "");
379                 goto out_free;
380         }
381
382         if (!mds_local_realm || strcasecmp(mds_local_realm, realm)) {
383                 cred->cr_remote = 1;
384
385                 if (cred->cr_mapped_uid == -1)
386                         printerr(0, "ERROR: %s from %016llx is remote but "
387                                  "without mapping\n", sname, nid);
388                 /* mapped, skip user checking */
389                 goto out_free;
390         }
391
392         /* 3. check user */
393         if (!(pw = getpwnam(sname))) {
394                 /* map lustre_root/lustre_mds to root user, which is subject
395                  * to further mapping by root-squash in kernel. */
396                 if (!strcmp(sname, LUSTRE_ROOT_NAME)) {
397                         cred->cr_uid = 0;
398                         cred->cr_usr_root = 1;
399                 } else if (!strcmp(sname, GSSD_SERVICE_MDS)) {
400                         cred->cr_uid = 0;
401                         cred->cr_usr_mds = 1;
402                 } else {
403                         if (cred->cr_mapped_uid == -1) {
404                                 printerr(0, "ERROR: invalid user, %s/%s@%s "
405                                          "from %016llx\n", sname, host,
406                                          realm, nid);
407                                 goto out_free;
408                         }
409                 }
410                 printerr(2, "user %s from %016llx is mapped to %u\n",
411                          sname, nid, cred->cr_mapped_uid);
412         } else {
413                 /* note: a mapped local user will go to here too */
414                 cred->cr_uid = pw->pw_uid;
415                 printerr(2, "%s resolve to uid %u\n", sname, cred->cr_uid);
416         }
417
418         printerr(1, "%s: authenticated %s%s%s@%s from %016llx\n",
419                  lustre_svc_name[lustre_svc], sname,
420                  host ? "/" : "", host ? host : "", realm, nid);
421         res = 0;
422 out_free:
423         free(sname);
424         return res;
425 }
426
427 typedef struct gss_union_ctx_id_t {
428         gss_OID         mech_type;
429         gss_ctx_id_t    internal_ctx_id;
430 } gss_union_ctx_id_desc, *gss_union_ctx_id_t;
431
432 /*
433  * return -1 only if we detect error during reading from upcall channel,
434  * all other cases return 0.
435  */
436 int
437 handle_nullreq(FILE *f) {
438         uint64_t                handle_seq;
439         char                    in_tok_buf[TOKEN_BUF_SIZE];
440         char                    in_handle_buf[15];
441         char                    out_handle_buf[15];
442         gss_buffer_desc         in_tok = {.value = in_tok_buf},
443                                 out_tok = {.value = NULL},
444                                 in_handle = {.value = in_handle_buf},
445                                 out_handle = {.value = out_handle_buf},
446                                 ctx_token = {.value = NULL},
447                                 ignore_out_tok = {.value = NULL},
448         /* XXX isn't there a define for this?: */
449                                 null_token = {.value = NULL};
450         uint32_t                lustre_svc;
451         lnet_nid_t              nid;
452         u_int32_t               ret_flags;
453         gss_ctx_id_t            ctx = GSS_C_NO_CONTEXT;
454         gss_name_t              client_name;
455         gss_OID                 mech = GSS_C_NO_OID;
456         gss_cred_id_t           svc_cred;
457         u_int32_t               maj_stat = GSS_S_FAILURE, min_stat = 0;
458         u_int32_t               ignore_min_stat;
459         struct svc_cred         cred;
460         static char             *lbuf = NULL;
461         static int              lbuflen = 0;
462         static char             *cp;
463
464         printerr(2, "handling null request\n");
465
466         if (readline(fileno(f), &lbuf, &lbuflen) != 1) {
467                 printerr(0, "WARNING: handle_nullreq: "
468                             "failed reading request\n");
469                 return -1;
470         }
471
472         cp = lbuf;
473
474         qword_get(&cp, (char *) &lustre_svc, sizeof(lustre_svc));
475         qword_get(&cp, (char *) &nid, sizeof(nid));
476         qword_get(&cp, (char *) &handle_seq, sizeof(handle_seq));
477         printerr(2, "handling req: svc %u, nid %016llx, idx %llx\n",
478                  lustre_svc, nid, handle_seq);
479
480         in_handle.length = (size_t) qword_get(&cp, in_handle.value,
481                                               sizeof(in_handle_buf));
482         printerr(3, "in_handle: \n");
483         print_hexl(3, in_handle.value, in_handle.length);
484
485         in_tok.length = (size_t) qword_get(&cp, in_tok.value,
486                                            sizeof(in_tok_buf));
487         printerr(3, "in_tok: \n");
488         print_hexl(3, in_tok.value, in_tok.length);
489
490         if (in_tok.length < 0) {
491                 printerr(0, "WARNING: handle_nullreq: "
492                             "failed parsing request\n");
493                 goto out_err;
494         }
495
496         if (in_handle.length != 0) { /* CONTINUE_INIT case */
497                 if (in_handle.length != sizeof(ctx)) {
498                         printerr(0, "WARNING: handle_nullreq: "
499                                     "input handle has unexpected length %d\n",
500                                     in_handle.length);
501                         goto out_err;
502                 }
503                 /* in_handle is the context id stored in the out_handle
504                  * for the GSS_S_CONTINUE_NEEDED case below.  */
505                 memcpy(&ctx, in_handle.value, in_handle.length);
506         }
507
508         svc_cred = gssd_select_svc_cred(lustre_svc);
509         if (!svc_cred) {
510                 printerr(0, "no service credential for svc %u\n", lustre_svc);
511                 goto out_err;
512         }
513
514         maj_stat = gss_accept_sec_context(&min_stat, &ctx, svc_cred,
515                         &in_tok, GSS_C_NO_CHANNEL_BINDINGS, &client_name,
516                         &mech, &out_tok, &ret_flags, NULL, NULL);
517
518         if (maj_stat == GSS_S_CONTINUE_NEEDED) {
519                 printerr(1, "gss_accept_sec_context GSS_S_CONTINUE_NEEDED\n");
520
521                 /* Save the context handle for future calls */
522                 out_handle.length = sizeof(ctx);
523                 memcpy(out_handle.value, &ctx, sizeof(ctx));
524                 goto continue_needed;
525         }
526         else if (maj_stat != GSS_S_COMPLETE) {
527                 printerr(0, "WARNING: gss_accept_sec_context failed\n");
528                 pgsserr("handle_nullreq: gss_accept_sec_context",
529                         maj_stat, min_stat, mech);
530                 goto out_err;
531         }
532
533         if (get_ids(client_name, mech, &cred, nid, lustre_svc)) {
534                 /* get_ids() prints error msg */
535                 maj_stat = GSS_S_BAD_NAME; /* XXX ? */
536                 gss_release_name(&ignore_min_stat, &client_name);
537                 goto out_err;
538         }
539         gss_release_name(&ignore_min_stat, &client_name);
540
541         /* Context complete. Pass handle_seq in out_handle to use
542          * for context lookup in the kernel. */
543         out_handle.length = sizeof(handle_seq);
544         memcpy(out_handle.value, &handle_seq, sizeof(handle_seq));
545
546         /* kernel needs ctx to calculate verifier on null response, so
547          * must give it context before doing null call: */
548         if (serialize_context_for_kernel(ctx, &ctx_token, mech)) {
549                 printerr(0, "WARNING: handle_nullreq: "
550                             "serialize_context_for_kernel failed\n");
551                 maj_stat = GSS_S_FAILURE;
552                 goto out_err;
553         }
554         /* We no longer need the gss context */
555         gss_delete_sec_context(&ignore_min_stat, &ctx, &ignore_out_tok);
556
557         do_svc_downcall(&out_handle, &cred, mech, &ctx_token);
558 continue_needed:
559         send_response(f, &in_handle, &in_tok, maj_stat, min_stat,
560                         &out_handle, &out_tok);
561 out:
562         if (ctx_token.value != NULL)
563                 free(ctx_token.value);
564         if (out_tok.value != NULL)
565                 gss_release_buffer(&ignore_min_stat, &out_tok);
566         return 0;
567
568 out_err:
569         if (ctx != GSS_C_NO_CONTEXT)
570                 gss_delete_sec_context(&ignore_min_stat, &ctx, &ignore_out_tok);
571         send_response(f, &in_handle, &in_tok, maj_stat, min_stat,
572                         &null_token, &null_token);
573         goto out;
574 }