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