Merge commit 'cc543d0f9e35a75cc302a4cb152756d233299564'
[unleashed.git] / usr / src / lib / libsmbfs / smb / ctx.c
blobb4fc7aa8e706f58a43ba5735fa025d13dade195e
1 /*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * $Id: ctx.c,v 1.32.70.2 2005/06/02 00:55:40 lindak Exp $
36 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
37 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
40 #include <sys/param.h>
41 #include <sys/ioctl.h>
42 #include <sys/time.h>
43 #include <sys/mount.h>
44 #include <sys/types.h>
45 #include <sys/byteorder.h>
47 #include <fcntl.h>
48 #include <ctype.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <strings.h>
53 #include <stdlib.h>
54 #include <pwd.h>
55 #include <grp.h>
56 #include <unistd.h>
57 #include <libintl.h>
58 #include <assert.h>
59 #include <nss_dbdefs.h>
61 #include <cflib.h>
62 #include <netsmb/smb_lib.h>
63 #include <netsmb/netbios.h>
64 #include <netsmb/nb_lib.h>
65 #include <netsmb/smb_dev.h>
67 #include "charsets.h"
68 #include "private.h"
69 #include "ntlm.h"
71 #ifndef FALSE
72 #define FALSE 0
73 #endif
74 #ifndef TRUE
75 #define TRUE 1
76 #endif
78 #define SMB_AT_DEFAULT (SMB_AT_KRB5 | SMB_AT_NTLM2)
79 #define SMB_AT_MINAUTH (SMB_AT_KRB5 | SMB_AT_NTLM2 | SMB_AT_NTLM1)
81 struct nv {
82 char *name;
83 int value;
86 /* These two may be set by commands. */
87 int smb_debug, smb_verbose;
90 * Was: STDPARAM_OPT - see smb_ctx_scan_argv, smb_ctx_opt
92 const char smbutil_std_opts[] = "ABCD:E:I:L:M:NO:P:U:R:S:T:W:";
95 * Defaults for new contexts (connections to servers).
96 * These are set by smbfs_set_default_...
98 static char default_domain[SMBIOC_MAX_NAME];
99 static char default_user[SMBIOC_MAX_NAME];
103 * Give the RPC library a callback hook that will be
104 * called whenever we destroy or reinit an smb_ctx_t.
105 * The name rpc_cleanup_smbctx() is legacy, and was
106 * originally a direct call into the RPC code.
108 static smb_ctx_close_hook_t close_hook;
109 static void
110 rpc_cleanup_smbctx(struct smb_ctx *ctx)
112 if (close_hook)
113 (*close_hook)(ctx);
115 void
116 smb_ctx_set_close_hook(smb_ctx_close_hook_t hook)
118 close_hook = hook;
121 void
122 dump_ctx_flags(int flags)
124 printf(" Flags: ");
125 if (flags == 0)
126 printf("0");
127 if (flags & SMBCF_NOPWD)
128 printf("NOPWD ");
129 if (flags & SMBCF_SRIGHTS)
130 printf("SRIGHTS ");
131 if (flags & SMBCF_LOCALE)
132 printf("LOCALE ");
133 if (flags & SMBCF_CMD_DOM)
134 printf("CMD_DOM ");
135 if (flags & SMBCF_CMD_USR)
136 printf("CMD_USR ");
137 if (flags & SMBCF_CMD_PW)
138 printf("CMD_PW ");
139 if (flags & SMBCF_RESOLVED)
140 printf("RESOLVED ");
141 if (flags & SMBCF_KCBAD)
142 printf("KCBAD ");
143 if (flags & SMBCF_KCFOUND)
144 printf("KCFOUND ");
145 if (flags & SMBCF_BROWSEOK)
146 printf("BROWSEOK ");
147 if (flags & SMBCF_AUTHREQ)
148 printf("AUTHREQ ");
149 if (flags & SMBCF_KCSAVE)
150 printf("KCSAVE ");
151 if (flags & SMBCF_XXX)
152 printf("XXX ");
153 if (flags & SMBCF_SSNACTIVE)
154 printf("SSNACTIVE ");
155 if (flags & SMBCF_KCDOMAIN)
156 printf("KCDOMAIN ");
157 printf("\n");
160 void
161 dump_iod_ssn(smb_iod_ssn_t *is)
163 static const char zeros[NTLM_HASH_SZ] = {0};
164 struct smbioc_ossn *ssn = &is->iod_ossn;
166 printf(" ct_srvname=\"%s\", ", ssn->ssn_srvname);
167 dump_sockaddr(&ssn->ssn_srvaddr.sa);
168 printf(" dom=\"%s\", user=\"%s\"\n",
169 ssn->ssn_domain, ssn->ssn_user);
170 printf(" ct_vopt=0x%x, ct_owner=%d\n",
171 ssn->ssn_vopt, ssn->ssn_owner);
172 printf(" ct_authflags=0x%x\n", is->iod_authflags);
174 printf(" ct_nthash:");
175 if (bcmp(zeros, &is->iod_nthash, NTLM_HASH_SZ))
176 smb_hexdump(&is->iod_nthash, NTLM_HASH_SZ);
177 else
178 printf(" {0}\n");
180 printf(" ct_lmhash:");
181 if (bcmp(zeros, &is->iod_lmhash, NTLM_HASH_SZ))
182 smb_hexdump(&is->iod_lmhash, NTLM_HASH_SZ);
183 else
184 printf(" {0}\n");
187 void
188 dump_ctx(char *where, struct smb_ctx *ctx)
190 printf("context %s:\n", where);
191 dump_ctx_flags(ctx->ct_flags);
193 if (ctx->ct_locname)
194 printf(" localname=\"%s\"", ctx->ct_locname);
195 else
196 printf(" localname=NULL");
198 if (ctx->ct_fullserver)
199 printf(" fullserver=\"%s\"", ctx->ct_fullserver);
200 else
201 printf(" fullserver=NULL");
203 if (ctx->ct_srvaddr_s)
204 printf(" srvaddr_s=\"%s\"\n", ctx->ct_srvaddr_s);
205 else
206 printf(" srvaddr_s=NULL\n");
208 if (ctx->ct_addrinfo)
209 dump_addrinfo(ctx->ct_addrinfo);
210 else
211 printf(" ct_addrinfo = NULL\n");
213 dump_iod_ssn(&ctx->ct_iod_ssn);
215 printf(" share_name=\"%s\", share_type=%d\n",
216 ctx->ct_origshare ? ctx->ct_origshare : "",
217 ctx->ct_shtype_req);
219 printf(" ct_home=\"%s\"\n", ctx->ct_home);
220 printf(" ct_rpath=\"%s\"\n", ctx->ct_rpath);
224 smb_ctx_alloc(struct smb_ctx **ctx_pp)
226 smb_ctx_t *ctx;
227 int err;
229 ctx = malloc(sizeof (*ctx));
230 if (ctx == NULL)
231 return (ENOMEM);
232 err = smb_ctx_init(ctx);
233 if (err != 0) {
234 free(ctx);
235 return (err);
237 *ctx_pp = ctx;
238 return (0);
242 * Initialize an smb_ctx struct (defaults)
245 smb_ctx_init(struct smb_ctx *ctx)
247 int error;
249 bzero(ctx, sizeof (*ctx));
251 error = nb_ctx_create(&ctx->ct_nb);
252 if (error)
253 return (error);
255 ctx->ct_dev_fd = -1;
256 ctx->ct_door_fd = -1;
257 ctx->ct_tran_fd = -1;
258 ctx->ct_parsedlevel = SMBL_NONE;
259 ctx->ct_minlevel = SMBL_NONE;
260 ctx->ct_maxlevel = SMBL_PATH;
262 /* Fill in defaults */
263 ctx->ct_vopt = SMBVOPT_EXT_SEC;
264 ctx->ct_owner = SMBM_ANY_OWNER;
265 ctx->ct_authflags = SMB_AT_DEFAULT;
266 ctx->ct_minauth = SMB_AT_MINAUTH;
269 * Default domain, user, ...
271 strlcpy(ctx->ct_domain, default_domain,
272 sizeof (ctx->ct_domain));
273 strlcpy(ctx->ct_user, default_user,
274 sizeof (ctx->ct_user));
276 return (0);
280 * "Scan" the command line args to find the server name,
281 * user name, and share name, as needed. We need these
282 * before reading the RC files and/or sharectl values.
284 * The sequence for getting all the members filled in
285 * has some tricky aspects. Here's how it works:
287 * The search order for options is as follows:
288 * command line options
289 * values parsed from UNC path (cmd)
290 * values from RC file (per-user)
291 * values from SMF (system-wide)
292 * built-in defaults
294 * Normally, one would simply get all the values starting with
295 * the bottom of the above list and working to the top, and
296 * overwriting values as you go. But we need an exception.
298 * In this function, we parse the UNC path and command line options,
299 * because we need (at least) the server name when we're getting the
300 * SMF and RC file values. However, values we get from the command
301 * should not be overwritten by SMF or RC file parsing, so we mark
302 * values from the command as "from CMD" and the RC file parser
303 * leaves in place any values so marked. See: SMBCF_CMD_*
305 * The semantics of these flags are: "This value came from the
306 * current command instance, not from sources that may apply to
307 * multiple commands." (Different from the old "FROMUSR" flag.)
309 * Note that smb_ctx_opt() is called later to handle the
310 * remaining options, which should be ignored here.
311 * The (magic) leading ":" in cf_getopt() makes it
312 * ignore options not in the options string.
315 smb_ctx_scan_argv(struct smb_ctx *ctx, int argc, char **argv,
316 int minlevel, int maxlevel, int sharetype)
318 int ind, opt, error = 0;
319 int aflg = 0, uflg = 0;
320 const char *arg;
323 * Parse options, if any. Values from here too
324 * are marked as "from CMD".
326 if (argv == NULL)
327 return (0);
329 ctx->ct_minlevel = minlevel;
330 ctx->ct_maxlevel = maxlevel;
331 ctx->ct_shtype_req = sharetype;
333 cf_opt_lock();
334 /* Careful: no return/goto before cf_opt_unlock! */
335 while (error == 0) {
336 opt = cf_getopt(argc, argv, STDPARAM_OPT);
337 if (opt == -1)
338 break;
339 arg = cf_optarg;
340 /* NB: handle most in smb_ctx_opt */
341 switch (opt) {
342 case 'A':
343 aflg = 1;
344 error = smb_ctx_setuser(ctx, "", TRUE);
345 ctx->ct_flags |= SMBCF_NOPWD;
346 break;
347 case 'U':
348 uflg = 1;
349 error = smb_ctx_setuser(ctx, arg, TRUE);
350 break;
351 default:
352 DPRINT("skip opt=%c", opt);
353 break;
356 ind = cf_optind;
357 arg = argv[ind];
358 cf_optind = cf_optreset = 1;
359 cf_opt_unlock();
361 if (error)
362 return (error);
364 if (aflg && uflg) {
365 printf(gettext("-A and -U flags are exclusive.\n"));
366 return (EINVAL);
370 * Parse the UNC path. Values from here are
371 * marked as "from CMD".
373 for (; ind < argc; ind++) {
374 arg = argv[ind];
375 if (strncmp(arg, "//", 2) != 0)
376 continue;
377 error = smb_ctx_parseunc(ctx, arg,
378 minlevel, maxlevel, sharetype, &arg);
379 if (error)
380 return (error);
381 break;
384 return (error);
387 void
388 smb_ctx_free(smb_ctx_t *ctx)
390 smb_ctx_done(ctx);
391 free(ctx);
394 void
395 smb_ctx_done(struct smb_ctx *ctx)
398 rpc_cleanup_smbctx(ctx);
400 if (ctx->ct_dev_fd != -1) {
401 close(ctx->ct_dev_fd);
402 ctx->ct_dev_fd = -1;
404 if (ctx->ct_door_fd != -1) {
405 close(ctx->ct_door_fd);
406 ctx->ct_door_fd = -1;
408 if (ctx->ct_tran_fd != -1) {
409 close(ctx->ct_tran_fd);
410 ctx->ct_tran_fd = -1;
412 if (ctx->ct_srvaddr_s) {
413 free(ctx->ct_srvaddr_s);
414 ctx->ct_srvaddr_s = NULL;
416 if (ctx->ct_nb) {
417 nb_ctx_done(ctx->ct_nb);
418 ctx->ct_nb = NULL;
420 if (ctx->ct_locname) {
421 free(ctx->ct_locname);
422 ctx->ct_locname = NULL;
424 if (ctx->ct_origshare) {
425 free(ctx->ct_origshare);
426 ctx->ct_origshare = NULL;
428 if (ctx->ct_fullserver) {
429 free(ctx->ct_fullserver);
430 ctx->ct_fullserver = NULL;
432 if (ctx->ct_addrinfo) {
433 freeaddrinfo(ctx->ct_addrinfo);
434 ctx->ct_addrinfo = NULL;
436 if (ctx->ct_home) {
437 free(ctx->ct_home);
438 ctx->ct_home = NULL;
440 if (ctx->ct_rpath) {
441 free(ctx->ct_rpath);
442 ctx->ct_rpath = NULL;
444 if (ctx->ct_srv_OS) {
445 free(ctx->ct_srv_OS);
446 ctx->ct_srv_OS = NULL;
448 if (ctx->ct_srv_LM) {
449 free(ctx->ct_srv_LM);
450 ctx->ct_srv_LM = NULL;
452 if (ctx->ct_mackey) {
453 free(ctx->ct_mackey);
454 ctx->ct_mackey = NULL;
459 * Parse the UNC path. Here we expect something like
460 * "//[[domain;]user[:password]@]host[/share[/path]]"
461 * See http://ietf.org/internet-drafts/draft-crhertel-smb-url-07.txt
462 * Values found here are marked as "from CMD".
465 smb_ctx_parseunc(struct smb_ctx *ctx, const char *unc,
466 int minlevel, int maxlevel, int sharetype,
467 const char **next)
469 char tmp[1024];
470 char *host, *share, *path;
471 char *dom, *usr, *pw, *p;
472 int error;
475 * This may be called outside of _scan_argv,
476 * so make sure these get initialized.
478 ctx->ct_minlevel = minlevel;
479 ctx->ct_maxlevel = maxlevel;
480 ctx->ct_shtype_req = sharetype;
481 ctx->ct_parsedlevel = SMBL_NONE;
483 dom = usr = pw = host = NULL;
485 /* Work on a temporary copy, fix back slashes. */
486 strlcpy(tmp, unc, sizeof (tmp));
487 for (p = tmp; *p; p++)
488 if (*p == '\\')
489 *p = '/';
491 if (tmp[0] != '/' || tmp[1] != '/') {
492 smb_error(dgettext(TEXT_DOMAIN,
493 "UNC should start with '//'"), 0);
494 error = EINVAL;
495 goto out;
497 p = tmp + 2; /* user@host... */
499 /* Find the share part, if any. */
500 share = strchr(p, '/');
501 if (share)
502 *share = '\0';
503 (void) unpercent(p); /* host component */
506 * Parse the "host" stuff right to left:
507 * 1: trailing "@hostname" (or whole field)
508 * 2: trailing ":password"
509 * 3: trailing "domain;user" (or just user)
511 host = strrchr(p, '@');
512 if (host == NULL) {
513 host = p; /* no user@ prefix */
514 } else {
515 *host++ = '\0';
517 /* may have [[domain;]user[:passwd]] */
518 pw = strchr(p, ':');
519 if (pw)
520 *pw++ = '\0';
521 usr = strchr(p, ';');
522 if (usr) {
523 *usr++ = '\0';
524 dom = p;
525 } else
526 usr = p;
529 if (*host == '\0') {
530 smb_error(dgettext(TEXT_DOMAIN, "empty server name"), 0);
531 error = EINVAL;
532 goto out;
534 error = smb_ctx_setfullserver(ctx, host);
535 if (error)
536 goto out;
537 ctx->ct_parsedlevel = SMBL_VC;
539 if (dom != NULL) {
540 error = smb_ctx_setdomain(ctx, dom, TRUE);
541 if (error)
542 goto out;
544 if (usr != NULL) {
545 if (*usr == '\0') {
546 smb_error(dgettext(TEXT_DOMAIN,
547 "empty user name"), 0);
548 error = EINVAL;
549 goto out;
551 if (ctx->ct_maxlevel < SMBL_VC) {
552 smb_error(dgettext(TEXT_DOMAIN,
553 "no user name required"), 0);
554 error = EINVAL;
555 goto out;
557 error = smb_ctx_setuser(ctx, usr, TRUE);
558 if (error)
559 goto out;
561 if (pw != NULL) {
562 error = smb_ctx_setpassword(ctx, pw, TRUE);
563 if (error)
564 goto out;
567 if (share != NULL) {
568 /* restore the slash */
569 *share = '/';
570 p = share + 1;
572 /* Find the path part, if any. */
573 path = strchr(p, '/');
574 if (path)
575 *path = '\0';
576 (void) unpercent(p); /* share component */
578 if (*p == '\0') {
579 smb_error(dgettext(TEXT_DOMAIN,
580 "empty share name"), 0);
581 error = EINVAL;
582 goto out;
584 if (ctx->ct_maxlevel < SMBL_SHARE) {
585 smb_error(dgettext(TEXT_DOMAIN,
586 "no share name required"), 0);
587 error = EINVAL;
588 goto out;
592 * Special case UNC names like:
593 * //host/PIPE/endpoint
594 * to have share: IPC$
596 if (strcasecmp(p, "PIPE") == 0) {
597 sharetype = USE_IPC;
598 p = "IPC$";
600 error = smb_ctx_setshare(ctx, p, sharetype);
601 if (error)
602 goto out;
603 ctx->ct_parsedlevel = SMBL_SHARE;
605 if (path) {
606 /* restore the slash */
607 *path = '/';
608 p = path + 1;
609 (void) unpercent(p); /* remainder */
610 free(ctx->ct_rpath);
611 ctx->ct_rpath = strdup(path);
613 } else if (ctx->ct_minlevel >= SMBL_SHARE) {
614 smb_error(dgettext(TEXT_DOMAIN, "empty share name"), 0);
615 error = EINVAL;
616 goto out;
619 if (next)
620 *next = NULL;
622 out:
623 if (error == 0 && smb_debug > 0)
624 dump_ctx("after smb_ctx_parseunc", ctx);
626 return (error);
629 #ifdef KICONV_SUPPORT
631 smb_ctx_setcharset(struct smb_ctx *ctx, const char *arg)
633 char *cp, *servercs, *localcs;
634 int cslen = sizeof (ctx->ct_ssn.ioc_localcs);
635 int scslen, lcslen, error;
637 cp = strchr(arg, ':');
638 lcslen = cp ? (cp - arg) : 0;
639 if (lcslen == 0 || lcslen >= cslen) {
640 smb_error(dgettext(TEXT_DOMAIN,
641 "invalid local charset specification (%s)"), 0, arg);
642 return (EINVAL);
644 scslen = (size_t)strlen(++cp);
645 if (scslen == 0 || scslen >= cslen) {
646 smb_error(dgettext(TEXT_DOMAIN,
647 "invalid server charset specification (%s)"), 0, arg);
648 return (EINVAL);
650 localcs = memcpy(ctx->ct_ssn.ioc_localcs, arg, lcslen);
651 localcs[lcslen] = 0;
652 servercs = strcpy(ctx->ct_ssn.ioc_servercs, cp);
653 error = nls_setrecode(localcs, servercs);
654 if (error == 0)
655 return (0);
656 smb_error(dgettext(TEXT_DOMAIN,
657 "can't initialize iconv support (%s:%s)"),
658 error, localcs, servercs);
659 localcs[0] = 0;
660 servercs[0] = 0;
661 return (error);
663 #endif /* KICONV_SUPPORT */
666 smb_ctx_setauthflags(struct smb_ctx *ctx, int flags)
668 ctx->ct_authflags = flags;
669 return (0);
673 smb_ctx_setfullserver(struct smb_ctx *ctx, const char *name)
675 char *p = strdup(name);
677 if (p == NULL)
678 return (ENOMEM);
679 free(ctx->ct_fullserver);
680 ctx->ct_fullserver = p;
681 return (0);
685 smb_ctx_setserver(struct smb_ctx *ctx, const char *name)
687 strlcpy(ctx->ct_srvname, name,
688 sizeof (ctx->ct_srvname));
689 return (0);
693 smb_ctx_setuser(struct smb_ctx *ctx, const char *name, int from_cmd)
696 if (strlen(name) >= sizeof (ctx->ct_user)) {
697 smb_error(dgettext(TEXT_DOMAIN,
698 "user name '%s' too long"), 0, name);
699 return (ENAMETOOLONG);
703 * Don't overwrite a value from the command line
704 * with one from anywhere else.
706 if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_USR))
707 return (0);
709 strlcpy(ctx->ct_user, name,
710 sizeof (ctx->ct_user));
712 /* Mark this as "from the command line". */
713 if (from_cmd)
714 ctx->ct_flags |= SMBCF_CMD_USR;
716 return (0);
720 * Don't overwrite a domain name from the
721 * command line with one from anywhere else.
722 * See smb_ctx_init() for notes about this.
725 smb_ctx_setdomain(struct smb_ctx *ctx, const char *name, int from_cmd)
728 if (strlen(name) >= sizeof (ctx->ct_domain)) {
729 smb_error(dgettext(TEXT_DOMAIN,
730 "workgroup name '%s' too long"), 0, name);
731 return (ENAMETOOLONG);
735 * Don't overwrite a value from the command line
736 * with one from anywhere else.
738 if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_DOM))
739 return (0);
741 strlcpy(ctx->ct_domain, name,
742 sizeof (ctx->ct_domain));
744 /* Mark this as "from the command line". */
745 if (from_cmd)
746 ctx->ct_flags |= SMBCF_CMD_DOM;
748 return (0);
752 smb_ctx_setpassword(struct smb_ctx *ctx, const char *passwd, int from_cmd)
754 int err;
756 if (passwd == NULL)
757 return (EINVAL);
758 if (strlen(passwd) >= sizeof (ctx->ct_password)) {
759 smb_error(dgettext(TEXT_DOMAIN, "password too long"), 0);
760 return (ENAMETOOLONG);
764 * If called again after comand line parsing,
765 * don't overwrite a value from the command line
766 * with one from any stored config.
768 if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_PW))
769 return (0);
771 memset(ctx->ct_password, 0, sizeof (ctx->ct_password));
772 if (strncmp(passwd, "$$1", 3) == 0)
773 (void) smb_simpledecrypt(ctx->ct_password, passwd);
774 else
775 strlcpy(ctx->ct_password, passwd,
776 sizeof (ctx->ct_password));
779 * Compute LM hash, NT hash.
781 if (ctx->ct_password[0]) {
782 err = ntlm_compute_nt_hash(ctx->ct_nthash, ctx->ct_password);
783 if (err != 0)
784 return (err);
785 err = ntlm_compute_lm_hash(ctx->ct_lmhash, ctx->ct_password);
786 if (err != 0)
787 return (err);
790 /* Mark this as "from the command line". */
791 if (from_cmd)
792 ctx->ct_flags |= SMBCF_CMD_PW;
794 return (0);
798 * Use this to set NTLM auth. info (hashes)
799 * when we don't have the password.
802 smb_ctx_setpwhash(smb_ctx_t *ctx,
803 const uchar_t *nthash, const uchar_t *lmhash)
806 /* Need ct_password to be non-null. */
807 if (ctx->ct_password[0] == '\0')
808 strlcpy(ctx->ct_password, "$HASH",
809 sizeof (ctx->ct_password));
812 * Compute LM hash, NT hash.
814 memcpy(ctx->ct_nthash, nthash, NTLM_HASH_SZ);
816 /* The LM hash is optional */
817 if (lmhash) {
818 memcpy(ctx->ct_nthash, nthash, NTLM_HASH_SZ);
821 return (0);
825 smb_ctx_setshare(struct smb_ctx *ctx, const char *share, int stype)
827 if (strlen(share) >= SMBIOC_MAX_NAME) {
828 smb_error(dgettext(TEXT_DOMAIN,
829 "share name '%s' too long"), 0, share);
830 return (ENAMETOOLONG);
832 free(ctx->ct_origshare);
833 if ((ctx->ct_origshare = strdup(share)) == NULL)
834 return (ENOMEM);
836 ctx->ct_shtype_req = stype;
838 return (0);
842 smb_ctx_setsrvaddr(struct smb_ctx *ctx, const char *addr)
844 if (addr == NULL || addr[0] == 0)
845 return (EINVAL);
846 free(ctx->ct_srvaddr_s);
847 if ((ctx->ct_srvaddr_s = strdup(addr)) == NULL)
848 return (ENOMEM);
849 return (0);
853 * API for library caller to set signing enabled, required
854 * Note: if not enable, ignore require
857 smb_ctx_setsigning(struct smb_ctx *ctx, int enable, int require)
859 ctx->ct_vopt &= ~SMBVOPT_SIGNING_MASK;
860 if (enable) {
861 ctx->ct_vopt |= SMBVOPT_SIGNING_ENABLED;
862 if (require)
863 ctx->ct_vopt |= SMBVOPT_SIGNING_REQUIRED;
865 return (0);
868 static int
869 smb_parse_owner(char *pair, uid_t *uid, gid_t *gid)
871 struct group gr;
872 struct passwd pw;
873 char buf[NSS_BUFLEN_PASSWD];
874 char *cp;
876 cp = strchr(pair, ':');
877 if (cp) {
878 *cp++ = '\0';
879 if (*cp && gid) {
880 if (getgrnam_r(cp, &gr, buf, sizeof (buf)) != NULL) {
881 *gid = gr.gr_gid;
882 } else
883 smb_error(dgettext(TEXT_DOMAIN,
884 "Invalid group name %s, ignored"), 0, cp);
887 if (*pair) {
888 struct passwd *result;
889 getpwnam_r(pair, &pw, buf, sizeof (buf), &result);
890 if (!result)
891 *uid = pw.pw_uid;
892 else
893 smb_error(dgettext(TEXT_DOMAIN,
894 "Invalid user name %s, ignored"), 0, pair);
897 return (0);
901 * Suport a securty options arg, i.e. -S noext,lm,ntlm
902 * for testing various type of authenticators.
904 static struct nv
905 sectype_table[] = {
906 /* noext - handled below */
907 { "anon", SMB_AT_ANON },
908 { "lm", SMB_AT_LM1 },
909 { "ntlm", SMB_AT_NTLM1 },
910 { "ntlm2", SMB_AT_NTLM2 },
911 { "krb5", SMB_AT_KRB5 },
912 { NULL, 0 },
915 smb_parse_secopts(struct smb_ctx *ctx, const char *arg)
917 const char *sep = ":;,";
918 const char *p = arg;
919 struct nv *nv;
920 int nlen, tlen;
921 int authflags = 0;
923 for (;;) {
924 /* skip separators */
925 tlen = strspn(p, sep);
926 p += tlen;
928 nlen = strcspn(p, sep);
929 if (nlen == 0)
930 break;
932 if (nlen == 5 && 0 == strncmp(p, "noext", nlen)) {
933 /* Don't offer extended security. */
934 ctx->ct_vopt &= ~SMBVOPT_EXT_SEC;
935 p += nlen;
936 continue;
939 /* This is rarely called, so not optimized. */
940 for (nv = sectype_table; nv->name; nv++) {
941 tlen = strlen(nv->name);
942 if (tlen == nlen && 0 == strncmp(p, nv->name, tlen))
943 break;
945 if (nv->name == NULL) {
946 smb_error(dgettext(TEXT_DOMAIN,
947 "%s: invalid security options"), 0, p);
948 return (EINVAL);
950 authflags |= nv->value;
951 p += nlen;
954 if (authflags)
955 ctx->ct_authflags = authflags;
957 return (0);
961 * Commands use this with getopt. See:
962 * STDPARAM_OPT, STDPARAM_ARGS
963 * Called after smb_ctx_readrc().
966 smb_ctx_opt(struct smb_ctx *ctx, int opt, const char *arg)
968 int error = 0;
969 char *p, *cp;
970 char tmp[1024];
972 switch (opt) {
973 case 'A':
974 case 'U':
975 /* Handled in smb_ctx_init() */
976 break;
977 case 'I':
978 error = smb_ctx_setsrvaddr(ctx, arg);
979 break;
980 case 'M':
981 /* share connect rights - ignored */
982 ctx->ct_flags |= SMBCF_SRIGHTS;
983 break;
984 case 'N':
985 ctx->ct_flags |= SMBCF_NOPWD;
986 break;
987 case 'O':
988 p = strdup(arg);
989 cp = strchr(p, '/');
990 if (cp)
991 *cp = '\0';
992 error = smb_parse_owner(cp, &ctx->ct_owner, NULL);
993 free(p);
994 break;
995 case 'P':
996 /* ctx->ct_vopt |= SMBCOPT_PERMANENT; */
997 break;
998 case 'R':
999 /* retry count - ignored */
1000 break;
1001 case 'S':
1002 /* Security options (undocumented, just for tests) */
1003 error = smb_parse_secopts(ctx, arg);
1004 break;
1005 case 'T':
1006 /* timeout - ignored */
1007 break;
1008 case 'D': /* domain */
1009 case 'W': /* workgroup (legacy alias) */
1010 error = smb_ctx_setdomain(ctx, tmp, TRUE);
1011 break;
1013 return (error);
1018 * Original code injected iconv tables into the kernel.
1019 * Not sure if we'll need this or not... REVISIT
1021 #ifdef KICONV_SUPPORT
1022 static int
1023 smb_addiconvtbl(const char *to, const char *from, const uchar_t *tbl)
1025 int error = 0;
1027 error = kiconv_add_xlat_table(to, from, tbl);
1028 if (error && error != EEXIST) {
1029 smb_error(dgettext(TEXT_DOMAIN,
1030 "can not setup kernel iconv table (%s:%s)"),
1031 error, from, to);
1032 return (error);
1034 return (error);
1036 #endif /* KICONV_SUPPORT */
1039 * Verify context info. before connect operation(s),
1040 * lookup specified server and try to fill all forgotten fields.
1041 * Legacy name used by commands.
1044 smb_ctx_resolve(struct smb_ctx *ctx)
1046 struct smbioc_ossn *ssn = &ctx->ct_ssn;
1047 int error = 0;
1048 #ifdef KICONV_SUPPORT
1049 uchar_t cstbl[256];
1050 uint_t i;
1051 #endif
1053 if (smb_debug)
1054 dump_ctx("before smb_ctx_resolve", ctx);
1056 ctx->ct_flags &= ~SMBCF_RESOLVED;
1058 if (ctx->ct_fullserver == NULL) {
1059 smb_error(dgettext(TEXT_DOMAIN,
1060 "no server name specified"), 0);
1061 return (EINVAL);
1064 if (ctx->ct_minlevel >= SMBL_SHARE &&
1065 ctx->ct_origshare == NULL) {
1066 smb_error(dgettext(TEXT_DOMAIN,
1067 "no share name specified for %s@%s"),
1068 0, ssn->ssn_user, ctx->ct_fullserver);
1069 return (EINVAL);
1071 error = nb_ctx_resolve(ctx->ct_nb);
1072 if (error)
1073 return (error);
1074 #ifdef KICONV_SUPPORT
1075 if (ssn->ioc_localcs[0] == 0)
1076 strcpy(ssn->ioc_localcs, "default"); /* XXX: locale name ? */
1077 error = smb_addiconvtbl("tolower", ssn->ioc_localcs, nls_lower);
1078 if (error)
1079 return (error);
1080 error = smb_addiconvtbl("toupper", ssn->ioc_localcs, nls_upper);
1081 if (error)
1082 return (error);
1083 if (ssn->ioc_servercs[0] != 0) {
1084 for (i = 0; i < sizeof (cstbl); i++)
1085 cstbl[i] = i;
1086 nls_mem_toext(cstbl, cstbl, sizeof (cstbl));
1087 error = smb_addiconvtbl(ssn->ioc_servercs, ssn->ioc_localcs,
1088 cstbl);
1089 if (error)
1090 return (error);
1091 for (i = 0; i < sizeof (cstbl); i++)
1092 cstbl[i] = i;
1093 nls_mem_toloc(cstbl, cstbl, sizeof (cstbl));
1094 error = smb_addiconvtbl(ssn->ioc_localcs, ssn->ioc_servercs,
1095 cstbl);
1096 if (error)
1097 return (error);
1099 #endif /* KICONV_SUPPORT */
1102 * Lookup the IP address and fill in ct_addrinfo.
1104 * Note: smb_ctx_getaddr() returns a EAI_xxx
1105 * error value like getaddrinfo(3), but this
1106 * function needs to return an errno value.
1108 error = smb_ctx_getaddr(ctx);
1109 if (error) {
1110 const char *ais = gai_strerror(error);
1111 smb_error(dgettext(TEXT_DOMAIN,
1112 "can't resolve name\"%s\", %s"),
1113 0, ctx->ct_fullserver, ais);
1114 return (ENODATA);
1116 assert(ctx->ct_addrinfo != NULL);
1119 * If we have a user name but no password,
1120 * check for a keychain entry.
1121 * XXX: Only for auth NTLM?
1123 if (ctx->ct_user[0] != '\0') {
1125 * Have a user name.
1126 * If we don't have a p/w yet,
1127 * try the keychain.
1129 if (ctx->ct_password[0] == '\0')
1130 (void) smb_get_keychain(ctx);
1132 * Mask out disallowed auth types.
1134 ctx->ct_authflags &= ctx->ct_minauth;
1136 if (ctx->ct_authflags == 0) {
1137 smb_error(dgettext(TEXT_DOMAIN,
1138 "no valid auth. types"), 0);
1139 return (ENOTSUP);
1142 ctx->ct_flags |= SMBCF_RESOLVED;
1143 if (smb_debug)
1144 dump_ctx("after smb_ctx_resolve", ctx);
1146 return (0);
1150 smb_open_driver()
1152 int fd;
1154 fd = open("/dev/"NSMB_NAME, O_RDWR);
1155 if (fd < 0) {
1156 return (-1);
1159 /* This handle controls per-process resources. */
1160 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
1162 return (fd);
1166 smb_ctx_gethandle(struct smb_ctx *ctx)
1168 int fd, err;
1169 uint32_t version;
1171 if (ctx->ct_dev_fd != -1) {
1172 rpc_cleanup_smbctx(ctx);
1173 close(ctx->ct_dev_fd);
1174 ctx->ct_dev_fd = -1;
1175 ctx->ct_flags &= ~SMBCF_SSNACTIVE;
1178 fd = smb_open_driver();
1179 if (fd < 0) {
1180 err = errno;
1181 smb_error(dgettext(TEXT_DOMAIN,
1182 "failed to open driver"), err);
1183 return (err);
1187 * Check the driver version (paranoia)
1189 if (ioctl(fd, SMBIOC_GETVERS, &version) < 0)
1190 version = 0;
1191 if (version != NSMB_VERSION) {
1192 smb_error(dgettext(TEXT_DOMAIN,
1193 "incorrect driver version"), 0);
1194 close(fd);
1195 return (ENODEV);
1198 ctx->ct_dev_fd = fd;
1199 return (0);
1204 * Find or create a connection + logon session
1207 smb_ctx_get_ssn(struct smb_ctx *ctx)
1209 int err = 0;
1211 if ((ctx->ct_flags & SMBCF_RESOLVED) == 0)
1212 return (EINVAL);
1215 * Check whether the driver already has a VC
1216 * we can use. If so, we're done!
1218 err = smb_ctx_findvc(ctx);
1219 if (err == 0) {
1220 DPRINT("found an existing VC");
1221 } else {
1223 * This calls the IOD to create a new session.
1225 DPRINT("setup a new VC");
1226 err = smb_ctx_newvc(ctx);
1227 if (err != 0)
1228 return (err);
1231 * Call findvc again. The new VC sould be
1232 * found in the driver this time.
1234 err = smb_ctx_findvc(ctx);
1237 return (err);
1241 * Find or create a tree connection
1244 smb_ctx_get_tree(struct smb_ctx *ctx)
1246 smbioc_tcon_t *tcon = NULL;
1247 int cmd, err = 0;
1249 if (ctx->ct_dev_fd < 0 ||
1250 ctx->ct_origshare == NULL) {
1251 return (EINVAL);
1254 cmd = SMBIOC_TREE_CONNECT;
1255 tcon = malloc(sizeof (*tcon));
1256 if (tcon == NULL)
1257 return (ENOMEM);
1258 bzero(tcon, sizeof (*tcon));
1259 tcon->tc_flags = SMBLK_CREATE;
1260 tcon->tc_opt = 0;
1262 /* The share name */
1263 strlcpy(tcon->tc_sh.sh_name, ctx->ct_origshare,
1264 sizeof (tcon->tc_sh.sh_name));
1266 /* The share "use" type. */
1267 tcon->tc_sh.sh_use = ctx->ct_shtype_req;
1270 * Todo: share passwords for share-level security.
1272 * The driver does the actual TCON call.
1274 if (ioctl(ctx->ct_dev_fd, cmd, tcon) == -1) {
1275 err = errno;
1276 goto out;
1280 * Check the returned share type
1282 DPRINT("ret. sh_type: \"%d\"", tcon->tc_sh.sh_type);
1283 if (ctx->ct_shtype_req != USE_WILDCARD &&
1284 ctx->ct_shtype_req != tcon->tc_sh.sh_type) {
1285 smb_error(dgettext(TEXT_DOMAIN,
1286 "%s: incompatible share type"),
1287 0, ctx->ct_origshare);
1290 out:
1291 free(tcon);
1293 return (err);
1297 * Return the hflags2 word for an smb_ctx.
1300 smb_ctx_flags2(struct smb_ctx *ctx)
1302 uint16_t flags2;
1304 if (ioctl(ctx->ct_dev_fd, SMBIOC_FLAGS2, &flags2) == -1) {
1305 smb_error(dgettext(TEXT_DOMAIN,
1306 "can't get flags2 for a session"), errno);
1307 return (-1);
1309 return (flags2);
1313 * Get the transport level session key.
1314 * Must already have an active SMB session.
1317 smb_fh_getssnkey(int dev_fd, uchar_t *key, size_t len)
1319 if (len < SMBIOC_HASH_SZ)
1320 return (EINVAL);
1322 if (ioctl(dev_fd, SMBIOC_GETSSNKEY, key) == -1)
1323 return (errno);
1325 return (0);
1329 * RC file parsing stuff
1332 static struct nv
1333 minauth_table[] = {
1334 /* Allowed auth. types */
1335 { "kerberos", SMB_AT_KRB5 },
1336 { "ntlmv2", SMB_AT_KRB5|SMB_AT_NTLM2 },
1337 { "ntlm", SMB_AT_KRB5|SMB_AT_NTLM2|SMB_AT_NTLM1 },
1338 { "lm", SMB_AT_KRB5|SMB_AT_NTLM2|SMB_AT_NTLM1|SMB_AT_LM1 },
1339 { "none", SMB_AT_KRB5|SMB_AT_NTLM2|SMB_AT_NTLM1|SMB_AT_LM1|
1340 SMB_AT_ANON },
1341 { NULL }
1346 * level values:
1347 * 0 - default
1348 * 1 - server
1349 * 2 - server:user
1350 * 3 - server:user:share
1352 static int
1353 smb_ctx_readrcsection(struct smb_ctx *ctx, const char *sname, int level)
1355 char *p;
1356 int error;
1358 #ifdef KICONV_SUPPORT
1359 if (level > 0) {
1360 rc_getstringptr(smb_rc, sname, "charsets", &p);
1361 if (p) {
1362 error = smb_ctx_setcharset(ctx, p);
1363 if (error)
1364 smb_error(dgettext(TEXT_DOMAIN,
1365 "charset specification in the section '%s' ignored"),
1366 error, sname);
1369 #endif
1371 if (level <= 1) {
1372 /* Section is: [default] or [server] */
1374 rc_getstringptr(smb_rc, sname, "minauth", &p);
1375 if (p) {
1377 * "minauth" was set in this section; override
1378 * the current minimum authentication setting.
1380 struct nv *nvp;
1381 for (nvp = minauth_table; nvp->name; nvp++)
1382 if (strcmp(p, nvp->name) == 0)
1383 break;
1384 if (nvp->name)
1385 ctx->ct_minauth = nvp->value;
1386 else {
1388 * Unknown minimum authentication level.
1390 smb_error(dgettext(TEXT_DOMAIN,
1391 "invalid minimum authentication level \"%s\" specified in the section %s"),
1392 0, p, sname);
1393 return (EINVAL);
1397 rc_getstringptr(smb_rc, sname, "signing", &p);
1398 if (p) {
1400 * "signing" was set in this section; override
1401 * the current signing settings. Note:
1402 * setsigning flags are: enable, require
1404 if (strcmp(p, "disabled") == 0) {
1405 (void) smb_ctx_setsigning(ctx, FALSE, FALSE);
1406 } else if (strcmp(p, "enabled") == 0) {
1407 (void) smb_ctx_setsigning(ctx, TRUE, FALSE);
1408 } else if (strcmp(p, "required") == 0) {
1409 (void) smb_ctx_setsigning(ctx, TRUE, TRUE);
1410 } else {
1412 * Unknown "signing" value.
1414 smb_error(dgettext(TEXT_DOMAIN,
1415 "invalid signing policy \"%s\" specified in the section %s"),
1416 0, p, sname);
1417 return (EINVAL);
1422 * Domain name. Allow both keywords:
1423 * "workgroup", "domain"
1425 * Note: these are NOT marked "from CMD".
1426 * See long comment at smb_ctx_init()
1428 rc_getstringptr(smb_rc, sname, "workgroup", &p);
1429 if (p) {
1430 error = smb_ctx_setdomain(ctx, p, 0);
1431 if (error)
1432 smb_error(dgettext(TEXT_DOMAIN,
1433 "workgroup specification in the "
1434 "section '%s' ignored"), error, sname);
1436 rc_getstringptr(smb_rc, sname, "domain", &p);
1437 if (p) {
1438 error = smb_ctx_setdomain(ctx, p, 0);
1439 if (error)
1440 smb_error(dgettext(TEXT_DOMAIN,
1441 "domain specification in the "
1442 "section '%s' ignored"), error, sname);
1445 rc_getstringptr(smb_rc, sname, "user", &p);
1446 if (p) {
1447 error = smb_ctx_setuser(ctx, p, 0);
1448 if (error)
1449 smb_error(dgettext(TEXT_DOMAIN,
1450 "user specification in the "
1451 "section '%s' ignored"), error, sname);
1455 if (level == 1) {
1456 /* Section is: [server] */
1457 rc_getstringptr(smb_rc, sname, "addr", &p);
1458 if (p) {
1459 error = smb_ctx_setsrvaddr(ctx, p);
1460 if (error) {
1461 smb_error(dgettext(TEXT_DOMAIN,
1462 "invalid address specified in section %s"),
1463 0, sname);
1464 return (error);
1469 rc_getstringptr(smb_rc, sname, "password", &p);
1470 if (p) {
1471 error = smb_ctx_setpassword(ctx, p, 0);
1472 if (error)
1473 smb_error(dgettext(TEXT_DOMAIN,
1474 "password specification in the section '%s' ignored"),
1475 error, sname);
1478 return (0);
1482 * read rc file as follows:
1483 * 0: read [default] section
1484 * 1: override with [server] section
1485 * 2: override with [server:user] section
1486 * 3: override with [server:user:share] section
1487 * Since absence of rcfile is not fatal, silently ignore this fact.
1488 * smb_rc file should be closed by caller.
1491 smb_ctx_readrc(struct smb_ctx *ctx)
1493 char pwbuf[NSS_BUFLEN_PASSWD];
1494 struct passwd pw;
1495 char *sname = NULL;
1496 int sname_max;
1497 int err = 0;
1498 struct passwd *result;
1501 * If the user name is not specified some other way,
1502 * use the current user name. Also save the homedir.
1503 * NB: ct_home=NULL is allowed, and we don't want to
1504 * bail out with an error for a missing ct_home.
1506 getpwuid_r(getuid(), &pw, pwbuf, sizeof (pwbuf), &result);
1507 if (result) {
1508 if (ctx->ct_user[0] == 0)
1509 (void) smb_ctx_setuser(ctx, pw.pw_name, B_FALSE);
1510 if (ctx->ct_home == NULL)
1511 ctx->ct_home = strdup(pw.pw_dir);
1514 if ((err = smb_open_rcfile(ctx->ct_home)) != 0) {
1515 DPRINT("smb_open_rcfile, err=%d", err);
1516 /* ignore any error here */
1517 return (0);
1520 sname_max = 3 * SMBIOC_MAX_NAME + 4;
1521 sname = malloc(sname_max);
1522 if (sname == NULL) {
1523 err = ENOMEM;
1524 goto done;
1528 * default parameters (level=0)
1530 smb_ctx_readrcsection(ctx, "default", 0);
1531 nb_ctx_readrcsection(smb_rc, ctx->ct_nb, "default", 0);
1534 * If we don't have a server name, we can't read any of the
1535 * [server...] sections.
1537 if (ctx->ct_fullserver == NULL)
1538 goto done;
1540 * SERVER parameters.
1542 smb_ctx_readrcsection(ctx, ctx->ct_fullserver, 1);
1545 * If we don't have a user name, we can't read any of the
1546 * [server:user...] sections.
1548 if (ctx->ct_user[0] == 0)
1549 goto done;
1551 * SERVER:USER parameters
1553 snprintf(sname, sname_max, "%s:%s",
1554 ctx->ct_fullserver,
1555 ctx->ct_user);
1556 smb_ctx_readrcsection(ctx, sname, 2);
1560 * If we don't have a share name, we can't read any of the
1561 * [server:user:share] sections.
1563 if (ctx->ct_origshare == NULL)
1564 goto done;
1566 * SERVER:USER:SHARE parameters
1568 snprintf(sname, sname_max, "%s:%s:%s",
1569 ctx->ct_fullserver,
1570 ctx->ct_user,
1571 ctx->ct_origshare);
1572 smb_ctx_readrcsection(ctx, sname, 3);
1574 done:
1575 free(sname);
1576 smb_close_rcfile();
1577 if (smb_debug)
1578 dump_ctx("after smb_ctx_readrc", ctx);
1579 if (err)
1580 DPRINT("err=%d\n", err);
1582 return (err);
1585 void
1586 smbfs_set_default_domain(const char *domain)
1588 strlcpy(default_domain, domain, sizeof (default_domain));
1591 void
1592 smbfs_set_default_user(const char *user)
1594 strlcpy(default_user, user, sizeof (default_user));