2 Unix SMB/CIFS implementation.
4 implement the DsWriteAccountSpn call
6 Copyright (C) Stefan Metzmacher 2009
7 Copyright (C) Andrew Tridgell 2010
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 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "dsdb/common/util.h"
27 #include "system/kerberos.h"
28 #include "auth/kerberos/kerberos.h"
29 #include "libcli/security/security.h"
30 #include "libcli/security/session.h"
31 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
32 #include "auth/session.h"
35 check that the SPN update should be allowed as an override
38 This is only called if the client is not a domain controller or
41 static bool writespn_check_spn(struct drsuapi_bind_state
*b_state
,
42 struct dcesrv_call_state
*dce_call
,
47 * we only allow SPN updates if:
49 * 1) they are on the clients own account object
50 * 2) they are of the form SERVICE/dnshostname
52 struct dom_sid
*user_sid
, *sid
;
53 TALLOC_CTX
*tmp_ctx
= talloc_new(dce_call
);
54 struct ldb_result
*res
;
55 const char *attrs
[] = { "objectSID", "dNSHostName", NULL
};
59 krb5_principal principal
;
60 const char *dns_name
, *dnsHostName
;
62 /* The service principal name shouldn't be NULL */
69 get the objectSid of the DN that is being modified, and
70 check it matches the user_sid in their token
73 ret
= dsdb_search_dn(b_state
->sam_ctx
, tmp_ctx
, &res
, dn
, attrs
,
74 DSDB_SEARCH_ONE_ONLY
);
75 if (ret
!= LDB_SUCCESS
) {
80 user_sid
= &dce_call
->conn
->auth_state
.session_info
->security_token
->sids
[PRIMARY_USER_SID_INDEX
];
81 sid
= samdb_result_dom_sid(tmp_ctx
, res
->msgs
[0], "objectSid");
87 dnsHostName
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName",
89 if (dnsHostName
== NULL
) {
94 if (!dom_sid_equal(sid
, user_sid
)) {
99 kerr
= smb_krb5_init_context_basic(tmp_ctx
,
100 dce_call
->conn
->dce_ctx
->lp_ctx
,
103 talloc_free(tmp_ctx
);
107 ret
= krb5_parse_name_flags(krb_ctx
, spn
, KRB5_PRINCIPAL_PARSE_NO_REALM
,
110 krb5_free_context(krb_ctx
);
111 talloc_free(tmp_ctx
);
115 if (principal
->name
.name_string
.len
!= 2) {
116 krb5_free_principal(krb_ctx
, principal
);
117 krb5_free_context(krb_ctx
);
118 talloc_free(tmp_ctx
);
122 dns_name
= principal
->name
.name_string
.val
[1];
124 if (strcasecmp(dns_name
, dnsHostName
) != 0) {
125 krb5_free_principal(krb_ctx
, principal
);
126 krb5_free_context(krb_ctx
);
127 talloc_free(tmp_ctx
);
131 /* its a simple update on their own account - allow it with
132 * permissions override */
133 krb5_free_principal(krb_ctx
, principal
);
134 krb5_free_context(krb_ctx
);
135 talloc_free(tmp_ctx
);
141 drsuapi_DsWriteAccountSpn
143 WERROR
dcesrv_drsuapi_DsWriteAccountSpn(struct dcesrv_call_state
*dce_call
, TALLOC_CTX
*mem_ctx
,
144 struct drsuapi_DsWriteAccountSpn
*r
)
146 struct drsuapi_bind_state
*b_state
;
147 struct dcesrv_handle
*h
;
148 enum security_user_level level
;
150 *r
->out
.level_out
= r
->in
.level
;
152 DCESRV_PULL_HANDLE_WERR(h
, r
->in
.bind_handle
, DRSUAPI_BIND_HANDLE
);
155 r
->out
.res
= talloc(mem_ctx
, union drsuapi_DsWriteAccountSpnResult
);
156 W_ERROR_HAVE_NO_MEMORY(r
->out
.res
);
158 level
= security_session_user_level(dce_call
->conn
->auth_state
.session_info
, NULL
);
160 switch (r
->in
.level
) {
162 struct drsuapi_DsWriteAccountSpnRequest1
*req
;
163 struct ldb_message
*msg
;
167 unsigned spn_count
=0;
168 bool passed_checks
= true;
169 struct ldb_context
*sam_ctx
;
171 req
= &r
->in
.req
->req1
;
174 msg
= ldb_msg_new(mem_ctx
);
179 msg
->dn
= ldb_dn_new(msg
, b_state
->sam_ctx
,
181 if ( ! ldb_dn_validate(msg
->dn
)) {
182 r
->out
.res
->res1
.status
= WERR_OK
;
187 for (i
= 0; i
< count
; i
++) {
188 if (!writespn_check_spn(b_state
,
191 req
->spn_names
[i
].str
)) {
192 passed_checks
= false;
194 ret
= ldb_msg_add_string(msg
,
195 "servicePrincipalName",
196 req
->spn_names
[i
].str
);
197 if (ret
!= LDB_SUCCESS
) {
203 if (msg
->num_elements
== 0) {
204 DEBUG(2,("No SPNs need changing on %s\n",
205 ldb_dn_get_linearized(msg
->dn
)));
206 r
->out
.res
->res1
.status
= WERR_OK
;
210 for (i
=0;i
<msg
->num_elements
;i
++) {
211 switch (req
->operation
) {
212 case DRSUAPI_DS_SPN_OPERATION_ADD
:
213 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
215 case DRSUAPI_DS_SPN_OPERATION_REPLACE
:
216 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
218 case DRSUAPI_DS_SPN_OPERATION_DELETE
:
219 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
224 if (passed_checks
&& b_state
->sam_ctx_system
) {
225 sam_ctx
= b_state
->sam_ctx_system
;
227 sam_ctx
= b_state
->sam_ctx
;
230 /* Apply to database */
231 ret
= dsdb_modify(sam_ctx
, msg
, DSDB_MODIFY_PERMISSIVE
);
232 if (ret
!= LDB_SUCCESS
) {
233 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
234 ldb_dn_get_linearized(msg
->dn
),
235 ldb_errstring(b_state
->sam_ctx
)));
236 r
->out
.res
->res1
.status
= WERR_ACCESS_DENIED
;
238 DEBUG(2,("Modified %u SPNs on %s\n", spn_count
,
239 ldb_dn_get_linearized(msg
->dn
)));
240 r
->out
.res
->res1
.status
= WERR_OK
;
247 return WERR_UNKNOWN_LEVEL
;