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