rpc_server3: Remove pipes_struct->remote_address
[Samba.git] / source3 / rpc_server / dfs / srv_dfs_nt.c
blob23ddf11ac77a38657c366b6c009e5f89ff9d1728
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines for Dfs
4 * Copyright (C) Shirish Kalele 2000.
5 * Copyright (C) Jeremy Allison 2001-2007.
6 * Copyright (C) Jelmer Vernooij 2005-2006.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* This is the implementation of the dfs pipe. */
24 #include "includes.h"
25 #include "ntdomain.h"
26 #include "librpc/rpc/dcesrv_core.h"
27 #include "librpc/gen_ndr/ndr_dfs.h"
28 #include "librpc/gen_ndr/ndr_dfs_scompat.h"
29 #include "msdfs.h"
30 #include "smbd/smbd.h"
31 #include "smbd/globals.h"
32 #include "auth.h"
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_MSDFS
37 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
38 dfs exists, or 0 otherwise. */
40 void _dfs_GetManagerVersion(struct pipes_struct *p, struct dfs_GetManagerVersion *r)
42 if (lp_host_msdfs()) {
43 *r->out.version = DFS_MANAGER_VERSION_NT4;
44 } else {
45 *r->out.version = (enum dfs_ManagerVersion)0;
49 WERROR _dfs_Add(struct pipes_struct *p, struct dfs_Add *r)
51 struct dcesrv_call_state *dce_call = p->dce_call;
52 struct dcesrv_connection *dcesrv_conn = dce_call->conn;
53 const struct tsocket_address *local_address =
54 dcesrv_connection_get_local_address(dcesrv_conn);
55 const struct tsocket_address *remote_address =
56 dcesrv_connection_get_remote_address(dcesrv_conn);
57 struct junction_map *jn = NULL;
58 struct referral *old_referral_list = NULL;
59 bool self_ref = False;
60 int consumedcnt = 0;
61 char *altpath = NULL;
62 NTSTATUS status;
63 TALLOC_CTX *ctx = talloc_tos();
65 if (p->session_info->unix_token->uid != sec_initial_uid()) {
66 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
67 return WERR_ACCESS_DENIED;
70 jn = talloc_zero(ctx, struct junction_map);
71 if (!jn) {
72 return WERR_NOT_ENOUGH_MEMORY;
75 DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
76 r->in.path, r->in.server, r->in.share));
78 altpath = talloc_asprintf(ctx, "%s\\%s",
79 r->in.server,
80 r->in.share);
81 if (!altpath) {
82 return WERR_NOT_ENOUGH_MEMORY;
85 /* The following call can change the cwd. */
86 status = get_referred_path(ctx,
87 p->session_info,
88 r->in.path,
89 remote_address,
90 local_address,
91 true, /*allow_broken_path */
92 jn, &consumedcnt, &self_ref);
93 if(!NT_STATUS_IS_OK(status)) {
94 return ntstatus_to_werror(status);
97 jn->referral_count += 1;
98 old_referral_list = jn->referral_list;
100 if (jn->referral_count < 1) {
101 return WERR_NOT_ENOUGH_MEMORY;
104 jn->referral_list = talloc_array(ctx, struct referral, jn->referral_count);
105 if(jn->referral_list == NULL) {
106 DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
107 return WERR_NERR_DFSINTERNALERROR;
110 if(old_referral_list && jn->referral_list) {
111 memcpy(jn->referral_list, old_referral_list,
112 sizeof(struct referral)*jn->referral_count-1);
115 jn->referral_list[jn->referral_count-1].proximity = 0;
116 jn->referral_list[jn->referral_count-1].ttl = REFERRAL_TTL;
117 jn->referral_list[jn->referral_count-1].alternate_path = altpath;
119 if(!create_msdfs_link(jn, p->session_info)) {
120 return WERR_NERR_DFSCANTCREATEJUNCTIONPOINT;
123 return WERR_OK;
126 WERROR _dfs_Remove(struct pipes_struct *p, struct dfs_Remove *r)
128 struct dcesrv_call_state *dce_call = p->dce_call;
129 struct dcesrv_connection *dcesrv_conn = dce_call->conn;
130 const struct tsocket_address *local_address =
131 dcesrv_connection_get_local_address(dcesrv_conn);
132 const struct tsocket_address *remote_address =
133 dcesrv_connection_get_remote_address(dcesrv_conn);
134 struct junction_map *jn = NULL;
135 bool self_ref = False;
136 int consumedcnt = 0;
137 bool found = False;
138 TALLOC_CTX *ctx = talloc_tos();
139 char *altpath = NULL;
140 NTSTATUS status;
142 if (p->session_info->unix_token->uid != sec_initial_uid()) {
143 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
144 return WERR_ACCESS_DENIED;
147 jn = talloc_zero(ctx, struct junction_map);
148 if (!jn) {
149 return WERR_NOT_ENOUGH_MEMORY;
152 if (r->in.servername && r->in.sharename) {
153 altpath = talloc_asprintf(ctx, "%s\\%s",
154 r->in.servername,
155 r->in.sharename);
156 if (!altpath) {
157 return WERR_NOT_ENOUGH_MEMORY;
159 if (!strlower_m(altpath)) {
160 return WERR_INVALID_PARAMETER;
162 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
163 r->in.dfs_entry_path, r->in.servername, r->in.sharename));
166 status = get_referred_path(ctx,
167 p->session_info,
168 r->in.dfs_entry_path,
169 remote_address,
170 local_address,
171 true, /*allow_broken_path */
172 jn, &consumedcnt, &self_ref);
173 if(!NT_STATUS_IS_OK(status)) {
174 return WERR_NERR_DFSNOSUCHVOLUME;
177 /* if no server-share pair given, remove the msdfs link completely */
178 if(!r->in.servername && !r->in.sharename) {
179 if(!remove_msdfs_link(jn, p->session_info)) {
180 return WERR_NERR_DFSNOSUCHVOLUME;
182 } else {
183 size_t i = 0;
184 /* compare each referral in the list with the one to remove */
185 DBG_DEBUG("altpath: .%s. refcnt: %zu\n",
186 altpath,
187 jn->referral_count);
188 for(i=0;i<jn->referral_count;i++) {
189 char *refpath = talloc_strdup(ctx,
190 jn->referral_list[i].alternate_path);
191 if (!refpath) {
192 return WERR_NOT_ENOUGH_MEMORY;
194 trim_char(refpath, '\\', '\\');
195 DEBUG(10,("_dfs_remove: refpath: .%s.\n", refpath));
196 if(strequal(refpath, altpath)) {
197 *(jn->referral_list[i].alternate_path)='\0';
198 DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
199 refpath));
200 found = True;
204 if(!found) {
205 return WERR_NERR_DFSNOSUCHSHARE;
208 /* Only one referral, remove it */
209 if(jn->referral_count == 1) {
210 if(!remove_msdfs_link(jn, p->session_info)) {
211 return WERR_NERR_DFSNOSUCHVOLUME;
213 } else {
214 if(!create_msdfs_link(jn, p->session_info)) {
215 return WERR_NERR_DFSCANTCREATEJUNCTIONPOINT;
220 return WERR_OK;
223 static bool init_reply_dfs_info_1(TALLOC_CTX *mem_ctx, struct junction_map* j,struct dfs_Info1* dfs1)
225 dfs1->path = talloc_asprintf(mem_ctx,
226 "\\\\%s\\%s\\%s", lp_netbios_name(),
227 j->service_name, j->volume_name);
228 if (dfs1->path == NULL)
229 return False;
231 DEBUG(5,("init_reply_dfs_info_1: initing entrypath: %s\n",dfs1->path));
232 return True;
235 static bool init_reply_dfs_info_2(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info2* dfs2)
237 dfs2->path = talloc_asprintf(mem_ctx,
238 "\\\\%s\\%s\\%s", lp_netbios_name(), j->service_name, j->volume_name);
239 if (dfs2->path == NULL)
240 return False;
241 dfs2->comment = talloc_strdup(mem_ctx, j->comment);
242 dfs2->state = 1; /* set up state of dfs junction as OK */
243 dfs2->num_stores = j->referral_count;
244 return True;
247 static bool init_reply_dfs_info_3(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info3* dfs3)
249 size_t ii;
250 if (j->volume_name[0] == '\0')
251 dfs3->path = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
252 lp_netbios_name(), j->service_name);
253 else
254 dfs3->path = talloc_asprintf(mem_ctx, "\\\\%s\\%s\\%s", lp_netbios_name(),
255 j->service_name, j->volume_name);
257 if (dfs3->path == NULL)
258 return False;
260 dfs3->comment = talloc_strdup(mem_ctx, j->comment);
261 dfs3->state = 1;
262 dfs3->num_stores = j->referral_count;
264 /* also enumerate the stores */
265 if (j->referral_count) {
266 dfs3->stores = talloc_array(mem_ctx, struct dfs_StorageInfo, j->referral_count);
267 if (!dfs3->stores)
268 return False;
269 memset(dfs3->stores, '\0', j->referral_count * sizeof(struct dfs_StorageInfo));
270 } else {
271 dfs3->stores = NULL;
274 for(ii=0;ii<j->referral_count;ii++) {
275 char* p;
276 char *path = NULL;
277 struct dfs_StorageInfo* stor = &(dfs3->stores[ii]);
278 struct referral* ref = &(j->referral_list[ii]);
280 path = talloc_strdup(mem_ctx, ref->alternate_path);
281 if (!path) {
282 return False;
284 trim_char(path,'\\','\0');
285 p = strrchr_m(path,'\\');
286 if(p==NULL) {
287 DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
288 continue;
290 *p = '\0';
291 DBG_INFO("storage %zu: %s.%s\n",ii,path,p+1);
292 stor->state = 2; /* set all stores as ONLINE */
293 stor->server = talloc_strdup(mem_ctx, path);
294 stor->share = talloc_strdup(mem_ctx, p+1);
296 return True;
299 static bool init_reply_dfs_info_100(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info100* dfs100)
301 dfs100->comment = talloc_strdup(mem_ctx, j->comment);
302 return True;
305 WERROR _dfs_Enum(struct pipes_struct *p, struct dfs_Enum *r)
307 struct junction_map *jn = NULL;
308 size_t num_jn = 0;
309 size_t i;
310 TALLOC_CTX *ctx = talloc_tos();
312 jn = enum_msdfs_links(ctx, p->session_info, &num_jn);
313 if (!jn || num_jn == 0) {
314 num_jn = 0;
315 jn = NULL;
318 DEBUG(5,("_dfs_Enum: %u junctions found in Dfs, doing level %d\n",
319 (unsigned int)num_jn, r->in.level));
321 *r->out.total = num_jn;
323 /* Create the return array */
324 switch (r->in.level) {
325 case 1:
326 if (num_jn) {
327 if ((r->out.info->e.info1->s = talloc_array(ctx, struct dfs_Info1, num_jn)) == NULL) {
328 return WERR_NOT_ENOUGH_MEMORY;
330 } else {
331 r->out.info->e.info1->s = NULL;
333 r->out.info->e.info1->count = num_jn;
334 break;
335 case 2:
336 if (num_jn) {
337 if ((r->out.info->e.info2->s = talloc_array(ctx, struct dfs_Info2, num_jn)) == NULL) {
338 return WERR_NOT_ENOUGH_MEMORY;
340 } else {
341 r->out.info->e.info2->s = NULL;
343 r->out.info->e.info2->count = num_jn;
344 break;
345 case 3:
346 if (num_jn) {
347 if ((r->out.info->e.info3->s = talloc_array(ctx, struct dfs_Info3, num_jn)) == NULL) {
348 return WERR_NOT_ENOUGH_MEMORY;
350 } else {
351 r->out.info->e.info3->s = NULL;
353 r->out.info->e.info3->count = num_jn;
354 break;
355 default:
356 return WERR_INVALID_PARAMETER;
359 for (i = 0; i < num_jn; i++) {
360 switch (r->in.level) {
361 case 1:
362 init_reply_dfs_info_1(ctx, &jn[i], &r->out.info->e.info1->s[i]);
363 break;
364 case 2:
365 init_reply_dfs_info_2(ctx, &jn[i], &r->out.info->e.info2->s[i]);
366 break;
367 case 3:
368 init_reply_dfs_info_3(ctx, &jn[i], &r->out.info->e.info3->s[i]);
369 break;
370 default:
371 return WERR_INVALID_PARAMETER;
375 return WERR_OK;
378 WERROR _dfs_GetInfo(struct pipes_struct *p, struct dfs_GetInfo *r)
380 struct dcesrv_call_state *dce_call = p->dce_call;
381 struct dcesrv_connection *dcesrv_conn = dce_call->conn;
382 const struct tsocket_address *local_address =
383 dcesrv_connection_get_local_address(dcesrv_conn);
384 const struct tsocket_address *remote_address =
385 dcesrv_connection_get_remote_address(dcesrv_conn);
386 int consumedcnt = strlen(r->in.dfs_entry_path);
387 struct junction_map *jn = NULL;
388 bool self_ref = False;
389 TALLOC_CTX *ctx = talloc_tos();
390 bool ret;
391 NTSTATUS status;
393 jn = talloc_zero(ctx, struct junction_map);
394 if (!jn) {
395 return WERR_NOT_ENOUGH_MEMORY;
398 ret = create_junction(ctx, r->in.dfs_entry_path,
399 true, /* allow broken_path */
400 jn);
401 if (!ret) {
402 return WERR_NERR_DFSNOSUCHSERVER;
405 /* The following call can change the cwd. */
406 status = get_referred_path(ctx,
407 p->session_info,
408 r->in.dfs_entry_path,
409 remote_address,
410 local_address,
411 true, /*allow_broken_path */
412 jn, &consumedcnt, &self_ref);
413 if(!NT_STATUS_IS_OK(status) ||
414 consumedcnt < strlen(r->in.dfs_entry_path)) {
415 return WERR_NERR_DFSNOSUCHVOLUME;
418 switch (r->in.level) {
419 case 1:
420 r->out.info->info1 = talloc_zero(ctx,struct dfs_Info1);
421 if (!r->out.info->info1) {
422 return WERR_NOT_ENOUGH_MEMORY;
424 ret = init_reply_dfs_info_1(ctx, jn, r->out.info->info1);
425 break;
426 case 2:
427 r->out.info->info2 = talloc_zero(ctx,struct dfs_Info2);
428 if (!r->out.info->info2) {
429 return WERR_NOT_ENOUGH_MEMORY;
431 ret = init_reply_dfs_info_2(ctx, jn, r->out.info->info2);
432 break;
433 case 3:
434 r->out.info->info3 = talloc_zero(ctx,struct dfs_Info3);
435 if (!r->out.info->info3) {
436 return WERR_NOT_ENOUGH_MEMORY;
438 ret = init_reply_dfs_info_3(ctx, jn, r->out.info->info3);
439 break;
440 case 100:
441 r->out.info->info100 = talloc_zero(ctx,struct dfs_Info100);
442 if (!r->out.info->info100) {
443 return WERR_NOT_ENOUGH_MEMORY;
445 ret = init_reply_dfs_info_100(ctx, jn, r->out.info->info100);
446 break;
447 default:
448 r->out.info->info1 = NULL;
449 return WERR_INVALID_PARAMETER;
452 if (!ret)
453 return WERR_INVALID_PARAMETER;
455 return WERR_OK;
458 WERROR _dfs_SetInfo(struct pipes_struct *p, struct dfs_SetInfo *r)
460 /* FIXME: Implement your code here */
461 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
462 return WERR_NOT_SUPPORTED;
465 WERROR _dfs_Rename(struct pipes_struct *p, struct dfs_Rename *r)
467 /* FIXME: Implement your code here */
468 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
469 return WERR_NOT_SUPPORTED;
472 WERROR _dfs_Move(struct pipes_struct *p, struct dfs_Move *r)
474 /* FIXME: Implement your code here */
475 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
476 return WERR_NOT_SUPPORTED;
479 WERROR _dfs_ManagerGetConfigInfo(struct pipes_struct *p, struct dfs_ManagerGetConfigInfo *r)
481 /* FIXME: Implement your code here */
482 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
483 return WERR_NOT_SUPPORTED;
486 WERROR _dfs_ManagerSendSiteInfo(struct pipes_struct *p, struct dfs_ManagerSendSiteInfo *r)
488 /* FIXME: Implement your code here */
489 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
490 return WERR_NOT_SUPPORTED;
493 WERROR _dfs_AddFtRoot(struct pipes_struct *p, struct dfs_AddFtRoot *r)
495 /* FIXME: Implement your code here */
496 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
497 return WERR_NOT_SUPPORTED;
500 WERROR _dfs_RemoveFtRoot(struct pipes_struct *p, struct dfs_RemoveFtRoot *r)
502 /* FIXME: Implement your code here */
503 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
504 return WERR_NOT_SUPPORTED;
507 WERROR _dfs_AddStdRoot(struct pipes_struct *p, struct dfs_AddStdRoot *r)
509 /* FIXME: Implement your code here */
510 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
511 return WERR_NOT_SUPPORTED;
514 WERROR _dfs_RemoveStdRoot(struct pipes_struct *p, struct dfs_RemoveStdRoot *r)
516 /* FIXME: Implement your code here */
517 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
518 return WERR_NOT_SUPPORTED;
521 WERROR _dfs_ManagerInitialize(struct pipes_struct *p, struct dfs_ManagerInitialize *r)
523 /* FIXME: Implement your code here */
524 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
525 return WERR_NOT_SUPPORTED;
528 WERROR _dfs_AddStdRootForced(struct pipes_struct *p, struct dfs_AddStdRootForced *r)
530 /* FIXME: Implement your code here */
531 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
532 return WERR_NOT_SUPPORTED;
535 WERROR _dfs_GetDcAddress(struct pipes_struct *p, struct dfs_GetDcAddress *r)
537 /* FIXME: Implement your code here */
538 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
539 return WERR_NOT_SUPPORTED;
542 WERROR _dfs_SetDcAddress(struct pipes_struct *p, struct dfs_SetDcAddress *r)
544 /* FIXME: Implement your code here */
545 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
546 return WERR_NOT_SUPPORTED;
549 WERROR _dfs_FlushFtTable(struct pipes_struct *p, struct dfs_FlushFtTable *r)
551 /* FIXME: Implement your code here */
552 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
553 return WERR_NOT_SUPPORTED;
556 WERROR _dfs_Add2(struct pipes_struct *p, struct dfs_Add2 *r)
558 /* FIXME: Implement your code here */
559 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
560 return WERR_NOT_SUPPORTED;
563 WERROR _dfs_Remove2(struct pipes_struct *p, struct dfs_Remove2 *r)
565 /* FIXME: Implement your code here */
566 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
567 return WERR_NOT_SUPPORTED;
570 WERROR _dfs_EnumEx(struct pipes_struct *p, struct dfs_EnumEx *r)
572 /* FIXME: Implement your code here */
573 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
574 return WERR_NOT_SUPPORTED;
577 WERROR _dfs_SetInfo2(struct pipes_struct *p, struct dfs_SetInfo2 *r)
579 /* FIXME: Implement your code here */
580 p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
581 return WERR_NOT_SUPPORTED;
584 /* include the generated boilerplate */
585 #include "librpc/gen_ndr/ndr_dfs_scompat.c"