2 * Copyright (c) 2000, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $Id: login.c,v 1.8 2004/03/19 01:49:48 lindak Exp $
35 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/param.h>
38 #include <sys/errno.h>
48 #include <netsmb/smb_lib.h>
49 #include <netsmb/smb_keychain.h>
54 static char def_dom
[256];
55 static char def_usr
[256];
56 static char tmp_arg
[256];
60 * Parse the string: domuser, which may be any of:
61 * "user@domain" or "domain/user" or "domain\\user"
62 * and return pointers to the domain and user parts.
63 * Modifies the string domuser in-place. Returned
64 * string pointers are within the string domusr.
67 smbfs_parse_domuser(char *domuser
, char **dom
, char **usr
)
69 const char sep
[] = "@/\\";
70 char sc
, *p
, *s1
, *s2
;
72 p
= strpbrk(domuser
, sep
);
74 /* No separators - whole string is the user. */
80 /* Have two strings. */
82 sc
= *p
; /* Save the sep. char */
83 *p
++ = '\0'; /* zap it */
86 /* Enforce just one separator */
92 * Now, which order are they?
93 * "user@domain" or "domain/user"
109 printf(gettext("usage: smbutil login [-c] [[domain/]user]\n"));
110 printf(gettext(" smbutil login [-c] [user[@domain]]\n"));
115 cmd_login(int argc
, char *argv
[])
117 static char prompt
[64];
118 char *dom
, *usr
, *pass
;
122 while ((opt
= getopt(argc
, argv
, "c")) != EOF
) {
125 case 'c': /* smbutil login -c ... */
137 strcpy(tmp_arg
, argv
[optind
]);
138 err
= smbfs_parse_domuser(tmp_arg
, &dom
, &usr
);
140 errx(1, gettext("failed to parse %s"), argv
[optind
]);
146 if (dom
== NULL
|| usr
== NULL
) {
147 err
= smbfs_default_dom_usr(NULL
, NULL
,
148 def_dom
, sizeof (def_dom
),
149 def_usr
, sizeof (def_usr
));
151 errx(1, gettext("failed to get defaults"));
156 nls_str_upper(dom
, dom
);
161 err
= smbfs_keychain_chk(dom
, usr
);
163 printf(gettext("Keychain entry exists.\n"));
165 printf(gettext("Keychain entry not found.\n"));
169 snprintf(prompt
, sizeof (prompt
),
170 gettext("Password for %s/%s:"), dom
, usr
);
171 pass
= getpassphrase(prompt
);
173 err
= smbfs_keychain_add((uid_t
)-1, dom
, usr
, pass
);
175 errx(1, gettext("failed to add keychain entry"));
184 printf(gettext("usage: smbutil logout [[domain/]user]\n"));
185 printf(gettext(" smbutil logout [user[@domain]]\n"));
186 printf(gettext(" smbutil logout -a\n"));
191 cmd_logout(int argc
, char *argv
[])
196 while ((opt
= getopt(argc
, argv
, "a")) != EOF
) {
199 case 'a': /* smbutil logout -a */
202 err
= smbfs_keychain_del_owner();
205 gettext("failed to delete keychain entries"));
214 /* This part is like login. */
217 strcpy(tmp_arg
, argv
[optind
]);
218 err
= smbfs_parse_domuser(tmp_arg
, &dom
, &usr
);
220 errx(1, gettext("failed to parse %s"), argv
[optind
]);
226 if (dom
== NULL
|| usr
== NULL
) {
227 err
= smbfs_default_dom_usr(NULL
, NULL
,
228 def_dom
, sizeof (def_dom
),
229 def_usr
, sizeof (def_usr
));
231 errx(1, gettext("failed to get defaults"));
236 nls_str_upper(dom
, dom
);
240 err
= smbfs_keychain_del((uid_t
)-1, dom
, usr
);
242 errx(1, gettext("failed to delete keychain entry"));
249 logoutall_usage(void)
251 printf(gettext("usage: smbutil logoutall\n"));
256 cmd_logoutall(int argc
, char *argv
[])
263 err
= smbfs_keychain_del_everyone();
266 gettext("You must have super-user privileges to use this sub-command\n"));
269 errx(1, gettext("Failed to delete all keychain entries: %s\n"),