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