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