14bff34e3Sthurlow /* 24bff34e3Sthurlow * Copyright (c) 2000, Boris Popov 34bff34e3Sthurlow * All rights reserved. 44bff34e3Sthurlow * 54bff34e3Sthurlow * Redistribution and use in source and binary forms, with or without 64bff34e3Sthurlow * modification, are permitted provided that the following conditions 74bff34e3Sthurlow * are met: 84bff34e3Sthurlow * 1. Redistributions of source code must retain the above copyright 94bff34e3Sthurlow * notice, this list of conditions and the following disclaimer. 104bff34e3Sthurlow * 2. Redistributions in binary form must reproduce the above copyright 114bff34e3Sthurlow * notice, this list of conditions and the following disclaimer in the 124bff34e3Sthurlow * documentation and/or other materials provided with the distribution. 134bff34e3Sthurlow * 3. All advertising materials mentioning features or use of this software 144bff34e3Sthurlow * must display the following acknowledgement: 154bff34e3Sthurlow * This product includes software developed by Boris Popov. 164bff34e3Sthurlow * 4. Neither the name of the author nor the names of any co-contributors 174bff34e3Sthurlow * may be used to endorse or promote products derived from this software 184bff34e3Sthurlow * without specific prior written permission. 194bff34e3Sthurlow * 204bff34e3Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 214bff34e3Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224bff34e3Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234bff34e3Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 244bff34e3Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254bff34e3Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264bff34e3Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274bff34e3Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284bff34e3Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294bff34e3Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304bff34e3Sthurlow * SUCH DAMAGE. 314bff34e3Sthurlow * 324bff34e3Sthurlow * $Id: ctx.c,v 1.32.70.2 2005/06/02 00:55:40 lindak Exp $ 334bff34e3Sthurlow */ 344bff34e3Sthurlow 359c9af259SGordon Ross /* 3615359501SGordon Ross * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 37fd75ca8dSGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 389c9af259SGordon Ross */ 394bff34e3Sthurlow 404bff34e3Sthurlow #include <sys/param.h> 414bff34e3Sthurlow #include <sys/ioctl.h> 424bff34e3Sthurlow #include <sys/time.h> 434bff34e3Sthurlow #include <sys/mount.h> 444bff34e3Sthurlow #include <sys/types.h> 454bff34e3Sthurlow #include <sys/byteorder.h> 464bff34e3Sthurlow 474bff34e3Sthurlow #include <fcntl.h> 484bff34e3Sthurlow #include <ctype.h> 494bff34e3Sthurlow #include <errno.h> 504bff34e3Sthurlow #include <stdio.h> 514bff34e3Sthurlow #include <string.h> 524bff34e3Sthurlow #include <strings.h> 534bff34e3Sthurlow #include <stdlib.h> 544bff34e3Sthurlow #include <pwd.h> 554bff34e3Sthurlow #include <grp.h> 564bff34e3Sthurlow #include <unistd.h> 574bff34e3Sthurlow #include <libintl.h> 584bff34e3Sthurlow #include <assert.h> 594bff34e3Sthurlow #include <nss_dbdefs.h> 604bff34e3Sthurlow 61613a2f6bSGordon Ross #include <cflib.h> 624bff34e3Sthurlow #include <netsmb/smb_lib.h> 634bff34e3Sthurlow #include <netsmb/netbios.h> 644bff34e3Sthurlow #include <netsmb/nb_lib.h> 654bff34e3Sthurlow #include <netsmb/smb_dev.h> 664bff34e3Sthurlow 67613a2f6bSGordon Ross #include "charsets.h" 689c9af259SGordon Ross #include "private.h" 69613a2f6bSGordon Ross #include "ntlm.h" 704bff34e3Sthurlow 71613a2f6bSGordon Ross #ifndef FALSE 72613a2f6bSGordon Ross #define FALSE 0 73613a2f6bSGordon Ross #endif 74613a2f6bSGordon Ross #ifndef TRUE 75613a2f6bSGordon Ross #define TRUE 1 76613a2f6bSGordon Ross #endif 774bff34e3Sthurlow 78fd75ca8dSGordon Ross #define SMB_AT_DEFAULT (SMB_AT_KRB5 | SMB_AT_NTLM2) 79fd75ca8dSGordon Ross #define SMB_AT_MINAUTH (SMB_AT_KRB5 | SMB_AT_NTLM2 | SMB_AT_NTLM1) 80fd75ca8dSGordon Ross 8115359501SGordon Ross struct nv { 8215359501SGordon Ross char *name; 8315359501SGordon Ross int value; 8415359501SGordon Ross }; 854bff34e3Sthurlow 864bff34e3Sthurlow /* These two may be set by commands. */ 874bff34e3Sthurlow int smb_debug, smb_verbose; 884bff34e3Sthurlow 894bff34e3Sthurlow /* 90613a2f6bSGordon Ross * Was: STDPARAM_OPT - see smb_ctx_scan_argv, smb_ctx_opt 91613a2f6bSGordon Ross */ 92613a2f6bSGordon Ross const char smbutil_std_opts[] = "ABCD:E:I:L:M:NO:P:U:R:S:T:W:"; 93613a2f6bSGordon Ross 94613a2f6bSGordon Ross /* 95430b4c46SGordon Ross * Defaults for new contexts (connections to servers). 96430b4c46SGordon Ross * These are set by smbfs_set_default_... 97430b4c46SGordon Ross */ 98430b4c46SGordon Ross static char default_domain[SMBIOC_MAX_NAME]; 99430b4c46SGordon Ross static char default_user[SMBIOC_MAX_NAME]; 100430b4c46SGordon Ross 101430b4c46SGordon Ross 102430b4c46SGordon Ross /* 1039c9af259SGordon Ross * Give the RPC library a callback hook that will be 1049c9af259SGordon Ross * called whenever we destroy or reinit an smb_ctx_t. 1059c9af259SGordon Ross * The name rpc_cleanup_smbctx() is legacy, and was 1069c9af259SGordon Ross * originally a direct call into the RPC code. 1074bff34e3Sthurlow */ 1089c9af259SGordon Ross static smb_ctx_close_hook_t close_hook; 1094bff34e3Sthurlow static void 1104bff34e3Sthurlow rpc_cleanup_smbctx(struct smb_ctx *ctx) 1114bff34e3Sthurlow { 1129c9af259SGordon Ross if (close_hook) 1139c9af259SGordon Ross (*close_hook)(ctx); 1144bff34e3Sthurlow } 1159c9af259SGordon Ross void 1169c9af259SGordon Ross smb_ctx_set_close_hook(smb_ctx_close_hook_t hook) 1179c9af259SGordon Ross { 1189c9af259SGordon Ross close_hook = hook; 1199c9af259SGordon Ross } 1204bff34e3Sthurlow 1214bff34e3Sthurlow void 1224bff34e3Sthurlow dump_ctx_flags(int flags) 1234bff34e3Sthurlow { 1244bff34e3Sthurlow printf(" Flags: "); 1254bff34e3Sthurlow if (flags == 0) 1264bff34e3Sthurlow printf("0"); 1274bff34e3Sthurlow if (flags & SMBCF_NOPWD) 1284bff34e3Sthurlow printf("NOPWD "); 1294bff34e3Sthurlow if (flags & SMBCF_SRIGHTS) 1304bff34e3Sthurlow printf("SRIGHTS "); 1314bff34e3Sthurlow if (flags & SMBCF_LOCALE) 1324bff34e3Sthurlow printf("LOCALE "); 1334bff34e3Sthurlow if (flags & SMBCF_CMD_DOM) 1344bff34e3Sthurlow printf("CMD_DOM "); 1354bff34e3Sthurlow if (flags & SMBCF_CMD_USR) 1364bff34e3Sthurlow printf("CMD_USR "); 1374bff34e3Sthurlow if (flags & SMBCF_CMD_PW) 1384bff34e3Sthurlow printf("CMD_PW "); 1394bff34e3Sthurlow if (flags & SMBCF_RESOLVED) 1404bff34e3Sthurlow printf("RESOLVED "); 1414bff34e3Sthurlow if (flags & SMBCF_KCBAD) 1424bff34e3Sthurlow printf("KCBAD "); 1434bff34e3Sthurlow if (flags & SMBCF_KCFOUND) 1444bff34e3Sthurlow printf("KCFOUND "); 1454bff34e3Sthurlow if (flags & SMBCF_BROWSEOK) 1464bff34e3Sthurlow printf("BROWSEOK "); 1474bff34e3Sthurlow if (flags & SMBCF_AUTHREQ) 1484bff34e3Sthurlow printf("AUTHREQ "); 1494bff34e3Sthurlow if (flags & SMBCF_KCSAVE) 1504bff34e3Sthurlow printf("KCSAVE "); 1514bff34e3Sthurlow if (flags & SMBCF_XXX) 1524bff34e3Sthurlow printf("XXX "); 1534bff34e3Sthurlow if (flags & SMBCF_SSNACTIVE) 1544bff34e3Sthurlow printf("SSNACTIVE "); 1554bff34e3Sthurlow if (flags & SMBCF_KCDOMAIN) 1564bff34e3Sthurlow printf("KCDOMAIN "); 1574bff34e3Sthurlow printf("\n"); 1584bff34e3Sthurlow } 1594bff34e3Sthurlow 1604bff34e3Sthurlow void 161613a2f6bSGordon Ross dump_iod_ssn(smb_iod_ssn_t *is) 1624bff34e3Sthurlow { 163613a2f6bSGordon Ross static const char zeros[NTLM_HASH_SZ] = {0}; 164613a2f6bSGordon Ross struct smbioc_ossn *ssn = &is->iod_ossn; 1654bff34e3Sthurlow 166613a2f6bSGordon Ross printf(" ct_srvname=\"%s\", ", ssn->ssn_srvname); 167613a2f6bSGordon Ross dump_sockaddr(&ssn->ssn_srvaddr.sa); 168613a2f6bSGordon Ross printf(" dom=\"%s\", user=\"%s\"\n", 169613a2f6bSGordon Ross ssn->ssn_domain, ssn->ssn_user); 170613a2f6bSGordon Ross printf(" ct_vopt=0x%x, ct_owner=%d\n", 171613a2f6bSGordon Ross ssn->ssn_vopt, ssn->ssn_owner); 172613a2f6bSGordon Ross printf(" ct_authflags=0x%x\n", is->iod_authflags); 173613a2f6bSGordon Ross 174613a2f6bSGordon Ross printf(" ct_nthash:"); 175613a2f6bSGordon Ross if (bcmp(zeros, &is->iod_nthash, NTLM_HASH_SZ)) 176613a2f6bSGordon Ross smb_hexdump(&is->iod_nthash, NTLM_HASH_SZ); 177613a2f6bSGordon Ross else 178613a2f6bSGordon Ross printf(" {0}\n"); 179613a2f6bSGordon Ross 180613a2f6bSGordon Ross printf(" ct_lmhash:"); 181613a2f6bSGordon Ross if (bcmp(zeros, &is->iod_lmhash, NTLM_HASH_SZ)) 182613a2f6bSGordon Ross smb_hexdump(&is->iod_lmhash, NTLM_HASH_SZ); 183613a2f6bSGordon Ross else 184613a2f6bSGordon Ross printf(" {0}\n"); 1854bff34e3Sthurlow } 1864bff34e3Sthurlow 1874bff34e3Sthurlow void 1884bff34e3Sthurlow dump_ctx(char *where, struct smb_ctx *ctx) 1894bff34e3Sthurlow { 1904bff34e3Sthurlow printf("context %s:\n", where); 1914bff34e3Sthurlow dump_ctx_flags(ctx->ct_flags); 1924bff34e3Sthurlow 193613a2f6bSGordon Ross if (ctx->ct_locname) 1944bff34e3Sthurlow printf(" localname=\"%s\"", ctx->ct_locname); 195613a2f6bSGordon Ross else 196613a2f6bSGordon Ross printf(" localname=NULL"); 1974bff34e3Sthurlow 1984bff34e3Sthurlow if (ctx->ct_fullserver) 1994bff34e3Sthurlow printf(" fullserver=\"%s\"", ctx->ct_fullserver); 2004bff34e3Sthurlow else 2014bff34e3Sthurlow printf(" fullserver=NULL"); 2024bff34e3Sthurlow 203613a2f6bSGordon Ross if (ctx->ct_srvaddr_s) 204613a2f6bSGordon Ross printf(" srvaddr_s=\"%s\"\n", ctx->ct_srvaddr_s); 2054bff34e3Sthurlow else 206613a2f6bSGordon Ross printf(" srvaddr_s=NULL\n"); 2074bff34e3Sthurlow 208613a2f6bSGordon Ross if (ctx->ct_addrinfo) 209613a2f6bSGordon Ross dump_addrinfo(ctx->ct_addrinfo); 210613a2f6bSGordon Ross else 211613a2f6bSGordon Ross printf(" ct_addrinfo = NULL\n"); 212613a2f6bSGordon Ross 213613a2f6bSGordon Ross dump_iod_ssn(&ctx->ct_iod_ssn); 214613a2f6bSGordon Ross 215613a2f6bSGordon Ross printf(" share_name=\"%s\", share_type=%d\n", 216613a2f6bSGordon Ross ctx->ct_origshare ? ctx->ct_origshare : "", 217613a2f6bSGordon Ross ctx->ct_shtype_req); 218613a2f6bSGordon Ross 219430b4c46SGordon Ross printf(" ct_home=\"%s\"\n", ctx->ct_home); 220430b4c46SGordon Ross printf(" ct_rpath=\"%s\"\n", ctx->ct_rpath); 221613a2f6bSGordon Ross } 222613a2f6bSGordon Ross 223613a2f6bSGordon Ross int 224613a2f6bSGordon Ross smb_ctx_alloc(struct smb_ctx **ctx_pp) 225613a2f6bSGordon Ross { 226613a2f6bSGordon Ross smb_ctx_t *ctx; 227613a2f6bSGordon Ross int err; 228613a2f6bSGordon Ross 229613a2f6bSGordon Ross ctx = malloc(sizeof (*ctx)); 230613a2f6bSGordon Ross if (ctx == NULL) 231613a2f6bSGordon Ross return (ENOMEM); 232613a2f6bSGordon Ross err = smb_ctx_init(ctx); 233613a2f6bSGordon Ross if (err != 0) { 234613a2f6bSGordon Ross free(ctx); 235613a2f6bSGordon Ross return (err); 236613a2f6bSGordon Ross } 237613a2f6bSGordon Ross *ctx_pp = ctx; 238613a2f6bSGordon Ross return (0); 2394bff34e3Sthurlow } 2404bff34e3Sthurlow 2414bff34e3Sthurlow /* 242613a2f6bSGordon Ross * Initialize an smb_ctx struct (defaults) 243613a2f6bSGordon Ross */ 244613a2f6bSGordon Ross int 245613a2f6bSGordon Ross smb_ctx_init(struct smb_ctx *ctx) 246613a2f6bSGordon Ross { 247430b4c46SGordon Ross int error; 248613a2f6bSGordon Ross 249613a2f6bSGordon Ross bzero(ctx, sizeof (*ctx)); 250613a2f6bSGordon Ross 251613a2f6bSGordon Ross error = nb_ctx_create(&ctx->ct_nb); 252613a2f6bSGordon Ross if (error) 253613a2f6bSGordon Ross return (error); 254613a2f6bSGordon Ross 255613a2f6bSGordon Ross ctx->ct_dev_fd = -1; 2566b2bcd8eSGordon Ross ctx->ct_door_fd = -1; 257613a2f6bSGordon Ross ctx->ct_tran_fd = -1; 258613a2f6bSGordon Ross ctx->ct_parsedlevel = SMBL_NONE; 259613a2f6bSGordon Ross ctx->ct_minlevel = SMBL_NONE; 260613a2f6bSGordon Ross ctx->ct_maxlevel = SMBL_PATH; 261613a2f6bSGordon Ross 262613a2f6bSGordon Ross /* Fill in defaults */ 263613a2f6bSGordon Ross ctx->ct_vopt = SMBVOPT_EXT_SEC; 264613a2f6bSGordon Ross ctx->ct_owner = SMBM_ANY_OWNER; 265613a2f6bSGordon Ross ctx->ct_authflags = SMB_AT_DEFAULT; 266fd75ca8dSGordon Ross ctx->ct_minauth = SMB_AT_MINAUTH; 267613a2f6bSGordon Ross 268613a2f6bSGordon Ross /* 269430b4c46SGordon Ross * Default domain, user, ... 270613a2f6bSGordon Ross */ 271430b4c46SGordon Ross strlcpy(ctx->ct_domain, default_domain, 272430b4c46SGordon Ross sizeof (ctx->ct_domain)); 273430b4c46SGordon Ross strlcpy(ctx->ct_user, default_user, 274430b4c46SGordon Ross sizeof (ctx->ct_user)); 275613a2f6bSGordon Ross 276430b4c46SGordon Ross return (0); 277613a2f6bSGordon Ross } 278613a2f6bSGordon Ross 279613a2f6bSGordon Ross /* 280613a2f6bSGordon Ross * "Scan" the command line args to find the server name, 281613a2f6bSGordon Ross * user name, and share name, as needed. We need these 282613a2f6bSGordon Ross * before reading the RC files and/or sharectl values. 2834bff34e3Sthurlow * 2844bff34e3Sthurlow * The sequence for getting all the members filled in 2854bff34e3Sthurlow * has some tricky aspects. Here's how it works: 2864bff34e3Sthurlow * 2874bff34e3Sthurlow * The search order for options is as follows: 2884bff34e3Sthurlow * command line options 2894bff34e3Sthurlow * values parsed from UNC path (cmd) 2904bff34e3Sthurlow * values from RC file (per-user) 2914bff34e3Sthurlow * values from SMF (system-wide) 2924bff34e3Sthurlow * built-in defaults 2934bff34e3Sthurlow * 2944bff34e3Sthurlow * Normally, one would simply get all the values starting with 2954bff34e3Sthurlow * the bottom of the above list and working to the top, and 2964bff34e3Sthurlow * overwriting values as you go. But we need an exception. 2974bff34e3Sthurlow * 2984bff34e3Sthurlow * In this function, we parse the UNC path and command line options, 2994bff34e3Sthurlow * because we need (at least) the server name when we're getting the 3004bff34e3Sthurlow * SMF and RC file values. However, values we get from the command 3014bff34e3Sthurlow * should not be overwritten by SMF or RC file parsing, so we mark 3024bff34e3Sthurlow * values from the command as "from CMD" and the RC file parser 3034bff34e3Sthurlow * leaves in place any values so marked. See: SMBCF_CMD_* 3044bff34e3Sthurlow * 3054bff34e3Sthurlow * The semantics of these flags are: "This value came from the 3064bff34e3Sthurlow * current command instance, not from sources that may apply to 3074bff34e3Sthurlow * multiple commands." (Different from the old "FROMUSR" flag.) 3084bff34e3Sthurlow * 3094bff34e3Sthurlow * Note that smb_ctx_opt() is called later to handle the 3104bff34e3Sthurlow * remaining options, which should be ignored here. 3114bff34e3Sthurlow * The (magic) leading ":" in cf_getopt() makes it 3124bff34e3Sthurlow * ignore options not in the options string. 3134bff34e3Sthurlow */ 3144bff34e3Sthurlow int 315613a2f6bSGordon Ross smb_ctx_scan_argv(struct smb_ctx *ctx, int argc, char **argv, 3164bff34e3Sthurlow int minlevel, int maxlevel, int sharetype) 3174bff34e3Sthurlow { 318613a2f6bSGordon Ross int ind, opt, error = 0; 3194bff34e3Sthurlow int aflg = 0, uflg = 0; 320613a2f6bSGordon Ross const char *arg; 3214bff34e3Sthurlow 3224bff34e3Sthurlow /* 3234bff34e3Sthurlow * Parse options, if any. Values from here too 3244bff34e3Sthurlow * are marked as "from CMD". 3254bff34e3Sthurlow */ 326613a2f6bSGordon Ross if (argv == NULL) 327613a2f6bSGordon Ross return (0); 328613a2f6bSGordon Ross 329613a2f6bSGordon Ross ctx->ct_minlevel = minlevel; 330613a2f6bSGordon Ross ctx->ct_maxlevel = maxlevel; 331613a2f6bSGordon Ross ctx->ct_shtype_req = sharetype; 332613a2f6bSGordon Ross 333613a2f6bSGordon Ross cf_opt_lock(); 334613a2f6bSGordon Ross /* Careful: no return/goto before cf_opt_unlock! */ 335613a2f6bSGordon Ross while (error == 0) { 336613a2f6bSGordon Ross opt = cf_getopt(argc, argv, STDPARAM_OPT); 337613a2f6bSGordon Ross if (opt == -1) 338613a2f6bSGordon Ross break; 3394bff34e3Sthurlow arg = cf_optarg; 340613a2f6bSGordon Ross /* NB: handle most in smb_ctx_opt */ 3414bff34e3Sthurlow switch (opt) { 3424bff34e3Sthurlow case 'A': 3434bff34e3Sthurlow aflg = 1; 3444bff34e3Sthurlow error = smb_ctx_setuser(ctx, "", TRUE); 3454bff34e3Sthurlow ctx->ct_flags |= SMBCF_NOPWD; 3464bff34e3Sthurlow break; 3474bff34e3Sthurlow case 'U': 3484bff34e3Sthurlow uflg = 1; 3494bff34e3Sthurlow error = smb_ctx_setuser(ctx, arg, TRUE); 3504bff34e3Sthurlow break; 351613a2f6bSGordon Ross default: 352613a2f6bSGordon Ross DPRINT("skip opt=%c", opt); 353613a2f6bSGordon Ross break; 3544bff34e3Sthurlow } 3554bff34e3Sthurlow } 356613a2f6bSGordon Ross ind = cf_optind; 357613a2f6bSGordon Ross arg = argv[ind]; 358613a2f6bSGordon Ross cf_optind = cf_optreset = 1; 359613a2f6bSGordon Ross cf_opt_unlock(); 360613a2f6bSGordon Ross 361613a2f6bSGordon Ross if (error) 362613a2f6bSGordon Ross return (error); 363613a2f6bSGordon Ross 3644bff34e3Sthurlow if (aflg && uflg) { 3654bff34e3Sthurlow printf(gettext("-A and -U flags are exclusive.\n")); 366613a2f6bSGordon Ross return (EINVAL); 3674bff34e3Sthurlow } 3684bff34e3Sthurlow 369613a2f6bSGordon Ross /* 370613a2f6bSGordon Ross * Parse the UNC path. Values from here are 371613a2f6bSGordon Ross * marked as "from CMD". 372613a2f6bSGordon Ross */ 373613a2f6bSGordon Ross for (; ind < argc; ind++) { 374613a2f6bSGordon Ross arg = argv[ind]; 375613a2f6bSGordon Ross if (strncmp(arg, "//", 2) != 0) 376613a2f6bSGordon Ross continue; 377613a2f6bSGordon Ross error = smb_ctx_parseunc(ctx, arg, 378613a2f6bSGordon Ross minlevel, maxlevel, sharetype, &arg); 379613a2f6bSGordon Ross if (error) 380613a2f6bSGordon Ross return (error); 381613a2f6bSGordon Ross break; 382613a2f6bSGordon Ross } 3834bff34e3Sthurlow 3844bff34e3Sthurlow return (error); 3854bff34e3Sthurlow } 3864bff34e3Sthurlow 3874bff34e3Sthurlow void 388613a2f6bSGordon Ross smb_ctx_free(smb_ctx_t *ctx) 389613a2f6bSGordon Ross { 390613a2f6bSGordon Ross smb_ctx_done(ctx); 391613a2f6bSGordon Ross free(ctx); 392613a2f6bSGordon Ross } 393613a2f6bSGordon Ross 394613a2f6bSGordon Ross void 3954bff34e3Sthurlow smb_ctx_done(struct smb_ctx *ctx) 3964bff34e3Sthurlow { 3974bff34e3Sthurlow 3984bff34e3Sthurlow rpc_cleanup_smbctx(ctx); 3994bff34e3Sthurlow 400613a2f6bSGordon Ross if (ctx->ct_dev_fd != -1) { 401613a2f6bSGordon Ross close(ctx->ct_dev_fd); 402613a2f6bSGordon Ross ctx->ct_dev_fd = -1; 4034bff34e3Sthurlow } 4046b2bcd8eSGordon Ross if (ctx->ct_door_fd != -1) { 4056b2bcd8eSGordon Ross close(ctx->ct_door_fd); 4066b2bcd8eSGordon Ross ctx->ct_door_fd = -1; 4076b2bcd8eSGordon Ross } 408613a2f6bSGordon Ross if (ctx->ct_tran_fd != -1) { 409613a2f6bSGordon Ross close(ctx->ct_tran_fd); 410613a2f6bSGordon Ross ctx->ct_tran_fd = -1; 411613a2f6bSGordon Ross } 412613a2f6bSGordon Ross if (ctx->ct_srvaddr_s) { 413613a2f6bSGordon Ross free(ctx->ct_srvaddr_s); 414613a2f6bSGordon Ross ctx->ct_srvaddr_s = NULL; 415613a2f6bSGordon Ross } 416613a2f6bSGordon Ross if (ctx->ct_nb) { 4174bff34e3Sthurlow nb_ctx_done(ctx->ct_nb); 418613a2f6bSGordon Ross ctx->ct_nb = NULL; 419613a2f6bSGordon Ross } 420613a2f6bSGordon Ross if (ctx->ct_locname) { 421613a2f6bSGordon Ross free(ctx->ct_locname); 422613a2f6bSGordon Ross ctx->ct_locname = NULL; 423613a2f6bSGordon Ross } 424613a2f6bSGordon Ross if (ctx->ct_origshare) { 4254bff34e3Sthurlow free(ctx->ct_origshare); 426613a2f6bSGordon Ross ctx->ct_origshare = NULL; 427613a2f6bSGordon Ross } 428613a2f6bSGordon Ross if (ctx->ct_fullserver) { 4294bff34e3Sthurlow free(ctx->ct_fullserver); 430613a2f6bSGordon Ross ctx->ct_fullserver = NULL; 431613a2f6bSGordon Ross } 432613a2f6bSGordon Ross if (ctx->ct_addrinfo) { 433613a2f6bSGordon Ross freeaddrinfo(ctx->ct_addrinfo); 434613a2f6bSGordon Ross ctx->ct_addrinfo = NULL; 435613a2f6bSGordon Ross } 436430b4c46SGordon Ross if (ctx->ct_home) { 437613a2f6bSGordon Ross free(ctx->ct_home); 438430b4c46SGordon Ross ctx->ct_home = NULL; 439430b4c46SGordon Ross } 440430b4c46SGordon Ross if (ctx->ct_rpath) { 441430b4c46SGordon Ross free(ctx->ct_rpath); 442430b4c46SGordon Ross ctx->ct_rpath = NULL; 443430b4c46SGordon Ross } 444613a2f6bSGordon Ross if (ctx->ct_srv_OS) { 445613a2f6bSGordon Ross free(ctx->ct_srv_OS); 446613a2f6bSGordon Ross ctx->ct_srv_OS = NULL; 447613a2f6bSGordon Ross } 448613a2f6bSGordon Ross if (ctx->ct_srv_LM) { 449613a2f6bSGordon Ross free(ctx->ct_srv_LM); 450613a2f6bSGordon Ross ctx->ct_srv_LM = NULL; 451613a2f6bSGordon Ross } 452613a2f6bSGordon Ross if (ctx->ct_mackey) { 453613a2f6bSGordon Ross free(ctx->ct_mackey); 454613a2f6bSGordon Ross ctx->ct_mackey = NULL; 455613a2f6bSGordon Ross } 4564bff34e3Sthurlow } 4574bff34e3Sthurlow 4584bff34e3Sthurlow /* 4594bff34e3Sthurlow * Parse the UNC path. Here we expect something like 460430b4c46SGordon Ross * "//[[domain;]user[:password]@]host[/share[/path]]" 4614bff34e3Sthurlow * See http://ietf.org/internet-drafts/draft-crhertel-smb-url-07.txt 4624bff34e3Sthurlow * Values found here are marked as "from CMD". 4634bff34e3Sthurlow */ 4644bff34e3Sthurlow int 465613a2f6bSGordon Ross smb_ctx_parseunc(struct smb_ctx *ctx, const char *unc, 466613a2f6bSGordon Ross int minlevel, int maxlevel, int sharetype, 4674bff34e3Sthurlow const char **next) 4684bff34e3Sthurlow { 4694bff34e3Sthurlow char tmp[1024]; 470430b4c46SGordon Ross char *host, *share, *path; 471430b4c46SGordon Ross char *dom, *usr, *pw, *p; 4724bff34e3Sthurlow int error; 4734bff34e3Sthurlow 474613a2f6bSGordon Ross /* 475613a2f6bSGordon Ross * This may be called outside of _scan_argv, 476613a2f6bSGordon Ross * so make sure these get initialized. 477613a2f6bSGordon Ross */ 478613a2f6bSGordon Ross ctx->ct_minlevel = minlevel; 479613a2f6bSGordon Ross ctx->ct_maxlevel = maxlevel; 480613a2f6bSGordon Ross ctx->ct_shtype_req = sharetype; 4814bff34e3Sthurlow ctx->ct_parsedlevel = SMBL_NONE; 482430b4c46SGordon Ross 483430b4c46SGordon Ross dom = usr = pw = host = NULL; 484430b4c46SGordon Ross 485430b4c46SGordon Ross /* Work on a temporary copy, fix back slashes. */ 486430b4c46SGordon Ross strlcpy(tmp, unc, sizeof (tmp)); 487430b4c46SGordon Ross for (p = tmp; *p; p++) 488430b4c46SGordon Ross if (*p == '\\') 489430b4c46SGordon Ross *p = '/'; 490430b4c46SGordon Ross 491430b4c46SGordon Ross if (tmp[0] != '/' || tmp[1] != '/') { 4924bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 4934bff34e3Sthurlow "UNC should start with '//'"), 0); 494613a2f6bSGordon Ross error = EINVAL; 495613a2f6bSGordon Ross goto out; 4964bff34e3Sthurlow } 497430b4c46SGordon Ross p = tmp + 2; /* user@host... */ 498430b4c46SGordon Ross 499430b4c46SGordon Ross /* Find the share part, if any. */ 500430b4c46SGordon Ross share = strchr(p, '/'); 501430b4c46SGordon Ross if (share) 502430b4c46SGordon Ross *share = '\0'; 503430b4c46SGordon Ross (void) unpercent(p); /* host component */ 504430b4c46SGordon Ross 505430b4c46SGordon Ross /* 506430b4c46SGordon Ross * Parse the "host" stuff right to left: 507430b4c46SGordon Ross * 1: trailing "@hostname" (or whole field) 508430b4c46SGordon Ross * 2: trailing ":password" 509430b4c46SGordon Ross * 3: trailing "domain;user" (or just user) 510430b4c46SGordon Ross */ 511430b4c46SGordon Ross host = strrchr(p, '@'); 512430b4c46SGordon Ross if (host == NULL) { 513430b4c46SGordon Ross host = p; /* no user@ prefix */ 514430b4c46SGordon Ross } else { 515430b4c46SGordon Ross *host++ = '\0'; 516430b4c46SGordon Ross 517430b4c46SGordon Ross /* may have [[domain;]user[:passwd]] */ 518430b4c46SGordon Ross pw = strchr(p, ':'); 519430b4c46SGordon Ross if (pw) 520430b4c46SGordon Ross *pw++ = '\0'; 521430b4c46SGordon Ross usr = strchr(p, ';'); 522430b4c46SGordon Ross if (usr) { 523430b4c46SGordon Ross *usr++ = '\0'; 524430b4c46SGordon Ross dom = p; 525430b4c46SGordon Ross } else 526430b4c46SGordon Ross usr = p; 527430b4c46SGordon Ross } 528430b4c46SGordon Ross 529430b4c46SGordon Ross if (*host == '\0') { 530430b4c46SGordon Ross smb_error(dgettext(TEXT_DOMAIN, "empty server name"), 0); 531613a2f6bSGordon Ross error = EINVAL; 532613a2f6bSGordon Ross goto out; 5334bff34e3Sthurlow } 534430b4c46SGordon Ross error = smb_ctx_setfullserver(ctx, host); 535430b4c46SGordon Ross if (error) 536430b4c46SGordon Ross goto out; 537430b4c46SGordon Ross ctx->ct_parsedlevel = SMBL_VC; 538430b4c46SGordon Ross 539430b4c46SGordon Ross if (dom != NULL) { 540430b4c46SGordon Ross error = smb_ctx_setdomain(ctx, dom, TRUE); 5414bff34e3Sthurlow if (error) 542613a2f6bSGordon Ross goto out; 5434bff34e3Sthurlow } 544430b4c46SGordon Ross if (usr != NULL) { 545430b4c46SGordon Ross if (*usr == '\0') { 546430b4c46SGordon Ross smb_error(dgettext(TEXT_DOMAIN, 547430b4c46SGordon Ross "empty user name"), 0); 548430b4c46SGordon Ross error = EINVAL; 549430b4c46SGordon Ross goto out; 550430b4c46SGordon Ross } 5514bff34e3Sthurlow if (ctx->ct_maxlevel < SMBL_VC) { 5524bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 5534bff34e3Sthurlow "no user name required"), 0); 554613a2f6bSGordon Ross error = EINVAL; 555613a2f6bSGordon Ross goto out; 5564bff34e3Sthurlow } 557430b4c46SGordon Ross error = smb_ctx_setuser(ctx, usr, TRUE); 5584bff34e3Sthurlow if (error) 559613a2f6bSGordon Ross goto out; 5604bff34e3Sthurlow } 561430b4c46SGordon Ross if (pw != NULL) { 562430b4c46SGordon Ross error = smb_ctx_setpassword(ctx, pw, TRUE); 563430b4c46SGordon Ross if (error) 564430b4c46SGordon Ross goto out; 565430b4c46SGordon Ross } 566430b4c46SGordon Ross 567430b4c46SGordon Ross if (share != NULL) { 568430b4c46SGordon Ross /* restore the slash */ 569430b4c46SGordon Ross *share = '/'; 570430b4c46SGordon Ross p = share + 1; 571430b4c46SGordon Ross 572430b4c46SGordon Ross /* Find the path part, if any. */ 573430b4c46SGordon Ross path = strchr(p, '/'); 574430b4c46SGordon Ross if (path) 575430b4c46SGordon Ross *path = '\0'; 576430b4c46SGordon Ross (void) unpercent(p); /* share component */ 577430b4c46SGordon Ross 578430b4c46SGordon Ross if (*p == '\0') { 5794bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 580430b4c46SGordon Ross "empty share name"), 0); 581613a2f6bSGordon Ross error = EINVAL; 582613a2f6bSGordon Ross goto out; 5834bff34e3Sthurlow } 584430b4c46SGordon Ross if (ctx->ct_maxlevel < SMBL_SHARE) { 5854bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 586430b4c46SGordon Ross "no share name required"), 0); 587613a2f6bSGordon Ross error = EINVAL; 588613a2f6bSGordon Ross goto out; 5894bff34e3Sthurlow } 5904bff34e3Sthurlow 5914bff34e3Sthurlow /* 592430b4c46SGordon Ross * Special case UNC names like: 593430b4c46SGordon Ross * //host/PIPE/endpoint 594430b4c46SGordon Ross * to have share: IPC$ 5954bff34e3Sthurlow */ 596430b4c46SGordon Ross if (strcasecmp(p, "PIPE") == 0) { 597430b4c46SGordon Ross sharetype = USE_IPC; 598430b4c46SGordon Ross p = "IPC$"; 599430b4c46SGordon Ross } 600430b4c46SGordon Ross error = smb_ctx_setshare(ctx, p, sharetype); 6014bff34e3Sthurlow if (error) 602613a2f6bSGordon Ross goto out; 603430b4c46SGordon Ross ctx->ct_parsedlevel = SMBL_SHARE; 604613a2f6bSGordon Ross 605430b4c46SGordon Ross if (path) { 606430b4c46SGordon Ross /* restore the slash */ 607430b4c46SGordon Ross *path = '/'; 608430b4c46SGordon Ross p = path + 1; 609430b4c46SGordon Ross (void) unpercent(p); /* remainder */ 610430b4c46SGordon Ross free(ctx->ct_rpath); 611430b4c46SGordon Ross ctx->ct_rpath = strdup(path); 6124bff34e3Sthurlow } 613430b4c46SGordon Ross } else if (ctx->ct_minlevel >= SMBL_SHARE) { 6144bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, "empty share name"), 0); 615613a2f6bSGordon Ross error = EINVAL; 616613a2f6bSGordon Ross goto out; 6174bff34e3Sthurlow } 618430b4c46SGordon Ross 619613a2f6bSGordon Ross if (next) 620430b4c46SGordon Ross *next = NULL; 621613a2f6bSGordon Ross 622613a2f6bSGordon Ross out: 623613a2f6bSGordon Ross if (error == 0 && smb_debug > 0) 624613a2f6bSGordon Ross dump_ctx("after smb_ctx_parseunc", ctx); 625613a2f6bSGordon Ross 6264bff34e3Sthurlow return (error); 6274bff34e3Sthurlow } 6284bff34e3Sthurlow 629613a2f6bSGordon Ross #ifdef KICONV_SUPPORT 6304bff34e3Sthurlow int 6314bff34e3Sthurlow smb_ctx_setcharset(struct smb_ctx *ctx, const char *arg) 6324bff34e3Sthurlow { 6334bff34e3Sthurlow char *cp, *servercs, *localcs; 6344bff34e3Sthurlow int cslen = sizeof (ctx->ct_ssn.ioc_localcs); 6354bff34e3Sthurlow int scslen, lcslen, error; 6364bff34e3Sthurlow 6374bff34e3Sthurlow cp = strchr(arg, ':'); 6384bff34e3Sthurlow lcslen = cp ? (cp - arg) : 0; 6394bff34e3Sthurlow if (lcslen == 0 || lcslen >= cslen) { 6404bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 6414bff34e3Sthurlow "invalid local charset specification (%s)"), 0, arg); 6424bff34e3Sthurlow return (EINVAL); 6434bff34e3Sthurlow } 6444bff34e3Sthurlow scslen = (size_t)strlen(++cp); 6454bff34e3Sthurlow if (scslen == 0 || scslen >= cslen) { 6464bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 6474bff34e3Sthurlow "invalid server charset specification (%s)"), 0, arg); 6484bff34e3Sthurlow return (EINVAL); 6494bff34e3Sthurlow } 6504bff34e3Sthurlow localcs = memcpy(ctx->ct_ssn.ioc_localcs, arg, lcslen); 6514bff34e3Sthurlow localcs[lcslen] = 0; 6524bff34e3Sthurlow servercs = strcpy(ctx->ct_ssn.ioc_servercs, cp); 6534bff34e3Sthurlow error = nls_setrecode(localcs, servercs); 6544bff34e3Sthurlow if (error == 0) 6554bff34e3Sthurlow return (0); 6564bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 6574bff34e3Sthurlow "can't initialize iconv support (%s:%s)"), 6584bff34e3Sthurlow error, localcs, servercs); 6594bff34e3Sthurlow localcs[0] = 0; 6604bff34e3Sthurlow servercs[0] = 0; 6614bff34e3Sthurlow return (error); 6624bff34e3Sthurlow } 663613a2f6bSGordon Ross #endif /* KICONV_SUPPORT */ 664613a2f6bSGordon Ross 665613a2f6bSGordon Ross int 666613a2f6bSGordon Ross smb_ctx_setauthflags(struct smb_ctx *ctx, int flags) 667613a2f6bSGordon Ross { 668613a2f6bSGordon Ross ctx->ct_authflags = flags; 669613a2f6bSGordon Ross return (0); 670613a2f6bSGordon Ross } 6714bff34e3Sthurlow 6724bff34e3Sthurlow int 6734bff34e3Sthurlow smb_ctx_setfullserver(struct smb_ctx *ctx, const char *name) 6744bff34e3Sthurlow { 675613a2f6bSGordon Ross char *p = strdup(name); 676613a2f6bSGordon Ross 677613a2f6bSGordon Ross if (p == NULL) 6784bff34e3Sthurlow return (ENOMEM); 679613a2f6bSGordon Ross if (ctx->ct_fullserver) 680613a2f6bSGordon Ross free(ctx->ct_fullserver); 681613a2f6bSGordon Ross ctx->ct_fullserver = p; 6824bff34e3Sthurlow return (0); 6834bff34e3Sthurlow } 6844bff34e3Sthurlow 685613a2f6bSGordon Ross int 6864bff34e3Sthurlow smb_ctx_setserver(struct smb_ctx *ctx, const char *name) 6874bff34e3Sthurlow { 688613a2f6bSGordon Ross strlcpy(ctx->ct_srvname, name, 689613a2f6bSGordon Ross sizeof (ctx->ct_srvname)); 690613a2f6bSGordon Ross return (0); 6914bff34e3Sthurlow } 6924bff34e3Sthurlow 6934bff34e3Sthurlow int 6944bff34e3Sthurlow smb_ctx_setuser(struct smb_ctx *ctx, const char *name, int from_cmd) 6954bff34e3Sthurlow { 6964bff34e3Sthurlow 697613a2f6bSGordon Ross if (strlen(name) >= sizeof (ctx->ct_user)) { 6984bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 6994bff34e3Sthurlow "user name '%s' too long"), 0, name); 7004bff34e3Sthurlow return (ENAMETOOLONG); 7014bff34e3Sthurlow } 7024bff34e3Sthurlow 7034bff34e3Sthurlow /* 7044bff34e3Sthurlow * Don't overwrite a value from the command line 7054bff34e3Sthurlow * with one from anywhere else. 7064bff34e3Sthurlow */ 7074bff34e3Sthurlow if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_USR)) 7084bff34e3Sthurlow return (0); 7094bff34e3Sthurlow 710613a2f6bSGordon Ross strlcpy(ctx->ct_user, name, 711613a2f6bSGordon Ross sizeof (ctx->ct_user)); 7124bff34e3Sthurlow 7134bff34e3Sthurlow /* Mark this as "from the command line". */ 7144bff34e3Sthurlow if (from_cmd) 7154bff34e3Sthurlow ctx->ct_flags |= SMBCF_CMD_USR; 7164bff34e3Sthurlow 7174bff34e3Sthurlow return (0); 7184bff34e3Sthurlow } 7194bff34e3Sthurlow 7204bff34e3Sthurlow /* 7214bff34e3Sthurlow * Don't overwrite a domain name from the 7224bff34e3Sthurlow * command line with one from anywhere else. 7234bff34e3Sthurlow * See smb_ctx_init() for notes about this. 7244bff34e3Sthurlow */ 7254bff34e3Sthurlow int 726613a2f6bSGordon Ross smb_ctx_setdomain(struct smb_ctx *ctx, const char *name, int from_cmd) 7274bff34e3Sthurlow { 7284bff34e3Sthurlow 729613a2f6bSGordon Ross if (strlen(name) >= sizeof (ctx->ct_domain)) { 7304bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 7314bff34e3Sthurlow "workgroup name '%s' too long"), 0, name); 7324bff34e3Sthurlow return (ENAMETOOLONG); 7334bff34e3Sthurlow } 7344bff34e3Sthurlow 7354bff34e3Sthurlow /* 7364bff34e3Sthurlow * Don't overwrite a value from the command line 7374bff34e3Sthurlow * with one from anywhere else. 7384bff34e3Sthurlow */ 7394bff34e3Sthurlow if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_DOM)) 7404bff34e3Sthurlow return (0); 7414bff34e3Sthurlow 742613a2f6bSGordon Ross strlcpy(ctx->ct_domain, name, 743613a2f6bSGordon Ross sizeof (ctx->ct_domain)); 7444bff34e3Sthurlow 7454bff34e3Sthurlow /* Mark this as "from the command line". */ 7464bff34e3Sthurlow if (from_cmd) 7474bff34e3Sthurlow ctx->ct_flags |= SMBCF_CMD_DOM; 7484bff34e3Sthurlow 7494bff34e3Sthurlow return (0); 7504bff34e3Sthurlow } 7514bff34e3Sthurlow 7524bff34e3Sthurlow int 7534bff34e3Sthurlow smb_ctx_setpassword(struct smb_ctx *ctx, const char *passwd, int from_cmd) 7544bff34e3Sthurlow { 755613a2f6bSGordon Ross int err; 7564bff34e3Sthurlow 757613a2f6bSGordon Ross if (passwd == NULL) 7584bff34e3Sthurlow return (EINVAL); 759613a2f6bSGordon Ross if (strlen(passwd) >= sizeof (ctx->ct_password)) { 7604bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, "password too long"), 0); 7614bff34e3Sthurlow return (ENAMETOOLONG); 7624bff34e3Sthurlow } 7634bff34e3Sthurlow 7644bff34e3Sthurlow /* 765613a2f6bSGordon Ross * If called again after comand line parsing, 766613a2f6bSGordon Ross * don't overwrite a value from the command line 767613a2f6bSGordon Ross * with one from any stored config. 7684bff34e3Sthurlow */ 7694bff34e3Sthurlow if (!from_cmd && (ctx->ct_flags & SMBCF_CMD_PW)) 7704bff34e3Sthurlow return (0); 7714bff34e3Sthurlow 772613a2f6bSGordon Ross memset(ctx->ct_password, 0, sizeof (ctx->ct_password)); 7734bff34e3Sthurlow if (strncmp(passwd, "$$1", 3) == 0) 77402d09e03SGordon Ross (void) smb_simpledecrypt(ctx->ct_password, passwd); 7754bff34e3Sthurlow else 776613a2f6bSGordon Ross strlcpy(ctx->ct_password, passwd, 777613a2f6bSGordon Ross sizeof (ctx->ct_password)); 778613a2f6bSGordon Ross 779613a2f6bSGordon Ross /* 780613a2f6bSGordon Ross * Compute LM hash, NT hash. 781613a2f6bSGordon Ross */ 782613a2f6bSGordon Ross if (ctx->ct_password[0]) { 783613a2f6bSGordon Ross err = ntlm_compute_nt_hash(ctx->ct_nthash, ctx->ct_password); 784613a2f6bSGordon Ross if (err != 0) 785613a2f6bSGordon Ross return (err); 786613a2f6bSGordon Ross err = ntlm_compute_lm_hash(ctx->ct_lmhash, ctx->ct_password); 787613a2f6bSGordon Ross if (err != 0) 788613a2f6bSGordon Ross return (err); 789613a2f6bSGordon Ross } 7904bff34e3Sthurlow 7914bff34e3Sthurlow /* Mark this as "from the command line". */ 7924bff34e3Sthurlow if (from_cmd) 7934bff34e3Sthurlow ctx->ct_flags |= SMBCF_CMD_PW; 7944bff34e3Sthurlow 7954bff34e3Sthurlow return (0); 7964bff34e3Sthurlow } 7974bff34e3Sthurlow 798613a2f6bSGordon Ross /* 799613a2f6bSGordon Ross * Use this to set NTLM auth. info (hashes) 800613a2f6bSGordon Ross * when we don't have the password. 801613a2f6bSGordon Ross */ 802613a2f6bSGordon Ross int 803613a2f6bSGordon Ross smb_ctx_setpwhash(smb_ctx_t *ctx, 804613a2f6bSGordon Ross const uchar_t *nthash, const uchar_t *lmhash) 805613a2f6bSGordon Ross { 806613a2f6bSGordon Ross 807613a2f6bSGordon Ross /* Need ct_password to be non-null. */ 808613a2f6bSGordon Ross if (ctx->ct_password[0] == '\0') 809613a2f6bSGordon Ross strlcpy(ctx->ct_password, "$HASH", 810613a2f6bSGordon Ross sizeof (ctx->ct_password)); 811613a2f6bSGordon Ross 812613a2f6bSGordon Ross /* 813613a2f6bSGordon Ross * Compute LM hash, NT hash. 814613a2f6bSGordon Ross */ 815613a2f6bSGordon Ross memcpy(ctx->ct_nthash, nthash, NTLM_HASH_SZ); 816613a2f6bSGordon Ross 817613a2f6bSGordon Ross /* The LM hash is optional */ 818613a2f6bSGordon Ross if (lmhash) { 819613a2f6bSGordon Ross memcpy(ctx->ct_nthash, nthash, NTLM_HASH_SZ); 820613a2f6bSGordon Ross } 821613a2f6bSGordon Ross 822613a2f6bSGordon Ross return (0); 823613a2f6bSGordon Ross } 824613a2f6bSGordon Ross 8254bff34e3Sthurlow int 8264bff34e3Sthurlow smb_ctx_setshare(struct smb_ctx *ctx, const char *share, int stype) 8274bff34e3Sthurlow { 828613a2f6bSGordon Ross if (strlen(share) >= SMBIOC_MAX_NAME) { 8294bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 8304bff34e3Sthurlow "share name '%s' too long"), 0, share); 8314bff34e3Sthurlow return (ENAMETOOLONG); 8324bff34e3Sthurlow } 8334bff34e3Sthurlow if (ctx->ct_origshare) 8344bff34e3Sthurlow free(ctx->ct_origshare); 8354bff34e3Sthurlow if ((ctx->ct_origshare = strdup(share)) == NULL) 8364bff34e3Sthurlow return (ENOMEM); 837613a2f6bSGordon Ross 838613a2f6bSGordon Ross ctx->ct_shtype_req = stype; 839613a2f6bSGordon Ross 8404bff34e3Sthurlow return (0); 8414bff34e3Sthurlow } 8424bff34e3Sthurlow 8434bff34e3Sthurlow int 8444bff34e3Sthurlow smb_ctx_setsrvaddr(struct smb_ctx *ctx, const char *addr) 8454bff34e3Sthurlow { 8464bff34e3Sthurlow if (addr == NULL || addr[0] == 0) 8474bff34e3Sthurlow return (EINVAL); 848613a2f6bSGordon Ross if (ctx->ct_srvaddr_s) 849613a2f6bSGordon Ross free(ctx->ct_srvaddr_s); 850613a2f6bSGordon Ross if ((ctx->ct_srvaddr_s = strdup(addr)) == NULL) 8514bff34e3Sthurlow return (ENOMEM); 8524bff34e3Sthurlow return (0); 8534bff34e3Sthurlow } 8544bff34e3Sthurlow 8558eb99b82SGordon Ross /* 8568eb99b82SGordon Ross * API for library caller to set signing enabled, required 8578eb99b82SGordon Ross * Note: if not enable, ignore require 8588eb99b82SGordon Ross */ 8598eb99b82SGordon Ross int 8608eb99b82SGordon Ross smb_ctx_setsigning(struct smb_ctx *ctx, int enable, int require) 8618eb99b82SGordon Ross { 8628eb99b82SGordon Ross ctx->ct_vopt &= ~SMBVOPT_SIGNING_MASK; 8638eb99b82SGordon Ross if (enable) { 8648eb99b82SGordon Ross ctx->ct_vopt |= SMBVOPT_SIGNING_ENABLED; 8658eb99b82SGordon Ross if (require) 8668eb99b82SGordon Ross ctx->ct_vopt |= SMBVOPT_SIGNING_REQUIRED; 8678eb99b82SGordon Ross } 8688eb99b82SGordon Ross return (0); 8698eb99b82SGordon Ross } 8708eb99b82SGordon Ross 8714bff34e3Sthurlow static int 8724bff34e3Sthurlow smb_parse_owner(char *pair, uid_t *uid, gid_t *gid) 8734bff34e3Sthurlow { 8744bff34e3Sthurlow struct group gr; 8754bff34e3Sthurlow struct passwd pw; 8764bff34e3Sthurlow char buf[NSS_BUFLEN_PASSWD]; 8774bff34e3Sthurlow char *cp; 8784bff34e3Sthurlow 8794bff34e3Sthurlow cp = strchr(pair, ':'); 8804bff34e3Sthurlow if (cp) { 8814bff34e3Sthurlow *cp++ = '\0'; 882613a2f6bSGordon Ross if (*cp && gid) { 8834bff34e3Sthurlow if (getgrnam_r(cp, &gr, buf, sizeof (buf)) != NULL) { 8844bff34e3Sthurlow *gid = gr.gr_gid; 8854bff34e3Sthurlow } else 8864bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 8874bff34e3Sthurlow "Invalid group name %s, ignored"), 0, cp); 8884bff34e3Sthurlow } 8894bff34e3Sthurlow } 8904bff34e3Sthurlow if (*pair) { 8914bff34e3Sthurlow if (getpwnam_r(pair, &pw, buf, sizeof (buf)) != NULL) { 8924bff34e3Sthurlow *uid = pw.pw_uid; 8934bff34e3Sthurlow } else 8944bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 8954bff34e3Sthurlow "Invalid user name %s, ignored"), 0, pair); 8964bff34e3Sthurlow } 8974bff34e3Sthurlow 8984bff34e3Sthurlow return (0); 8994bff34e3Sthurlow } 9004bff34e3Sthurlow 9014bff34e3Sthurlow /* 90215359501SGordon Ross * Suport a securty options arg, i.e. -S noext,lm,ntlm 90315359501SGordon Ross * for testing various type of authenticators. 90415359501SGordon Ross */ 90515359501SGordon Ross static struct nv 90615359501SGordon Ross sectype_table[] = { 90715359501SGordon Ross /* noext - handled below */ 90815359501SGordon Ross { "anon", SMB_AT_ANON }, 90915359501SGordon Ross { "lm", SMB_AT_LM1 }, 91015359501SGordon Ross { "ntlm", SMB_AT_NTLM1 }, 91115359501SGordon Ross { "ntlm2", SMB_AT_NTLM2 }, 91215359501SGordon Ross { "krb5", SMB_AT_KRB5 }, 91315359501SGordon Ross { NULL, 0 }, 91415359501SGordon Ross }; 91515359501SGordon Ross int 91615359501SGordon Ross smb_parse_secopts(struct smb_ctx *ctx, const char *arg) 91715359501SGordon Ross { 91815359501SGordon Ross const char *sep = ":;,"; 91915359501SGordon Ross const char *p = arg; 92015359501SGordon Ross struct nv *nv; 92115359501SGordon Ross int nlen, tlen; 92215359501SGordon Ross int authflags = 0; 92315359501SGordon Ross 92415359501SGordon Ross for (;;) { 92515359501SGordon Ross /* skip separators */ 92615359501SGordon Ross tlen = strspn(p, sep); 92715359501SGordon Ross p += tlen; 92815359501SGordon Ross 92915359501SGordon Ross nlen = strcspn(p, sep); 93015359501SGordon Ross if (nlen == 0) 93115359501SGordon Ross break; 93215359501SGordon Ross 93315359501SGordon Ross if (nlen == 5 && 0 == strncmp(p, "noext", nlen)) { 93415359501SGordon Ross /* Don't offer extended security. */ 93515359501SGordon Ross ctx->ct_vopt &= ~SMBVOPT_EXT_SEC; 93615359501SGordon Ross p += nlen; 93715359501SGordon Ross continue; 93815359501SGordon Ross } 93915359501SGordon Ross 94015359501SGordon Ross /* This is rarely called, so not optimized. */ 94115359501SGordon Ross for (nv = sectype_table; nv->name; nv++) { 94215359501SGordon Ross tlen = strlen(nv->name); 94315359501SGordon Ross if (tlen == nlen && 0 == strncmp(p, nv->name, tlen)) 94415359501SGordon Ross break; 94515359501SGordon Ross } 94615359501SGordon Ross if (nv->name == NULL) { 94715359501SGordon Ross smb_error(dgettext(TEXT_DOMAIN, 94815359501SGordon Ross "%s: invalid security options"), 0, p); 94915359501SGordon Ross return (EINVAL); 95015359501SGordon Ross } 95115359501SGordon Ross authflags |= nv->value; 95215359501SGordon Ross p += nlen; 95315359501SGordon Ross } 95415359501SGordon Ross 95515359501SGordon Ross if (authflags) 95615359501SGordon Ross ctx->ct_authflags = authflags; 95715359501SGordon Ross 95815359501SGordon Ross return (0); 95915359501SGordon Ross } 96015359501SGordon Ross 96115359501SGordon Ross /* 9624bff34e3Sthurlow * Commands use this with getopt. See: 9634bff34e3Sthurlow * STDPARAM_OPT, STDPARAM_ARGS 9644bff34e3Sthurlow * Called after smb_ctx_readrc(). 9654bff34e3Sthurlow */ 9664bff34e3Sthurlow int 9674bff34e3Sthurlow smb_ctx_opt(struct smb_ctx *ctx, int opt, const char *arg) 9684bff34e3Sthurlow { 9694bff34e3Sthurlow int error = 0; 9704bff34e3Sthurlow char *p, *cp; 9714bff34e3Sthurlow char tmp[1024]; 9724bff34e3Sthurlow 9734bff34e3Sthurlow switch (opt) { 9744bff34e3Sthurlow case 'A': 9754bff34e3Sthurlow case 'U': 9764bff34e3Sthurlow /* Handled in smb_ctx_init() */ 9774bff34e3Sthurlow break; 9784bff34e3Sthurlow case 'I': 9794bff34e3Sthurlow error = smb_ctx_setsrvaddr(ctx, arg); 9804bff34e3Sthurlow break; 9814bff34e3Sthurlow case 'M': 982613a2f6bSGordon Ross /* share connect rights - ignored */ 9834bff34e3Sthurlow ctx->ct_flags |= SMBCF_SRIGHTS; 9844bff34e3Sthurlow break; 9854bff34e3Sthurlow case 'N': 9864bff34e3Sthurlow ctx->ct_flags |= SMBCF_NOPWD; 9874bff34e3Sthurlow break; 9884bff34e3Sthurlow case 'O': 9894bff34e3Sthurlow p = strdup(arg); 9904bff34e3Sthurlow cp = strchr(p, '/'); 991613a2f6bSGordon Ross if (cp) 992613a2f6bSGordon Ross *cp = '\0'; 993613a2f6bSGordon Ross error = smb_parse_owner(cp, &ctx->ct_owner, NULL); 9944bff34e3Sthurlow free(p); 9954bff34e3Sthurlow break; 9964bff34e3Sthurlow case 'P': 997613a2f6bSGordon Ross /* ctx->ct_vopt |= SMBCOPT_PERMANENT; */ 9984bff34e3Sthurlow break; 9994bff34e3Sthurlow case 'R': 1000613a2f6bSGordon Ross /* retry count - ignored */ 10014bff34e3Sthurlow break; 100215359501SGordon Ross case 'S': 100315359501SGordon Ross /* Security options (undocumented, just for tests) */ 100415359501SGordon Ross error = smb_parse_secopts(ctx, arg); 100515359501SGordon Ross break; 10064bff34e3Sthurlow case 'T': 1007613a2f6bSGordon Ross /* timeout - ignored */ 10084bff34e3Sthurlow break; 1009613a2f6bSGordon Ross case 'D': /* domain */ 1010613a2f6bSGordon Ross case 'W': /* workgroup (legacy alias) */ 1011613a2f6bSGordon Ross error = smb_ctx_setdomain(ctx, tmp, TRUE); 10124bff34e3Sthurlow break; 10134bff34e3Sthurlow } 10144bff34e3Sthurlow return (error); 10154bff34e3Sthurlow } 10164bff34e3Sthurlow 10174bff34e3Sthurlow 1018613a2f6bSGordon Ross /* 1019613a2f6bSGordon Ross * Original code injected iconv tables into the kernel. 1020613a2f6bSGordon Ross * Not sure if we'll need this or not... REVISIT 1021613a2f6bSGordon Ross */ 1022613a2f6bSGordon Ross #ifdef KICONV_SUPPORT 10234bff34e3Sthurlow static int 10244bff34e3Sthurlow smb_addiconvtbl(const char *to, const char *from, const uchar_t *tbl) 10254bff34e3Sthurlow { 1026613a2f6bSGordon Ross int error = 0; 10274bff34e3Sthurlow 10284bff34e3Sthurlow error = kiconv_add_xlat_table(to, from, tbl); 10294bff34e3Sthurlow if (error && error != EEXIST) { 10304bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 10314bff34e3Sthurlow "can not setup kernel iconv table (%s:%s)"), 10324bff34e3Sthurlow error, from, to); 10334bff34e3Sthurlow return (error); 10344bff34e3Sthurlow } 1035613a2f6bSGordon Ross return (error); 10364bff34e3Sthurlow } 1037613a2f6bSGordon Ross #endif /* KICONV_SUPPORT */ 10384bff34e3Sthurlow 10394bff34e3Sthurlow /* 1040613a2f6bSGordon Ross * Verify context info. before connect operation(s), 10414bff34e3Sthurlow * lookup specified server and try to fill all forgotten fields. 1042613a2f6bSGordon Ross * Legacy name used by commands. 10434bff34e3Sthurlow */ 10444bff34e3Sthurlow int 10454bff34e3Sthurlow smb_ctx_resolve(struct smb_ctx *ctx) 10464bff34e3Sthurlow { 10474bff34e3Sthurlow struct smbioc_ossn *ssn = &ctx->ct_ssn; 1048613a2f6bSGordon Ross int error = 0; 1049613a2f6bSGordon Ross #ifdef KICONV_SUPPORT 10504bff34e3Sthurlow uchar_t cstbl[256]; 10514bff34e3Sthurlow uint_t i; 1052613a2f6bSGordon Ross #endif 10534bff34e3Sthurlow 105415359501SGordon Ross if (smb_debug) 105515359501SGordon Ross dump_ctx("before smb_ctx_resolve", ctx); 105615359501SGordon Ross 10574bff34e3Sthurlow ctx->ct_flags &= ~SMBCF_RESOLVED; 1058613a2f6bSGordon Ross 1059613a2f6bSGordon Ross if (ctx->ct_fullserver == NULL) { 10604bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 10614bff34e3Sthurlow "no server name specified"), 0); 10624bff34e3Sthurlow return (EINVAL); 10634bff34e3Sthurlow } 1064613a2f6bSGordon Ross 1065613a2f6bSGordon Ross if (ctx->ct_minlevel >= SMBL_SHARE && 1066613a2f6bSGordon Ross ctx->ct_origshare == NULL) { 10674bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 10684bff34e3Sthurlow "no share name specified for %s@%s"), 1069613a2f6bSGordon Ross 0, ssn->ssn_user, ctx->ct_fullserver); 10704bff34e3Sthurlow return (EINVAL); 10714bff34e3Sthurlow } 10724bff34e3Sthurlow error = nb_ctx_resolve(ctx->ct_nb); 10734bff34e3Sthurlow if (error) 10744bff34e3Sthurlow return (error); 1075613a2f6bSGordon Ross #ifdef KICONV_SUPPORT 10764bff34e3Sthurlow if (ssn->ioc_localcs[0] == 0) 10774bff34e3Sthurlow strcpy(ssn->ioc_localcs, "default"); /* XXX: locale name ? */ 10784bff34e3Sthurlow error = smb_addiconvtbl("tolower", ssn->ioc_localcs, nls_lower); 10794bff34e3Sthurlow if (error) 10804bff34e3Sthurlow return (error); 10814bff34e3Sthurlow error = smb_addiconvtbl("toupper", ssn->ioc_localcs, nls_upper); 10824bff34e3Sthurlow if (error) 10834bff34e3Sthurlow return (error); 10844bff34e3Sthurlow if (ssn->ioc_servercs[0] != 0) { 10854bff34e3Sthurlow for (i = 0; i < sizeof (cstbl); i++) 10864bff34e3Sthurlow cstbl[i] = i; 10874bff34e3Sthurlow nls_mem_toext(cstbl, cstbl, sizeof (cstbl)); 10884bff34e3Sthurlow error = smb_addiconvtbl(ssn->ioc_servercs, ssn->ioc_localcs, 10894bff34e3Sthurlow cstbl); 10904bff34e3Sthurlow if (error) 10914bff34e3Sthurlow return (error); 10924bff34e3Sthurlow for (i = 0; i < sizeof (cstbl); i++) 10934bff34e3Sthurlow cstbl[i] = i; 10944bff34e3Sthurlow nls_mem_toloc(cstbl, cstbl, sizeof (cstbl)); 10954bff34e3Sthurlow error = smb_addiconvtbl(ssn->ioc_localcs, ssn->ioc_servercs, 10964bff34e3Sthurlow cstbl); 10974bff34e3Sthurlow if (error) 10984bff34e3Sthurlow return (error); 10994bff34e3Sthurlow } 1100613a2f6bSGordon Ross #endif /* KICONV_SUPPORT */ 11014bff34e3Sthurlow 11024bff34e3Sthurlow /* 11036b2bcd8eSGordon Ross * Lookup the IP address and fill in ct_addrinfo. 11046b2bcd8eSGordon Ross * 11056b2bcd8eSGordon Ross * Note: smb_ctx_getaddr() returns a EAI_xxx 11066b2bcd8eSGordon Ross * error value like getaddrinfo(3), but this 11076b2bcd8eSGordon Ross * function needs to return an errno value. 11084bff34e3Sthurlow */ 1109613a2f6bSGordon Ross error = smb_ctx_getaddr(ctx); 11104bff34e3Sthurlow if (error) { 11116b2bcd8eSGordon Ross const char *ais = gai_strerror(error); 11124bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 1113ae3d7f90SGordon Ross "can't resolve name\"%s\", %s"), 1114ae3d7f90SGordon Ross 0, ctx->ct_fullserver, ais); 11156b2bcd8eSGordon Ross return (ENODATA); 11164bff34e3Sthurlow } 1117613a2f6bSGordon Ross assert(ctx->ct_addrinfo != NULL); 11184bff34e3Sthurlow 11194bff34e3Sthurlow /* 1120613a2f6bSGordon Ross * If we have a user name but no password, 1121613a2f6bSGordon Ross * check for a keychain entry. 1122613a2f6bSGordon Ross * XXX: Only for auth NTLM? 11234bff34e3Sthurlow */ 1124*85e6b674SGordon Ross if (ctx->ct_user[0] != '\0') { 11254bff34e3Sthurlow /* 1126613a2f6bSGordon Ross * Have a user name. 1127613a2f6bSGordon Ross * If we don't have a p/w yet, 1128613a2f6bSGordon Ross * try the keychain. 11294bff34e3Sthurlow */ 1130613a2f6bSGordon Ross if (ctx->ct_password[0] == '\0') 1131613a2f6bSGordon Ross (void) smb_get_keychain(ctx); 1132613a2f6bSGordon Ross /* 1133613a2f6bSGordon Ross * Mask out disallowed auth types. 1134613a2f6bSGordon Ross */ 1135613a2f6bSGordon Ross ctx->ct_authflags &= ctx->ct_minauth; 11364bff34e3Sthurlow } 1137613a2f6bSGordon Ross if (ctx->ct_authflags == 0) { 11384bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 1139613a2f6bSGordon Ross "no valid auth. types"), 0); 1140613a2f6bSGordon Ross return (ENOTSUP); 11414bff34e3Sthurlow } 1142613a2f6bSGordon Ross 11434bff34e3Sthurlow ctx->ct_flags |= SMBCF_RESOLVED; 11444bff34e3Sthurlow if (smb_debug) 11454bff34e3Sthurlow dump_ctx("after smb_ctx_resolve", ctx); 11464bff34e3Sthurlow 11474bff34e3Sthurlow return (0); 11484bff34e3Sthurlow } 11494bff34e3Sthurlow 11504bff34e3Sthurlow int 11514bff34e3Sthurlow smb_open_driver() 11524bff34e3Sthurlow { 1153430b4c46SGordon Ross int fd; 11544bff34e3Sthurlow 11554bff34e3Sthurlow fd = open("/dev/"NSMB_NAME, O_RDWR); 1156613a2f6bSGordon Ross if (fd < 0) { 11574bff34e3Sthurlow return (-1); 11584bff34e3Sthurlow } 11594bff34e3Sthurlow 1160613a2f6bSGordon Ross /* This handle controls per-process resources. */ 1161613a2f6bSGordon Ross (void) fcntl(fd, F_SETFD, FD_CLOEXEC); 1162613a2f6bSGordon Ross 11634bff34e3Sthurlow return (fd); 11644bff34e3Sthurlow } 11654bff34e3Sthurlow 1166613a2f6bSGordon Ross int 11674bff34e3Sthurlow smb_ctx_gethandle(struct smb_ctx *ctx) 11684bff34e3Sthurlow { 1169430b4c46SGordon Ross int fd, err; 1170430b4c46SGordon Ross uint32_t version; 11714bff34e3Sthurlow 1172613a2f6bSGordon Ross if (ctx->ct_dev_fd != -1) { 11734bff34e3Sthurlow rpc_cleanup_smbctx(ctx); 1174613a2f6bSGordon Ross close(ctx->ct_dev_fd); 1175613a2f6bSGordon Ross ctx->ct_dev_fd = -1; 11764bff34e3Sthurlow ctx->ct_flags &= ~SMBCF_SSNACTIVE; 11774bff34e3Sthurlow } 11784bff34e3Sthurlow 11794bff34e3Sthurlow fd = smb_open_driver(); 1180430b4c46SGordon Ross if (fd < 0) { 1181430b4c46SGordon Ross err = errno; 1182430b4c46SGordon Ross smb_error(dgettext(TEXT_DOMAIN, 1183430b4c46SGordon Ross "failed to open driver"), err); 1184430b4c46SGordon Ross return (err); 1185430b4c46SGordon Ross } 1186430b4c46SGordon Ross 1187430b4c46SGordon Ross /* 1188430b4c46SGordon Ross * Check the driver version (paranoia) 1189430b4c46SGordon Ross */ 1190430b4c46SGordon Ross if (ioctl(fd, SMBIOC_GETVERS, &version) < 0) 1191430b4c46SGordon Ross version = 0; 1192430b4c46SGordon Ross if (version != NSMB_VERSION) { 1193430b4c46SGordon Ross smb_error(dgettext(TEXT_DOMAIN, 1194430b4c46SGordon Ross "incorrect driver version"), 0); 1195430b4c46SGordon Ross close(fd); 11964bff34e3Sthurlow return (ENODEV); 1197430b4c46SGordon Ross } 11984bff34e3Sthurlow 1199613a2f6bSGordon Ross ctx->ct_dev_fd = fd; 12004bff34e3Sthurlow return (0); 12014bff34e3Sthurlow } 12024bff34e3Sthurlow 1203613a2f6bSGordon Ross 1204613a2f6bSGordon Ross /* 1205613a2f6bSGordon Ross * Find or create a connection + logon session 1206613a2f6bSGordon Ross */ 12074bff34e3Sthurlow int 1208613a2f6bSGordon Ross smb_ctx_get_ssn(struct smb_ctx *ctx) 12094bff34e3Sthurlow { 1210613a2f6bSGordon Ross int err = 0; 12114bff34e3Sthurlow 1212613a2f6bSGordon Ross if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) 12134bff34e3Sthurlow return (EINVAL); 12144bff34e3Sthurlow 1215613a2f6bSGordon Ross if (ctx->ct_dev_fd < 0) { 1216613a2f6bSGordon Ross if ((err = smb_ctx_gethandle(ctx))) 1217613a2f6bSGordon Ross return (err); 12184bff34e3Sthurlow } 12194bff34e3Sthurlow 12204bff34e3Sthurlow /* 1221613a2f6bSGordon Ross * Check whether the driver already has a VC 1222613a2f6bSGordon Ross * we can use. If so, we're done! 12234bff34e3Sthurlow */ 1224613a2f6bSGordon Ross err = smb_ctx_findvc(ctx); 1225613a2f6bSGordon Ross if (err == 0) { 1226613a2f6bSGordon Ross DPRINT("found an existing VC"); 12274bff34e3Sthurlow } else { 1228613a2f6bSGordon Ross /* 1229613a2f6bSGordon Ross * This calls the IOD to create a new session. 1230613a2f6bSGordon Ross */ 1231613a2f6bSGordon Ross DPRINT("setup a new VC"); 1232613a2f6bSGordon Ross err = smb_ctx_newvc(ctx); 1233613a2f6bSGordon Ross if (err != 0) 1234613a2f6bSGordon Ross return (err); 1235613a2f6bSGordon Ross 1236613a2f6bSGordon Ross /* 1237613a2f6bSGordon Ross * Call findvc again. The new VC sould be 1238613a2f6bSGordon Ross * found in the driver this time. 1239613a2f6bSGordon Ross */ 1240613a2f6bSGordon Ross err = smb_ctx_findvc(ctx); 12414bff34e3Sthurlow } 1242613a2f6bSGordon Ross 1243613a2f6bSGordon Ross return (err); 12444bff34e3Sthurlow } 12454bff34e3Sthurlow 12464bff34e3Sthurlow /* 1247613a2f6bSGordon Ross * Find or create a tree connection 12484bff34e3Sthurlow */ 1249613a2f6bSGordon Ross int 1250613a2f6bSGordon Ross smb_ctx_get_tree(struct smb_ctx *ctx) 1251613a2f6bSGordon Ross { 1252613a2f6bSGordon Ross smbioc_tcon_t *tcon = NULL; 1253613a2f6bSGordon Ross int cmd, err = 0; 1254613a2f6bSGordon Ross 1255613a2f6bSGordon Ross if (ctx->ct_dev_fd < 0 || 1256613a2f6bSGordon Ross ctx->ct_origshare == NULL) { 1257613a2f6bSGordon Ross return (EINVAL); 1258613a2f6bSGordon Ross } 1259613a2f6bSGordon Ross 1260613a2f6bSGordon Ross cmd = SMBIOC_TREE_CONNECT; 1261613a2f6bSGordon Ross tcon = malloc(sizeof (*tcon)); 1262613a2f6bSGordon Ross if (tcon == NULL) 1263613a2f6bSGordon Ross return (ENOMEM); 1264613a2f6bSGordon Ross bzero(tcon, sizeof (*tcon)); 1265613a2f6bSGordon Ross tcon->tc_flags = SMBLK_CREATE; 1266613a2f6bSGordon Ross tcon->tc_opt = 0; 1267613a2f6bSGordon Ross 1268613a2f6bSGordon Ross /* The share name */ 1269613a2f6bSGordon Ross strlcpy(tcon->tc_sh.sh_name, ctx->ct_origshare, 1270613a2f6bSGordon Ross sizeof (tcon->tc_sh.sh_name)); 1271613a2f6bSGordon Ross 1272613a2f6bSGordon Ross /* The share "use" type. */ 1273430b4c46SGordon Ross tcon->tc_sh.sh_use = ctx->ct_shtype_req; 1274613a2f6bSGordon Ross 1275613a2f6bSGordon Ross /* 1276613a2f6bSGordon Ross * Todo: share passwords for share-level security. 1277613a2f6bSGordon Ross * 1278613a2f6bSGordon Ross * The driver does the actual TCON call. 1279613a2f6bSGordon Ross */ 1280613a2f6bSGordon Ross if (ioctl(ctx->ct_dev_fd, cmd, tcon) == -1) { 1281613a2f6bSGordon Ross err = errno; 12824bff34e3Sthurlow goto out; 12834bff34e3Sthurlow } 1284613a2f6bSGordon Ross 1285613a2f6bSGordon Ross /* 1286613a2f6bSGordon Ross * Check the returned share type 1287613a2f6bSGordon Ross */ 1288430b4c46SGordon Ross DPRINT("ret. sh_type: \"%d\"", tcon->tc_sh.sh_type); 1289613a2f6bSGordon Ross if (ctx->ct_shtype_req != USE_WILDCARD && 1290430b4c46SGordon Ross ctx->ct_shtype_req != tcon->tc_sh.sh_type) { 1291613a2f6bSGordon Ross smb_error(dgettext(TEXT_DOMAIN, 1292613a2f6bSGordon Ross "%s: incompatible share type"), 1293613a2f6bSGordon Ross 0, ctx->ct_origshare); 12944bff34e3Sthurlow } 12954bff34e3Sthurlow 12964bff34e3Sthurlow out: 1297613a2f6bSGordon Ross if (tcon != NULL) 1298613a2f6bSGordon Ross free(tcon); 1299613a2f6bSGordon Ross 1300613a2f6bSGordon Ross return (err); 13014bff34e3Sthurlow } 13024bff34e3Sthurlow 13034bff34e3Sthurlow /* 13044bff34e3Sthurlow * Return the hflags2 word for an smb_ctx. 13054bff34e3Sthurlow */ 13064bff34e3Sthurlow int 13074bff34e3Sthurlow smb_ctx_flags2(struct smb_ctx *ctx) 13084bff34e3Sthurlow { 13094bff34e3Sthurlow uint16_t flags2; 13104bff34e3Sthurlow 1311613a2f6bSGordon Ross if (ioctl(ctx->ct_dev_fd, SMBIOC_FLAGS2, &flags2) == -1) { 13124bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 13134bff34e3Sthurlow "can't get flags2 for a session"), errno); 13144bff34e3Sthurlow return (-1); 13154bff34e3Sthurlow } 13164bff34e3Sthurlow return (flags2); 13174bff34e3Sthurlow } 13184bff34e3Sthurlow 13194bff34e3Sthurlow /* 1320613a2f6bSGordon Ross * Get the transport level session key. 1321613a2f6bSGordon Ross * Must already have an active SMB session. 1322613a2f6bSGordon Ross */ 1323613a2f6bSGordon Ross int 1324430b4c46SGordon Ross smb_fh_getssnkey(int dev_fd, uchar_t *key, size_t len) 1325613a2f6bSGordon Ross { 1326613a2f6bSGordon Ross if (len < SMBIOC_HASH_SZ) 1327613a2f6bSGordon Ross return (EINVAL); 1328613a2f6bSGordon Ross 1329430b4c46SGordon Ross if (ioctl(dev_fd, SMBIOC_GETSSNKEY, key) == -1) 1330613a2f6bSGordon Ross return (errno); 1331613a2f6bSGordon Ross 1332613a2f6bSGordon Ross return (0); 1333613a2f6bSGordon Ross } 1334613a2f6bSGordon Ross 1335613a2f6bSGordon Ross /* 1336613a2f6bSGordon Ross * RC file parsing stuff 1337613a2f6bSGordon Ross */ 1338613a2f6bSGordon Ross 133915359501SGordon Ross static struct nv 134015359501SGordon Ross minauth_table[] = { 1341613a2f6bSGordon Ross /* Allowed auth. types */ 1342613a2f6bSGordon Ross { "kerberos", SMB_AT_KRB5 }, 1343613a2f6bSGordon Ross { "ntlmv2", SMB_AT_KRB5|SMB_AT_NTLM2 }, 1344613a2f6bSGordon Ross { "ntlm", SMB_AT_KRB5|SMB_AT_NTLM2|SMB_AT_NTLM1 }, 1345613a2f6bSGordon Ross { "lm", SMB_AT_KRB5|SMB_AT_NTLM2|SMB_AT_NTLM1|SMB_AT_LM1 }, 1346613a2f6bSGordon Ross { "none", SMB_AT_KRB5|SMB_AT_NTLM2|SMB_AT_NTLM1|SMB_AT_LM1| 1347613a2f6bSGordon Ross SMB_AT_ANON }, 1348613a2f6bSGordon Ross { NULL } 1349613a2f6bSGordon Ross }; 1350613a2f6bSGordon Ross 1351613a2f6bSGordon Ross 1352613a2f6bSGordon Ross /* 13534bff34e3Sthurlow * level values: 13544bff34e3Sthurlow * 0 - default 13554bff34e3Sthurlow * 1 - server 13564bff34e3Sthurlow * 2 - server:user 13574bff34e3Sthurlow * 3 - server:user:share 13584bff34e3Sthurlow */ 13594bff34e3Sthurlow static int 13604bff34e3Sthurlow smb_ctx_readrcsection(struct smb_ctx *ctx, const char *sname, int level) 13614bff34e3Sthurlow { 13624bff34e3Sthurlow char *p; 13634bff34e3Sthurlow int error; 13644bff34e3Sthurlow 1365613a2f6bSGordon Ross #ifdef KICONV_SUPPORT 13664bff34e3Sthurlow if (level > 0) { 13674bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "charsets", &p); 13684bff34e3Sthurlow if (p) { 13694bff34e3Sthurlow error = smb_ctx_setcharset(ctx, p); 13704bff34e3Sthurlow if (error) 13714bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 13724bff34e3Sthurlow "charset specification in the section '%s' ignored"), 13734bff34e3Sthurlow error, sname); 13744bff34e3Sthurlow } 13754bff34e3Sthurlow } 13764bff34e3Sthurlow #endif 13774bff34e3Sthurlow 13784bff34e3Sthurlow if (level <= 1) { 13794bff34e3Sthurlow /* Section is: [default] or [server] */ 13804bff34e3Sthurlow 13814bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "minauth", &p); 13824bff34e3Sthurlow if (p) { 13834bff34e3Sthurlow /* 13844bff34e3Sthurlow * "minauth" was set in this section; override 13854bff34e3Sthurlow * the current minimum authentication setting. 13864bff34e3Sthurlow */ 1387613a2f6bSGordon Ross struct nv *nvp; 1388613a2f6bSGordon Ross for (nvp = minauth_table; nvp->name; nvp++) 1389613a2f6bSGordon Ross if (strcmp(p, nvp->name) == 0) 1390613a2f6bSGordon Ross break; 1391613a2f6bSGordon Ross if (nvp->name) 1392613a2f6bSGordon Ross ctx->ct_minauth = nvp->value; 1393613a2f6bSGordon Ross else { 13944bff34e3Sthurlow /* 13954bff34e3Sthurlow * Unknown minimum authentication level. 13964bff34e3Sthurlow */ 13974bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 13984bff34e3Sthurlow "invalid minimum authentication level \"%s\" specified in the section %s"), 13994bff34e3Sthurlow 0, p, sname); 14004bff34e3Sthurlow return (EINVAL); 14014bff34e3Sthurlow } 14024bff34e3Sthurlow } 14034bff34e3Sthurlow 14049c9af259SGordon Ross rc_getstringptr(smb_rc, sname, "signing", &p); 14059c9af259SGordon Ross if (p) { 14069c9af259SGordon Ross /* 14079c9af259SGordon Ross * "signing" was set in this section; override 140802d09e03SGordon Ross * the current signing settings. Note: 140902d09e03SGordon Ross * setsigning flags are: enable, require 14109c9af259SGordon Ross */ 14119c9af259SGordon Ross if (strcmp(p, "disabled") == 0) { 141202d09e03SGordon Ross (void) smb_ctx_setsigning(ctx, FALSE, FALSE); 14139c9af259SGordon Ross } else if (strcmp(p, "enabled") == 0) { 141402d09e03SGordon Ross (void) smb_ctx_setsigning(ctx, TRUE, FALSE); 14159c9af259SGordon Ross } else if (strcmp(p, "required") == 0) { 141602d09e03SGordon Ross (void) smb_ctx_setsigning(ctx, TRUE, TRUE); 14179c9af259SGordon Ross } else { 14189c9af259SGordon Ross /* 14199c9af259SGordon Ross * Unknown "signing" value. 14209c9af259SGordon Ross */ 14219c9af259SGordon Ross smb_error(dgettext(TEXT_DOMAIN, 14229c9af259SGordon Ross "invalid signing policy \"%s\" specified in the section %s"), 14239c9af259SGordon Ross 0, p, sname); 14249c9af259SGordon Ross return (EINVAL); 14259c9af259SGordon Ross } 14269c9af259SGordon Ross } 14279c9af259SGordon Ross 14284bff34e3Sthurlow /* 14294bff34e3Sthurlow * Domain name. Allow both keywords: 14304bff34e3Sthurlow * "workgroup", "domain" 14314bff34e3Sthurlow * 14324bff34e3Sthurlow * Note: these are NOT marked "from CMD". 14334bff34e3Sthurlow * See long comment at smb_ctx_init() 14344bff34e3Sthurlow */ 14354bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "workgroup", &p); 14364bff34e3Sthurlow if (p) { 1437613a2f6bSGordon Ross error = smb_ctx_setdomain(ctx, p, 0); 14384bff34e3Sthurlow if (error) 14394bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 14404bff34e3Sthurlow "workgroup specification in the " 14414bff34e3Sthurlow "section '%s' ignored"), error, sname); 14424bff34e3Sthurlow } 14434bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "domain", &p); 14444bff34e3Sthurlow if (p) { 1445613a2f6bSGordon Ross error = smb_ctx_setdomain(ctx, p, 0); 14464bff34e3Sthurlow if (error) 14474bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 14484bff34e3Sthurlow "domain specification in the " 14494bff34e3Sthurlow "section '%s' ignored"), error, sname); 14504bff34e3Sthurlow } 14514bff34e3Sthurlow 14524bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "user", &p); 14534bff34e3Sthurlow if (p) { 14544bff34e3Sthurlow error = smb_ctx_setuser(ctx, p, 0); 14554bff34e3Sthurlow if (error) 14564bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 14574bff34e3Sthurlow "user specification in the " 14584bff34e3Sthurlow "section '%s' ignored"), error, sname); 14594bff34e3Sthurlow } 14604bff34e3Sthurlow } 14614bff34e3Sthurlow 14624bff34e3Sthurlow if (level == 1) { 14634bff34e3Sthurlow /* Section is: [server] */ 14644bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "addr", &p); 14654bff34e3Sthurlow if (p) { 14664bff34e3Sthurlow error = smb_ctx_setsrvaddr(ctx, p); 14674bff34e3Sthurlow if (error) { 14684bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 14694bff34e3Sthurlow "invalid address specified in section %s"), 14704bff34e3Sthurlow 0, sname); 14714bff34e3Sthurlow return (error); 14724bff34e3Sthurlow } 14734bff34e3Sthurlow } 14744bff34e3Sthurlow } 14754bff34e3Sthurlow 14764bff34e3Sthurlow rc_getstringptr(smb_rc, sname, "password", &p); 14774bff34e3Sthurlow if (p) { 14784bff34e3Sthurlow error = smb_ctx_setpassword(ctx, p, 0); 14794bff34e3Sthurlow if (error) 14804bff34e3Sthurlow smb_error(dgettext(TEXT_DOMAIN, 14814bff34e3Sthurlow "password specification in the section '%s' ignored"), 14824bff34e3Sthurlow error, sname); 14834bff34e3Sthurlow } 14844bff34e3Sthurlow 14854bff34e3Sthurlow return (0); 14864bff34e3Sthurlow } 14874bff34e3Sthurlow 14884bff34e3Sthurlow /* 14894bff34e3Sthurlow * read rc file as follows: 14904bff34e3Sthurlow * 0: read [default] section 14914bff34e3Sthurlow * 1: override with [server] section 14924bff34e3Sthurlow * 2: override with [server:user] section 14934bff34e3Sthurlow * 3: override with [server:user:share] section 14944bff34e3Sthurlow * Since absence of rcfile is not fatal, silently ignore this fact. 14954bff34e3Sthurlow * smb_rc file should be closed by caller. 14964bff34e3Sthurlow */ 14974bff34e3Sthurlow int 14984bff34e3Sthurlow smb_ctx_readrc(struct smb_ctx *ctx) 14994bff34e3Sthurlow { 1500430b4c46SGordon Ross char pwbuf[NSS_BUFLEN_PASSWD]; 1501430b4c46SGordon Ross struct passwd pw; 1502613a2f6bSGordon Ross char *sname = NULL; 1503613a2f6bSGordon Ross int sname_max; 1504613a2f6bSGordon Ross int err = 0; 15054bff34e3Sthurlow 1506430b4c46SGordon Ross /* 1507430b4c46SGordon Ross * If the user name is not specified some other way, 1508430b4c46SGordon Ross * use the current user name. Also save the homedir. 1509430b4c46SGordon Ross * NB: ct_home=NULL is allowed, and we don't want to 1510430b4c46SGordon Ross * bail out with an error for a missing ct_home. 1511430b4c46SGordon Ross */ 1512430b4c46SGordon Ross if (getpwuid_r(getuid(), &pw, pwbuf, sizeof (pwbuf)) != NULL) { 1513430b4c46SGordon Ross if (ctx->ct_user[0] == 0) 1514430b4c46SGordon Ross (void) smb_ctx_setuser(ctx, pw.pw_name, B_FALSE); 1515430b4c46SGordon Ross if (ctx->ct_home == NULL) 1516430b4c46SGordon Ross ctx->ct_home = strdup(pw.pw_dir); 1517430b4c46SGordon Ross } 1518430b4c46SGordon Ross 1519430b4c46SGordon Ross if ((err = smb_open_rcfile(ctx->ct_home)) != 0) { 1520613a2f6bSGordon Ross DPRINT("smb_open_rcfile, err=%d", err); 1521613a2f6bSGordon Ross /* ignore any error here */ 1522613a2f6bSGordon Ross return (0); 1523613a2f6bSGordon Ross } 1524613a2f6bSGordon Ross 1525613a2f6bSGordon Ross sname_max = 3 * SMBIOC_MAX_NAME + 4; 1526613a2f6bSGordon Ross sname = malloc(sname_max); 1527613a2f6bSGordon Ross if (sname == NULL) { 1528613a2f6bSGordon Ross err = ENOMEM; 15294bff34e3Sthurlow goto done; 1530613a2f6bSGordon Ross } 15314bff34e3Sthurlow 15324bff34e3Sthurlow /* 15334bff34e3Sthurlow * default parameters (level=0) 15344bff34e3Sthurlow */ 15354bff34e3Sthurlow smb_ctx_readrcsection(ctx, "default", 0); 15364bff34e3Sthurlow nb_ctx_readrcsection(smb_rc, ctx->ct_nb, "default", 0); 15374bff34e3Sthurlow 15384bff34e3Sthurlow /* 15394bff34e3Sthurlow * If we don't have a server name, we can't read any of the 15404bff34e3Sthurlow * [server...] sections. 15414bff34e3Sthurlow */ 1542613a2f6bSGordon Ross if (ctx->ct_fullserver == NULL) 15434bff34e3Sthurlow goto done; 15444bff34e3Sthurlow /* 15454bff34e3Sthurlow * SERVER parameters. 15464bff34e3Sthurlow */ 1547613a2f6bSGordon Ross smb_ctx_readrcsection(ctx, ctx->ct_fullserver, 1); 15484bff34e3Sthurlow 15494bff34e3Sthurlow /* 15504bff34e3Sthurlow * If we don't have a user name, we can't read any of the 15514bff34e3Sthurlow * [server:user...] sections. 15524bff34e3Sthurlow */ 1553613a2f6bSGordon Ross if (ctx->ct_user[0] == 0) 15544bff34e3Sthurlow goto done; 15554bff34e3Sthurlow /* 15564bff34e3Sthurlow * SERVER:USER parameters 15574bff34e3Sthurlow */ 1558613a2f6bSGordon Ross snprintf(sname, sname_max, "%s:%s", 1559613a2f6bSGordon Ross ctx->ct_fullserver, 1560613a2f6bSGordon Ross ctx->ct_user); 15614bff34e3Sthurlow smb_ctx_readrcsection(ctx, sname, 2); 15624bff34e3Sthurlow 1563613a2f6bSGordon Ross 15644bff34e3Sthurlow /* 15654bff34e3Sthurlow * If we don't have a share name, we can't read any of the 15664bff34e3Sthurlow * [server:user:share] sections. 15674bff34e3Sthurlow */ 1568613a2f6bSGordon Ross if (ctx->ct_origshare == NULL) 1569613a2f6bSGordon Ross goto done; 15704bff34e3Sthurlow /* 15714bff34e3Sthurlow * SERVER:USER:SHARE parameters 15724bff34e3Sthurlow */ 1573613a2f6bSGordon Ross snprintf(sname, sname_max, "%s:%s:%s", 1574613a2f6bSGordon Ross ctx->ct_fullserver, 1575613a2f6bSGordon Ross ctx->ct_user, 1576613a2f6bSGordon Ross ctx->ct_origshare); 15774bff34e3Sthurlow smb_ctx_readrcsection(ctx, sname, 3); 15784bff34e3Sthurlow 15794bff34e3Sthurlow done: 1580613a2f6bSGordon Ross if (sname) 1581613a2f6bSGordon Ross free(sname); 1582613a2f6bSGordon Ross smb_close_rcfile(); 15834bff34e3Sthurlow if (smb_debug) 15844bff34e3Sthurlow dump_ctx("after smb_ctx_readrc", ctx); 1585613a2f6bSGordon Ross if (err) 1586613a2f6bSGordon Ross DPRINT("err=%d\n", err); 15874bff34e3Sthurlow 1588613a2f6bSGordon Ross return (err); 15894bff34e3Sthurlow } 1590430b4c46SGordon Ross 1591430b4c46SGordon Ross void 1592430b4c46SGordon Ross smbfs_set_default_domain(const char *domain) 1593430b4c46SGordon Ross { 1594430b4c46SGordon Ross strlcpy(default_domain, domain, sizeof (default_domain)); 1595430b4c46SGordon Ross } 1596430b4c46SGordon Ross 1597430b4c46SGordon Ross void 1598430b4c46SGordon Ross smbfs_set_default_user(const char *user) 1599430b4c46SGordon Ross { 1600430b4c46SGordon Ross strlcpy(default_user, user, sizeof (default_user)); 1601430b4c46SGordon Ross } 1602