1 /*
   2  * CDDL HEADER START
   3  *
   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.
   7  *
   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.
  12  *
  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]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  25  */
  26 
  27 /*
  28  * Test program for opening an SMB connection directly.
  29  */
  30 
  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>
  40 
  41 #include <netsmb/smb_lib.h>
  42 
  43 extern char *optarg;
  44 extern int optind, opterr, optopt;
  45 extern int smb_iod_connect(struct smb_ctx *);
  46 
  47 static char *server;
  48 
  49 static void
  50 tconn_usage(void)
  51 {
  52         printf("usage: tconn [-d domain][-u user][-p passwd] server\n");
  53         exit(1);
  54 }
  55 
  56 int
  57 main(int argc, char *argv[])
  58 {
  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;
  66 
  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;
  73 
  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;
  89                 }
  90         }
  91         if (optind >= argc)
  92                 tconn_usage();
  93         server = argv[optind];
  94 
  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();
  99         }
 100 
 101         /*
 102          * This section is intended to demonstrate how an
 103          * RPC client library might use this interface.
 104          */
 105         error = smb_ctx_alloc(&ctx);
 106         if (error) {
 107                 fprintf(stderr, "%s: smb_ctx_alloc failed\n", argv[0]);
 108                 goto out;
 109         }
 110 
 111         /*
 112          * Set server, share, domain, user
 113          * (in the ctx handle).
 114          */
 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, NULL);
 123 
 124         /*
 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.
 129          */
 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();
 135                 }
 136                 smb_ctx_setauthflags(ctx, aflags);
 137         }
 138 
 139         /*
 140          * Resolve the server address,
 141          * setup derived defaults.
 142          */
 143         error = smb_ctx_resolve(ctx);
 144         if (error) {
 145                 fprintf(stderr, "%s: smb_ctx_resolve failed\n", argv[0]);
 146                 goto out;
 147         }
 148 
 149         if ((ai = ctx->ct_addrinfo) == NULL) {
 150                 fprintf(stderr, "%s: no ct_addrinfo\n", argv[0]);
 151                 goto out;
 152         }
 153         memcpy(&ctx->ct_srvaddr, ai->ai_addr, ai->ai_addrlen);
 154 
 155         /*
 156          * If this code were in smbutil or mount_smbfs, it would
 157          * get system and $HOME/.nsmbrc settings here, like this:
 158          */
 159         error = smb_iod_connect(ctx);
 160         if (error) {
 161                 fprintf(stderr, "%s: smb_iod_connect failed\n", argv[0]);
 162                 goto out;
 163         }
 164 
 165         printf("Yea, we connected!\n");
 166 
 167 out:
 168         smb_ctx_free(ctx);
 169 
 170         return ((error) ? 1 : 0);
 171 }