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