login.c revision 87866
1/* 2 * Copyright (c) 2000, Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Boris Popov. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id: login.c,v 1.5 2001/01/28 07:35:00 bp Exp $ 33 */ 34#include <sys/param.h> 35#include <sys/errno.h> 36#include <sys/stat.h> 37#include <err.h> 38#include <stdio.h> 39#include <unistd.h> 40#include <strings.h> 41#include <stdlib.h> 42#include <sysexits.h> 43 44#include <cflib.h> 45 46#include <netsmb/smb_lib.h> 47#include <netsmb/smb_conn.h> 48 49#include "common.h" 50 51 52int 53cmd_login(int argc, char *argv[]) 54{ 55 struct smb_ctx sctx, *ctx = &sctx; 56 int error, opt, setprimary = 0, level; 57 58 if (argc < 2) 59 login_usage(); 60 if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_SHARE, SMB_ST_ANY) != 0) 61 exit(1); 62 if (smb_ctx_readrc(ctx) != 0) 63 exit(1); 64 if (smb_rc) 65 rc_close(smb_rc); 66 while ((opt = getopt(argc, argv, STDPARAM_OPT"D")) != EOF) { 67 switch(opt){ 68 case STDPARAM_ARGS: 69 error = smb_ctx_opt(ctx, opt, optarg); 70 if (error) 71 exit(1); 72 break; 73 case 'D': 74 setprimary = 1; 75 break; 76 default: 77 login_usage(); 78 /*NOTREACHED*/ 79 } 80 } 81 if (smb_ctx_resolve(ctx) != 0) 82 exit(1); 83 level = ctx->ct_parsedlevel; 84 error = smb_ctx_lookup(ctx, level, 0); 85 if (error == 0) { 86 smb_error("connection already exists", error); 87 exit(0); 88 } 89 error = smb_ctx_lookup(ctx, level, SMBLK_CREATE); 90 if (error) { 91 smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname); 92 exit(1); 93 } 94 switch (level) { 95 case SMBL_VC: 96 opt = SMBV_PERMANENT; 97 break; 98 case SMBL_SHARE: 99 opt = SMBS_PERMANENT; 100 break; 101 default: 102 smb_error("unknown connection level %d", 0, level); 103 exit(1); 104 } 105 error = smb_ctx_setflags(ctx, level, opt, opt); 106 if (error && error != EACCES) { 107 smb_error("Can't make connection permanent", error); 108 exit(1); 109 } 110 printf("Connected to %s%s%s\n", ctx->ct_ssn.ioc_user, 111 level == SMBL_SHARE ? "@" : "", 112 level == SMBL_SHARE ? ctx->ct_sh.ioc_share : ""); 113 return 0; 114} 115 116int 117cmd_logout(int argc, char *argv[]) 118{ 119 struct smb_ctx sctx, *ctx = &sctx; 120 int error, opt, level; 121 122 if (argc < 2) 123 logout_usage(); 124 if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_SHARE, SMB_ST_ANY) != 0) 125 exit(1); 126 if (smb_ctx_readrc(ctx) != 0) 127 exit(1); 128 if (smb_rc) 129 rc_close(smb_rc); 130 while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF){ 131 switch (opt) { 132 case STDPARAM_ARGS: 133 error = smb_ctx_opt(ctx, opt, optarg); 134 if (error) 135 exit(1); 136 break; 137 default: 138 logout_usage(); 139 /*NOTREACHED*/ 140 } 141 } 142 ctx->ct_ssn.ioc_opt &= ~SMBVOPT_CREATE; 143 ctx->ct_sh.ioc_opt &= ~SMBSOPT_CREATE; 144 if (smb_ctx_resolve(ctx) != 0) 145 exit(1); 146 level = ctx->ct_parsedlevel; 147 error = smb_ctx_lookup(ctx, level, 0); 148 if (error == ENOENT) { 149/* ctx->ct_ssn.ioc_opt |= SMBCOPT_SINGLE; 150 error = smb_ctx_login(ctx); 151 if (error == ENOENT) { 152 ctx->ct_ssn.ioc_opt |= SMBCOPT_PRIVATE; 153 error = smb_ctx_login(ctx); 154 if (error == ENOENT) { 155 ctx->ct_ssn.ioc_opt &= ~SMBCOPT_SINGLE; 156 error = smb_ctx_login(ctx); 157 } 158 }*/ 159 if (error) { 160 smb_error("There is no connection to %s", error, ctx->ct_ssn.ioc_srvname); 161 exit(1); 162 } 163 } 164 if (error) 165 exit(1); 166 switch (level) { 167 case SMBL_VC: 168 opt = SMBV_PERMANENT; 169 break; 170 case SMBL_SHARE: 171 opt = SMBS_PERMANENT; 172 break; 173 default: 174 smb_error("unknown connection level %d", 0, level); 175 exit(1); 176 } 177 error = smb_ctx_setflags(ctx, level, opt, 0); 178 if (error && error != EACCES) { 179 smb_error("Can't release connection", error); 180 exit(1); 181 } 182 printf("Connection unmarked as permanent and will be closed when possible\n"); 183 exit(0); 184} 185 186void 187login_usage(void) 188{ 189 printf( 190 "usage: smbutil login [-E cs1:cs2] [-I host] [-L locale] [-M crights:srights]\n" 191 " [-N cowner:cgroup/sowner:sgroup] [-P]\n" 192 " [-R retrycount] [-T timeout]\n" 193 " [-W workgroup] //user@server\n"); 194 exit(1); 195} 196 197void 198logout_usage(void) 199{ 200 printf("usage: smbutil logout [user@server]\n"); 201 exit(1); 202} 203