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