s3/param: Check return of talloc_strdup
[Samba.git] / examples / fuse / smb2mount.c
blob7133927ad16e9606e0a68d3de1f19ac07cfd48a6
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 "client.h"
25 #include "libsmb/proto.h"
26 #include "clifuse.h"
28 static struct cli_state *connect_one(struct cli_credentials *creds,
29 const char *server, int port,
30 const char *share)
32 struct cli_state *c = NULL;
33 NTSTATUS nt_status;
34 uint32_t flags = 0;
36 nt_status = cli_full_connection_creds(&c, lp_netbios_name(), server,
37 NULL, port,
38 share, "?????",
39 creds,
40 flags);
41 if (!NT_STATUS_IS_OK(nt_status)) {
42 DBG_ERR("cli_full_connection failed! (%s)\n",
43 nt_errstr(nt_status));
44 return NULL;
47 return c;
50 int main(int argc, char *argv[])
52 const char **argv_const = discard_const_p(const char *, argv);
53 TALLOC_CTX *frame = talloc_stackframe();
54 poptContext pc;
55 int opt, ret;
56 int port = 0;
57 char *unc, *mountpoint, *server, *share;
58 struct cli_state *cli;
59 struct cli_credentials *creds = NULL;
60 bool ok;
62 struct poptOption long_options[] = {
63 POPT_AUTOHELP
64 POPT_COMMON_SAMBA
65 POPT_COMMON_CREDENTIALS
66 { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to",
67 "PORT" },
68 POPT_TABLEEND
71 smb_init_locale();
73 ok = samba_cmdline_init(frame,
74 SAMBA_CMDLINE_CONFIG_CLIENT,
75 false /* require_smbconf */);
76 if (!ok) {
77 DBG_ERR("Failed to init cmdline parser!\n");
78 TALLOC_FREE(frame);
79 exit(1);
81 lp_set_cmdline("client min protocol", "SMB2");
82 lp_set_cmdline("client max protocol", "SMB3_11");
84 pc = samba_popt_get_context(getprogname(),
85 argc,
86 argv_const,
87 long_options,
88 0);
89 if (pc == NULL) {
90 DBG_ERR("Failed to setup popt context!\n");
91 TALLOC_FREE(frame);
92 exit(1);
95 poptSetOtherOptionHelp(pc, "//server1/share1 mountpoint");
97 while ((opt = poptGetNextOpt(pc)) != -1) {
98 switch(opt) {
99 case 'p':
100 break;
101 default:
102 fprintf(stderr, "Unknown Option: %c\n", opt);
103 exit(1);
107 if (!poptPeekArg(pc)) {
108 poptPrintUsage(pc, stderr, 0);
109 return -1;
111 unc = talloc_strdup(frame, poptGetArg(pc));
112 if (unc == NULL) {
113 return -1;
115 string_replace(unc,'/','\\');
117 if (!poptPeekArg(pc)) {
118 poptPrintUsage(pc, stderr, 0);
119 return -1;
121 mountpoint = talloc_strdup(frame, poptGetArg(pc));
122 if (mountpoint == NULL) {
123 return -1;
126 poptFreeContext(pc);
127 samba_cmdline_burn(argc, argv);
129 server = talloc_strdup(frame, unc+2);
130 if (!server) {
131 return -1;
133 share = strchr_m(server,'\\');
134 if (!share) {
135 fprintf(stderr, "Invalid argument: %s\n", server);
136 return -1;
139 *share = 0;
140 share++;
142 creds = samba_cmdline_get_creds();
144 cli = connect_one(creds, server, port, share);
145 if (cli == NULL) {
146 return -1;
149 ret = do_mount(cli, mountpoint);
150 if (ret != 0) {
151 fprintf(stderr, "mount failed\n");
152 return -1;
155 TALLOC_FREE(frame);
156 return 0;