1*6007Sthurlow /*
2*6007Sthurlow * Copyright (c) 2000, Boris Popov
3*6007Sthurlow * All rights reserved.
4*6007Sthurlow *
5*6007Sthurlow * Redistribution and use in source and binary forms, with or without
6*6007Sthurlow * modification, are permitted provided that the following conditions
7*6007Sthurlow * are met:
8*6007Sthurlow * 1. Redistributions of source code must retain the above copyright
9*6007Sthurlow * notice, this list of conditions and the following disclaimer.
10*6007Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
11*6007Sthurlow * notice, this list of conditions and the following disclaimer in the
12*6007Sthurlow * documentation and/or other materials provided with the distribution.
13*6007Sthurlow * 3. All advertising materials mentioning features or use of this software
14*6007Sthurlow * must display the following acknowledgement:
15*6007Sthurlow * This product includes software developed by Boris Popov.
16*6007Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
17*6007Sthurlow * may be used to endorse or promote products derived from this software
18*6007Sthurlow * without specific prior written permission.
19*6007Sthurlow *
20*6007Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*6007Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*6007Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*6007Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*6007Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*6007Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*6007Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*6007Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*6007Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*6007Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*6007Sthurlow * SUCH DAMAGE.
31*6007Sthurlow *
32*6007Sthurlow * $Id: login.c,v 1.8 2004/03/19 01:49:48 lindak Exp $
33*6007Sthurlow */
34*6007Sthurlow
35*6007Sthurlow #pragma ident "%Z%%M% %I% %E% SMI"
36*6007Sthurlow
37*6007Sthurlow #include <sys/param.h>
38*6007Sthurlow #include <sys/errno.h>
39*6007Sthurlow #include <sys/stat.h>
40*6007Sthurlow
41*6007Sthurlow #include <stdio.h>
42*6007Sthurlow #include <stdlib.h>
43*6007Sthurlow #include <strings.h>
44*6007Sthurlow #include <unistd.h>
45*6007Sthurlow #include <err.h>
46*6007Sthurlow #include <libintl.h>
47*6007Sthurlow
48*6007Sthurlow #include <netsmb/smb_lib.h>
49*6007Sthurlow #include <netsmb/smb_keychain.h>
50*6007Sthurlow
51*6007Sthurlow #include "common.h"
52*6007Sthurlow
53*6007Sthurlow /* defaults */
54*6007Sthurlow static char def_dom[256];
55*6007Sthurlow static char def_usr[256];
56*6007Sthurlow static char tmp_arg[256];
57*6007Sthurlow
58*6007Sthurlow
59*6007Sthurlow /*
60*6007Sthurlow * Parse the string: domuser, which may be any of:
61*6007Sthurlow * "user@domain" or "domain/user" or "domain\\user"
62*6007Sthurlow * and return pointers to the domain and user parts.
63*6007Sthurlow * Modifies the string domuser in-place. Returned
64*6007Sthurlow * string pointers are within the string domusr.
65*6007Sthurlow */
66*6007Sthurlow int
smbfs_parse_domuser(char * domuser,char ** dom,char ** usr)67*6007Sthurlow smbfs_parse_domuser(char *domuser, char **dom, char **usr)
68*6007Sthurlow {
69*6007Sthurlow const char sep[] = "@/\\";
70*6007Sthurlow char sc, *p, *s1, *s2;
71*6007Sthurlow
72*6007Sthurlow p = strpbrk(domuser, sep);
73*6007Sthurlow if (p == NULL) {
74*6007Sthurlow /* No separators - whole string is the user. */
75*6007Sthurlow *dom = NULL;
76*6007Sthurlow *usr = domuser;
77*6007Sthurlow return (0);
78*6007Sthurlow }
79*6007Sthurlow
80*6007Sthurlow /* Have two strings. */
81*6007Sthurlow s1 = domuser;
82*6007Sthurlow sc = *p; /* Save the sep. char */
83*6007Sthurlow *p++ = '\0'; /* zap it */
84*6007Sthurlow s2 = p;
85*6007Sthurlow
86*6007Sthurlow /* Enforce just one separator */
87*6007Sthurlow p = strpbrk(s2, sep);
88*6007Sthurlow if (p)
89*6007Sthurlow return (-1);
90*6007Sthurlow
91*6007Sthurlow /*
92*6007Sthurlow * Now, which order are they?
93*6007Sthurlow * "user@domain" or "domain/user"
94*6007Sthurlow */
95*6007Sthurlow if (sc == '@') {
96*6007Sthurlow *usr = s1;
97*6007Sthurlow *dom = s2;
98*6007Sthurlow } else {
99*6007Sthurlow *dom = s1;
100*6007Sthurlow *usr = s2;
101*6007Sthurlow }
102*6007Sthurlow
103*6007Sthurlow return (0);
104*6007Sthurlow }
105*6007Sthurlow
106*6007Sthurlow void
login_usage(void)107*6007Sthurlow login_usage(void)
108*6007Sthurlow {
109*6007Sthurlow printf(gettext("usage: smbutil login [-c] [[domain/]user]\n"));
110*6007Sthurlow printf(gettext(" smbutil login [-c] [user[@domain]]\n"));
111*6007Sthurlow exit(1);
112*6007Sthurlow }
113*6007Sthurlow
114*6007Sthurlow int
cmd_login(int argc,char * argv[])115*6007Sthurlow cmd_login(int argc, char *argv[])
116*6007Sthurlow {
117*6007Sthurlow static char prompt[64];
118*6007Sthurlow char *dom, *usr, *pass;
119*6007Sthurlow int err, opt;
120*6007Sthurlow int check = 0;
121*6007Sthurlow
122*6007Sthurlow while ((opt = getopt(argc, argv, "c")) != EOF) {
123*6007Sthurlow switch (opt) {
124*6007Sthurlow
125*6007Sthurlow case 'c': /* smbutil login -c ... */
126*6007Sthurlow check = 1;
127*6007Sthurlow break;
128*6007Sthurlow
129*6007Sthurlow default:
130*6007Sthurlow login_usage();
131*6007Sthurlow break;
132*6007Sthurlow }
133*6007Sthurlow }
134*6007Sthurlow
135*6007Sthurlow dom = usr = NULL;
136*6007Sthurlow if (optind < argc) {
137*6007Sthurlow strcpy(tmp_arg, argv[optind]);
138*6007Sthurlow err = smbfs_parse_domuser(tmp_arg, &dom, &usr);
139*6007Sthurlow if (err)
140*6007Sthurlow errx(1, gettext("failed to parse %s"), argv[optind]);
141*6007Sthurlow optind++;
142*6007Sthurlow }
143*6007Sthurlow if (optind != argc)
144*6007Sthurlow login_usage();
145*6007Sthurlow
146*6007Sthurlow if (dom == NULL || usr == NULL) {
147*6007Sthurlow err = smbfs_default_dom_usr(NULL, NULL,
148*6007Sthurlow def_dom, sizeof (def_dom),
149*6007Sthurlow def_usr, sizeof (def_usr));
150*6007Sthurlow if (err)
151*6007Sthurlow errx(1, gettext("failed to get defaults"));
152*6007Sthurlow }
153*6007Sthurlow if (dom == NULL)
154*6007Sthurlow dom = def_dom;
155*6007Sthurlow else
156*6007Sthurlow nls_str_upper(dom, dom);
157*6007Sthurlow if (usr == NULL)
158*6007Sthurlow usr = def_usr;
159*6007Sthurlow
160*6007Sthurlow if (check) {
161*6007Sthurlow err = smbfs_keychain_chk(dom, usr);
162*6007Sthurlow if (!err)
163*6007Sthurlow printf(gettext("Keychain entry exists.\n"));
164*6007Sthurlow else
165*6007Sthurlow printf(gettext("Keychain entry not found.\n"));
166*6007Sthurlow return (0);
167*6007Sthurlow }
168*6007Sthurlow
169*6007Sthurlow snprintf(prompt, sizeof (prompt),
170*6007Sthurlow gettext("Password for %s/%s:"), dom, usr);
171*6007Sthurlow pass = getpassphrase(prompt);
172*6007Sthurlow
173*6007Sthurlow err = smbfs_keychain_add((uid_t)-1, dom, usr, pass);
174*6007Sthurlow if (err)
175*6007Sthurlow errx(1, gettext("failed to add keychain entry"));
176*6007Sthurlow
177*6007Sthurlow return (0);
178*6007Sthurlow }
179*6007Sthurlow
180*6007Sthurlow
181*6007Sthurlow void
logout_usage(void)182*6007Sthurlow logout_usage(void)
183*6007Sthurlow {
184*6007Sthurlow printf(gettext("usage: smbutil logout [[domain/]user]\n"));
185*6007Sthurlow printf(gettext(" smbutil logout [user[@domain]]\n"));
186*6007Sthurlow printf(gettext(" smbutil logout -a\n"));
187*6007Sthurlow exit(1);
188*6007Sthurlow }
189*6007Sthurlow
190*6007Sthurlow int
cmd_logout(int argc,char * argv[])191*6007Sthurlow cmd_logout(int argc, char *argv[])
192*6007Sthurlow {
193*6007Sthurlow char *dom, *usr;
194*6007Sthurlow int err, opt;
195*6007Sthurlow
196*6007Sthurlow while ((opt = getopt(argc, argv, "a")) != EOF) {
197*6007Sthurlow switch (opt) {
198*6007Sthurlow
199*6007Sthurlow case 'a': /* smbutil logout -a */
200*6007Sthurlow if (optind != argc)
201*6007Sthurlow logout_usage();
202*6007Sthurlow err = smbfs_keychain_del_owner();
203*6007Sthurlow if (err)
204*6007Sthurlow errx(1,
205*6007Sthurlow gettext("failed to delete keychain entries"));
206*6007Sthurlow return (0);
207*6007Sthurlow
208*6007Sthurlow default:
209*6007Sthurlow logout_usage();
210*6007Sthurlow break;
211*6007Sthurlow }
212*6007Sthurlow }
213*6007Sthurlow
214*6007Sthurlow /* This part is like login. */
215*6007Sthurlow dom = usr = NULL;
216*6007Sthurlow if (optind < argc) {
217*6007Sthurlow strcpy(tmp_arg, argv[optind]);
218*6007Sthurlow err = smbfs_parse_domuser(tmp_arg, &dom, &usr);
219*6007Sthurlow if (err)
220*6007Sthurlow errx(1, gettext("failed to parse %s"), argv[optind]);
221*6007Sthurlow optind++;
222*6007Sthurlow }
223*6007Sthurlow if (optind != argc)
224*6007Sthurlow logout_usage();
225*6007Sthurlow
226*6007Sthurlow if (dom == NULL || usr == NULL) {
227*6007Sthurlow err = smbfs_default_dom_usr(NULL, NULL,
228*6007Sthurlow def_dom, sizeof (def_dom),
229*6007Sthurlow def_usr, sizeof (def_usr));
230*6007Sthurlow if (err)
231*6007Sthurlow errx(1, gettext("failed to get defaults"));
232*6007Sthurlow }
233*6007Sthurlow if (dom == NULL)
234*6007Sthurlow dom = def_dom;
235*6007Sthurlow else
236*6007Sthurlow nls_str_upper(dom, dom);
237*6007Sthurlow if (usr == NULL)
238*6007Sthurlow usr = def_usr;
239*6007Sthurlow
240*6007Sthurlow err = smbfs_keychain_del((uid_t)-1, dom, usr);
241*6007Sthurlow if (err)
242*6007Sthurlow errx(1, gettext("failed to delete keychain entry"));
243*6007Sthurlow
244*6007Sthurlow return (0);
245*6007Sthurlow }
246*6007Sthurlow
247*6007Sthurlow
248*6007Sthurlow void
logoutall_usage(void)249*6007Sthurlow logoutall_usage(void)
250*6007Sthurlow {
251*6007Sthurlow printf(gettext("usage: smbutil logoutall\n"));
252*6007Sthurlow exit(1);
253*6007Sthurlow }
254*6007Sthurlow
255*6007Sthurlow int
cmd_logoutall(int argc,char * argv[])256*6007Sthurlow cmd_logoutall(int argc, char *argv[])
257*6007Sthurlow {
258*6007Sthurlow int err;
259*6007Sthurlow
260*6007Sthurlow if (optind != argc)
261*6007Sthurlow logoutall_usage();
262*6007Sthurlow
263*6007Sthurlow err = smbfs_keychain_del_everyone();
264*6007Sthurlow if (err == EPERM) {
265*6007Sthurlow errx(1,
266*6007Sthurlow gettext("You must have super-user privileges to use this sub-command\n"));
267*6007Sthurlow }
268*6007Sthurlow if (err) {
269*6007Sthurlow errx(1, gettext("Failed to delete all keychain entries: %s\n"),
270*6007Sthurlow smb_strerror(err));
271*6007Sthurlow }
272*6007Sthurlow
273*6007Sthurlow return (0);
274*6007Sthurlow }
275