2 Unix SMB/CIFS implementation.
4 implement the DsWriteAccountSpn call
6 Copyright (C) Stefan Metzmacher 2009
7 Copyright (C) Andrew Tridgell 2010
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.
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.
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/>.
24 #include "rpc_server/dcerpc_server.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
29 drsuapi_DsWriteAccountSpn
31 WERROR dcesrv_drsuapi_DsWriteAccountSpn(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
32 struct drsuapi_DsWriteAccountSpn *r)
34 struct drsuapi_bind_state *b_state;
35 struct dcesrv_handle *h;
37 *r->out.level_out = r->in.level;
39 DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
42 r->out.res = talloc(mem_ctx, union drsuapi_DsWriteAccountSpnResult);
43 W_ERROR_HAVE_NO_MEMORY(r->out.res);
45 switch (r->in.level) {
47 struct drsuapi_DsWriteAccountSpnRequest1 *req;
48 struct ldb_message *msg;
50 struct ldb_result *res;
51 const char *attrs[] = { "servicePrincipalName", NULL };
52 struct ldb_message_element *el;
55 req = &r->in.req->req1;
58 msg = ldb_msg_new(mem_ctx);
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;
69 /* load the existing SPNs, as these are
70 * ignored for adds and deletes (see MS-DRSR
73 ret = ldb_search(b_state->sam_ctx, msg, &res, msg->dn, LDB_SCOPE_BASE,
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;
82 el = ldb_msg_find_element(res->msgs[0], "servicePrincipalName");
85 for (i = 0; i < count; i++) {
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) {
94 if ((req->operation == DRSUAPI_DS_SPN_OPERATION_ADD && found) ||
95 (req->operation == DRSUAPI_DS_SPN_OPERATION_DELETE && !found)) {
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) {
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;
111 case DRSUAPI_DS_SPN_OPERATION_REPLACE:
112 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
114 case DRSUAPI_DS_SPN_OPERATION_DELETE:
115 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
120 /* Apply to database */
122 ret = ldb_modify(b_state->sam_ctx, msg);
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;
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;
137 return WERR_UNKNOWN_LEVEL;