s3:idmap_ad: add support for ADS_AUTH_SASL_{STARTTLS,LDAPS}
[Samba.git] / examples / fuse / smb2mount.c
blob0594ced1308d9b6d7613f97ec1b143576a0df70d
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(&c, lp_netbios_name(), server,
38 NULL, port,
39 share, "?????",
40 creds,
41 flags);
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;
48 return c;
51 int main(int argc, char *argv[])
53 const char **argv_const = discard_const_p(const char *, argv);
54 TALLOC_CTX *frame = talloc_stackframe();
55 struct loadparm_context *lp_ctx = NULL;
56 poptContext pc;
57 int opt, ret;
58 int port = 0;
59 char *unc, *mountpoint, *server, *share;
60 struct cli_state *cli;
61 struct cli_credentials *creds = NULL;
62 bool ok;
64 struct poptOption long_options[] = {
65 POPT_AUTOHELP
66 POPT_COMMON_SAMBA
67 POPT_COMMON_CREDENTIALS
68 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to",
69 "PORT" },
70 POPT_TABLEEND
73 smb_init_locale();
75 ok = samba_cmdline_init(frame,
76 SAMBA_CMDLINE_CONFIG_CLIENT,
77 false /* require_smbconf */);
78 if (!ok) {
79 DBG_ERR("Failed to init cmdline parser!\n");
80 TALLOC_FREE(frame);
81 exit(1);
83 lp_ctx = samba_cmdline_get_lp_ctx();
84 lpcfg_set_cmdline(lp_ctx, "client min protocol", "SMB2");
85 lpcfg_set_cmdline(lp_ctx, "client max protocol", "SMB3_11");
87 pc = samba_popt_get_context(getprogname(),
88 argc,
89 argv_const,
90 long_options,
91 0);
92 if (pc == NULL) {
93 DBG_ERR("Failed to setup popt context!\n");
94 TALLOC_FREE(frame);
95 exit(1);
98 poptSetOtherOptionHelp(pc, "//server1/share1 mountpoint");
100 while ((opt = poptGetNextOpt(pc)) != -1) {
101 switch(opt) {
102 case 'p':
103 break;
104 default:
105 fprintf(stderr, "Unknown Option: %c\n", opt);
106 exit(1);
110 if (!poptPeekArg(pc)) {
111 poptPrintUsage(pc, stderr, 0);
112 return -1;
114 unc = talloc_strdup(frame, poptGetArg(pc));
115 if (unc == NULL) {
116 return -1;
118 string_replace(unc,'/','\\');
120 if (!poptPeekArg(pc)) {
121 poptPrintUsage(pc, stderr, 0);
122 return -1;
124 mountpoint = talloc_strdup(frame, poptGetArg(pc));
125 if (mountpoint == NULL) {
126 return -1;
129 poptFreeContext(pc);
130 samba_cmdline_burn(argc, argv);
132 server = talloc_strdup(frame, unc+2);
133 if (!server) {
134 return -1;
136 share = strchr_m(server,'\\');
137 if (!share) {
138 fprintf(stderr, "Invalid argument: %s\n", server);
139 return -1;
142 *share = 0;
143 share++;
145 creds = samba_cmdline_get_creds();
147 cli = connect_one(creds, server, port, share);
148 if (cli == NULL) {
149 return -1;
152 ret = do_mount(cli, mountpoint);
153 if (ret != 0) {
154 fprintf(stderr, "mount failed\n");
155 return -1;
158 TALLOC_FREE(frame);
159 return 0;