r11200: Reposition the creation of the kerberos keytab for GSSAPI and Krb5
[garming/samba-autobuild/.git] / source4 / auth / credentials / credentials_files.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    User credentials handling (as regards on-disk files)
5
6    Copyright (C) Jelmer Vernooij 2005
7    Copyright (C) Tim Potter 2001
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_samr.h" /* for struct samrPassword */
28 #include "include/secrets.h"
29 #include "system/filesys.h"
30
31 /**
32  * Read a file descriptor, and parse it for a password (eg from a file or stdin)
33  *
34  * @param credentials Credentials structure on which to set the password
35  * @param fd open file descriptor to read the password from 
36  * @param obtained This enum describes how 'specified' this password is
37  */
38
39 BOOL cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
40                                        int fd, enum credentials_obtained obtained)
41 {
42         char *p;
43         char pass[128];
44
45         for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
46                 p && p - pass < sizeof(pass);) {
47                 switch (read(fd, p, 1)) {
48                 case 1:
49                         if (*p != '\n' && *p != '\0') {
50                                 *++p = '\0'; /* advance p, and null-terminate pass */
51                                 break;
52                         }
53                 case 0:
54                         if (p - pass) {
55                                 *p = '\0'; /* null-terminate it, just in case... */
56                                 p = NULL; /* then force the loop condition to become false */
57                                 break;
58                         } else {
59                                 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
60                                 return False;
61                         }
62
63                 default:
64                         fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
65                                         fd, strerror(errno));
66                         return False;
67                 }
68         }
69
70         cli_credentials_set_password(credentials, pass, obtained);
71         return True;
72 }
73
74 /**
75  * Read a named file, and parse it for a password
76  *
77  * @param credentials Credentials structure on which to set the password
78  * @param file a named file to read the password from 
79  * @param obtained This enum describes how 'specified' this password is
80  */
81
82 BOOL cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
83 {
84         int fd = open(file, O_RDONLY, 0);
85         BOOL ret;
86
87         if (fd < 0) {
88                 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
89                                 file, strerror(errno));
90                 return False;
91         }
92
93         ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
94
95         close(fd);
96         
97         return ret;
98 }
99
100 /**
101  * Read a named file, and parse it for username, domain, realm and password
102  *
103  * @param credentials Credentials structure on which to set the password
104  * @param file a named file to read the details from 
105  * @param obtained This enum describes how 'specified' this password is
106  */
107
108 BOOL cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained) 
109 {
110         uint16_t len = 0;
111         char *ptr, *val, *param;
112         char **lines;
113         int i, numlines;
114
115         lines = file_lines_load(file, &numlines, NULL);
116
117         if (lines == NULL)
118         {
119                 /* fail if we can't open the credentials file */
120                 d_printf("ERROR: Unable to open credentials file!\n");
121                 return False;
122         }
123
124         for (i = 0; i < numlines; i++) {
125                 len = strlen(lines[i]);
126
127                 if (len == 0)
128                         continue;
129
130                 /* break up the line into parameter & value.
131                  * will need to eat a little whitespace possibly */
132                 param = lines[i];
133                 if (!(ptr = strchr_m (lines[i], '=')))
134                         continue;
135
136                 val = ptr+1;
137                 *ptr = '\0';
138
139                 /* eat leading white space */
140                 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
141                         val++;
142
143                 if (strwicmp("password", param) == 0) {
144                         cli_credentials_set_password(cred, val, obtained);
145                 } else if (strwicmp("username", param) == 0) {
146                         cli_credentials_set_username(cred, val, obtained);
147                 } else if (strwicmp("domain", param) == 0) {
148                         cli_credentials_set_domain(cred, val, obtained);
149                 } else if (strwicmp("realm", param) == 0) {
150                         cli_credentials_set_realm(cred, val, obtained);
151                 }
152                 memset(lines[i], 0, len);
153         }
154
155         talloc_free(lines);
156
157         return True;
158 }
159
160
161 /**
162  * Fill in credentials for the machine trust account, from the secrets database.
163  * 
164  * @param cred Credentials structure to fill in
165  * @retval NTSTATUS error detailing any failure
166  */
167 static NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred, 
168                                             const char *base,
169                                             const char *filter)
170 {
171         TALLOC_CTX *mem_ctx;
172         
173         struct ldb_context *ldb;
174         int ldb_ret;
175         struct ldb_message **msgs;
176         const char *attrs[] = {
177                 "secret",
178                 "samAccountName",
179                 "flatname",
180                 "realm",
181                 "secureChannelType",
182                 "ntPwdHash",
183                 "msDS-KeyVersionNumber",
184                 NULL
185         };
186         
187         const char *machine_account;
188         const char *password;
189         const char *old_password;
190         const char *domain;
191         const char *realm;
192         enum netr_SchannelType sct;
193         
194         /* ok, we are going to get it now, don't recurse back here */
195         cred->machine_account_pending = False;
196
197         mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
198         /* Local secrets are stored in secrets.ldb */
199         ldb = secrets_db_connect(mem_ctx);
200         if (!ldb) {
201                 DEBUG(1, ("Could not open secrets.ldb\n"));
202                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
203         }
204
205         /* search for the secret record */
206         ldb_ret = gendb_search(ldb,
207                                mem_ctx, ldb_dn_explode(mem_ctx, base), 
208                                &msgs, attrs,
209                                "%s", filter);
210         if (ldb_ret == 0) {
211                 DEBUG(1, ("Could not find join record to domain: %s\n",
212                           cli_credentials_get_domain(cred)));
213                 talloc_free(mem_ctx);
214                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
215         } else if (ldb_ret != 1) {
216                 DEBUG(1, ("Found more than one (%d) join records to domain: %s\n",
217                           ldb_ret, cli_credentials_get_domain(cred)));
218                 talloc_free(mem_ctx);
219                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
220         }
221         
222         password = ldb_msg_find_string(msgs[0], "secret", NULL);
223         old_password = ldb_msg_find_string(msgs[0], "priorSecret", NULL);
224
225         machine_account = ldb_msg_find_string(msgs[0], "samAccountName", NULL);
226
227         if (!machine_account) {
228                 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s\n",
229                           cli_credentials_get_domain(cred)));
230                 talloc_free(mem_ctx);
231                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
232         }
233         
234         sct = ldb_msg_find_int(msgs[0], "secureChannelType", 0);
235         if (!sct) { 
236                 DEBUG(1, ("Domain join for acocunt %s did not have a secureChannelType set!\n",
237                           machine_account));
238                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
239         }
240         
241         if (!password) {
242                 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msgs[0], "ntPwdHash");
243                 struct samr_Password hash;
244                 ZERO_STRUCT(hash);
245                 if (nt_password_hash) {
246                         memcpy(hash.hash, nt_password_hash->data, 
247                                MIN(nt_password_hash->length, sizeof(hash.hash)));
248                 
249                         cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
250                 } else {
251                 
252                         DEBUG(1, ("Could not find 'secret' in join record to domain: %s\n",
253                                   cli_credentials_get_domain(cred)));
254                         talloc_free(mem_ctx);
255                         return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
256                 }
257         }
258         
259         cli_credentials_set_secure_channel_type(cred, sct);
260
261         domain = ldb_msg_find_string(msgs[0], "flatname", NULL);
262         if (domain) {
263                 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
264         }
265
266         realm = ldb_msg_find_string(msgs[0], "realm", NULL);
267         if (realm) {
268                 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
269         }
270
271         cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
272         if (password) {
273                 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
274         }
275
276         cli_credentials_set_kvno(cred, ldb_msg_find_int(msgs[0], "msDS-KeyVersionNumber", 0));
277         
278         talloc_free(mem_ctx);
279         
280         return NT_STATUS_OK;
281 }
282
283 /**
284  * Fill in credentials for the machine trust account, from the secrets database.
285  * 
286  * @param cred Credentials structure to fill in
287  * @retval NTSTATUS error detailing any failure
288  */
289 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
290 {
291         char *filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER, 
292                                        cli_credentials_get_domain(cred));
293         return cli_credentials_set_secrets(cred, SECRETS_PRIMARY_DOMAIN_DN,
294                                            filter);
295 }
296
297 /**
298  * Fill in credentials for the machine trust account, from the secrets database.
299  * 
300  * @param cred Credentials structure to fill in
301  * @retval NTSTATUS error detailing any failure
302  */
303 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred)
304 {
305         char *filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
306                                        cli_credentials_get_realm(cred),
307                                        cli_credentials_get_domain(cred));
308         return cli_credentials_set_secrets(cred, SECRETS_PRINCIPALS_DN,
309                                            filter);
310 }
311
312 /**
313  * Fill in credentials for the machine trust account, from the secrets database.
314  * 
315  * @param cred Credentials structure to fill in
316  * @retval NTSTATUS error detailing any failure
317  */
318 NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
319                                               const char *serviceprincipal)
320 {
321         char *filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
322                                        cli_credentials_get_realm(cred),
323                                        cli_credentials_get_domain(cred),
324                                        serviceprincipal);
325         return cli_credentials_set_secrets(cred, SECRETS_PRINCIPALS_DN,
326                                            filter);
327 }
328
329 /**
330  * Ask that when required, the credentials system will be filled with
331  * machine trust account, from the secrets database.
332  * 
333  * @param cred Credentials structure to fill in
334  * @note This function is used to call the above function after, rather 
335  *       than during, popt processing.
336  *
337  */
338 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
339 {
340         cred->machine_account_pending = True;
341 }
342