1/*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source.  A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12/*
13 * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
14 */
15
16/*
17 * smbutil "discon" sub-command to disconnect a session
18 * (mostly for usr/src/test/smbclient-tests)
19 */
20
21#include <sys/types.h>
22#include <errno.h>
23#include <stdio.h>
24#include <err.h>
25#include <unistd.h>
26#include <strings.h>
27#include <stdlib.h>
28#include <sysexits.h>
29#include <libintl.h>
30
31#include <netsmb/smb.h>
32#include <netsmb/smb_lib.h>
33#include "common.h"
34
35void
36discon_usage(void)
37{
38	printf(gettext("usage: smbutil discon [connection options] "
39	    "//[workgroup;][user@]server\n"));
40	exit(1);
41}
42
43int
44cmd_discon(int argc, char *argv[])
45{
46	struct smb_ctx *ctx;
47	int error, opt;
48
49	if (argc < 2)
50		discon_usage();
51
52	error = smb_ctx_alloc(&ctx);
53	if (error != 0)
54		return (error);
55
56	error = smb_ctx_scan_argv(ctx, argc, argv,
57	    SMBL_SERVER, SMBL_SERVER, USE_WILDCARD);
58	if (error != 0)
59		goto out;
60
61	error = smb_ctx_readrc(ctx);
62	if (error != 0)
63		goto out;
64
65	while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) {
66		if (opt == '?')
67			discon_usage();
68		error = smb_ctx_opt(ctx, opt, optarg);
69		if (error != 0)
70			goto out;
71	}
72
73	/*
74	 * Resolve the server address,
75	 * setup derived defaults.
76	 */
77	error = smb_ctx_resolve(ctx);
78	if (error != 0)
79		goto out;
80
81	/*
82	 * Have server, user, etc. from above:
83	 * smb_ctx_scan_argv, option settings.
84	 *
85	 * Lookup a session without creating.
86	 * (First part of smb_ctx_get_ssn)
87	 * If we find the session, kill it.
88	 */
89	error = smb_ctx_findvc(ctx);
90	if (error == ENOENT) {
91		/* Already gone. We're done. */
92		if (smb_debug)
93			fprintf(stderr, "session not found\n");
94		error = 0;
95		goto out;
96	}
97	if (error == 0) {
98		/* Found session.  Kill it. */
99		error = smb_ctx_kill(ctx);
100	}
101
102	if (error != 0) {
103		smb_error(gettext("//%s: discon failed"),
104		    error, ctx->ct_fullserver);
105	}
106
107out:
108	smb_ctx_free(ctx);
109	return (error);
110}
111