Commit the auth associated changes I missed from the last commit.
[ira/wip.git] / source3 / auth / auth_util.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Authentication utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Andrew Bartlett 2001
7    Copyright (C) Jeremy Allison 2000-2001
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 extern fstring remote_machine;
27 extern pstring global_myname;
28
29 /****************************************************************************
30  Create a UNIX user on demand.
31 ****************************************************************************/
32
33 static int smb_create_user(const char *unix_user, const char *homedir)
34 {
35         pstring add_script;
36         int ret;
37
38         pstrcpy(add_script, lp_adduser_script());
39         if (! *add_script)
40                 return -1;
41         all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
42         if (homedir)
43                 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
44         ret = smbrun(add_script,NULL);
45         DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
46         return ret;
47 }
48
49 /****************************************************************************
50  Delete a UNIX user on demand.
51 ****************************************************************************/
52
53 int smb_delete_user(const char *unix_user)
54 {
55         pstring del_script;
56         int ret;
57
58         pstrcpy(del_script, lp_deluser_script());
59         if (! *del_script)
60                 return -1;
61         all_string_sub(del_script, "%u", unix_user, sizeof(pstring));
62         ret = smbrun(del_script,NULL);
63         DEBUG(3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
64         return ret;
65 }
66
67 /****************************************************************************
68  Add and Delete UNIX users on demand, based on NTSTATUS codes.
69 ****************************************************************************/
70
71 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status) 
72 {
73         struct passwd *pwd=NULL;
74
75         if (NT_STATUS_IS_OK(nt_status)) {
76
77                 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
78                         
79                         /*
80                          * User validated ok against Domain controller.
81                          * If the admin wants us to try and create a UNIX
82                          * user on the fly, do so.
83                          */
84                         
85                         if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
86                                 smb_create_user(user_info->internal_username.str, NULL);
87                         }
88                 }
89         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
90                 /*
91                  * User failed to validate ok against Domain controller.
92                  * If the failure was "user doesn't exist" and admin 
93                  * wants us to try and delete that UNIX user on the fly,
94                  * do so.
95                  */
96                 if (lp_deluser_script()) {
97                         smb_delete_user(user_info->internal_username.str);
98                 }
99         }
100 }
101
102 /****************************************************************************
103  Create an auth_usersupplied_data structure
104 ****************************************************************************/
105
106 static BOOL make_user_info(auth_usersupplied_info **user_info, 
107                            const char *smb_name, 
108                            const char *internal_username,
109                            const char *client_domain, 
110                            const char *domain,
111                            const char *wksta_name, 
112                            DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
113                            DATA_BLOB plaintext, 
114                            uint32 auth_flags, BOOL encrypted)
115 {
116
117         DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
118
119         *user_info = malloc(sizeof(**user_info));
120         if (!user_info) {
121                 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
122                 return False;
123         }
124
125         ZERO_STRUCTP(*user_info);
126
127         DEBUG(5,("makeing strings for %s's user_info struct\n", internal_username));
128
129         (*user_info)->smb_name.str = strdup(smb_name);
130         if ((*user_info)->smb_name.str) { 
131                 (*user_info)->smb_name.len = strlen(smb_name);
132         } else {
133                 free_user_info(user_info);
134                 return False;
135         }
136         
137         (*user_info)->internal_username.str = strdup(internal_username);
138         if ((*user_info)->internal_username.str) { 
139                 (*user_info)->internal_username.len = strlen(internal_username);
140         } else {
141                 free_user_info(user_info);
142                 return False;
143         }
144
145         (*user_info)->domain.str = strdup(domain);
146         if ((*user_info)->domain.str) { 
147                 (*user_info)->domain.len = strlen(domain);
148         } else {
149                 free_user_info(user_info);
150                 return False;
151         }
152
153         (*user_info)->client_domain.str = strdup(client_domain);
154         if ((*user_info)->client_domain.str) { 
155                 (*user_info)->client_domain.len = strlen(client_domain);
156         } else {
157                 free_user_info(user_info);
158                 return False;
159         }
160
161         (*user_info)->wksta_name.str = strdup(wksta_name);
162         if ((*user_info)->wksta_name.str) { 
163                 (*user_info)->wksta_name.len = strlen(wksta_name);
164         } else {
165                 free_user_info(user_info);
166                 return False;
167         }
168
169         DEBUG(5,("makeing blobs for %s's user_info struct\n", internal_username));
170
171         (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
172         (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
173         (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
174
175         (*user_info)->encrypted = encrypted;
176         (*user_info)->auth_flags = auth_flags;
177
178         DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
179
180         return True;
181 }
182
183 /****************************************************************************
184  Create an auth_usersupplied_data structure after appropriate mapping.
185 ****************************************************************************/
186
187 BOOL make_user_info_map(auth_usersupplied_info **user_info, 
188                         const char *smb_name, 
189                         const char *client_domain, 
190                         const char *wksta_name, 
191                         DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
192                         DATA_BLOB plaintext, 
193                         uint32 ntlmssp_flags, BOOL encrypted)
194 {
195         const char *domain;
196         fstring internal_username;
197         fstrcpy(internal_username, smb_name);
198         map_username(internal_username); 
199         
200         if (lp_allow_trusted_domains()) {
201                 char *user;
202                 /* the client could have given us a workstation name
203                    or other crap for the workgroup - we really need a
204                    way of telling if this domain name is one of our
205                    trusted domain names 
206
207                    The way I do it here is by checking if the fully
208                    qualified username exists. This is rather reliant
209                    on winbind, but until we have a better method this
210                    will have to do 
211                 */
212                 asprintf(&user, "%s%s%s", 
213                          client_domain, lp_winbind_separator(), 
214                          smb_name);
215                 if (Get_Pwnam(user) != NULL) {
216                         domain = client_domain;
217                 } else {
218                         domain = lp_workgroup();
219                 }
220                 free(user);
221         } else {
222                 domain = lp_workgroup();
223         }
224         
225         return make_user_info(user_info, 
226                               smb_name, internal_username,
227                               client_domain, domain,
228                               wksta_name, 
229                               lm_pwd, nt_pwd,
230                               plaintext, 
231                               ntlmssp_flags, encrypted);
232         
233 }
234
235 /****************************************************************************
236  Create an auth_usersupplied_data, making the DATA_BLOBs here. 
237  Decrypt and encrypt the passwords.
238 ****************************************************************************/
239
240 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info, 
241                                      char *smb_name, 
242                                      char *client_domain, 
243                                      char *wksta_name, 
244                                      uchar *lm_network_pwd, int lm_pwd_len,
245                                      uchar *nt_network_pwd, int nt_pwd_len)
246 {
247         BOOL ret;
248         DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
249         DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
250         DATA_BLOB plaintext_blob = data_blob(NULL, 0);
251         uint32 auth_flags = AUTH_FLAG_NONE;
252
253         if (lm_pwd_len)
254                 auth_flags |= AUTH_FLAG_LM_RESP;
255         if (nt_pwd_len == 24) {
256                 auth_flags |= AUTH_FLAG_NTLM_RESP; 
257         } else if (nt_pwd_len != 0) {
258                 auth_flags |= AUTH_FLAG_NTLMv2_RESP; 
259         }
260
261         ret = make_user_info_map(user_info, 
262                                  smb_name, client_domain, 
263                                  wksta_name, 
264                                  lm_blob, nt_blob,
265                                  plaintext_blob, 
266                                  auth_flags, True);
267                 
268         data_blob_free(&lm_blob);
269         data_blob_free(&nt_blob);
270         return ret;
271 }
272
273 /****************************************************************************
274  Create an auth_usersupplied_data, making the DATA_BLOBs here. 
275  Decrypt and encrypt the passwords.
276 ****************************************************************************/
277
278 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info, 
279                                          const char *smb_name, 
280                                          const char *client_domain, 
281                                          const char *wksta_name, 
282                                          const uchar chal[8], 
283                                          const uchar lm_interactive_pwd[16], 
284                                          const uchar nt_interactive_pwd[16], 
285                                          const uchar *dc_sess_key)
286 {
287         char lm_pwd[16];
288         char nt_pwd[16];
289         unsigned char local_lm_response[24];
290         unsigned char local_nt_response[24];
291         unsigned char key[16];
292         uint32 auth_flags = AUTH_FLAG_NONE;
293         
294         ZERO_STRUCT(key);
295         memcpy(key, dc_sess_key, 8);
296         
297         if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
298         if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
299         
300 #ifdef DEBUG_PASSWORD
301         DEBUG(100,("key:"));
302         dump_data(100, (char *)key, sizeof(key));
303         
304         DEBUG(100,("lm owf password:"));
305         dump_data(100, lm_pwd, sizeof(lm_pwd));
306         
307         DEBUG(100,("nt owf password:"));
308         dump_data(100, nt_pwd, sizeof(nt_pwd));
309 #endif
310         
311         SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
312         SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
313         
314 #ifdef DEBUG_PASSWORD
315         DEBUG(100,("decrypt of lm owf password:"));
316         dump_data(100, lm_pwd, sizeof(lm_pwd));
317         
318         DEBUG(100,("decrypt of nt owf password:"));
319         dump_data(100, nt_pwd, sizeof(nt_pwd));
320 #endif
321         
322         SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
323         SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
324         
325         /* Password info parinoia */
326         ZERO_STRUCT(lm_pwd);
327         ZERO_STRUCT(nt_pwd);
328         ZERO_STRUCT(key);
329
330         {
331                 BOOL ret;
332                 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
333                 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
334                 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
335
336                 if (lm_interactive_pwd)
337                         auth_flags |= AUTH_FLAG_LM_RESP;
338                 if (nt_interactive_pwd)
339                         auth_flags |= AUTH_FLAG_NTLM_RESP; 
340
341                 ret = make_user_info_map(user_info, 
342                                          smb_name, client_domain, 
343                                          wksta_name, 
344                                          local_lm_blob,
345                                          local_nt_blob,
346                                          plaintext_blob, 
347                                          auth_flags, True);
348                 
349                 data_blob_free(&local_lm_blob);
350                 data_blob_free(&local_nt_blob);
351                 return ret;
352         }
353 }
354
355
356 /****************************************************************************
357  Create an auth_usersupplied_data structure
358 ****************************************************************************/
359
360 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info, 
361                               char *smb_name, 
362                               char *client_domain,
363                               const uint8 chal[8],
364                               DATA_BLOB plaintext_password)
365 {
366
367         DATA_BLOB local_lm_blob;
368         DATA_BLOB local_nt_blob;
369         BOOL ret = False;
370         uint32 auth_flags = AUTH_FLAG_NONE;
371                         
372         /*
373          * Not encrypted - do so.
374          */
375         
376         DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
377         
378         if (plaintext_password.data) {
379                 unsigned char local_lm_response[24];
380                 
381 #ifdef DEBUG_PASSWORD
382                 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
383                 dump_data(100, plaintext_password.data, plaintext_password.length);
384 #endif
385
386                 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
387                 local_lm_blob = data_blob(local_lm_response, 24);
388                 
389                 /* We can't do an NT hash here, as the password needs to be
390                    case insensitive */
391                 local_nt_blob = data_blob(NULL, 0); 
392                 
393                 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
394         } else {
395                 local_lm_blob = data_blob(NULL, 0); 
396                 local_nt_blob = data_blob(NULL, 0); 
397         }
398         
399         ret = make_user_info_map(user_info, smb_name,
400                                  client_domain, 
401                                  remote_machine,
402                                  local_lm_blob,
403                                  local_nt_blob,
404                                  plaintext_password, 
405                                  auth_flags, False);
406         
407         data_blob_free(&local_lm_blob);
408         return ret;
409 }
410
411 /****************************************************************************
412  Create an auth_usersupplied_data structure
413 ****************************************************************************/
414
415 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info, 
416                               char *smb_name,
417                               char *client_domain, 
418                               DATA_BLOB lm_resp, DATA_BLOB nt_resp)
419 {
420         uint32 auth_flags = AUTH_FLAG_NONE;
421
422         DATA_BLOB no_plaintext_blob = data_blob(NULL, 0); 
423         
424         if (lm_resp.length == 24) {
425                 auth_flags |= AUTH_FLAG_LM_RESP;
426         }
427         if (nt_resp.length == 0) {
428         } else if (nt_resp.length == 24) {
429                 auth_flags |= AUTH_FLAG_NTLM_RESP;
430         } else {
431                 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
432         }
433
434         return make_user_info_map(user_info, smb_name, 
435                                  client_domain, 
436                                  remote_machine, 
437                                  lm_resp, 
438                                  nt_resp, 
439                                  no_plaintext_blob, 
440                                  auth_flags, True);
441 }
442
443 /****************************************************************************
444  Create a guest user_info blob, for anonymous authenticaion.
445 ****************************************************************************/
446
447 BOOL make_user_info_guest(auth_usersupplied_info **user_info) 
448 {
449         DATA_BLOB lm_blob = data_blob(NULL, 0);
450         DATA_BLOB nt_blob = data_blob(NULL, 0);
451         DATA_BLOB plaintext_blob = data_blob(NULL, 0);
452         uint32 auth_flags = AUTH_FLAG_NONE;
453
454         return make_user_info(user_info, 
455                               "","", 
456                               "","", 
457                               "", 
458                               nt_blob, lm_blob,
459                               plaintext_blob, 
460                               auth_flags, True);
461 }
462
463 /***************************************************************************
464  Make a user_info struct
465 ***************************************************************************/
466
467 BOOL make_server_info(auth_serversupplied_info **server_info) 
468 {
469         *server_info = malloc(sizeof(**server_info));
470         if (!*server_info) {
471                 DEBUG(0,("make_server_info: malloc failed!\n"));
472                 return False;
473         }
474         ZERO_STRUCTP(*server_info);
475         return True;
476 }
477
478 /***************************************************************************
479  Make (and fill) a user_info struct from a SAM_ACCOUNT
480 ***************************************************************************/
481
482 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass) 
483 {
484         if (!make_server_info(server_info)) {
485                 return False;
486         }
487
488         (*server_info)->sam_fill_level = SAM_FILL_ALL;
489         (*server_info)->sam_account = sampass;
490
491         DEBUG(5,("make_server_info_sam: made server info for user %s\n",
492                  pdb_get_username((*server_info)->sam_account)));
493         return True;
494 }
495
496 /***************************************************************************
497  Make (and fill) a user_info struct from a 'struct passwd' by conversion 
498  to a SAM_ACCOUNT
499 ***************************************************************************/
500
501 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
502 {
503         SAM_ACCOUNT *sampass = NULL;
504         if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {         
505                 return False;
506         }
507         return make_server_info_sam(server_info, sampass);
508 }
509
510 /***************************************************************************
511  Free a user_info struct
512 ***************************************************************************/
513
514 void free_user_info(auth_usersupplied_info **user_info)
515 {
516         DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
517         if (*user_info != NULL) {
518                 if ((*user_info)->smb_name.str) {
519                         DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
520                 }
521                 SAFE_FREE((*user_info)->smb_name.str);
522                 SAFE_FREE((*user_info)->internal_username.str);
523                 SAFE_FREE((*user_info)->client_domain.str);
524                 SAFE_FREE((*user_info)->domain.str);
525                 SAFE_FREE((*user_info)->wksta_name.str);
526                 data_blob_free(&(*user_info)->lm_resp);
527                 data_blob_free(&(*user_info)->nt_resp);
528                 SAFE_FREE((*user_info)->interactive_password);
529                 data_blob_clear_free(&(*user_info)->plaintext_password);
530                 ZERO_STRUCT(**user_info);
531         }
532         SAFE_FREE(*user_info);
533 }
534
535 /***************************************************************************
536  Clear out a server_info struct that has been allocated
537 ***************************************************************************/
538
539 void free_server_info(auth_serversupplied_info **server_info)
540 {
541         if (*server_info != NULL) {
542                 pdb_free_sam(&(*server_info)->sam_account);
543                 
544                 /* call pam_end here, unless we know we are keeping it */
545                 delete_nt_token( &(*server_info)->ptok );
546                 ZERO_STRUCT(**server_info);
547         }
548         SAFE_FREE(*server_info);
549 }
550
551 /***************************************************************************
552  Make a server_info struct for a guest user 
553 ***************************************************************************/
554
555 BOOL make_server_info_guest(auth_serversupplied_info **server_info) 
556 {
557         struct passwd *pass = sys_getpwnam(lp_guestaccount());
558         
559         if (pass) {
560                 if (!make_server_info_pw(server_info, pass)) {
561                         return False;
562                 }
563                 (*server_info)->guest = True;
564                 return True;
565         }
566         DEBUG(0,("make_server_info_guest: sys_getpwnam() failed on guest account!\n")); 
567         return False;
568 }
569
570 /***************************************************************************
571  Make an auth_methods struct
572 ***************************************************************************/
573
574 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method) 
575 {
576         if (!auth_context) {
577                 smb_panic("no auth_context supplied to make_auth_methods()!\n");
578         }
579
580         if (!auth_method) {
581                 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
582         }
583
584         *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
585         if (!*auth_method) {
586                 DEBUG(0,("make_auth_method: malloc failed!\n"));
587                 return False;
588         }
589         ZERO_STRUCTP(*auth_method);
590         
591         return True;
592 }
593
594 /****************************************************************************
595  Delete a SID token.
596 ****************************************************************************/
597
598 void delete_nt_token(NT_USER_TOKEN **pptoken)
599 {
600     if (*pptoken) {
601                 NT_USER_TOKEN *ptoken = *pptoken;
602         SAFE_FREE( ptoken->user_sids );
603         ZERO_STRUCTP(ptoken);
604     }
605     SAFE_FREE(*pptoken);
606 }
607
608 /****************************************************************************
609  Duplicate a SID token.
610 ****************************************************************************/
611
612 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
613 {
614         NT_USER_TOKEN *token;
615
616         if (!ptoken)
617                 return NULL;
618
619     if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
620         return NULL;
621
622     ZERO_STRUCTP(token);
623
624     if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
625         SAFE_FREE(token);
626         return NULL;
627     }
628
629     token->num_sids = ptoken->num_sids;
630
631         return token;
632 }
633
634 /**
635  * Squash an NT_STATUS in line with security requirements.
636  * In an attempt to avoid giving the whole game away when users
637  * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and 
638  * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations 
639  * (session setups in particular).
640  *
641  * @param nt_status NTSTATUS input for squashing.
642  * @return the 'squashed' nt_status
643  **/
644
645 NTSTATUS nt_status_squash(NTSTATUS nt_status)
646 {
647         if NT_STATUS_IS_OK(nt_status) {
648                 return nt_status;               
649         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
650                 /* Match WinXP and don't give the game away */
651                 return NT_STATUS_LOGON_FAILURE;
652                 
653         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
654                 /* Match WinXP and don't give the game away */
655                 return NT_STATUS_LOGON_FAILURE;
656         } else {
657                 return nt_status;
658         }  
659 }
660
661
662