s4-drs: fixed writespn to ignore add/delete errors
[ira/wip.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 "rpc_server/drsuapi/dcesrv_drsuapi.h"
27
28 /*
29   drsuapi_DsWriteAccountSpn
30 */
31 WERROR dcesrv_drsuapi_DsWriteAccountSpn(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
32                                         struct drsuapi_DsWriteAccountSpn *r)
33 {
34         struct drsuapi_bind_state *b_state;
35         struct dcesrv_handle *h;
36
37         *r->out.level_out = r->in.level;
38
39         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
40         b_state = h->data;
41
42         r->out.res = talloc(mem_ctx, union drsuapi_DsWriteAccountSpnResult);
43         W_ERROR_HAVE_NO_MEMORY(r->out.res);
44
45         switch (r->in.level) {
46                 case 1: {
47                         struct drsuapi_DsWriteAccountSpnRequest1 *req;
48                         struct ldb_message *msg;
49                         int count, i, ret;
50                         struct ldb_result *res;
51                         const char *attrs[] = { "servicePrincipalName", NULL };
52                         struct ldb_message_element *el;
53                         unsigned spn_count=0;
54
55                         req = &r->in.req->req1;
56                         count = req->count;
57
58                         msg = ldb_msg_new(mem_ctx);
59                         if (msg == NULL) {
60                                 return WERR_NOMEM;
61                         }
62
63                         msg->dn = ldb_dn_new(msg, b_state->sam_ctx, req->object_dn);
64                         if ( ! ldb_dn_validate(msg->dn)) {
65                                 r->out.res->res1.status = WERR_OK;
66                                 return WERR_OK;
67                         }
68
69                         /* load the existing SPNs, as these are
70                          * ignored for adds and deletes (see MS-DRSR
71                          * section 4.1.28.3)
72                          */
73                         ret = ldb_search(b_state->sam_ctx, msg, &res, msg->dn, LDB_SCOPE_BASE,
74                                          attrs, NULL);
75                         if (ret != LDB_SUCCESS) {
76                                 DEBUG(0,("Failed to load existing SPNs on %s: %s\n",
77                                          ldb_dn_get_linearized(msg->dn),
78                                          ldb_errstring(b_state->sam_ctx)));
79                                 r->out.res->res1.status = WERR_DS_OBJ_NOT_FOUND;
80                                 return WERR_OK;
81                         }
82                         el = ldb_msg_find_element(res->msgs[0], "servicePrincipalName");
83
84                         /* construct mods */
85                         for (i = 0; i < count; i++) {
86                                 bool found = false;
87                                 int j;
88                                 for (j=0; el && j<el->num_values; j++) {
89                                         if (samdb_ldb_val_case_cmp(req->spn_names[i].str, &el->values[j]) == 0) {
90                                                 found = true;
91                                                 break;
92                                         }
93                                 }
94                                 if ((req->operation == DRSUAPI_DS_SPN_OPERATION_ADD && found) ||
95                                     (req->operation == DRSUAPI_DS_SPN_OPERATION_DELETE && !found)) {
96                                         continue;
97                                 }
98                                 ret = samdb_msg_add_string(b_state->sam_ctx,
99                                                            msg, msg, "servicePrincipalName",
100                                                            req->spn_names[i].str);
101                                 if (ret != LDB_SUCCESS) {
102                                         return WERR_NOMEM;
103                                 }
104                                 spn_count++;
105                         }
106                         for (i=0;i<msg->num_elements;i++) {
107                                 switch (req->operation) {
108                                 case DRSUAPI_DS_SPN_OPERATION_ADD:
109                                         msg->elements[i].flags = LDB_FLAG_MOD_ADD;
110                                         break;
111                                 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
112                                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
113                                         break;
114                                 case DRSUAPI_DS_SPN_OPERATION_DELETE:
115                                         msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
116                                         break;
117                                 }
118                         }
119
120                         /* Apply to database */
121
122                         ret = ldb_modify(b_state->sam_ctx, msg);
123                         if (ret != 0) {
124                                 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
125                                          ldb_dn_get_linearized(msg->dn),
126                                          ldb_errstring(b_state->sam_ctx)));
127                                 r->out.res->res1.status = WERR_ACCESS_DENIED;
128                         } else {
129                                 DEBUG(2,("Modified %u SPNs on %s\n", spn_count, ldb_dn_get_linearized(msg->dn)));
130                                 r->out.res->res1.status = WERR_OK;
131                         }
132
133                         return WERR_OK;
134                 }
135         }
136
137         return WERR_UNKNOWN_LEVEL;
138 }