s3: Use SBVAL in put_long_date_timespec
[kai/samba.git] / source3 / lib / util_cmdline.c
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 "auth_info.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
33 struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
34 {
35         struct user_auth_info *result;
36
37         result = talloc_zero(mem_ctx, struct user_auth_info);
38         if (result == NULL) {
39                 return NULL;
40         }
41
42         result->signing_state = SMB_SIGNING_DEFAULT;
43         return result;
44 }
45
46 const 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
54 void 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
64 const 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
72 void 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
82 const 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
90 void 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
104 bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
105                                          const char *arg)
106 {
107         auth_info->signing_state = SMB_SIGNING_DEFAULT;
108         if (strequal(arg, "off") || strequal(arg, "no") ||
109                         strequal(arg, "false")) {
110                 auth_info->signing_state = SMB_SIGNING_OFF;
111         } else if (strequal(arg, "on") || strequal(arg, "yes") ||
112                         strequal(arg, "if_required") ||
113                         strequal(arg, "true") || strequal(arg, "auto")) {
114                 auth_info->signing_state = SMB_SIGNING_IF_REQUIRED;
115         } else if (strequal(arg, "force") || strequal(arg, "required") ||
116                         strequal(arg, "forced")) {
117                 auth_info->signing_state = SMB_SIGNING_REQUIRED;
118         } else {
119                 return false;
120         }
121         return true;
122 }
123
124 int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
125 {
126         return auth_info->signing_state;
127 }
128
129 void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
130 {
131         auth_info->use_ccache = b;
132 }
133
134 bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
135 {
136         return auth_info->use_ccache;
137 }
138
139 void set_cmdline_auth_info_use_pw_nt_hash(struct user_auth_info *auth_info,
140                                           bool b)
141 {
142         auth_info->use_pw_nt_hash = b;
143 }
144
145 bool get_cmdline_auth_info_use_pw_nt_hash(
146         const struct user_auth_info *auth_info)
147 {
148         return auth_info->use_pw_nt_hash;
149 }
150
151 void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
152                                         bool b)
153 {
154         auth_info->use_kerberos = b;
155 }
156
157 bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)
158 {
159         return auth_info->use_kerberos;
160 }
161
162 void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,
163                                         bool b)
164 {
165         auth_info->fallback_after_kerberos = b;
166 }
167
168 bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)
169 {
170         return auth_info->fallback_after_kerberos;
171 }
172
173 /* This should only be used by lib/popt_common.c JRA */
174 void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)
175 {
176         auth_info->use_kerberos = true;
177         auth_info->got_pass = true;
178 }
179
180 /* This should only be used by lib/popt_common.c JRA */
181 void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
182 {
183         auth_info->smb_encrypt = true;
184 }
185
186 void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
187 {
188         auth_info->use_machine_account = true;
189 }
190
191 bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
192 {
193         return auth_info->got_pass;
194 }
195
196 bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
197 {
198         return auth_info->smb_encrypt;
199 }
200
201 bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
202 {
203         return auth_info->use_machine_account;
204 }
205
206 bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)
207 {
208         char *pass = NULL;
209         char *account = NULL;
210
211         if (!get_cmdline_auth_info_use_machine_account(auth_info)) {
212                 return false;
213         }
214
215         if (!secrets_init()) {
216                 d_printf("ERROR: Unable to open secrets database\n");
217                 return false;
218         }
219
220         if (asprintf(&account, "%s$@%s", lp_netbios_name(), lp_realm()) < 0) {
221                 return false;
222         }
223
224         pass = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
225         if (!pass) {
226                 d_printf("ERROR: Unable to fetch machine password for "
227                         "%s in domain %s\n",
228                         account, lp_workgroup());
229                 SAFE_FREE(account);
230                 return false;
231         }
232
233         set_cmdline_auth_info_username(auth_info, account);
234         set_cmdline_auth_info_password(auth_info, pass);
235
236         SAFE_FREE(account);
237         SAFE_FREE(pass);
238
239         return true;
240 }
241
242 /****************************************************************************
243  Ensure we have a password if one not given.
244 ****************************************************************************/
245
246 void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
247 {
248         char *label = NULL;
249         char *pass;
250         TALLOC_CTX *frame;
251
252         if (get_cmdline_auth_info_got_pass(auth_info) ||
253                         get_cmdline_auth_info_use_kerberos(auth_info)) {
254                 /* Already got one... */
255                 return;
256         }
257
258         frame = talloc_stackframe();
259         label = talloc_asprintf(frame, "Enter %s's password: ",
260                         get_cmdline_auth_info_username(auth_info));
261         pass = getpass(label);
262         if (pass) {
263                 set_cmdline_auth_info_password(auth_info, pass);
264         }
265         TALLOC_FREE(frame);
266 }