summaryrefslogtreecommitdiffstats
path: root/source/nsswitch/winbindd_pam.c
blob: c648090bbeb483055c5a49c032455058523fc2dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* 
   Unix SMB/Netbios implementation.
   Version 3.0

   Winbind daemon - pam auuth funcions

   Copyright (C) Andrew Tridgell 2000
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "winbindd.h"

/* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
   form DOMAIN/user into a domain and a user */

static BOOL parse_domain_user(char *domuser, fstring domain, fstring user)
{
        char *p = strchr(domuser,*lp_winbind_separator());

        if (!p)
		return False;
        
        fstrcpy(user, p+1);
        fstrcpy(domain, domuser);
        domain[PTR_DIFF(p, domuser)] = 0;
	unix_to_dos(domain, True);
	strupper(domain);
	dos_to_unix(domain, True);
	return True;
}

/* Authenticate a user from a plaintext password */

enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
{
	NET_USER_INFO_3 info3;
	uchar ntpw[16];
	uchar lmpw[16];
	uchar trust_passwd[16];
	uint32 status;
	fstring server;
	fstring name_domain, name_user;
	extern pstring global_myname;

	DEBUG(3, ("[%5d]: pam auth %s\n", state->pid,
		  state->request.data.auth.user));

	/* Parse domain and username */
	if (!parse_domain_user(state->request.data.auth.user, name_domain, 
                          name_user))
		return WINBINDD_ERROR;

	ZERO_STRUCT(info3);

	if (!_get_trust_account_password(lp_workgroup(), trust_passwd, NULL)) {
            DEBUG(1, ("could not get trust password for domain %s\n",
                      name_domain));
            return WINBINDD_ERROR;
        }

	nt_lm_owf_gen(state->request.data.auth.pass, ntpw, lmpw);

	slprintf(server, sizeof(server), "\\\\%s", server_state.controller);

	status = domain_client_validate_backend(server, 
						name_user, name_domain,
						global_myname, SEC_CHAN_WKSTA,
						trust_passwd,
						NULL,
						lmpw, sizeof(lmpw),
						ntpw, sizeof(ntpw), &info3);

	if (status != NT_STATUS_NOPROBLEMO) {
                DEBUG(3, ("winbindd_pam_auth() failed with status 0x%08x\n",
                          status));
                return WINBINDD_ERROR;
        }

	return WINBINDD_OK;
}

/* Authenticate a user from a challenge/response */

enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
{
	NET_USER_INFO_3 info3;
	uchar trust_passwd[16];
	uint32 status;
	fstring server;
	fstring name_domain, name_user;
	extern pstring global_myname;

	DEBUG(3, ("[%5d]: pam auth crap %s\n", state->pid,
		  state->request.data.auth_crap.user));

	/* Parse domain and username */
	if (!parse_domain_user(state->request.data.auth_crap.user, 
                          name_domain, name_user))
		return WINBINDD_ERROR;

	ZERO_STRUCT(info3);

	if (!_get_trust_account_password(lp_workgroup(), trust_passwd, NULL)) {
            DEBUG(1, ("could not get trust password for domain %s\n",
                      name_domain));
            return WINBINDD_ERROR;
        }

	slprintf(server, sizeof(server), "\\\\%s", server_state.controller);

	status = domain_client_validate_backend
          (server, name_user, name_domain, global_myname, SEC_CHAN_WKSTA,
           trust_passwd, state->request.data.auth_crap.chal,
           state->request.data.auth_crap.lm_resp, 
	   state->request.data.auth_crap.lm_resp_len,
           state->request.data.auth_crap.nt_resp, 
	   state->request.data.auth_crap.nt_resp_len,
           &info3);

	if (status != NT_STATUS_NOPROBLEMO) {
                DEBUG(3, ("winbindd_pam_auth() failed with status 0x%08x\n",
                          status));
                return WINBINDD_ERROR;
        }

	return WINBINDD_OK;
}

/* Change a user password */

enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
{
    char *oldpass, *newpass;
    fstring domain, user;
    uchar nt_oldhash[16];
    uchar lm_oldhash[16];

    DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
	      state->request.data.chauthtok.user));

    /* Setup crap */

    if (state == NULL) return WINBINDD_ERROR;

    if (!parse_domain_user(state->request.data.chauthtok.user, domain, user))
        return WINBINDD_ERROR;

    oldpass = state->request.data.chauthtok.oldpass;
    newpass = state->request.data.chauthtok.newpass;

    nt_lm_owf_gen(oldpass, nt_oldhash, lm_oldhash);

    /* Change password */

    if (!msrpc_sam_ntchange_pwd(server_state.controller, domain, user,
                               lm_oldhash, nt_oldhash, newpass)) {
        DEBUG(0, ("password change failed for user %s/%s\n", domain, user));
        return WINBINDD_ERROR;
    }
    
    return WINBINDD_OK;
}