summaryrefslogtreecommitdiff
path: root/source3/pwd_validate.c
blob: 2eefa1e5f077e9801ff030760d877cab4591adcd (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Password and authentication handling
   Copyright (C) Andrew Tridgell 1992-1997
   
   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 "includes.h"

extern int DEBUGLEVEL;
extern int Protocol;


/****************************************************************************
initiates a connection to a user-level server.
****************************************************************************/
BOOL server_connect_init(struct cli_state *clnt, char my_netbios_name[16],
				struct in_addr dest_ip, char *desthost)
{
	DEBUG(5,("password server connection requested: %s [%s]\n",
		desthost, inet_ntoa(dest_ip)));

	if (clnt == NULL || !clnt->initialised)
	{
		DEBUG(1,("password server client state not initialised\n", desthost));
		return(False);
	}  

	if (!cli_connect(clnt, desthost, &dest_ip))
	{
		DEBUG(3,("connected to password server %s\n", desthost));
		return False;
	}

	if (!cli_session_request(clnt, desthost, 0x20, my_netbios_name, 0x0))
	{
		DEBUG(1,("%s rejected the session\n", desthost));
		return False;
	}

	DEBUG(3,("got session\n"));

	if (!cli_negprot(clnt))
	{
		DEBUG(1,("%s rejected the negprot\n", desthost));
		return False;
	}

	if (clnt->protocol < PROTOCOL_LANMAN2 || !(clnt->sec_mode & 1))
	{
		DEBUG(1,("%s isn't in user level security mode\n", desthost));
		return False;
	}

	DEBUG(3,("password server OK\n"));

	return True;
}

/****************************************************************************
support for server level security 
****************************************************************************/
BOOL server_cryptkey(struct cli_state *clnt, char my_netbios_name[16])
{
	fstring desthost;
	struct in_addr dest_ip;
	char *p;
	BOOL connection_found = False;

	if (clnt == NULL || !cli_initialise(clnt))
	{
		DEBUG(1,("server_cryptkey: failed to initialise client state\n"));
		return False;
	}
	    
	for (p = strtok(lp_passwordserver(),LIST_SEP); p && !connection_found; p = strtok(NULL,LIST_SEP))
	{
		fstrcpy(desthost,p);
		standard_sub_basic(desthost);
		strupper(desthost);

		dest_ip = *interpret_addr2(desthost);

		if (zero_ip(dest_ip))
		{
			DEBUG(1,("Can't resolve address for %s\n",p));
			continue;
		}

		if (ismyip(dest_ip))
		{
			DEBUG(1,("Password server loop - disabling password server %s\n", p));
			continue;
		}

		connection_found = server_connect_init(clnt, my_netbios_name, dest_ip, desthost);
	}

	if (!p && !connection_found)
	{
		DEBUG(1,("password server not available\n"));
		cli_shutdown(clnt);
		return False;
	}

	DEBUG(3,("password server OK\n"));

	return True;
}

/****************************************************************************
validate a password
****************************************************************************/
BOOL server_validate2(struct cli_state *clnt, char *user, char *domain, 
		     char *pass, int passlen,
		     char *ntpass, int ntpasslen)
{
	BOOL pwd_ok = False;

	DEBUG(4,("server_validate2: user:[%s] domain:[%s]\n", user, domain));

	if (clnt == NULL)
	{
		DEBUG(1,("server_validate2: NULL client_state. cannot validate\n"));
		return(False);
	}  

	if (pass == NULL && ntpass == NULL)
	{
		DEBUG(1,("server_validate2: both lm and nt passwords are NULL.  cannot validate\n"));
		return(False);
	}  
	
	if (!clnt->initialised)
	{
		DEBUG(1,("server %s is not connected\n", clnt->full_dest_host_name));
		return(False);
	}  

	if (!cli_session_setup(clnt, user, pass, passlen, ntpass, ntpasslen, domain)) {
		DEBUG(1,("server %s rejected the password\n", clnt->full_dest_host_name));
		return False;
	}

	/* if logged in as guest then reject */
	if ((SVAL(clnt->inbuf,smb_vwv2) & 1) != 0) {
		DEBUG(1,("server %s gave us guest only\n", clnt->full_dest_host_name));
		return(False);
	}

	pwd_ok = False;

	if (!pwd_ok)
	{
		pwd_ok = pass != NULL && !cli_send_tconX(clnt, "IPC$", "IPC", pass, passlen);
	}

	if (!pwd_ok)
	{
		DEBUG(1,("server %s refused IPC$ connect with LM password\n", clnt->full_dest_host_name));
	}

	if (!pwd_ok)
	{
		pwd_ok = ntpass != NULL && !cli_send_tconX(clnt, "IPC$", "IPC", ntpass, ntpasslen);
	}

	if (!pwd_ok)
	{
		DEBUG(1,("server %s refused IPC$ connect with NT password\n", clnt->full_dest_host_name));
		return False;
	}

	if (!pwd_ok)
	{
		DEBUG(3,("server %s rejected the password\n", clnt->full_dest_host_name));
	}
	else
	{
		DEBUG(3,("server %s accepted the password\n", clnt->full_dest_host_name));
	}

	cli_tdis(clnt);

	return(True);
}


/****************************************************************************
validate a password with the password server.  here's some good code to go
into an SMB pam, by the way...
****************************************************************************/
BOOL server_validate(struct cli_state *clnt, char *user, char *domain, 
		     char *pass, int passlen,
		     char *ntpass, int ntpasslen)
{
	if (clnt == NULL)
	{
		DEBUG(1,("server_validate: NULL client_state. cannot validate\n"));
		return(False);
	}  

	if (!clnt->initialised) {
		DEBUG(1,("password server %s is not connected\n", clnt->full_dest_host_name));
		return(False);
	}  

	if (!cli_session_setup(clnt, user, pass, passlen, ntpass, ntpasslen, domain)) {
		DEBUG(1,("password server %s rejected the password\n", clnt->full_dest_host_name));
		return False;
	}

	/* if logged in as guest then reject */
	if ((SVAL(clnt->inbuf,smb_vwv2) & 1) != 0) {
		DEBUG(1,("password server %s gave us guest only\n", clnt->full_dest_host_name));
		return(False);
	}


	if (!cli_send_tconX(clnt, "IPC$", "IPC", "", 1))
    {
		DEBUG(1,("password server %s refused IPC$ connect\n", clnt->full_dest_host_name));
		return False;
	}


	if (!cli_send_tconX(clnt, "IPC$", "IPC", "", 1)) {
		DEBUG(1,("password server %s refused IPC$ connect\n", clnt->full_dest_host_name));
		return False;
	}


	if (!cli_NetWkstaUserLogon(clnt, user, clnt->called_netbios_name))
	{
		DEBUG(1,("password server %s failed NetWkstaUserLogon\n", clnt->full_dest_host_name));
		cli_tdis(clnt);
		return False;
	}

	if (clnt->privileges == 0)
	{
		DEBUG(1,("password server %s gave guest privilages\n", clnt->full_dest_host_name));
		cli_tdis(clnt);
		return False;
	}

	if (!strequal(clnt->eff_name, user)) {
		DEBUG(1,("password server %s gave different username %s\n", 
			 clnt->full_dest_host_name,
			 clnt->eff_name));
		cli_tdis(clnt);
		return False;
	}

	DEBUG(3,("password server %s accepted the password\n", clnt->full_dest_host_name));

	cli_tdis(clnt);

	return(True);
}