xref: /freebsd/contrib/smbfs/lib/smb/ctx.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*
2  * Copyright (c) 2000-2002, Boris Popov
3  * All rights reserved.
4  *
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.
19  *
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.
31  *
32  * $Id: ctx.c,v 1.24 2002/04/13 14:35:28 bp Exp $
33  * $FreeBSD$
34  */
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39 #include <sys/mount.h>
40 #include <fcntl.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <pwd.h>
47 #include <grp.h>
48 #include <unistd.h>
49 #include <sys/iconv.h>
50 
51 #define NB_NEEDRESOLVER
52 
53 #include <netsmb/smb_lib.h>
54 #include <netsmb/netbios.h>
55 #include <netsmb/nb_lib.h>
56 #include <netsmb/smb_conn.h>
57 #include <cflib.h>
58 
59 /*
60  * Prescan command line for [-U user] argument
61  * and fill context with defaults
62  */
63 int
64 smb_ctx_init(struct smb_ctx *ctx, int argc, char *argv[],
65 	int minlevel, int maxlevel, int sharetype)
66 {
67 	int  opt, error = 0;
68 	uid_t euid;
69 	const char *arg, *cp;
70 	struct passwd *pwd;
71 
72 	bzero(ctx,sizeof(*ctx));
73 	error = nb_ctx_create(&ctx->ct_nb);
74 	if (error)
75 		return error;
76 	ctx->ct_fd = -1;
77 	ctx->ct_parsedlevel = SMBL_NONE;
78 	ctx->ct_minlevel = minlevel;
79 	ctx->ct_maxlevel = maxlevel;
80 	ctx->ct_smbtcpport = SMB_TCP_PORT;
81 
82 	ctx->ct_ssn.ioc_opt = SMBVOPT_CREATE;
83 	ctx->ct_ssn.ioc_timeout = 15;
84 	ctx->ct_ssn.ioc_retrycount = 4;
85 	ctx->ct_ssn.ioc_owner = SMBM_ANY_OWNER;
86 	ctx->ct_ssn.ioc_group = SMBM_ANY_GROUP;
87 	ctx->ct_ssn.ioc_mode = SMBM_EXEC;
88 	ctx->ct_ssn.ioc_rights = SMBM_DEFAULT;
89 
90 	ctx->ct_sh.ioc_opt = SMBVOPT_CREATE;
91 	ctx->ct_sh.ioc_owner = SMBM_ANY_OWNER;
92 	ctx->ct_sh.ioc_group = SMBM_ANY_GROUP;
93 	ctx->ct_sh.ioc_mode = SMBM_EXEC;
94 	ctx->ct_sh.ioc_rights = SMBM_DEFAULT;
95 	ctx->ct_sh.ioc_owner = SMBM_ANY_OWNER;
96 	ctx->ct_sh.ioc_group = SMBM_ANY_GROUP;
97 
98 	nb_ctx_setscope(ctx->ct_nb, "");
99 	euid = geteuid();
100 	if ((pwd = getpwuid(euid)) != NULL) {
101 		smb_ctx_setuser(ctx, pwd->pw_name);
102 		endpwent();
103 	} else if (euid == 0)
104 		smb_ctx_setuser(ctx, "root");
105 	else
106 		return 0;
107 	if (argv == NULL)
108 		return 0;
109 	for (opt = 1; opt < argc; opt++) {
110 		cp = argv[opt];
111 		if (strncmp(cp, "//", 2) != 0)
112 			continue;
113 		error = smb_ctx_parseunc(ctx, cp, sharetype, (const char**)&cp);
114 		if (error)
115 			return error;
116 		ctx->ct_uncnext = cp;
117 		break;
118 	}
119 	while (error == 0 && (opt = cf_getopt(argc, argv, ":E:L:U:")) != -1) {
120 		arg = cf_optarg;
121 		switch (opt) {
122 		    case 'E':
123 			error = smb_ctx_setcharset(ctx, arg);
124 			if (error)
125 				return error;
126 			break;
127 		    case 'L':
128 			error = nls_setlocale(arg);
129 			if (error)
130 				break;
131 			break;
132 		    case 'U':
133 			error = smb_ctx_setuser(ctx, arg);
134 			break;
135 		}
136 	}
137 	cf_optind = cf_optreset = 1;
138 	return error;
139 }
140 
141 void
142 smb_ctx_done(struct smb_ctx *ctx)
143 {
144 	if (ctx->ct_ssn.ioc_server)
145 		nb_snbfree(ctx->ct_ssn.ioc_server);
146 	if (ctx->ct_ssn.ioc_local)
147 		nb_snbfree(ctx->ct_ssn.ioc_local);
148 	if (ctx->ct_srvaddr)
149 		free(ctx->ct_srvaddr);
150 	if (ctx->ct_nb)
151 		nb_ctx_done(ctx->ct_nb);
152 }
153 
154 static int
155 getsubstring(const char *p, u_char sep, char *dest, int maxlen, const char **next)
156 {
157 	int len;
158 
159 	maxlen--;
160 	for (len = 0; len < maxlen && *p != sep; p++, len++, dest++) {
161 		if (*p == 0)
162 			return EINVAL;
163 		*dest = *p;
164 	}
165 	*dest = 0;
166 	*next = *p ? p + 1 : p;
167 	return 0;
168 }
169 
170 /*
171  * Here we expect something like "[proto:]//[user@]host[:psmb[:pnb]][/share][/path]"
172  */
173 int
174 smb_ctx_parseunc(struct smb_ctx *ctx, const char *unc, int sharetype,
175 	const char **next)
176 {
177 	const char *p = unc;
178 	char *p1, *psmb, *pnb;
179 	char tmp[1024];
180 	int error ;
181 
182 	ctx->ct_parsedlevel = SMBL_NONE;
183 	if (*p++ != '/' || *p++ != '/') {
184 		smb_error("UNC should start with '//'", 0);
185 		return EINVAL;
186 	}
187 	p1 = tmp;
188 	error = getsubstring(p, '@', p1, sizeof(tmp), &p);
189 	if (!error) {
190 		if (ctx->ct_maxlevel < SMBL_VC) {
191 			smb_error("no user name required", 0);
192 			return EINVAL;
193 		}
194 		error = smb_ctx_setuser(ctx, tmp);
195 		if (error)
196 			return error;
197 		ctx->ct_parsedlevel = SMBL_VC;
198 	}
199 	error = getsubstring(p, '/', p1, sizeof(tmp), &p);
200 	if (error) {
201 		error = getsubstring(p, '\0', p1, sizeof(tmp), &p);
202 		if (error) {
203 			smb_error("no server name found", 0);
204 			return error;
205 		}
206 	}
207 	if (*p1 == 0) {
208 		smb_error("empty server name", 0);
209 		return EINVAL;
210 	}
211 	/*
212 	 * Check for port number specification.
213 	 */
214 	psmb = strchr(tmp, ':');
215 	if (psmb) {
216 		*psmb++ = '\0';
217 		pnb = strchr(psmb, ':');
218 		if (pnb) {
219 			*pnb++ = '\0';
220 			error = smb_ctx_setnbport(ctx, atoi(pnb));
221 			if (error) {
222 				smb_error("Invalid NetBIOS port number", 0);
223 				return error;
224 			}
225 		}
226 		error = smb_ctx_setsmbport(ctx, atoi(psmb));
227 		if (error) {
228 			smb_error("Invalid SMB port number", 0);
229 			return error;
230 		}
231 	}
232 	error = smb_ctx_setserver(ctx, tmp);
233 	if (error)
234 		return error;
235 	if (sharetype == SMB_ST_NONE) {
236 		*next = p;
237 		return 0;
238 	}
239 	if (*p != 0 && ctx->ct_maxlevel < SMBL_SHARE) {
240 		smb_error("no share name required", 0);
241 		return EINVAL;
242 	}
243 	error = getsubstring(p, '/', p1, sizeof(tmp), &p);
244 	if (error) {
245 		error = getsubstring(p, '\0', p1, sizeof(tmp), &p);
246 		if (error) {
247 			smb_error("unexpected end of line", 0);
248 			return error;
249 		}
250 	}
251 	if (*p1 == 0 && ctx->ct_minlevel >= SMBL_SHARE) {
252 		smb_error("empty share name", 0);
253 		return EINVAL;
254 	}
255 	*next = p;
256 	if (*p1 == 0)
257 		return 0;
258 	error = smb_ctx_setshare(ctx, p1, sharetype);
259 	return error;
260 }
261 
262 int
263 smb_ctx_setcharset(struct smb_ctx *ctx, const char *arg)
264 {
265 	char *cp, *servercs, *localcs;
266 	int cslen = sizeof(ctx->ct_ssn.ioc_localcs);
267 	int scslen, lcslen, error;
268 
269 	cp = strchr(arg, ':');
270 	lcslen = cp ? (cp - arg) : 0;
271 	if (lcslen == 0 || lcslen >= cslen) {
272 		smb_error("invalid local charset specification (%s)", 0, arg);
273 		return EINVAL;
274 	}
275 	scslen = (size_t)strlen(++cp);
276 	if (scslen == 0 || scslen >= cslen) {
277 		smb_error("invalid server charset specification (%s)", 0, arg);
278 		return EINVAL;
279 	}
280 	localcs = memcpy(ctx->ct_ssn.ioc_localcs, arg, lcslen);
281 	localcs[lcslen] = 0;
282 	servercs = strcpy(ctx->ct_ssn.ioc_servercs, cp);
283 	error = nls_setrecode(localcs, servercs);
284 	if (error == 0)
285 		return 0;
286 	smb_error("can't initialize iconv support (%s:%s)",
287 	    error, localcs, servercs);
288 	localcs[0] = 0;
289 	servercs[0] = 0;
290 	return error;
291 }
292 
293 int
294 smb_ctx_setserver(struct smb_ctx *ctx, const char *name)
295 {
296 	if (strlen(name) > SMB_MAXSRVNAMELEN) {
297 		smb_error("server name '%s' too long", 0, name);
298 		return ENAMETOOLONG;
299 	}
300 	nls_str_upper(ctx->ct_ssn.ioc_srvname, name);
301 	return 0;
302 }
303 
304 int
305 smb_ctx_setnbport(struct smb_ctx *ctx, int port)
306 {
307 	if (port < 1 || port > 0xffff)
308 		return EINVAL;
309 	ctx->ct_nb->nb_nmbtcpport = port;
310 	return 0;
311 }
312 
313 int
314 smb_ctx_setsmbport(struct smb_ctx *ctx, int port)
315 {
316 	if (port < 1 || port > 0xffff)
317 		return EINVAL;
318 	ctx->ct_smbtcpport = port;
319 	ctx->ct_nb->nb_smbtcpport = port;
320 	return 0;
321 }
322 
323 int
324 smb_ctx_setuser(struct smb_ctx *ctx, const char *name)
325 {
326 	if (strlen(name) > SMB_MAXUSERNAMELEN) {
327 		smb_error("user name '%s' too long", 0, name);
328 		return ENAMETOOLONG;
329 	}
330 	nls_str_upper(ctx->ct_ssn.ioc_user, name);
331 	return 0;
332 }
333 
334 int
335 smb_ctx_setworkgroup(struct smb_ctx *ctx, const char *name)
336 {
337 	if (strlen(name) > SMB_MAXUSERNAMELEN) {
338 		smb_error("workgroup name '%s' too long", 0, name);
339 		return ENAMETOOLONG;
340 	}
341 	nls_str_upper(ctx->ct_ssn.ioc_workgroup, name);
342 	return 0;
343 }
344 
345 int
346 smb_ctx_setpassword(struct smb_ctx *ctx, const char *passwd)
347 {
348 	if (passwd == NULL)
349 		return EINVAL;
350 	if (strlen(passwd) > SMB_MAXPASSWORDLEN) {
351 		smb_error("password too long", 0);
352 		return ENAMETOOLONG;
353 	}
354 	if (strncmp(passwd, "$$1", 3) == 0)
355 		smb_simpledecrypt(ctx->ct_ssn.ioc_password, passwd);
356 	else
357 		strcpy(ctx->ct_ssn.ioc_password, passwd);
358 	strcpy(ctx->ct_sh.ioc_password, ctx->ct_ssn.ioc_password);
359 	return 0;
360 }
361 
362 int
363 smb_ctx_setshare(struct smb_ctx *ctx, const char *share, int stype)
364 {
365 	if (strlen(share) > SMB_MAXSHARENAMELEN) {
366 		smb_error("share name '%s' too long", 0, share);
367 		return ENAMETOOLONG;
368 	}
369 	nls_str_upper(ctx->ct_sh.ioc_share, share);
370 	if (share[0] != 0)
371 		ctx->ct_parsedlevel = SMBL_SHARE;
372 	ctx->ct_sh.ioc_stype = stype;
373 	return 0;
374 }
375 
376 int
377 smb_ctx_setsrvaddr(struct smb_ctx *ctx, const char *addr)
378 {
379 	if (addr == NULL || addr[0] == 0)
380 		return EINVAL;
381 	if (ctx->ct_srvaddr)
382 		free(ctx->ct_srvaddr);
383 	if ((ctx->ct_srvaddr = strdup(addr)) == NULL)
384 		return ENOMEM;
385 	return 0;
386 }
387 
388 static int
389 smb_parse_owner(char *pair, uid_t *uid, gid_t *gid)
390 {
391 	struct group *gr;
392 	struct passwd *pw;
393 	char *cp;
394 
395 	cp = strchr(pair, ':');
396 	if (cp) {
397 		*cp++ = '\0';
398 		if (*cp) {
399 			gr = getgrnam(cp);
400 			if (gr) {
401 				*gid = gr->gr_gid;
402 			} else
403 				smb_error("Invalid group name %s, ignored",
404 				    0, cp);
405 		}
406 	}
407 	if (*pair) {
408 		pw = getpwnam(pair);
409 		if (pw) {
410 			*uid = pw->pw_uid;
411 		} else
412 			smb_error("Invalid user name %s, ignored", 0, pair);
413 	}
414 	endpwent();
415 	return 0;
416 }
417 
418 int
419 smb_ctx_opt(struct smb_ctx *ctx, int opt, const char *arg)
420 {
421 	int error = 0;
422 	char *p, *cp;
423 
424 	switch(opt) {
425 	    case 'U':
426 		break;
427 	    case 'I':
428 		error = smb_ctx_setsrvaddr(ctx, arg);
429 		break;
430 	    case 'M':
431 		ctx->ct_ssn.ioc_rights = strtol(arg, &cp, 8);
432 		if (*cp == '/') {
433 			ctx->ct_sh.ioc_rights = strtol(cp + 1, &cp, 8);
434 			ctx->ct_flags |= SMBCF_SRIGHTS;
435 		}
436 		break;
437 	    case 'N':
438 		ctx->ct_flags |= SMBCF_NOPWD;
439 		break;
440 	    case 'O':
441 		p = strdup(arg);
442 		cp = strchr(p, '/');
443 		if (cp) {
444 			*cp++ = '\0';
445 			error = smb_parse_owner(cp, &ctx->ct_sh.ioc_owner,
446 			    &ctx->ct_sh.ioc_group);
447 		}
448 		if (*p && error == 0) {
449 			error = smb_parse_owner(p, &ctx->ct_ssn.ioc_owner,
450 			    &ctx->ct_ssn.ioc_group);
451 		}
452 		free(p);
453 		break;
454 	    case 'P':
455 /*		ctx->ct_ssn.ioc_opt |= SMBCOPT_PERMANENT;*/
456 		break;
457 	    case 'R':
458 		ctx->ct_ssn.ioc_retrycount = atoi(arg);
459 		break;
460 	    case 'T':
461 		ctx->ct_ssn.ioc_timeout = atoi(arg);
462 		break;
463 	    case 'W':
464 		error = smb_ctx_setworkgroup(ctx, arg);
465 		break;
466 	}
467 	return error;
468 }
469 
470 #if 0
471 static void
472 smb_hexdump(const u_char *buf, int len) {
473 	int ofs = 0;
474 
475 	while (len--) {
476 		if (ofs % 16 == 0)
477 			printf("\n%02X: ", ofs);
478 		printf("%02x ", *buf++);
479 		ofs++;
480 	}
481 	printf("\n");
482 }
483 #endif
484 
485 
486 static int
487 smb_addiconvtbl(const char *to, const char *from, const u_char *tbl)
488 {
489 	int error;
490 
491 	error = kiconv_add_xlat_table(to, from, tbl);
492 	if (error && error != EEXIST) {
493 		smb_error("can not setup kernel iconv table (%s:%s)", error,
494 		    from, to);
495 		return error;
496 	}
497 	return 0;
498 }
499 
500 /*
501  * Verify context before connect operation(s),
502  * lookup specified server and try to fill all forgotten fields.
503  */
504 int
505 smb_ctx_resolve(struct smb_ctx *ctx)
506 {
507 	struct smbioc_ossn *ssn = &ctx->ct_ssn;
508 	struct smbioc_oshare *sh = &ctx->ct_sh;
509 	struct nb_name nn;
510 	struct sockaddr *sap;
511 	struct sockaddr_nb *salocal, *saserver;
512 	char *cp;
513 	int error = 0;
514 
515 	ctx->ct_flags &= ~SMBCF_RESOLVED;
516 	if (ssn->ioc_srvname[0] == 0) {
517 		smb_error("no server name specified", 0);
518 		return EINVAL;
519 	}
520 	if (ctx->ct_minlevel >= SMBL_SHARE && sh->ioc_share[0] == 0) {
521 		smb_error("no share name specified for %s@%s",
522 		    0, ssn->ioc_user, ssn->ioc_srvname);
523 		return EINVAL;
524 	}
525 	error = nb_ctx_resolve(ctx->ct_nb);
526 	if (error)
527 		return error;
528 	if (ssn->ioc_localcs[0] == 0)
529 		strcpy(ssn->ioc_localcs, "ISO8859-1");
530 	error = smb_addiconvtbl("tolower", ssn->ioc_localcs, nls_lower);
531 	if (error)
532 		return error;
533 	error = smb_addiconvtbl("toupper", ssn->ioc_localcs, nls_upper);
534 	if (error)
535 		return error;
536 	if (ssn->ioc_servercs[0] != 0) {
537 		error = kiconv_add_xlat16_cspairs
538 			(ssn->ioc_servercs, ssn->ioc_localcs);
539 		if (error) return error;
540 	}
541 	if (ctx->ct_srvaddr) {
542 		error = nb_resolvehost_in(ctx->ct_srvaddr, &sap, ctx->ct_smbtcpport);
543 	} else {
544 		error = nbns_resolvename(ssn->ioc_srvname, ctx->ct_nb, &sap);
545 	}
546 	if (error) {
547 		smb_error("can't get server address", error);
548 		return error;
549 	}
550 	nn.nn_scope = ctx->ct_nb->nb_scope;
551 	nn.nn_type = NBT_SERVER;
552 	if (strlen(ssn->ioc_srvname) > NB_NAMELEN)
553 		return NBERROR(NBERR_NAMETOOLONG);
554 	strlcpy(nn.nn_name, ssn->ioc_srvname, sizeof(nn.nn_name));
555 	error = nb_sockaddr(sap, &nn, &saserver);
556 	nb_snbfree(sap);
557 	if (error) {
558 		smb_error("can't allocate server address", error);
559 		return error;
560 	}
561 	ssn->ioc_server = (struct sockaddr*)saserver;
562 	if (ctx->ct_locname[0] == 0) {
563 		error = nb_getlocalname(ctx->ct_locname);
564 		if (error) {
565 			smb_error("can't get local name", error);
566 			return error;
567 		}
568 		nls_str_upper(ctx->ct_locname, ctx->ct_locname);
569 	}
570 	/*
571 	 * Truncate the local host name to NB_NAMELEN-1 which gives a
572 	 * suffix of 0 which is "workstation name".
573 	 */
574 	strlcpy(nn.nn_name, ctx->ct_locname, NB_NAMELEN);
575 	nn.nn_type = NBT_WKSTA;
576 	nn.nn_scope = ctx->ct_nb->nb_scope;
577 	error = nb_sockaddr(NULL, &nn, &salocal);
578 	if (error) {
579 		nb_snbfree((struct sockaddr*)saserver);
580 		smb_error("can't allocate local address", error);
581 		return error;
582 	}
583 	ssn->ioc_local = (struct sockaddr*)salocal;
584 	ssn->ioc_lolen = salocal->snb_len;
585 	ssn->ioc_svlen = saserver->snb_len;
586 	if (ssn->ioc_password[0] == 0 && (ctx->ct_flags & SMBCF_NOPWD) == 0) {
587 		cp = getpass("Password:");
588 		error = smb_ctx_setpassword(ctx, cp);
589 		if (error)
590 			return error;
591 	}
592 	ctx->ct_flags |= SMBCF_RESOLVED;
593 	return 0;
594 }
595 
596 static int
597 smb_ctx_gethandle(struct smb_ctx *ctx)
598 {
599 	int fd, i;
600 	char buf[20];
601 
602 	fd = open("/dev/"NSMB_NAME, O_RDWR);
603 	if (fd >= 0) {
604 		ctx->ct_fd = fd;
605 		return 0;
606 	}
607 	return ENOENT;
608 }
609 
610 int
611 smb_ctx_lookup(struct smb_ctx *ctx, int level, int flags)
612 {
613 	struct smbioc_lookup rq;
614 	int error;
615 
616 	if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) {
617 		smb_error("smb_ctx_lookup() data is not resolved", 0);
618 		return EINVAL;
619 	}
620 	if (ctx->ct_fd != -1) {
621 		close(ctx->ct_fd);
622 		ctx->ct_fd = -1;
623 	}
624 	error = smb_ctx_gethandle(ctx);
625 	if (error) {
626 		smb_error("can't get handle to requester (no /dev/"NSMB_NAME"* device)", 0);
627 		return EINVAL;
628 	}
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) {
635 		error = errno;
636 		if (flags & SMBLK_CREATE)
637 			smb_error("unable to open connection", error);
638 		return error;
639 	}
640 	return 0;
641 }
642 
643 int
644 smb_ctx_login(struct smb_ctx *ctx)
645 {
646 	struct smbioc_ossn *ssn = &ctx->ct_ssn;
647 	struct smbioc_oshare *sh = &ctx->ct_sh;
648 	int error;
649 
650 	if ((ctx->ct_flags & SMBCF_RESOLVED) == 0) {
651 		smb_error("smb_ctx_resolve() should be called first", 0);
652 		return EINVAL;
653 	}
654 	if (ctx->ct_fd != -1) {
655 		close(ctx->ct_fd);
656 		ctx->ct_fd = -1;
657 	}
658 	error = smb_ctx_gethandle(ctx);
659 	if (error) {
660 		smb_error("can't get handle to requester", 0);
661 		return EINVAL;
662 	}
663 	if (ioctl(ctx->ct_fd, SMBIOC_OPENSESSION, ssn) == -1) {
664 		error = errno;
665 		smb_error("can't open session to server %s", error, ssn->ioc_srvname);
666 		return error;
667 	}
668 	if (sh->ioc_share[0] == 0)
669 		return 0;
670 	if (ioctl(ctx->ct_fd, SMBIOC_OPENSHARE, sh) == -1) {
671 		error = errno;
672 		smb_error("can't connect to share //%s/%s", error,
673 		    ssn->ioc_srvname, sh->ioc_share);
674 		return error;
675 	}
676 	return 0;
677 }
678 
679 int
680 smb_ctx_setflags(struct smb_ctx *ctx, int level, int mask, int flags)
681 {
682 	struct smbioc_flags fl;
683 
684 	if (ctx->ct_fd == -1)
685 		return EINVAL;
686 	fl.ioc_level = level;
687 	fl.ioc_mask = mask;
688 	fl.ioc_flags = flags;
689 	if (ioctl(ctx->ct_fd, SMBIOC_SETFLAGS, &fl) == -1)
690 		return errno;
691 	return 0;
692 }
693 
694 /*
695  * level values:
696  * 0 - default
697  * 1 - server
698  * 2 - server:user
699  * 3 - server:user:share
700  */
701 static int
702 smb_ctx_readrcsection(struct smb_ctx *ctx, const char *sname, int level)
703 {
704 	char *p;
705 	int error;
706 
707 	if (level >= 0) {
708 		rc_getstringptr(smb_rc, sname, "charsets", &p);
709 		if (p) {
710 			error = smb_ctx_setcharset(ctx, p);
711 			if (error)
712 				smb_error("charset specification in the section '%s' ignored", error, sname);
713 		}
714 	}
715 	if (level <= 1) {
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);
718 	}
719 	if (level == 1) {
720 		rc_getstringptr(smb_rc, sname, "addr", &p);
721 		if (p) {
722 			error = smb_ctx_setsrvaddr(ctx, p);
723 			if (error) {
724 				smb_error("invalid address specified in the section %s", 0, sname);
725 				return error;
726 			}
727 		}
728 	}
729 	if (level >= 2) {
730 		rc_getstringptr(smb_rc, sname, "password", &p);
731 		if (p)
732 			smb_ctx_setpassword(ctx, p);
733 	}
734 	rc_getstringptr(smb_rc, sname, "workgroup", &p);
735 	if (p)
736 		smb_ctx_setworkgroup(ctx, p);
737 	return 0;
738 }
739 
740 /*
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.
747  */
748 int
749 smb_ctx_readrc(struct smb_ctx *ctx)
750 {
751 	char sname[SMB_MAXSRVNAMELEN + SMB_MAXUSERNAMELEN + SMB_MAXSHARENAMELEN + 4];
752 /*	char *p;*/
753 
754 	if (smb_open_rcfile() != 0)
755 		return 0;
756 
757 	if (ctx->ct_ssn.ioc_user[0] == 0 || ctx->ct_ssn.ioc_srvname[0] == 0)
758 		return 0;
759 
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);
764 	/*
765 	 * SERVER:USER parameters
766 	 */
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);
770 
771 	if (ctx->ct_sh.ioc_share[0] != 0) {
772 		/*
773 		 * SERVER:USER:SHARE parameters
774 	         */
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);
778 	}
779 	return 0;
780 }
781 
782