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