s3:libads: add ads_connect_machine() helper
[Samba.git] / examples / fuse / smb2mount.c
blob69c14af9df1726a1415950e20cb4135099fe699a
1 /*
2 * Unix SMB/CIFS implementation.
3 * fusermount smb2 client
5 * Copyright (C) Volker Lendecke 2016
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "source3/include/includes.h"
22 #include "popt.h"
23 #include "lib/cmdline/cmdline.h"
24 #include "lib/param/param.h"
25 #include "client.h"
26 #include "libsmb/proto.h"
27 #include "clifuse.h"
29 static struct cli_state *connect_one(struct cli_credentials *creds,
30 const char *server, int port,
31 const char *share)
33 struct cli_state *c = NULL;
34 NTSTATUS nt_status;
35 uint32_t flags = 0;
37 nt_status = cli_full_connection_creds(talloc_tos(),
38 &c,
39 lp_netbios_name(),
40 server,
41 NULL,
42 port,
43 share,
44 "?????",
45 creds,
46 flags);
47 if (!NT_STATUS_IS_OK(nt_status)) {
48 DBG_ERR("cli_full_connection failed! (%s)\n",
49 nt_errstr(nt_status));
50 return NULL;
53 return c;
56 int main(int argc, char *argv[])
58 const char **argv_const = discard_const_p(const char *, argv);
59 TALLOC_CTX *frame = talloc_stackframe();
60 struct loadparm_context *lp_ctx = NULL;
61 poptContext pc;
62 int opt, ret;
63 int port = 0;
64 char *unc, *mountpoint, *server, *share;
65 struct cli_state *cli;
66 struct cli_credentials *creds = NULL;
67 bool ok;
69 struct poptOption long_options[] = {
70 POPT_AUTOHELP
71 POPT_COMMON_SAMBA
72 POPT_COMMON_CREDENTIALS
73 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to",
74 "PORT" },
75 POPT_TABLEEND
78 smb_init_locale();
80 ok = samba_cmdline_init(frame,
81 SAMBA_CMDLINE_CONFIG_CLIENT,
82 false /* require_smbconf */);
83 if (!ok) {
84 DBG_ERR("Failed to init cmdline parser!\n");
85 TALLOC_FREE(frame);
86 exit(1);
88 lp_ctx = samba_cmdline_get_lp_ctx();
89 lpcfg_set_cmdline(lp_ctx, "client min protocol", "SMB2");
90 lpcfg_set_cmdline(lp_ctx, "client max protocol", "SMB3_11");
92 pc = samba_popt_get_context(getprogname(),
93 argc,
94 argv_const,
95 long_options,
96 0);
97 if (pc == NULL) {
98 DBG_ERR("Failed to setup popt context!\n");
99 TALLOC_FREE(frame);
100 exit(1);
103 poptSetOtherOptionHelp(pc, "//server1/share1 mountpoint");
105 while ((opt = poptGetNextOpt(pc)) != -1) {
106 switch(opt) {
107 case 'p':
108 break;
109 default:
110 fprintf(stderr, "Unknown Option: %c\n", opt);
111 exit(1);
115 if (!poptPeekArg(pc)) {
116 poptPrintUsage(pc, stderr, 0);
117 return -1;
119 unc = talloc_strdup(frame, poptGetArg(pc));
120 if (unc == NULL) {
121 return -1;
123 string_replace(unc,'/','\\');
125 if (!poptPeekArg(pc)) {
126 poptPrintUsage(pc, stderr, 0);
127 return -1;
129 mountpoint = talloc_strdup(frame, poptGetArg(pc));
130 if (mountpoint == NULL) {
131 return -1;
134 poptFreeContext(pc);
135 samba_cmdline_burn(argc, argv);
137 server = talloc_strdup(frame, unc+2);
138 if (!server) {
139 return -1;
141 share = strchr_m(server,'\\');
142 if (!share) {
143 fprintf(stderr, "Invalid argument: %s\n", server);
144 return -1;
147 *share = 0;
148 share++;
150 creds = samba_cmdline_get_creds();
152 cli = connect_one(creds, server, port, share);
153 if (cli == NULL) {
154 return -1;
157 ret = do_mount(cli, mountpoint);
158 if (ret != 0) {
159 fprintf(stderr, "mount failed\n");
160 return -1;
163 TALLOC_FREE(frame);
164 return 0;