2 * Copyright (c) 2000-2002, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
32 * $Id: ctx.c,v 1.24 2002/04/13 14:35:28 bp Exp $
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
36 #include <sys/ioctl.h>
38 #include <sys/mount.h>
48 #include <sys/iconv.h>
50 #define NB_NEEDRESOLVER
52 #include <netsmb/smb_lib.h>
53 #include <netsmb/netbios.h>
54 #include <netsmb/nb_lib.h>
55 #include <netsmb/smb_conn.h>
59 * Prescan command line for [-U user] argument
60 * and fill context with defaults
63 smb_ctx_init(struct smb_ctx *ctx, int argc, char *argv[],
64 int minlevel, int maxlevel, int sharetype)
71 bzero(ctx,sizeof(*ctx));
72 error = nb_ctx_create(&ctx->ct_nb);
76 ctx->ct_parsedlevel = SMBL_NONE;
77 ctx->ct_minlevel = minlevel;
78 ctx->ct_maxlevel = maxlevel;
80 ctx->ct_ssn.ioc_opt = SMBVOPT_CREATE;
81 ctx->ct_ssn.ioc_timeout = 15;
82 ctx->ct_ssn.ioc_retrycount = 4;
83 ctx->ct_ssn.ioc_owner = SMBM_ANY_OWNER;
84 ctx->ct_ssn.ioc_group = SMBM_ANY_GROUP;
85 ctx->ct_ssn.ioc_mode = SMBM_EXEC;
86 ctx->ct_ssn.ioc_rights = SMBM_DEFAULT;
88 ctx->ct_sh.ioc_opt = SMBVOPT_CREATE;
89 ctx->ct_sh.ioc_owner = SMBM_ANY_OWNER;
90 ctx->ct_sh.ioc_group = SMBM_ANY_GROUP;
91 ctx->ct_sh.ioc_mode = SMBM_EXEC;
92 ctx->ct_sh.ioc_rights = SMBM_DEFAULT;
93 ctx->ct_sh.ioc_owner = SMBM_ANY_OWNER;
94 ctx->ct_sh.ioc_group = SMBM_ANY_GROUP;
96 nb_ctx_setscope(ctx->ct_nb, "");
98 if ((pwd = getpwuid(euid)) != NULL) {
99 smb_ctx_setuser(ctx, pwd->pw_name);
101 } else if (euid == 0)
102 smb_ctx_setuser(ctx, "root");
107 for (opt = 1; opt < argc; opt++) {
109 if (strncmp(cp, "//", 2) != 0)
111 error = smb_ctx_parseunc(ctx, cp, sharetype, (const char**)&cp);
114 ctx->ct_uncnext = cp;
117 while (error == 0 && (opt = cf_getopt(argc, argv, ":E:L:U:")) != -1) {
121 error = smb_ctx_setcharset(ctx, arg);
126 error = nls_setlocale(optarg);
131 error = smb_ctx_setuser(ctx, arg);
135 cf_optind = cf_optreset = 1;
140 smb_ctx_done(struct smb_ctx *ctx)
142 if (ctx->ct_ssn.ioc_server)
143 nb_snbfree(ctx->ct_ssn.ioc_server);
144 if (ctx->ct_ssn.ioc_local)
145 nb_snbfree(ctx->ct_ssn.ioc_local);
147 free(ctx->ct_srvaddr);
149 nb_ctx_done(ctx->ct_nb);
153 getsubstring(const char *p, u_char sep, char *dest, int maxlen, const char **next)
158 for (len = 0; len < maxlen && *p != sep; p++, len++, dest++) {
164 *next = *p ? p + 1 : p;
169 * Here we expect something like "[proto:]//[user@]host[/share][/path]"
172 smb_ctx_parseunc(struct smb_ctx *ctx, const char *unc, int sharetype,
180 ctx->ct_parsedlevel = SMBL_NONE;
181 if (*p++ != '/' || *p++ != '/') {
182 smb_error("UNC should start with '//'", 0);
186 error = getsubstring(p, '@', p1, sizeof(tmp), &p);
188 if (ctx->ct_maxlevel < SMBL_VC) {
189 smb_error("no user name required", 0);
193 smb_error("empty user name", 0);
196 error = smb_ctx_setuser(ctx, tmp);
199 ctx->ct_parsedlevel = SMBL_VC;
201 error = getsubstring(p, '/', p1, sizeof(tmp), &p);
203 error = getsubstring(p, '\0', p1, sizeof(tmp), &p);
205 smb_error("no server name found", 0);
210 smb_error("empty server name", 0);
213 error = smb_ctx_setserver(ctx, tmp);
216 if (sharetype == SMB_ST_NONE) {
220 if (*p != 0 && ctx->ct_maxlevel < SMBL_SHARE) {
221 smb_error("no share name required", 0);
224 error = getsubstring(p, '/', p1, sizeof(tmp), &p);
226 error = getsubstring(p, '\0', p1, sizeof(tmp), &p);
228 smb_error("unexpected end of line", 0);
232 if (*p1 == 0 && ctx->ct_minlevel >= SMBL_SHARE) {
233 smb_error("empty share name", 0);
239 error = smb_ctx_setshare(ctx, p1, sharetype);
244 smb_ctx_setcharset(struct smb_ctx *ctx, const char *arg)
246 char *cp, *servercs, *localcs;
247 int cslen = sizeof(ctx->ct_ssn.ioc_localcs);
248 int scslen, lcslen, error;
250 cp = strchr(arg, ':');
251 lcslen = cp ? (cp - arg) : 0;
252 if (lcslen == 0 || lcslen >= cslen) {
253 smb_error("invalid local charset specification (%s)", 0, arg);
256 scslen = (size_t)strlen(++cp);
257 if (scslen == 0 || scslen >= cslen) {
258 smb_error("invalid server charset specification (%s)", 0, arg);
261 localcs = memcpy(ctx->ct_ssn.ioc_localcs, arg, lcslen);
263 servercs = strcpy(ctx->ct_ssn.ioc_servercs, cp);
264 error = nls_setrecode(localcs, servercs);
267 smb_error("can't initialize iconv support (%s:%s)",
268 error, localcs, servercs);
275 smb_ctx_setserver(struct smb_ctx *ctx, const char *name)
277 if (strlen(name) >= SMB_MAXSRVNAMELEN) {
278 smb_error("server name '%s' too long", 0, name);
281 nls_str_upper(ctx->ct_ssn.ioc_srvname, name);
286 smb_ctx_setuser(struct smb_ctx *ctx, const char *name)
288 if (strlen(name) >= SMB_MAXUSERNAMELEN) {
289 smb_error("user name '%s' too long", 0, name);
292 nls_str_upper(ctx->ct_ssn.ioc_user, name);
297 smb_ctx_setworkgroup(struct smb_ctx *ctx, const char *name)
299 if (strlen(name) >= SMB_MAXUSERNAMELEN) {
300 smb_error("workgroup name '%s' too long", 0, name);
303 nls_str_upper(ctx->ct_ssn.ioc_workgroup, name);
308 smb_ctx_setpassword(struct smb_ctx *ctx, const char *passwd)
312 if (strlen(passwd) >= SMB_MAXPASSWORDLEN) {
313 smb_error("password too long", 0);
316 if (strncmp(passwd, "$$1", 3) == 0)
317 smb_simpledecrypt(ctx->ct_ssn.ioc_password, passwd);
319 strcpy(ctx->ct_ssn.ioc_password, passwd);
320 strcpy(ctx->ct_sh.ioc_password, ctx->ct_ssn.ioc_password);
325 smb_ctx_setshare(struct smb_ctx *ctx, const char *share, int stype)
327 if (strlen(share) >= SMB_MAXSHARENAMELEN) {
328 smb_error("share name '%s' too long", 0, share);
331 nls_str_upper(ctx->ct_sh.ioc_share, share);
333 ctx->ct_parsedlevel = SMBL_SHARE;
334 ctx->ct_sh.ioc_stype = stype;
339 smb_ctx_setsrvaddr(struct smb_ctx *ctx, const char *addr)
341 if (addr == NULL || addr[0] == 0)
344 free(ctx->ct_srvaddr);
345 if ((ctx->ct_srvaddr = strdup(addr)) == NULL)
351 smb_parse_owner(char *pair, uid_t *uid, gid_t *gid)
357 cp = strchr(pair, ':');
365 smb_error("Invalid group name %s, ignored",
374 smb_error("Invalid user name %s, ignored", 0, pair);
381 smb_ctx_opt(struct smb_ctx *ctx, int opt, const char *arg)
390 error = smb_ctx_setsrvaddr(ctx, arg);
393 ctx->ct_ssn.ioc_rights = strtol(arg, &cp, 8);
395 ctx->ct_sh.ioc_rights = strtol(cp + 1, &cp, 8);
396 ctx->ct_flags |= SMBCF_SRIGHTS;
400 ctx->ct_flags |= SMBCF_NOPWD;
407 error = smb_parse_owner(cp, &ctx->ct_sh.ioc_owner,
408 &ctx->ct_sh.ioc_group);
410 if (*p && error == 0) {
411 error = smb_parse_owner(cp, &ctx->ct_ssn.ioc_owner,
412 &ctx->ct_ssn.ioc_group);
417 /* ctx->ct_ssn.ioc_opt |= SMBCOPT_PERMANENT;*/
420 ctx->ct_ssn.ioc_retrycount = atoi(arg);
423 ctx->ct_ssn.ioc_timeout = atoi(arg);
426 error = smb_ctx_setworkgroup(ctx, arg);
434 smb_hexdump(const u_char *buf, int len) {
439 printf("\n%02X: ", ofs);
440 printf("%02x ", *buf++);
449 smb_addiconvtbl(const char *to, const char *from, const u_char *tbl)
453 error = kiconv_add_xlat_table(to, from, tbl);
454 if (error && error != EEXIST) {
455 smb_error("can not setup kernel iconv table (%s:%s)", error,
463 * Verify context before connect operation(s),
464 * lookup specified server and try to fill all forgotten fields.
467 smb_ctx_resolve(struct smb_ctx *ctx)
469 struct smbioc_ossn *ssn = &ctx->ct_ssn;
470 struct smbioc_oshare *sh = &ctx->ct_sh;
472 struct sockaddr *sap;
473 struct sockaddr_nb *salocal, *saserver;
479 ctx->ct_flags &= ~SMBCF_RESOLVED;
480 if (ssn->ioc_srvname[0] == 0) {
481 smb_error("no server name specified", 0);
484 if (ssn->ioc_user[0] == 0) {
485 smb_error("no user name specified for server %s",
486 0, ssn->ioc_srvname);
489 if (ctx->ct_minlevel >= SMBL_SHARE && sh->ioc_share[0] == 0) {
490 smb_error("no share name specified for %s@%s",
491 0, ssn->ioc_user, ssn->ioc_srvname);
494 error = nb_ctx_resolve(ctx->ct_nb);
497 if (ssn->ioc_localcs[0] == 0)
498 strcpy(ssn->ioc_localcs, "default"); /* XXX: locale name ? */
499 error = smb_addiconvtbl("tolower", ssn->ioc_localcs, nls_lower);
502 error = smb_addiconvtbl("toupper", ssn->ioc_localcs, nls_upper);
505 if (ssn->ioc_servercs[0] != 0) {
506 for(i = 0; i < sizeof(cstbl); i++)
508 nls_mem_toext(cstbl, cstbl, sizeof(cstbl));
509 error = smb_addiconvtbl(ssn->ioc_servercs, ssn->ioc_localcs, cstbl);
512 for(i = 0; i < sizeof(cstbl); i++)
514 nls_mem_toloc(cstbl, cstbl, sizeof(cstbl));
515 error = smb_addiconvtbl(ssn->ioc_localcs, ssn->ioc_servercs, cstbl);
519 if (ctx->ct_srvaddr) {
520 error = nb_resolvehost_in(ctx->ct_srvaddr, &sap);
522 error = nbns_resolvename(ssn->ioc_srvname, ctx->ct_nb, &sap);
525 smb_error("can't get server address", error);
528 nn.nn_scope = ctx->ct_nb->nb_scope;
529 nn.nn_type = NBT_SERVER;
530 strcpy(nn.nn_name, ssn->ioc_srvname);
531 error = nb_sockaddr(sap, &nn, &saserver);
534 smb_error("can't allocate server address", error);
537 ssn->ioc_server = (struct sockaddr*)saserver;
538 if (ctx->ct_locname[0] == 0) {
539 error = nb_getlocalname(ctx->ct_locname);
541 smb_error("can't get local name", error);
544 nls_str_upper(ctx->ct_locname, ctx->ct_locname);
546 strcpy(nn.nn_name, ctx->ct_locname);
547 nn.nn_type = NBT_WKSTA;
548 nn.nn_scope = ctx->ct_nb->nb_scope;
549 error = nb_sockaddr(NULL, &nn, &salocal);
551 nb_snbfree((struct sockaddr*)saserver);
552 smb_error("can't allocate local address", error);
555 ssn->ioc_local = (struct sockaddr*)salocal;
556 ssn->ioc_lolen = salocal->snb_len;
557 ssn->ioc_svlen = saserver->snb_len;
558 if (ssn->ioc_password[0] == 0 && (ctx->ct_flags & SMBCF_NOPWD) == 0) {
559 cp = getpass("Password:");
560 error = smb_ctx_setpassword(ctx, cp);
564 ctx->ct_flags |= SMBCF_RESOLVED;
569 smb_ctx_gethandle(struct smb_ctx *ctx)
575 * First, try to open as cloned device
577 fd = open("/dev/"NSMB_NAME, O_RDWR);
583 * well, no clone capabilities available - we have to scan
584 * all devices in order to get free one
586 for (i = 0; i < 1024; i++) {
587 snprintf(buf, sizeof(buf), "/dev/%s%d", NSMB_NAME, i);
588 fd = open(buf, O_RDWR);
595 * This is a compatibility with old /dev/net/nsmb device
597 for (i = 0; i < 1024; i++) {
598 snprintf(buf, sizeof(buf), "/dev/net/%s%d", NSMB_NAME, i);
599 fd = open(buf, O_RDWR);
611 smb_ctx_lookup(struct smb_ctx *ctx, int level, int flags)
613 struct smbioc_lookup rq;
616 if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) {
617 smb_error("smb_ctx_lookup() data is not resolved", 0);
620 if (ctx->ct_fd != -1) {
624 error = smb_ctx_gethandle(ctx);
626 smb_error("can't get handle to requester (no /dev/"NSMB_NAME"* device)", 0);
629 bzero(&rq, sizeof(rq));
630 bcopy(&ctx->ct_ssn, &rq.ioc_ssn, sizeof(struct smbioc_ossn));
631 bcopy(&ctx->ct_sh, &rq.ioc_sh, sizeof(struct smbioc_oshare));
632 rq.ioc_flags = flags;
633 rq.ioc_level = level;
634 if (ioctl(ctx->ct_fd, SMBIOC_LOOKUP, &rq) == -1) {
636 if (flags & SMBLK_CREATE)
637 smb_error("unable to open connection", error);
644 smb_ctx_login(struct smb_ctx *ctx)
646 struct smbioc_ossn *ssn = &ctx->ct_ssn;
647 struct smbioc_oshare *sh = &ctx->ct_sh;
650 if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) {
651 smb_error("smb_ctx_resolve() should be called first", 0);
654 if (ctx->ct_fd != -1) {
658 error = smb_ctx_gethandle(ctx);
660 smb_error("can't get handle to requester", 0);
663 if (ioctl(ctx->ct_fd, SMBIOC_OPENSESSION, ssn) == -1) {
665 smb_error("can't open session to server %s", error, ssn->ioc_srvname);
668 if (sh->ioc_share[0] == 0)
670 if (ioctl(ctx->ct_fd, SMBIOC_OPENSHARE, sh) == -1) {
672 smb_error("can't connect to share //%s/%s", error,
673 ssn->ioc_srvname, sh->ioc_share);
680 smb_ctx_setflags(struct smb_ctx *ctx, int level, int mask, int flags)
682 struct smbioc_flags fl;
684 if (ctx->ct_fd == -1)
686 fl.ioc_level = level;
688 fl.ioc_flags = flags;
689 if (ioctl(ctx->ct_fd, SMBIOC_SETFLAGS, &fl) == -1)
699 * 3 - server:user:share
702 smb_ctx_readrcsection(struct smb_ctx *ctx, const char *sname, int level)
708 rc_getstringptr(smb_rc, sname, "charsets", &p);
710 error = smb_ctx_setcharset(ctx, p);
712 smb_error("charset specification in the section '%s' ignored", error, sname);
716 rc_getint(smb_rc, sname, "timeout", &ctx->ct_ssn.ioc_timeout);
717 rc_getint(smb_rc, sname, "retry_count", &ctx->ct_ssn.ioc_retrycount);
720 rc_getstringptr(smb_rc, sname, "addr", &p);
722 error = smb_ctx_setsrvaddr(ctx, p);
724 smb_error("invalid address specified in the section %s", 0, sname);
730 rc_getstringptr(smb_rc, sname, "password", &p);
732 smb_ctx_setpassword(ctx, p);
734 rc_getstringptr(smb_rc, sname, "workgroup", &p);
736 smb_ctx_setworkgroup(ctx, p);
741 * read rc file as follows:
742 * 1. read [default] section
743 * 2. override with [server] section
744 * 3. override with [server:user:share] section
745 * Since abcence of rcfile is not fatal, silently ignore this fact.
746 * smb_rc file should be closed by caller.
749 smb_ctx_readrc(struct smb_ctx *ctx)
751 char sname[SMB_MAXSRVNAMELEN + SMB_MAXUSERNAMELEN + SMB_MAXSHARENAMELEN + 4];
754 if (smb_open_rcfile() != 0)
757 if (ctx->ct_ssn.ioc_user[0] == 0 || ctx->ct_ssn.ioc_srvname[0] == 0)
760 smb_ctx_readrcsection(ctx, "default", 0);
761 nb_ctx_readrcsection(smb_rc, ctx->ct_nb, "default", 0);
762 smb_ctx_readrcsection(ctx, ctx->ct_ssn.ioc_srvname, 1);
763 nb_ctx_readrcsection(smb_rc, ctx->ct_nb, ctx->ct_ssn.ioc_srvname, 1);
765 * SERVER:USER parameters
767 snprintf(sname, sizeof(sname), "%s:%s", ctx->ct_ssn.ioc_srvname,
768 ctx->ct_ssn.ioc_user);
769 smb_ctx_readrcsection(ctx, sname, 2);
771 if (ctx->ct_sh.ioc_share[0] != 0) {
773 * SERVER:USER:SHARE parameters
775 snprintf(sname, sizeof(sname), "%s:%s:%s", ctx->ct_ssn.ioc_srvname,
776 ctx->ct_ssn.ioc_user, ctx->ct_sh.ioc_share);
777 smb_ctx_readrcsection(ctx, sname, 3);