s4-drsuapi Allow DsAddEntry of normal objects
[amitay/samba.git] / source4 / rpc_server / drsuapi / addentry.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    implement the DsAddEntry call
5
6    Copyright (C) Stefan Metzmacher 2009
7    Copyright (C) Andrew Tridgell   2009
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 "param/param.h"
28 #include "libcli/security/security.h"
29 #include "libcli/security/session.h"
30 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
31 #include "librpc/gen_ndr/ndr_drsuapi.h"
32
33 /*
34   add special SPNs needed for DRS replication to machine accounts when
35   an AddEntry is done to create a nTDSDSA object
36  */
37 static WERROR drsuapi_add_SPNs(struct drsuapi_bind_state *b_state,
38                                struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
39                                const struct drsuapi_DsReplicaObjectListItem *first_object)
40 {
41         int ret;
42         const struct drsuapi_DsReplicaObjectListItem *obj;
43         const char *attrs[] = { "serverReference", "objectGUID", NULL };
44
45         for (obj = first_object; obj; obj=obj->next_object) {
46                 const char *dn_string = obj->object.identifier->dn;
47                 struct ldb_dn *dn = ldb_dn_new(mem_ctx, b_state->sam_ctx, dn_string);
48                 struct ldb_result *res, *res2;
49                 struct ldb_dn *ref_dn;
50                 struct GUID ntds_guid;
51                 struct ldb_message *msg;
52                 struct ldb_message_element *el;
53                 const char *ntds_guid_str;
54                 const char *dom_string;
55                 const char *attrs2[] = { "dNSHostName", "cn", NULL };
56                 const char *dNSHostName, *cn;
57
58                 DEBUG(6,(__location__ ": Adding SPNs for %s\n", 
59                          ldb_dn_get_linearized(dn)));
60                  
61                 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res,
62                                  dn, LDB_SCOPE_BASE, attrs,
63                                  "(objectClass=ntDSDSA)");
64                 if (ret != LDB_SUCCESS) {
65                         DEBUG(0,(__location__ ": Failed to find dn '%s'\n", dn_string));
66                         return WERR_DS_DRA_INTERNAL_ERROR;
67                 }
68
69                 if (res->count < 1) {
70                         /* we only add SPNs for nTDSDSA objects */
71                         continue;
72                 }
73
74                 ref_dn = samdb_result_dn(b_state->sam_ctx, mem_ctx, res->msgs[0], "serverReference", NULL);
75                 if (ref_dn == NULL) {
76                         /* we only add SPNs for objects with a
77                            serverReference */
78                         continue;
79                 }
80
81                 DEBUG(6,(__location__ ": serverReference %s\n", 
82                          ldb_dn_get_linearized(ref_dn)));
83
84                 ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
85
86                 ntds_guid_str = GUID_string(res, &ntds_guid);
87
88                 dom_string = lpcfg_dnsdomain(dce_call->conn->dce_ctx->lp_ctx);
89
90                 /* get the dNSHostName and cn */
91                 ret = ldb_search(b_state->sam_ctx, mem_ctx, &res2,
92                                  ref_dn, LDB_SCOPE_BASE, attrs2, NULL);
93                 if (ret != LDB_SUCCESS) {
94                         DEBUG(0,(__location__ ": Failed to find ref_dn '%s'\n",
95                                  ldb_dn_get_linearized(ref_dn)));
96                         return WERR_DS_DRA_INTERNAL_ERROR;
97                 }
98
99                 dNSHostName = ldb_msg_find_attr_as_string(res2->msgs[0], "dNSHostName", NULL);
100                 cn = ldb_msg_find_attr_as_string(res2->msgs[0], "cn", NULL);
101
102                 /*
103                  * construct a modify request to add the new SPNs to
104                  * the machine account
105                  */
106                 msg = ldb_msg_new(mem_ctx);
107                 if (msg == NULL) {
108                         return WERR_NOMEM;
109                 }
110
111                 msg->dn = ref_dn;
112                 ret = ldb_msg_add_empty(msg, "servicePrincipalName",
113                                         LDB_FLAG_MOD_ADD, &el);
114                 if (ret != LDB_SUCCESS) {
115                         return WERR_NOMEM;
116                 }
117
118
119                 ldb_msg_add_steal_string(msg, "servicePrincipalName",
120                                          talloc_asprintf(el->values,
121                                                          "E3514235-4B06-11D1-AB04-00C04FC2DCD2/%s/%s",
122                                                          ntds_guid_str, dom_string));
123                 ldb_msg_add_steal_string(msg, "servicePrincipalName",
124                                          talloc_asprintf(el->values, "ldap/%s._msdcs.%s",
125                                                          ntds_guid_str, dom_string));
126                 if (cn) {
127                         ldb_msg_add_steal_string(msg, "servicePrincipalName",
128                                                  talloc_asprintf(el->values, "ldap/%s", cn));
129                 }
130                 if (dNSHostName) {
131                         ldb_msg_add_steal_string(msg, "servicePrincipalName",
132                                                  talloc_asprintf(el->values, "ldap/%s", dNSHostName));
133                 }
134                 if (el->num_values < 2) {
135                         return WERR_NOMEM;
136                 }
137
138                 ret = dsdb_modify(b_state->sam_ctx, msg, DSDB_MODIFY_PERMISSIVE);
139                 if (ret != LDB_SUCCESS) {
140                         DEBUG(0,(__location__ ": Failed to add SPNs - %s\n",
141                                  ldb_errstring(b_state->sam_ctx)));
142                         return WERR_DS_DRA_INTERNAL_ERROR;
143                 }
144         }
145         
146         return WERR_OK;
147 }
148
149
150
151
152 /* 
153   drsuapi_DsAddEntry
154 */
155 WERROR dcesrv_drsuapi_DsAddEntry(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
156                                  struct drsuapi_DsAddEntry *r)
157 {
158         WERROR status;
159         struct drsuapi_bind_state *b_state;
160         struct dcesrv_handle *h;
161         uint32_t num = 0;
162         struct drsuapi_DsReplicaObjectIdentifier2 *ids = NULL;
163         int ret;
164         const struct drsuapi_DsReplicaObjectListItem *first_object;
165
166         if (DEBUGLVL(4)) {
167                 NDR_PRINT_FUNCTION_DEBUG(drsuapi_DsAddEntry, NDR_IN, r);
168         }
169
170         /* TODO: check which out level the client supports */
171
172         ZERO_STRUCTP(r->out.ctr);
173         *r->out.level_out = 3;
174         r->out.ctr->ctr3.err_ver = 1;
175         r->out.ctr->ctr3.err_data = talloc_zero(mem_ctx, union drsuapi_DsAddEntry_ErrData);
176
177         DCESRV_PULL_HANDLE_WERR(h, r->in.bind_handle, DRSUAPI_BIND_HANDLE);
178         b_state = h->data;
179
180         status = drs_security_level_check(dce_call, "DsAddEntry", SECURITY_DOMAIN_CONTROLLER, NULL);
181         if (!W_ERROR_IS_OK(status)) {
182                 return status;
183         }
184
185         switch (r->in.level) {
186         case 2:
187                 ret = ldb_transaction_start(b_state->sam_ctx);
188                 if (ret != LDB_SUCCESS) {
189                         return WERR_DS_DRA_INTERNAL_ERROR;
190                 }
191
192
193                 first_object = &r->in.req->req2.first_object;
194
195                 status = dsdb_origin_objects_commit(b_state->sam_ctx,
196                                                     mem_ctx,
197                                                     first_object,
198                                                     &num,
199                                                     &ids);
200                 if (!W_ERROR_IS_OK(status)) {
201                         r->out.ctr->ctr3.err_data->v1.status = status;
202                         ldb_transaction_cancel(b_state->sam_ctx);
203                         DEBUG(0,(__location__ ": DsAddEntry failed - %s\n", win_errstr(status)));
204                         return status;
205                 }
206
207                 r->out.ctr->ctr3.count = num;
208                 r->out.ctr->ctr3.objects = ids;
209
210                 break;
211         default:
212                 return WERR_FOOBAR;
213         }
214
215         /* if any of the added entries are nTDSDSA objects then we
216          * need to add the SPNs to the machine account
217          */
218         status = drsuapi_add_SPNs(b_state, dce_call, mem_ctx, first_object);
219         if (!W_ERROR_IS_OK(status)) {
220                 r->out.ctr->ctr3.err_data->v1.status = status;
221                 ldb_transaction_cancel(b_state->sam_ctx);
222                 DEBUG(0,(__location__ ": DsAddEntry add SPNs failed - %s\n", win_errstr(status)));
223                 return status;
224         }
225
226         ret = ldb_transaction_commit(b_state->sam_ctx);
227         if (ret != LDB_SUCCESS) {
228                 DEBUG(0,(__location__ ": DsAddEntry commit failed: %s\n",
229                          ldb_errstring(b_state->sam_ctx)));
230                 return WERR_DS_DRA_INTERNAL_ERROR;
231         }
232
233         return WERR_OK;
234 }