source: vendor/3.6.23/source3/lib/util_cmdline.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

File size: 7.2 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2007
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) James Peach 2006
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "includes.h"
25#include "popt_common.h"
26#include "secrets.h"
27
28/**************************************************************************n
29 Code to cope with username/password auth options from the commandline.
30 Used mainly in client tools.
31****************************************************************************/
32
33struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
34{
35 struct user_auth_info *result;
36
37 result = TALLOC_ZERO_P(mem_ctx, struct user_auth_info);
38 if (result == NULL) {
39 return NULL;
40 }
41
42 result->signing_state = Undefined;
43 return result;
44}
45
46const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)
47{
48 if (!auth_info->username) {
49 return "";
50 }
51 return auth_info->username;
52}
53
54void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
55 const char *username)
56{
57 TALLOC_FREE(auth_info->username);
58 auth_info->username = talloc_strdup(auth_info, username);
59 if (!auth_info->username) {
60 exit(ENOMEM);
61 }
62}
63
64const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)
65{
66 if (!auth_info->domain) {
67 return "";
68 }
69 return auth_info->domain;
70}
71
72void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,
73 const char *domain)
74{
75 TALLOC_FREE(auth_info->domain);
76 auth_info->domain = talloc_strdup(auth_info, domain);
77 if (!auth_info->domain) {
78 exit(ENOMEM);
79 }
80}
81
82const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
83{
84 if (!auth_info->password) {
85 return "";
86 }
87 return auth_info->password;
88}
89
90void set_cmdline_auth_info_password(struct user_auth_info *auth_info,
91 const char *password)
92{
93 TALLOC_FREE(auth_info->password);
94 if (password == NULL) {
95 password = "";
96 }
97 auth_info->password = talloc_strdup(auth_info, password);
98 if (!auth_info->password) {
99 exit(ENOMEM);
100 }
101 auth_info->got_pass = true;
102}
103
104bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
105 const char *arg)
106{
107 auth_info->signing_state = -1;
108 if (strequal(arg, "off") || strequal(arg, "no") ||
109 strequal(arg, "false")) {
110 auth_info->signing_state = false;
111 } else if (strequal(arg, "on") || strequal(arg, "yes") ||
112 strequal(arg, "true") || strequal(arg, "auto")) {
113 auth_info->signing_state = true;
114 } else if (strequal(arg, "force") || strequal(arg, "required") ||
115 strequal(arg, "forced")) {
116 auth_info->signing_state = Required;
117 } else {
118 return false;
119 }
120 return true;
121}
122
123int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
124{
125 return auth_info->signing_state;
126}
127
128void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
129{
130 auth_info->use_ccache = b;
131}
132
133bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
134{
135 return auth_info->use_ccache;
136}
137
138void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
139 bool b)
140{
141 auth_info->use_kerberos = b;
142}
143
144bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)
145{
146 return auth_info->use_kerberos;
147}
148
149void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,
150 bool b)
151{
152 auth_info->fallback_after_kerberos = b;
153}
154
155bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)
156{
157 return auth_info->fallback_after_kerberos;
158}
159
160/* This should only be used by lib/popt_common.c JRA */
161void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)
162{
163 auth_info->use_kerberos = true;
164 auth_info->got_pass = true;
165}
166
167/* This should only be used by lib/popt_common.c JRA */
168void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
169{
170 auth_info->smb_encrypt = true;
171}
172
173void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
174{
175 auth_info->use_machine_account = true;
176}
177
178bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
179{
180 return auth_info->got_pass;
181}
182
183bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
184{
185 return auth_info->smb_encrypt;
186}
187
188bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
189{
190 return auth_info->use_machine_account;
191}
192
193struct user_auth_info *get_cmdline_auth_info_copy(TALLOC_CTX *mem_ctx,
194 const struct user_auth_info *src)
195{
196 struct user_auth_info *result;
197
198 result = user_auth_info_init(mem_ctx);
199 if (result == NULL) {
200 return NULL;
201 }
202
203 *result = *src;
204
205 result->username = talloc_strdup(
206 result, get_cmdline_auth_info_username(src));
207 result->password = talloc_strdup(
208 result, get_cmdline_auth_info_password(src));
209 if ((result->username == NULL) || (result->password == NULL)) {
210 TALLOC_FREE(result);
211 return NULL;
212 }
213
214 return result;
215}
216
217bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)
218{
219 char *pass = NULL;
220 char *account = NULL;
221
222 if (!get_cmdline_auth_info_use_machine_account(auth_info)) {
223 return false;
224 }
225
226 if (!secrets_init()) {
227 d_printf("ERROR: Unable to open secrets database\n");
228 return false;
229 }
230
231 if (asprintf(&account, "%s$@%s", global_myname(), lp_realm()) < 0) {
232 return false;
233 }
234
235 pass = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
236 if (!pass) {
237 d_printf("ERROR: Unable to fetch machine password for "
238 "%s in domain %s\n",
239 account, lp_workgroup());
240 SAFE_FREE(account);
241 return false;
242 }
243
244 set_cmdline_auth_info_username(auth_info, account);
245 set_cmdline_auth_info_password(auth_info, pass);
246
247 SAFE_FREE(account);
248 SAFE_FREE(pass);
249
250 return true;
251}
252
253/****************************************************************************
254 Ensure we have a password if one not given.
255****************************************************************************/
256
257void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
258{
259 char *label = NULL;
260 char *pass;
261 TALLOC_CTX *frame;
262
263 if (get_cmdline_auth_info_got_pass(auth_info) ||
264 get_cmdline_auth_info_use_kerberos(auth_info)) {
265 /* Already got one... */
266 return;
267 }
268
269 frame = talloc_stackframe();
270 label = talloc_asprintf(frame, "Enter %s's password: ",
271 get_cmdline_auth_info_username(auth_info));
272 pass = getpass(label);
273 if (pass) {
274 set_cmdline_auth_info_password(auth_info, pass);
275 }
276 TALLOC_FREE(frame);
277}
Note: See TracBrowser for help on using the repository browser.