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