brought the winbindd code into head
[ira/wip.git] / source3 / nsswitch / winbindd_pam.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4
5    Winbind daemon - pam auuth funcions
6
7    Copyright (C) Andrew Tridgell 2000
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 "winbindd.h"
25 /************************************************************************
26 form a key for fetching a domain trust password
27 ************************************************************************/
28 static char *trust_keystr(char *domain)
29 {
30         static fstring keystr;
31         slprintf(keystr,sizeof(keystr),"%s/%s", SECRETS_MACHINE_ACCT_PASS, domain);
32         return keystr;
33 }
34
35 /************************************************************************
36  Routine to get the trust account password for a domain.
37  The user of this function must have locked the trust password file.
38 ************************************************************************/
39 static BOOL _get_trust_account_password(char *domain, unsigned char *ret_pwd, time_t *pass_last_set_time)
40 {
41         struct machine_acct_pass *pass;
42         size_t size;
43
44         if (!(pass = secrets_fetch(trust_keystr(domain), &size)) ||
45             size != sizeof(*pass)) return False;
46
47         if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
48         memcpy(ret_pwd, pass->hash, 16);
49         free(pass);
50         return True;
51 }
52
53
54 /* Return a password structure from a username.  Specify whether cached data 
55    can be returned. */
56
57 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
58 {
59         NET_USER_INFO_3 info3;
60         uchar ntpw[16];
61         uchar lmpw[16];
62         uchar trust_passwd[16];
63         uint32 status;
64         fstring server;
65         fstring name_domain, name_user;
66         extern pstring global_myname;
67
68         DEBUG(1,("winbindd_pam_auth user=%s\n", 
69                  state->request.data.auth.user));
70
71         /* Parse domain and username */
72         parse_domain_user(state->request.data.auth.user, name_domain, name_user);
73
74         /* don't allow the null domain */
75         if (strcmp(name_domain,"") == 0) return WINBINDD_ERROR;
76
77         ZERO_STRUCT(info3);
78
79         if (!_get_trust_account_password(name_domain, trust_passwd, NULL)) return WINBINDD_ERROR;
80
81         nt_lm_owf_gen(state->request.data.auth.pass, ntpw, lmpw);
82
83         slprintf(server, sizeof(server), "\\\\%s", server_state.controller);
84
85         status = domain_client_validate_backend(server, 
86                                                 name_user, name_domain,
87                                                 global_myname, SEC_CHAN_WKSTA,
88                                                 trust_passwd,
89                                                 NULL,
90                                                 lmpw, sizeof(lmpw),
91                                                 ntpw, sizeof(ntpw), &info3);
92
93         if (status != NT_STATUS_NOPROBLEMO) return WINBINDD_ERROR;
94
95         return WINBINDD_OK;
96 }
97