login.c revision 87866
187866Ssheldonh/* 287866Ssheldonh * Copyright (c) 2000, Boris Popov 387866Ssheldonh * All rights reserved. 487866Ssheldonh * 587866Ssheldonh * Redistribution and use in source and binary forms, with or without 687866Ssheldonh * modification, are permitted provided that the following conditions 787866Ssheldonh * are met: 887866Ssheldonh * 1. Redistributions of source code must retain the above copyright 987866Ssheldonh * notice, this list of conditions and the following disclaimer. 1087866Ssheldonh * 2. Redistributions in binary form must reproduce the above copyright 1187866Ssheldonh * notice, this list of conditions and the following disclaimer in the 1287866Ssheldonh * documentation and/or other materials provided with the distribution. 1387866Ssheldonh * 3. All advertising materials mentioning features or use of this software 1487866Ssheldonh * must display the following acknowledgement: 1587866Ssheldonh * This product includes software developed by Boris Popov. 1687866Ssheldonh * 4. Neither the name of the author nor the names of any co-contributors 1787866Ssheldonh * may be used to endorse or promote products derived from this software 1887866Ssheldonh * without specific prior written permission. 1987866Ssheldonh * 2087866Ssheldonh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2187866Ssheldonh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2287866Ssheldonh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2387866Ssheldonh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2487866Ssheldonh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2587866Ssheldonh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2687866Ssheldonh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2787866Ssheldonh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2887866Ssheldonh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2987866Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3087866Ssheldonh * SUCH DAMAGE. 3187866Ssheldonh * 3287866Ssheldonh * $Id: login.c,v 1.5 2001/01/28 07:35:00 bp Exp $ 3387866Ssheldonh */ 3487866Ssheldonh#include <sys/param.h> 3587866Ssheldonh#include <sys/errno.h> 3687866Ssheldonh#include <sys/stat.h> 3787866Ssheldonh#include <err.h> 3887866Ssheldonh#include <stdio.h> 3987866Ssheldonh#include <unistd.h> 4087866Ssheldonh#include <strings.h> 4187866Ssheldonh#include <stdlib.h> 4287866Ssheldonh#include <sysexits.h> 4387866Ssheldonh 4487866Ssheldonh#include <cflib.h> 4587866Ssheldonh 4687866Ssheldonh#include <netsmb/smb_lib.h> 4787866Ssheldonh#include <netsmb/smb_conn.h> 4887866Ssheldonh 4987866Ssheldonh#include "common.h" 5087866Ssheldonh 5187866Ssheldonh 5287866Ssheldonhint 5387866Ssheldonhcmd_login(int argc, char *argv[]) 5487866Ssheldonh{ 5587866Ssheldonh struct smb_ctx sctx, *ctx = &sctx; 5687866Ssheldonh int error, opt, setprimary = 0, level; 5787866Ssheldonh 5887866Ssheldonh if (argc < 2) 5987866Ssheldonh login_usage(); 6087866Ssheldonh if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_SHARE, SMB_ST_ANY) != 0) 6187866Ssheldonh exit(1); 6287866Ssheldonh if (smb_ctx_readrc(ctx) != 0) 6387866Ssheldonh exit(1); 6487866Ssheldonh if (smb_rc) 6587866Ssheldonh rc_close(smb_rc); 6687866Ssheldonh while ((opt = getopt(argc, argv, STDPARAM_OPT"D")) != EOF) { 6787866Ssheldonh switch(opt){ 6887866Ssheldonh case STDPARAM_ARGS: 6987866Ssheldonh error = smb_ctx_opt(ctx, opt, optarg); 7087866Ssheldonh if (error) 7187866Ssheldonh exit(1); 7287866Ssheldonh break; 7387866Ssheldonh case 'D': 7487866Ssheldonh setprimary = 1; 7587866Ssheldonh break; 7687866Ssheldonh default: 7787866Ssheldonh login_usage(); 7887866Ssheldonh /*NOTREACHED*/ 7987866Ssheldonh } 8087866Ssheldonh } 8187866Ssheldonh if (smb_ctx_resolve(ctx) != 0) 8287866Ssheldonh exit(1); 8387866Ssheldonh level = ctx->ct_parsedlevel; 8487866Ssheldonh error = smb_ctx_lookup(ctx, level, 0); 8587866Ssheldonh if (error == 0) { 8687866Ssheldonh smb_error("connection already exists", error); 8787866Ssheldonh exit(0); 8887866Ssheldonh } 8987866Ssheldonh error = smb_ctx_lookup(ctx, level, SMBLK_CREATE); 9087866Ssheldonh if (error) { 9187866Ssheldonh smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname); 9287866Ssheldonh exit(1); 9387866Ssheldonh } 9487866Ssheldonh switch (level) { 9587866Ssheldonh case SMBL_VC: 9687866Ssheldonh opt = SMBV_PERMANENT; 9787866Ssheldonh break; 9887866Ssheldonh case SMBL_SHARE: 9987866Ssheldonh opt = SMBS_PERMANENT; 10087866Ssheldonh break; 10187866Ssheldonh default: 10287866Ssheldonh smb_error("unknown connection level %d", 0, level); 10387866Ssheldonh exit(1); 10487866Ssheldonh } 10587866Ssheldonh error = smb_ctx_setflags(ctx, level, opt, opt); 10687866Ssheldonh if (error && error != EACCES) { 10787866Ssheldonh smb_error("Can't make connection permanent", error); 10887866Ssheldonh exit(1); 10987866Ssheldonh } 11087866Ssheldonh printf("Connected to %s%s%s\n", ctx->ct_ssn.ioc_user, 11187866Ssheldonh level == SMBL_SHARE ? "@" : "", 11287866Ssheldonh level == SMBL_SHARE ? ctx->ct_sh.ioc_share : ""); 11387866Ssheldonh return 0; 11487866Ssheldonh} 11587866Ssheldonh 11687866Ssheldonhint 11787866Ssheldonhcmd_logout(int argc, char *argv[]) 11887866Ssheldonh{ 11987866Ssheldonh struct smb_ctx sctx, *ctx = &sctx; 12087866Ssheldonh int error, opt, level; 12187866Ssheldonh 12287866Ssheldonh if (argc < 2) 12387866Ssheldonh logout_usage(); 12487866Ssheldonh if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_SHARE, SMB_ST_ANY) != 0) 12587866Ssheldonh exit(1); 12687866Ssheldonh if (smb_ctx_readrc(ctx) != 0) 12787866Ssheldonh exit(1); 12887866Ssheldonh if (smb_rc) 12987866Ssheldonh rc_close(smb_rc); 13087866Ssheldonh while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF){ 13187866Ssheldonh switch (opt) { 13287866Ssheldonh case STDPARAM_ARGS: 13387866Ssheldonh error = smb_ctx_opt(ctx, opt, optarg); 13487866Ssheldonh if (error) 13587866Ssheldonh exit(1); 13687866Ssheldonh break; 13787866Ssheldonh default: 13887866Ssheldonh logout_usage(); 13987866Ssheldonh /*NOTREACHED*/ 14087866Ssheldonh } 14187866Ssheldonh } 14287866Ssheldonh ctx->ct_ssn.ioc_opt &= ~SMBVOPT_CREATE; 14387866Ssheldonh ctx->ct_sh.ioc_opt &= ~SMBSOPT_CREATE; 14487866Ssheldonh if (smb_ctx_resolve(ctx) != 0) 14587866Ssheldonh exit(1); 14687866Ssheldonh level = ctx->ct_parsedlevel; 14787866Ssheldonh error = smb_ctx_lookup(ctx, level, 0); 14887866Ssheldonh if (error == ENOENT) { 14987866Ssheldonh/* ctx->ct_ssn.ioc_opt |= SMBCOPT_SINGLE; 15087866Ssheldonh error = smb_ctx_login(ctx); 15187866Ssheldonh if (error == ENOENT) { 15287866Ssheldonh ctx->ct_ssn.ioc_opt |= SMBCOPT_PRIVATE; 15387866Ssheldonh error = smb_ctx_login(ctx); 15487866Ssheldonh if (error == ENOENT) { 15587866Ssheldonh ctx->ct_ssn.ioc_opt &= ~SMBCOPT_SINGLE; 15687866Ssheldonh error = smb_ctx_login(ctx); 15787866Ssheldonh } 15887866Ssheldonh }*/ 15987866Ssheldonh if (error) { 16087866Ssheldonh smb_error("There is no connection to %s", error, ctx->ct_ssn.ioc_srvname); 16187866Ssheldonh exit(1); 16287866Ssheldonh } 16387866Ssheldonh } 16487866Ssheldonh if (error) 16587866Ssheldonh exit(1); 16687866Ssheldonh switch (level) { 16787866Ssheldonh case SMBL_VC: 16887866Ssheldonh opt = SMBV_PERMANENT; 16987866Ssheldonh break; 17087866Ssheldonh case SMBL_SHARE: 17187866Ssheldonh opt = SMBS_PERMANENT; 17287866Ssheldonh break; 17387866Ssheldonh default: 17487866Ssheldonh smb_error("unknown connection level %d", 0, level); 17587866Ssheldonh exit(1); 17687866Ssheldonh } 17787866Ssheldonh error = smb_ctx_setflags(ctx, level, opt, 0); 17887866Ssheldonh if (error && error != EACCES) { 17987866Ssheldonh smb_error("Can't release connection", error); 18087866Ssheldonh exit(1); 18187866Ssheldonh } 18287866Ssheldonh printf("Connection unmarked as permanent and will be closed when possible\n"); 18387866Ssheldonh exit(0); 18487866Ssheldonh} 18587866Ssheldonh 18687866Ssheldonhvoid 18787866Ssheldonhlogin_usage(void) 18887866Ssheldonh{ 18987866Ssheldonh printf( 19087866Ssheldonh "usage: smbutil login [-E cs1:cs2] [-I host] [-L locale] [-M crights:srights]\n" 19187866Ssheldonh " [-N cowner:cgroup/sowner:sgroup] [-P]\n" 19287866Ssheldonh " [-R retrycount] [-T timeout]\n" 19387866Ssheldonh " [-W workgroup] //user@server\n"); 19487866Ssheldonh exit(1); 19587866Ssheldonh} 19687866Ssheldonh 19787866Ssheldonhvoid 19887866Ssheldonhlogout_usage(void) 19987866Ssheldonh{ 20087866Ssheldonh printf("usage: smbutil logout [user@server]\n"); 20187866Ssheldonh exit(1); 20287866Ssheldonh} 203