summaryrefslogtreecommitdiff
path: root/source4/kdc/kpasswd-helper.c
blob: 55a2f5b3bf69245c5010b938fdd1b8c7c39a4b3d (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
/*
   Unix SMB/CIFS implementation.

   Samba kpasswd implementation

   Copyright (c) 2005      Andrew Bartlett <abartlet@samba.org>
   Copyright (c) 2016      Andreas Schneider <asn@samba.org>

   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 "system/kerberos.h"
#include "librpc/gen_ndr/samr.h"
#include "dsdb/samdb/samdb.h"
#include "auth/auth.h"
#include "kdc/kpasswd-helper.h"

bool kpasswd_make_error_reply(TALLOC_CTX *mem_ctx,
			      krb5_error_code error_code,
			      const char *error_string,
			      DATA_BLOB *error_data)
{
	bool ok;
	char *s;
	size_t slen;

	if (error_code == 0) {
		DBG_DEBUG("kpasswd reply - %s\n", error_string);
	} else {
		DBG_INFO("kpasswd reply - %s\n", error_string);
	}

	ok = push_utf8_talloc(mem_ctx, &s, error_string, &slen);
	if (!ok) {
		return false;
	}

	/*
	 * The string 's' has one terminating nul-byte which is also
	 * reflected by 'slen'. We subtract it from the length.
	 */
	if (slen < 1) {
		talloc_free(s);
		return false;
	}
	slen--;

	/* Two bytes are added to the length to account for the error code. */
	if (2 + slen < slen) {
		talloc_free(s);
		return false;
	}
	error_data->length = 2 + slen;
	error_data->data = talloc_size(mem_ctx, error_data->length);
	if (error_data->data == NULL) {
		talloc_free(s);
		return false;
	}

	RSSVAL(error_data->data, 0, error_code);
	memcpy(error_data->data + 2, s, slen);

	talloc_free(s);

	return true;
}

bool kpasswd_make_pwchange_reply(TALLOC_CTX *mem_ctx,
				 NTSTATUS status,
				 enum samPwdChangeReason reject_reason,
				 struct samr_DomInfo1 *dominfo,
				 DATA_BLOB *error_blob)
{
	const char *reject_string = NULL;

	if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
		return kpasswd_make_error_reply(mem_ctx,
						KRB5_KPASSWD_ACCESSDENIED,
						"No such user when changing password",
						error_blob);
	} else if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
		return kpasswd_make_error_reply(mem_ctx,
						KRB5_KPASSWD_ACCESSDENIED,
						"Not permitted to change password",
						error_blob);
	}
	if (dominfo != NULL &&
	    NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
		switch (reject_reason) {
		case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
			reject_string =
				talloc_asprintf(mem_ctx,
						"Password too short, password "
						"must be at least %d characters "
						"long.",
						dominfo->min_password_length);
			if (reject_string == NULL) {
				reject_string = "Password too short";
			}
			break;
		case SAM_PWD_CHANGE_NOT_COMPLEX:
			reject_string = "Password does not meet complexity "
					"requirements";
			break;
		case SAM_PWD_CHANGE_PWD_IN_HISTORY:
			reject_string =
				talloc_asprintf(mem_ctx,
						"Password is already in password "
						"history. New password must not "
						"match any of your %d previous "
						"passwords.",
						dominfo->password_history_length);
			if (reject_string == NULL) {
				reject_string = "Password is already in password "
						"history";
			}
			break;
		default:
			reject_string = "Password change rejected, password "
					"changes may not be permitted on this "
					"account, or the minimum password age "
					"may not have elapsed.";
			break;
		}

		return kpasswd_make_error_reply(mem_ctx,
						KRB5_KPASSWD_SOFTERROR,
						reject_string,
						error_blob);
	}

	if (!NT_STATUS_IS_OK(status)) {
		reject_string = talloc_asprintf(mem_ctx,
						"Failed to set password: %s",
						nt_errstr(status));
		if (reject_string == NULL) {
			reject_string = "Failed to set password";
		}
		return kpasswd_make_error_reply(mem_ctx,
						KRB5_KPASSWD_HARDERROR,
						reject_string,
						error_blob);
	}

	return kpasswd_make_error_reply(mem_ctx,
					KRB5_KPASSWD_SUCCESS,
					"Password changed",
					error_blob);
}

NTSTATUS kpasswd_samdb_set_password(TALLOC_CTX *mem_ctx,
				    struct tevent_context *event_ctx,
				    struct loadparm_context *lp_ctx,
				    struct auth_session_info *session_info,
				    bool is_service_principal,
				    const char *target_principal_name,
				    DATA_BLOB *password,
				    enum samPwdChangeReason *reject_reason,
				    struct samr_DomInfo1 **dominfo)
{
	NTSTATUS status;
	struct ldb_context *samdb;
	struct ldb_dn *target_dn = NULL;
	int rc;

	samdb = samdb_connect(mem_ctx,
			      event_ctx,
			      lp_ctx,
			      session_info,
			      NULL,
			      0);
	if (samdb == NULL) {
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	DBG_INFO("%s\\%s (%s) is changing password of %s\n",
		 session_info->info->domain_name,
		 session_info->info->account_name,
		 dom_sid_string(mem_ctx,
				&session_info->security_token->sids[PRIMARY_USER_SID_INDEX]),
		 target_principal_name);

	rc = ldb_transaction_start(samdb);
	if (rc != LDB_SUCCESS) {
		return NT_STATUS_TRANSACTION_ABORTED;
	}

	if (is_service_principal) {
		status = crack_service_principal_name(samdb,
						      mem_ctx,
						      target_principal_name,
						      &target_dn,
						      NULL);
	} else {
		status = crack_user_principal_name(samdb,
						   mem_ctx,
						   target_principal_name,
						   &target_dn,
						   NULL);
	}
	if (!NT_STATUS_IS_OK(status)) {
		ldb_transaction_cancel(samdb);
		return status;
	}

	status = samdb_set_password(samdb,
				    mem_ctx,
				    target_dn,
				    NULL, /* domain_dn */
				    password,
				    NULL, /* lmNewHash */
				    NULL, /* ntNewHash */
				    NULL, /* lmOldHash */
				    NULL, /* ntOldHash */
				    reject_reason,
				    dominfo);
	if (NT_STATUS_IS_OK(status)) {
		rc = ldb_transaction_commit(samdb);
		if (rc != LDB_SUCCESS) {
			DBG_WARNING("Failed to commit transaction to "
				    "set password on %s: %s\n",
				    ldb_dn_get_linearized(target_dn),
				    ldb_errstring(samdb));
			return NT_STATUS_TRANSACTION_ABORTED;
		}
	} else {
		ldb_transaction_cancel(samdb);
	}

	return status;
}