login.c revision 88282
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 * 3288282Ssheldonh * $Id: login.c,v 1.6 2001/08/22 03:33:38 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 } 8188282Ssheldonh#ifdef APPLE 8288282Ssheldonh if (loadsmbvfs()) 8388282Ssheldonh errx(EX_OSERR, "SMB filesystem is not available"); 8488282Ssheldonh#endif 8587866Ssheldonh if (smb_ctx_resolve(ctx) != 0) 8687866Ssheldonh exit(1); 8787866Ssheldonh level = ctx->ct_parsedlevel; 8887866Ssheldonh error = smb_ctx_lookup(ctx, level, 0); 8987866Ssheldonh if (error == 0) { 9087866Ssheldonh smb_error("connection already exists", error); 9187866Ssheldonh exit(0); 9287866Ssheldonh } 9387866Ssheldonh error = smb_ctx_lookup(ctx, level, SMBLK_CREATE); 9487866Ssheldonh if (error) { 9587866Ssheldonh smb_error("could not login to server %s", error, ctx->ct_ssn.ioc_srvname); 9687866Ssheldonh exit(1); 9787866Ssheldonh } 9887866Ssheldonh switch (level) { 9987866Ssheldonh case SMBL_VC: 10087866Ssheldonh opt = SMBV_PERMANENT; 10187866Ssheldonh break; 10287866Ssheldonh case SMBL_SHARE: 10387866Ssheldonh opt = SMBS_PERMANENT; 10487866Ssheldonh break; 10587866Ssheldonh default: 10687866Ssheldonh smb_error("unknown connection level %d", 0, level); 10787866Ssheldonh exit(1); 10887866Ssheldonh } 10987866Ssheldonh error = smb_ctx_setflags(ctx, level, opt, opt); 11087866Ssheldonh if (error && error != EACCES) { 11187866Ssheldonh smb_error("Can't make connection permanent", error); 11287866Ssheldonh exit(1); 11387866Ssheldonh } 11487866Ssheldonh printf("Connected to %s%s%s\n", ctx->ct_ssn.ioc_user, 11587866Ssheldonh level == SMBL_SHARE ? "@" : "", 11687866Ssheldonh level == SMBL_SHARE ? ctx->ct_sh.ioc_share : ""); 11787866Ssheldonh return 0; 11887866Ssheldonh} 11987866Ssheldonh 12087866Ssheldonhint 12187866Ssheldonhcmd_logout(int argc, char *argv[]) 12287866Ssheldonh{ 12387866Ssheldonh struct smb_ctx sctx, *ctx = &sctx; 12487866Ssheldonh int error, opt, level; 12587866Ssheldonh 12687866Ssheldonh if (argc < 2) 12787866Ssheldonh logout_usage(); 12887866Ssheldonh if (smb_ctx_init(ctx, argc, argv, SMBL_VC, SMBL_SHARE, SMB_ST_ANY) != 0) 12987866Ssheldonh exit(1); 13087866Ssheldonh if (smb_ctx_readrc(ctx) != 0) 13187866Ssheldonh exit(1); 13287866Ssheldonh if (smb_rc) 13387866Ssheldonh rc_close(smb_rc); 13487866Ssheldonh while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF){ 13587866Ssheldonh switch (opt) { 13687866Ssheldonh case STDPARAM_ARGS: 13787866Ssheldonh error = smb_ctx_opt(ctx, opt, optarg); 13887866Ssheldonh if (error) 13987866Ssheldonh exit(1); 14087866Ssheldonh break; 14187866Ssheldonh default: 14287866Ssheldonh logout_usage(); 14387866Ssheldonh /*NOTREACHED*/ 14487866Ssheldonh } 14587866Ssheldonh } 14688282Ssheldonh#ifdef APPLE 14788282Ssheldonh error = loadsmbvfs(); 14888282Ssheldonh if (error) 14988282Ssheldonh errx(EX_OSERR, "SMB filesystem is not available"); 15088282Ssheldonh#endif 15187866Ssheldonh ctx->ct_ssn.ioc_opt &= ~SMBVOPT_CREATE; 15287866Ssheldonh ctx->ct_sh.ioc_opt &= ~SMBSOPT_CREATE; 15387866Ssheldonh if (smb_ctx_resolve(ctx) != 0) 15487866Ssheldonh exit(1); 15587866Ssheldonh level = ctx->ct_parsedlevel; 15687866Ssheldonh error = smb_ctx_lookup(ctx, level, 0); 15787866Ssheldonh if (error == ENOENT) { 15887866Ssheldonh/* ctx->ct_ssn.ioc_opt |= SMBCOPT_SINGLE; 15987866Ssheldonh error = smb_ctx_login(ctx); 16087866Ssheldonh if (error == ENOENT) { 16187866Ssheldonh ctx->ct_ssn.ioc_opt |= SMBCOPT_PRIVATE; 16287866Ssheldonh error = smb_ctx_login(ctx); 16387866Ssheldonh if (error == ENOENT) { 16487866Ssheldonh ctx->ct_ssn.ioc_opt &= ~SMBCOPT_SINGLE; 16587866Ssheldonh error = smb_ctx_login(ctx); 16687866Ssheldonh } 16787866Ssheldonh }*/ 16887866Ssheldonh if (error) { 16987866Ssheldonh smb_error("There is no connection to %s", error, ctx->ct_ssn.ioc_srvname); 17087866Ssheldonh exit(1); 17187866Ssheldonh } 17287866Ssheldonh } 17387866Ssheldonh if (error) 17487866Ssheldonh exit(1); 17587866Ssheldonh switch (level) { 17687866Ssheldonh case SMBL_VC: 17787866Ssheldonh opt = SMBV_PERMANENT; 17887866Ssheldonh break; 17987866Ssheldonh case SMBL_SHARE: 18087866Ssheldonh opt = SMBS_PERMANENT; 18187866Ssheldonh break; 18287866Ssheldonh default: 18387866Ssheldonh smb_error("unknown connection level %d", 0, level); 18487866Ssheldonh exit(1); 18587866Ssheldonh } 18687866Ssheldonh error = smb_ctx_setflags(ctx, level, opt, 0); 18787866Ssheldonh if (error && error != EACCES) { 18887866Ssheldonh smb_error("Can't release connection", error); 18987866Ssheldonh exit(1); 19087866Ssheldonh } 19187866Ssheldonh printf("Connection unmarked as permanent and will be closed when possible\n"); 19287866Ssheldonh exit(0); 19387866Ssheldonh} 19487866Ssheldonh 19587866Ssheldonhvoid 19687866Ssheldonhlogin_usage(void) 19787866Ssheldonh{ 19887866Ssheldonh printf( 19987866Ssheldonh "usage: smbutil login [-E cs1:cs2] [-I host] [-L locale] [-M crights:srights]\n" 20087866Ssheldonh " [-N cowner:cgroup/sowner:sgroup] [-P]\n" 20187866Ssheldonh " [-R retrycount] [-T timeout]\n" 20287866Ssheldonh " [-W workgroup] //user@server\n"); 20387866Ssheldonh exit(1); 20487866Ssheldonh} 20587866Ssheldonh 20687866Ssheldonhvoid 20787866Ssheldonhlogout_usage(void) 20887866Ssheldonh{ 20987866Ssheldonh printf("usage: smbutil logout [user@server]\n"); 21087866Ssheldonh exit(1); 21187866Ssheldonh} 212