2c06ed563761df0bd8f30a3a81d359a74b891a11
[bbaumbach/samba-autobuild/.git] / examples / fuse / smb2mount.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * fusermount smb2 client
4  *
5  * Copyright (C) Volker Lendecke 2016
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "source3/include/includes.h"
22 #include "popt.h"
23 #include "popt_common.h"
24 #include "client.h"
25 #include "libsmb/proto.h"
26 #include "clifuse.h"
27
28 static struct cli_state *connect_one(struct user_auth_info *auth_info,
29                                      const char *server, const char *share)
30 {
31         struct cli_state *c = NULL;
32         NTSTATUS nt_status;
33         uint32_t flags = 0;
34
35         if (get_cmdline_auth_info_use_kerberos(auth_info)) {
36                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
37                          CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
38         }
39
40         if (get_cmdline_auth_info_use_machine_account(auth_info) &&
41             !set_cmdline_auth_info_machine_account_creds(auth_info)) {
42                 return NULL;
43         }
44
45         set_cmdline_auth_info_getpass(auth_info);
46
47         nt_status = cli_full_connection(&c, lp_netbios_name(), server,
48                                 NULL, 0,
49                                 share, "?????",
50                                 get_cmdline_auth_info_username(auth_info),
51                                 lp_workgroup(),
52                                 get_cmdline_auth_info_password(auth_info),
53                                 flags,
54                                 get_cmdline_auth_info_signing_state(auth_info));
55         if (!NT_STATUS_IS_OK(nt_status)) {
56                 DBG_ERR("cli_full_connection failed! (%s)\n",
57                         nt_errstr(nt_status));
58                 return NULL;
59         }
60
61         if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
62                 nt_status = cli_cm_force_encryption(
63                         c,
64                         get_cmdline_auth_info_username(auth_info),
65                         get_cmdline_auth_info_password(auth_info),
66                         lp_workgroup(),
67                         share);
68                 if (!NT_STATUS_IS_OK(nt_status)) {
69                         cli_shutdown(c);
70                         c = NULL;
71                 }
72         }
73
74         return c;
75 }
76
77 int main(int argc, char *argv[])
78 {
79         const char **argv_const = discard_const_p(const char *, argv);
80         TALLOC_CTX *frame = talloc_stackframe();
81         struct user_auth_info *auth_info;
82         poptContext pc;
83         int opt, ret;
84         char *unc, *mountpoint, *server, *share;
85         struct cli_state *cli;
86
87         struct poptOption long_options[] = {
88                 POPT_AUTOHELP
89                 POPT_COMMON_SAMBA
90                 POPT_COMMON_CREDENTIALS
91                 POPT_TABLEEND
92         };
93
94         smb_init_locale();
95         setup_logging(argv[0], DEBUG_STDERR);
96         lp_set_cmdline("client min protocol", "SMB2");
97         lp_set_cmdline("client max protocol", "SMB3_11");
98
99         auth_info = user_auth_info_init(frame);
100         if (auth_info == NULL) {
101                 exit(1);
102         }
103         popt_common_set_auth_info(auth_info);
104
105         lp_load_global(get_dyn_CONFIGFILE());
106         load_interfaces();
107
108         pc = poptGetContext("smb2mount", argc, argv_const, long_options, 0);
109         poptSetOtherOptionHelp(pc, "//server1/share1 mountpoint");
110
111         while ((opt = poptGetNextOpt(pc)) != -1) {
112                 switch(opt) {
113                     default:
114                             fprintf(stderr, "Unknown Option: %c\n", opt);
115                             exit(1);
116                 }
117         }
118
119         if (!poptPeekArg(pc)) {
120                 poptPrintUsage(pc, stderr, 0);
121                 return -1;
122         }
123         unc = talloc_strdup(frame, poptGetArg(pc));
124         if (unc == NULL) {
125                 return -1;
126         }
127         string_replace(unc,'/','\\');
128
129         if (!poptPeekArg(pc)) {
130                 poptPrintUsage(pc, stderr, 0);
131                 return -1;
132         }
133         mountpoint = talloc_strdup(frame, poptGetArg(pc));
134         if (mountpoint == NULL) {
135                 return -1;
136         }
137
138         poptFreeContext(pc);
139         popt_burn_cmdline_password(argc, argv);
140
141         server = talloc_strdup(frame, unc+2);
142         if (!server) {
143                 return -1;
144         }
145         share = strchr_m(server,'\\');
146         if (!share) {
147                 fprintf(stderr, "Invalid argument: %s\n", share);
148                 return -1;
149         }
150
151         *share = 0;
152         share++;
153
154         cli = connect_one(auth_info, server, share);
155         if (cli == NULL) {
156                 return -1;
157         }
158
159         ret = do_mount(cli, mountpoint);
160         if (ret != 0) {
161                 fprintf(stderr, "mount failed\n");
162                 return -1;
163         }
164
165         TALLOC_FREE(frame);
166         return 0;
167 }