summaryrefslogtreecommitdiffstats
path: root/source3/winbindd/winbindd_pam_auth.c
blob: 431da098f0b855e24439607dfb0b075fad8d4037 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
   Unix SMB/CIFS implementation.
   async implementation of WINBINDD_PAM_AUTH
   Copyright (C) Volker Lendecke 2010

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "winbindd.h"
#include "libcli/security/dom_sid.h"
#include "lib/util/string_wrappers.h"
#include "lib/global_contexts.h"
#include "librpc/gen_ndr/ndr_winbind_c.h"

static NTSTATUS fake_password_policy(struct winbindd_response *r,
				     uint16_t validation_level,
				     union netr_Validation  *validation)
{
	const struct netr_SamBaseInfo *bi = NULL;
	NTTIME min_password_age;
	NTTIME max_password_age;

	switch (validation_level) {
	case 3:
		bi = &validation->sam3->base;
		break;
	case 6:
		bi = &validation->sam6->base;
		break;
	default:
		return NT_STATUS_INTERNAL_ERROR;
	}

	if (bi->allow_password_change > bi->last_password_change) {
		min_password_age = bi->allow_password_change -
				   bi->last_password_change;
	} else {
		min_password_age = 0;
	}

	if (bi->force_password_change > bi->last_password_change) {
		max_password_age = bi->force_password_change -
				   bi->last_password_change;
	} else {
		max_password_age = 0;
	}

	r->data.auth.policy.min_length_password = 0;
	r->data.auth.policy.password_history = 0;
	r->data.auth.policy.password_properties = 0;
	r->data.auth.policy.expire =
		nt_time_to_unix_abs(&max_password_age);
	r->data.auth.policy.min_passwordage =
		nt_time_to_unix_abs(&min_password_age);

	return NT_STATUS_OK;
}

struct winbindd_pam_auth_state {
	struct wbint_PamAuth *r;
	char *name_namespace;
	char *name_domain;
	char *name_user;
};

static void winbindd_pam_auth_done(struct tevent_req *subreq);

struct tevent_req *winbindd_pam_auth_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  struct winbindd_cli_state *cli,
					  struct winbindd_request *request)
{
	struct tevent_req *req, *subreq;
	struct winbindd_pam_auth_state *state;
	struct winbindd_domain *domain;
	char *mapped = NULL;
	char *auth_user = NULL;
	NTSTATUS status;
	bool ok;

	req = tevent_req_create(mem_ctx, &state,
				struct winbindd_pam_auth_state);
	if (req == NULL) {
		return NULL;
	}

	D_NOTICE("[%s (%u)] Winbind external command PAM_AUTH start.\n"
		 "Authenticating user '%s'.\n",
		 cli->client_name,
		 (unsigned int)cli->pid,
		 request->data.auth.user);

	if (!check_request_flags(request->flags)) {
		tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
		return tevent_req_post(req, ev);
	}

	/* Parse domain and username */

	status = normalize_name_unmap(state, request->data.auth.user, &mapped);

	/* If the name normalization changed something, copy it over the given
	   name */

	if (NT_STATUS_IS_OK(status)
	    || NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
		fstrcpy(request->data.auth.user, mapped);
	}

	auth_user = request->data.auth.user;
	ok = canonicalize_username(state,
				   &auth_user,
				   &state->name_namespace,
				   &state->name_domain,
				   &state->name_user);
	if (!ok) {
		tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
		return tevent_req_post(req, ev);
	}

	fstrcpy(request->data.auth.user, auth_user);

	domain = find_auth_domain(request->flags, state->name_namespace);
	if (domain == NULL) {
		tevent_req_nterror(req, NT_STATUS_NO_SUCH_USER);
		return tevent_req_post(req, ev);
	}

	state->r = talloc_zero(state, struct wbint_PamAuth);
	if (tevent_req_nomem(state->r, req)) {
		return tevent_req_post(req, ev);
	}

	state->r->in.client_name = talloc_strdup(
			state->r, request->client_name);
	if (tevent_req_nomem(state->r, req)) {
		return tevent_req_post(req, ev);
	}

	state->r->in.client_pid = request->pid;
	state->r->in.flags = request->flags;

	state->r->in.info = talloc_zero(state->r, struct wbint_AuthUserInfo);
	if (tevent_req_nomem(state->r, req)) {
		return tevent_req_post(req, ev);
	}

	state->r->in.info->krb5_cc_type = talloc_strdup(
			state->r, request->data.auth.krb5_cc_type);
	if (tevent_req_nomem(state->r, req)) {
		return tevent_req_post(req, ev);
	}

	state->r->in.info->password = talloc_strdup(
			state->r, request->data.auth.pass);
	if (tevent_req_nomem(state->r, req)) {
		return tevent_req_post(req, ev);
	}

	state->r->in.info->username = talloc_strdup(
			state->r, request->data.auth.user);
	if (tevent_req_nomem(state->r, req)) {
		return tevent_req_post(req, ev);
	}

	state->r->in.info->uid = request->data.auth.uid;

	status = extra_data_to_sid_array(
				request->data.auth.require_membership_of_sid,
				state->r,
				&state->r->in.require_membership_of_sid);
	if (tevent_req_nterror(req, status)) {
		return tevent_req_post(req, ev);
	}

	subreq = dcerpc_wbint_PamAuth_r_send(state,
					     global_event_context(),
					     dom_child_handle(domain),
					     state->r);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, winbindd_pam_auth_done, req);
	return req;
}

static void winbindd_pam_auth_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct winbindd_pam_auth_state *state = tevent_req_data(
		req, struct winbindd_pam_auth_state);
	NTSTATUS status;

	status = dcerpc_wbint_PamAuth_r_recv(subreq, state);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	if (tevent_req_nterror(req, state->r->out.result)) {
		return;
	}

	tevent_req_done(req);
}

NTSTATUS winbindd_pam_auth_recv(struct tevent_req *req,
				struct winbindd_response *response)
{
	struct winbindd_pam_auth_state *state = tevent_req_data(
		req, struct winbindd_pam_auth_state);
	NTSTATUS status;

	D_NOTICE("Winbind external command PAM_AUTH end.\n");
	if (tevent_req_is_nterror(req, &status)) {
		set_auth_errors(response, status);
		return status;
	}

	response->result = WINBINDD_PENDING;

	status = append_auth_data(response,
				  response,
				  state->r->in.flags,
				  state->r->out.validation->level,
				  state->r->out.validation->validation,
				  state->name_domain,
				  state->name_user);
	fstrcpy(response->data.auth.krb5ccname,
		state->r->out.validation->krb5ccname);

	if (state->r->in.flags & WBFLAG_PAM_INFO3_TEXT) {
		bool ok;

		ok = add_trusted_domain_from_auth(
			state->r->out.validation->level,
			&response->data.auth.info3,
			&response->data.auth.info6);
		if (!ok) {
			DBG_ERR("add_trusted_domain_from_auth failed\n");
			set_auth_errors(response, NT_STATUS_LOGON_FAILURE);
			return NT_STATUS_LOGON_FAILURE;
		}
	}

	if (state->r->in.flags & WBFLAG_PAM_CACHED_LOGIN) {

		/* Store in-memory creds for single-signon using ntlm_auth. */

		status = winbindd_add_memory_creds(
			state->r->in.info->username,
			state->r->in.info->uid,
			state->r->in.info->password);
		D_DEBUG("winbindd_add_memory_creds returned: %s\n",
			   nt_errstr(status));
	}

	if (state->r->in.flags & WBFLAG_PAM_GET_PWD_POLICY) {
		/*
		 * WBFLAG_PAM_GET_PWD_POLICY is not used within
		 * any Samba caller anymore.
		 *
		 * We just fake this based on the effective values
		 * for the user, for legacy callers.
		 */
		status = fake_password_policy(response,
				state->r->out.validation->level,
				state->r->out.validation->validation);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_ERR("Failed to fake password policy: %s\n",
				nt_errstr(status));
			set_auth_errors(response, status);
			return status;
		}
	}

	return NT_STATUS_OK;
}