1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Test program for opening an SMB connection directly.
*/
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <libintl.h>
#include <netsmb/smb_lib.h>
extern char *optarg;
extern int optind, opterr, optopt;
extern int smb_iod_connect(struct smb_ctx *);
static char *server;
static void
tconn_usage(void)
{
printf("usage: tconn [-d domain][-u user][-p passwd] server\n");
exit(1);
}
int
main(int argc, char *argv[])
{
int c, error, aflags;
struct smb_ctx *ctx = NULL;
char *dom = NULL;
char *usr = NULL;
char *pw = NULL;
char *secopt = NULL;
struct addrinfo *ai;
while ((c = getopt(argc, argv, "vd:p:s:u:")) != -1) {
switch (c) {
case 'v':
smb_debug = 1;
smb_verbose = 1;
break;
case 'd':
dom = optarg;
break;
case 'u':
usr = optarg;
break;
case 'p':
pw = optarg;
break;
case 's':
secopt = optarg;
break;
case '?':
tconn_usage();
break;
}
}
if (optind >= argc)
tconn_usage();
server = argv[optind];
if (pw != NULL && (dom == NULL || usr == NULL)) {
fprintf(stderr, "%s: -p arg requires -d dom -u usr\n",
argv[0]);
tconn_usage();
}
/*
* This section is intended to demonstrate how an
* RPC client library might use this interface.
*/
error = smb_ctx_alloc(&ctx);
if (error) {
fprintf(stderr, "%s: smb_ctx_alloc failed\n", argv[0]);
goto out;
}
/*
* Set server, share, domain, user
* (in the ctx handle).
*/
smb_ctx_setfullserver(ctx, server);
smb_ctx_setshare(ctx, "IPC$", USE_IPC);
if (dom)
smb_ctx_setdomain(ctx, dom, B_TRUE);
if (usr)
smb_ctx_setuser(ctx, usr, B_TRUE);
if (pw)
smb_ctx_setpassword(ctx, pw, NULL);
/*
* Hackish option to override the Authentication Type flags.
* Sorry about exposing the flag values here, but this is
* really a programmer's test tool. See smbfs_api.h for
* the SMB_AT_... flag values.
*/
if (secopt != NULL) {
aflags = atoi(secopt);
if (aflags < 1 || aflags > 0x1f) {
fprintf(stderr, "%s: -s {0..31}\n", argv[0]);
tconn_usage();
}
smb_ctx_setauthflags(ctx, aflags);
}
/*
* Resolve the server address,
* setup derived defaults.
*/
error = smb_ctx_resolve(ctx);
if (error) {
fprintf(stderr, "%s: smb_ctx_resolve failed\n", argv[0]);
goto out;
}
if ((ai = ctx->ct_addrinfo) == NULL) {
fprintf(stderr, "%s: no ct_addrinfo\n", argv[0]);
goto out;
}
memcpy(&ctx->ct_srvaddr, ai->ai_addr, ai->ai_addrlen);
/*
* If this code were in smbutil or mount_smbfs, it would
* get system and $HOME/.nsmbrc settings here, like this:
*/
error = smb_iod_connect(ctx);
if (error) {
fprintf(stderr, "%s: smb_iod_connect failed\n", argv[0]);
goto out;
}
printf("Yea, we connected!\n");
out:
smb_ctx_free(ctx);
return ((error) ? 1 : 0);
}
|