991efccae389509345e2b95c0161b4fdfbb38570
[samba.git] / source4 / rpc_server / drsuapi / writespn.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    implement the DsWriteAccountSpn call
5
6    Copyright (C) Stefan Metzmacher 2009
7    Copyright (C) Andrew Tridgell   2010
8
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.
13
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.
18
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/>.
21 */
22
23 #include "includes.h"
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 "librpc/gen_ndr/ndr_drsuapi.h"
33 #include "auth/session.h"
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS            DBGC_DRS_REPL
37
38 /*
39   check that the SPN update should be allowed as an override
40   via sam_ctx_system
41
42   This is only called if the client is not a domain controller or
43   administrator
44  */
45 static bool writespn_check_spn(struct drsuapi_bind_state *b_state,
46                                struct dcesrv_call_state *dce_call,
47                                struct ldb_dn *dn,
48                                const char *spn)
49 {
50         /*
51          * we only allow SPN updates if:
52          *
53          * 1) they are on the clients own account object
54          * 2) they are of the form SERVICE/dnshostname
55          */
56         struct dom_sid *user_sid, *sid;
57         TALLOC_CTX *tmp_ctx = talloc_new(dce_call);
58         struct ldb_result *res;
59         const char *attrs[] = { "objectSID", "dNSHostName", NULL };
60         int ret;
61         krb5_context krb_ctx;
62         krb5_error_code kerr;
63         krb5_principal principal;
64         const krb5_data *component;
65         const char *dns_name, *dnsHostName;
66
67         /* The service principal name shouldn't be NULL */
68         if (spn == NULL) {
69                 talloc_free(tmp_ctx);
70                 return false;
71         }
72
73         /*
74           get the objectSid of the DN that is being modified, and
75           check it matches the user_sid in their token
76          */
77
78         ret = dsdb_search_dn(b_state->sam_ctx, tmp_ctx, &res, dn, attrs,
79                              DSDB_SEARCH_ONE_ONLY);
80         if (ret != LDB_SUCCESS) {
81                 talloc_free(tmp_ctx);
82                 return false;
83         }
84
85         user_sid = &dce_call->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
86         sid = samdb_result_dom_sid(tmp_ctx, res->msgs[0], "objectSid");
87         if (sid == NULL) {
88                 talloc_free(tmp_ctx);
89                 return false;
90         }
91
92         dnsHostName = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName",
93                                                   NULL);
94         if (dnsHostName == NULL) {
95                 talloc_free(tmp_ctx);
96                 return false;
97         }
98
99         if (!dom_sid_equal(sid, user_sid)) {
100                 talloc_free(tmp_ctx);
101                 return false;
102         }
103
104         kerr = smb_krb5_init_context_basic(tmp_ctx,
105                                            dce_call->conn->dce_ctx->lp_ctx,
106                                            &krb_ctx);
107         if (kerr != 0) {
108                 talloc_free(tmp_ctx);
109                 return false;
110         }
111
112         ret = krb5_parse_name_flags(krb_ctx, spn, KRB5_PRINCIPAL_PARSE_NO_REALM,
113                                     &principal);
114         if (kerr != 0) {
115                 krb5_free_context(krb_ctx);
116                 talloc_free(tmp_ctx);
117                 return false;
118         }
119
120         if (krb5_princ_size(krb_ctx, principal) != 2) {
121                 krb5_free_principal(krb_ctx, principal);
122                 krb5_free_context(krb_ctx);
123                 talloc_free(tmp_ctx);
124                 return false;
125         }
126
127         component = krb5_princ_component(krb_ctx, principal, 1);
128         dns_name = (const char *)component->data;
129
130         if (strcasecmp(dns_name, dnsHostName) != 0) {
131                 krb5_free_principal(krb_ctx, principal);
132                 krb5_free_context(krb_ctx);
133                 talloc_free(tmp_ctx);
134                 return false;
135         }
136
137         /* its a simple update on their own account - allow it with
138          * permissions override */
139         krb5_free_principal(krb_ctx, principal);
140         krb5_free_context(krb_ctx);
141         talloc_free(tmp_ctx);
142
143         return true;
144 }
145
146 /*
147   drsuapi_DsWriteAccountSpn
148 */
149 WERROR dcesrv_drsuapi_DsWriteAccountSpn(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
150                                         struct drsuapi_DsWriteAccountSpn *r)
151 {
152         struct drsuapi_bind_state *b_state;
153         struct dcesrv_handle *h;
154
155         *r->out.level_out = r->in.level;
156
157         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
158         b_state = h->data;
159
160         r->out.res = talloc(mem_ctx, union drsuapi_DsWriteAccountSpnResult);
161         W_ERROR_HAVE_NO_MEMORY(r->out.res);
162
163         switch (r->in.level) {
164                 case 1: {
165                         struct drsuapi_DsWriteAccountSpnRequest1 *req;
166                         struct ldb_message *msg;
167                         uint32_t count;
168                         unsigned int i;
169                         int ret;
170                         unsigned spn_count=0;
171                         bool passed_checks = true;
172                         struct ldb_context *sam_ctx;
173
174                         req = &r->in.req->req1;
175                         count = req->count;
176
177                         msg = ldb_msg_new(mem_ctx);
178                         if (msg == NULL) {
179                                 return WERR_NOT_ENOUGH_MEMORY;
180                         }
181
182                         msg->dn = ldb_dn_new(msg, b_state->sam_ctx,
183                                              req->object_dn);
184                         if ( ! ldb_dn_validate(msg->dn)) {
185                                 r->out.res->res1.status = WERR_OK;
186                                 return WERR_OK;
187                         }
188
189                         /* construct mods */
190                         for (i = 0; i < count; i++) {
191                                 if (!writespn_check_spn(b_state,
192                                                        dce_call,
193                                                        msg->dn,
194                                                        req->spn_names[i].str)) {
195                                         passed_checks = false;
196                                 }
197                                 ret = ldb_msg_add_string(msg,
198                                                          "servicePrincipalName",
199                                                          req->spn_names[i].str);
200                                 if (ret != LDB_SUCCESS) {
201                                         return WERR_NOT_ENOUGH_MEMORY;
202                                 }
203                                 spn_count++;
204                         }
205
206                         if (msg->num_elements == 0) {
207                                 DEBUG(2,("No SPNs need changing on %s\n",
208                                          ldb_dn_get_linearized(msg->dn)));
209                                 r->out.res->res1.status = WERR_OK;
210                                 return WERR_OK;
211                         }
212
213                         for (i=0;i<msg->num_elements;i++) {
214                                 switch (req->operation) {
215                                 case DRSUAPI_DS_SPN_OPERATION_ADD:
216                                         msg->elements[i].flags = LDB_FLAG_MOD_ADD;
217                                         break;
218                                 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
219                                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
220                                         break;
221                                 case DRSUAPI_DS_SPN_OPERATION_DELETE:
222                                         msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
223                                         break;
224                                 }
225                         }
226
227                         if (passed_checks && b_state->sam_ctx_system) {
228                                 sam_ctx = b_state->sam_ctx_system;
229                         } else {
230                                 sam_ctx = b_state->sam_ctx;
231                         }
232
233                         /* Apply to database */
234                         ret = dsdb_modify(sam_ctx, msg, DSDB_MODIFY_PERMISSIVE);
235                         if (ret != LDB_SUCCESS) {
236                                 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
237                                          ldb_dn_get_linearized(msg->dn),
238                                          ldb_errstring(b_state->sam_ctx)));
239                                 NDR_PRINT_IN_DEBUG(
240                                         drsuapi_DsWriteAccountSpn, r);
241                                 r->out.res->res1.status = WERR_ACCESS_DENIED;
242                         } else {
243                                 DEBUG(2,("Modified %u SPNs on %s\n", spn_count,
244                                          ldb_dn_get_linearized(msg->dn)));
245                                 r->out.res->res1.status = WERR_OK;
246                         }
247
248                         return WERR_OK;
249                 }
250         }
251
252         return WERR_INVALID_LEVEL;
253 }