s4-drs: replace manual checks with dsdb_modify_permissive()
[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                         unsigned spn_count=0;
51
52                         req = &r->in.req->req1;
53                         count = req->count;
54
55                         msg = ldb_msg_new(mem_ctx);
56                         if (msg == NULL) {
57                                 return WERR_NOMEM;
58                         }
59
60                         msg->dn = ldb_dn_new(msg, b_state->sam_ctx, req->object_dn);
61                         if ( ! ldb_dn_validate(msg->dn)) {
62                                 r->out.res->res1.status = WERR_OK;
63                                 return WERR_OK;
64                         }
65
66                         /* construct mods */
67                         for (i = 0; i < count; i++) {
68                                 ret = samdb_msg_add_string(b_state->sam_ctx,
69                                                            msg, msg, "servicePrincipalName",
70                                                            req->spn_names[i].str);
71                                 if (ret != LDB_SUCCESS) {
72                                         return WERR_NOMEM;
73                                 }
74                                 spn_count++;
75                         }
76
77                         if (msg->num_elements == 0) {
78                                 DEBUG(2,("No SPNs need changing on %s\n", ldb_dn_get_linearized(msg->dn)));
79                                 r->out.res->res1.status = WERR_OK;
80                                 return WERR_OK;
81                         }
82
83                         for (i=0;i<msg->num_elements;i++) {
84                                 switch (req->operation) {
85                                 case DRSUAPI_DS_SPN_OPERATION_ADD:
86                                         msg->elements[i].flags = LDB_FLAG_MOD_ADD;
87                                         break;
88                                 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
89                                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
90                                         break;
91                                 case DRSUAPI_DS_SPN_OPERATION_DELETE:
92                                         msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
93                                         break;
94                                 }
95                         }
96
97                         /* Apply to database */
98                         ret = dsdb_modify_permissive(b_state->sam_ctx, msg);
99                         if (ret != 0) {
100                                 DEBUG(0,("Failed to modify SPNs on %s: %s\n",
101                                          ldb_dn_get_linearized(msg->dn),
102                                          ldb_errstring(b_state->sam_ctx)));
103                                 r->out.res->res1.status = WERR_ACCESS_DENIED;
104                         } else {
105                                 DEBUG(2,("Modified %u SPNs on %s\n", spn_count, ldb_dn_get_linearized(msg->dn)));
106                                 r->out.res->res1.status = WERR_OK;
107                         }
108
109                         return WERR_OK;
110                 }
111         }
112
113         return WERR_UNKNOWN_LEVEL;
114 }