s3:popt_common: simplify popt_common_credentials handling
[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(const 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         nt_status = cli_full_connection(&c, lp_netbios_name(), server,
41                                 NULL, 0,
42                                 share, "?????",
43                                 get_cmdline_auth_info_username(auth_info),
44                                 lp_workgroup(),
45                                 get_cmdline_auth_info_password(auth_info),
46                                 flags,
47                                 get_cmdline_auth_info_signing_state(auth_info));
48         if (!NT_STATUS_IS_OK(nt_status)) {
49                 DBG_ERR("cli_full_connection failed! (%s)\n",
50                         nt_errstr(nt_status));
51                 return NULL;
52         }
53
54         if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
55                 nt_status = cli_cm_force_encryption(
56                         c,
57                         get_cmdline_auth_info_username(auth_info),
58                         get_cmdline_auth_info_password(auth_info),
59                         lp_workgroup(),
60                         share);
61                 if (!NT_STATUS_IS_OK(nt_status)) {
62                         cli_shutdown(c);
63                         c = NULL;
64                 }
65         }
66
67         return c;
68 }
69
70 int main(int argc, char *argv[])
71 {
72         const char **argv_const = discard_const_p(const char *, argv);
73         TALLOC_CTX *frame = talloc_stackframe();
74         poptContext pc;
75         int opt, ret;
76         char *unc, *mountpoint, *server, *share;
77         struct cli_state *cli;
78
79         struct poptOption long_options[] = {
80                 POPT_AUTOHELP
81                 POPT_COMMON_SAMBA
82                 POPT_COMMON_CREDENTIALS
83                 POPT_TABLEEND
84         };
85
86         smb_init_locale();
87         setup_logging(argv[0], DEBUG_STDERR);
88         lp_set_cmdline("client min protocol", "SMB2");
89         lp_set_cmdline("client max protocol", "SMB3_11");
90
91         lp_load_global(get_dyn_CONFIGFILE());
92         load_interfaces();
93
94         pc = poptGetContext("smb2mount", argc, argv_const, long_options, 0);
95         poptSetOtherOptionHelp(pc, "//server1/share1 mountpoint");
96
97         while ((opt = poptGetNextOpt(pc)) != -1) {
98                 switch(opt) {
99                     default:
100                             fprintf(stderr, "Unknown Option: %c\n", opt);
101                             exit(1);
102                 }
103         }
104
105         if (!poptPeekArg(pc)) {
106                 poptPrintUsage(pc, stderr, 0);
107                 return -1;
108         }
109         unc = talloc_strdup(frame, poptGetArg(pc));
110         if (unc == NULL) {
111                 return -1;
112         }
113         string_replace(unc,'/','\\');
114
115         if (!poptPeekArg(pc)) {
116                 poptPrintUsage(pc, stderr, 0);
117                 return -1;
118         }
119         mountpoint = talloc_strdup(frame, poptGetArg(pc));
120         if (mountpoint == NULL) {
121                 return -1;
122         }
123
124         poptFreeContext(pc);
125         popt_burn_cmdline_password(argc, argv);
126
127         server = talloc_strdup(frame, unc+2);
128         if (!server) {
129                 return -1;
130         }
131         share = strchr_m(server,'\\');
132         if (!share) {
133                 fprintf(stderr, "Invalid argument: %s\n", share);
134                 return -1;
135         }
136
137         *share = 0;
138         share++;
139
140         cli = connect_one(cmdline_auth_info, server, share);
141         if (cli == NULL) {
142                 return -1;
143         }
144
145         ret = do_mount(cli, mountpoint);
146         if (ret != 0) {
147                 fprintf(stderr, "mount failed\n");
148                 return -1;
149         }
150
151         TALLOC_FREE(frame);
152         return 0;
153 }