summaryrefslogtreecommitdiff
path: root/source3/rpc_server/dfs/srv_dfs_nt.c
blob: d77989db2fd32e8ef7da537be027cb6ff206f657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 *  Unix SMB/CIFS implementation.
 *  RPC Pipe client / server routines for Dfs
 *  Copyright (C) Shirish Kalele	2000.
 *  Copyright (C) Jeremy Allison	2001-2007.
 *  Copyright (C) Jelmer Vernooij	2005-2006.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/* This is the implementation of the dfs pipe. */

#include "includes.h"
#include "ntdomain.h"
#include "../librpc/gen_ndr/srv_dfs.h"
#include "msdfs.h"
#include "smbd/smbd.h"
#include "smbd/globals.h"
#include "auth.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_MSDFS

/* This function does not return a WERROR or NTSTATUS code but rather 1 if
   dfs exists, or 0 otherwise. */

void _dfs_GetManagerVersion(struct pipes_struct *p, struct dfs_GetManagerVersion *r)
{
	if (lp_host_msdfs()) {
		*r->out.version = DFS_MANAGER_VERSION_NT4;
	} else {
		*r->out.version = (enum dfs_ManagerVersion)0;
	}
}

WERROR _dfs_Add(struct pipes_struct *p, struct dfs_Add *r)
{
	struct junction_map *jn = NULL;
	struct referral *old_referral_list = NULL;
	bool self_ref = False;
	int consumedcnt = 0;
	char *altpath = NULL;
	NTSTATUS status;
	TALLOC_CTX *ctx = talloc_tos();

	if (p->session_info->unix_token->uid != sec_initial_uid()) {
		DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
		return WERR_ACCESS_DENIED;
	}

	jn = talloc_zero(ctx, struct junction_map);
	if (!jn) {
		return WERR_NOMEM;
	}

	DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
		r->in.path, r->in.server, r->in.share));

	altpath = talloc_asprintf(ctx, "%s\\%s",
			r->in.server,
			r->in.share);
	if (!altpath) {
		return WERR_NOMEM;
	}

	/* The following call can change the cwd. */
	status = get_referred_path(ctx, r->in.path, jn,
			&consumedcnt, &self_ref);
	if(!NT_STATUS_IS_OK(status)) {
		return ntstatus_to_werror(status);
	}

	jn->referral_count += 1;
	old_referral_list = jn->referral_list;

	if (jn->referral_count < 1) {
		return WERR_NOMEM;
	}

	jn->referral_list = talloc_array(ctx, struct referral, jn->referral_count);
	if(jn->referral_list == NULL) {
		DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
		return WERR_DFS_INTERNAL_ERROR;
	}

	if(old_referral_list && jn->referral_list) {
		memcpy(jn->referral_list, old_referral_list,
				sizeof(struct referral)*jn->referral_count-1);
	}

	jn->referral_list[jn->referral_count-1].proximity = 0;
	jn->referral_list[jn->referral_count-1].ttl = REFERRAL_TTL;
	jn->referral_list[jn->referral_count-1].alternate_path = altpath;

	if(!create_msdfs_link(jn)) {
		return WERR_DFS_CANT_CREATE_JUNCT;
	}

	return WERR_OK;
}

WERROR _dfs_Remove(struct pipes_struct *p, struct dfs_Remove *r)
{
	struct junction_map *jn = NULL;
	bool self_ref = False;
	int consumedcnt = 0;
	bool found = False;
	TALLOC_CTX *ctx = talloc_tos();
	char *altpath = NULL;

	if (p->session_info->unix_token->uid != sec_initial_uid()) {
		DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
		return WERR_ACCESS_DENIED;
	}

	jn = talloc_zero(ctx, struct junction_map);
	if (!jn) {
		return WERR_NOMEM;
	}

	if (r->in.servername && r->in.sharename) {
		altpath = talloc_asprintf(ctx, "%s\\%s",
			r->in.servername,
			r->in.sharename);
		if (!altpath) {
			return WERR_NOMEM;
		}
		strlower_m(altpath);
		DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
			r->in.dfs_entry_path, r->in.servername, r->in.sharename));
	}

	if(!NT_STATUS_IS_OK(get_referred_path(ctx, r->in.dfs_entry_path, jn,
				&consumedcnt, &self_ref))) {
		return WERR_DFS_NO_SUCH_VOL;
	}

	/* if no server-share pair given, remove the msdfs link completely */
	if(!r->in.servername && !r->in.sharename) {
		if(!remove_msdfs_link(jn)) {
			return WERR_DFS_NO_SUCH_VOL;
		}
	} else {
		int i=0;
		/* compare each referral in the list with the one to remove */
		DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn->referral_count));
		for(i=0;i<jn->referral_count;i++) {
			char *refpath = talloc_strdup(ctx,
					jn->referral_list[i].alternate_path);
			if (!refpath) {
				return WERR_NOMEM;
			}
			trim_char(refpath, '\\', '\\');
			DEBUG(10,("_dfs_remove:  refpath: .%s.\n", refpath));
			if(strequal(refpath, altpath)) {
				*(jn->referral_list[i].alternate_path)='\0';
				DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
					refpath));
				found = True;
			}
		}

		if(!found) {
			return WERR_DFS_NO_SUCH_SHARE;
		}

		/* Only one referral, remove it */
		if(jn->referral_count == 1) {
			if(!remove_msdfs_link(jn)) {
				return WERR_DFS_NO_SUCH_VOL;
			}
		} else {
			if(!create_msdfs_link(jn)) {
				return WERR_DFS_CANT_CREATE_JUNCT;
			}
		}
	}

	return WERR_OK;
}

static bool init_reply_dfs_info_1(TALLOC_CTX *mem_ctx, struct junction_map* j,struct dfs_Info1* dfs1)
{
	dfs1->path = talloc_asprintf(mem_ctx,
				"\\\\%s\\%s\\%s", lp_netbios_name(),
				j->service_name, j->volume_name);
	if (dfs1->path == NULL)
		return False;

	DEBUG(5,("init_reply_dfs_info_1: initing entrypath: %s\n",dfs1->path));
	return True;
}

static bool init_reply_dfs_info_2(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info2* dfs2)
{
	dfs2->path = talloc_asprintf(mem_ctx,
			"\\\\%s\\%s\\%s", lp_netbios_name(), j->service_name, j->volume_name);
	if (dfs2->path == NULL)
		return False;
	dfs2->comment = talloc_strdup(mem_ctx, j->comment);
	dfs2->state = 1; /* set up state of dfs junction as OK */
	dfs2->num_stores = j->referral_count;
	return True;
}

static bool init_reply_dfs_info_3(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info3* dfs3)
{
	int ii;
	if (j->volume_name[0] == '\0')
		dfs3->path = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
			lp_netbios_name(), j->service_name);
	else
		dfs3->path = talloc_asprintf(mem_ctx, "\\\\%s\\%s\\%s", lp_netbios_name(),
			j->service_name, j->volume_name);

	if (dfs3->path == NULL)
		return False;

	dfs3->comment = talloc_strdup(mem_ctx, j->comment);
	dfs3->state = 1;
	dfs3->num_stores = j->referral_count;

	/* also enumerate the stores */
	if (j->referral_count) {
		dfs3->stores = talloc_array(mem_ctx, struct dfs_StorageInfo, j->referral_count);
		if (!dfs3->stores)
			return False;
		memset(dfs3->stores, '\0', j->referral_count * sizeof(struct dfs_StorageInfo));
	} else {
		dfs3->stores = NULL;
	}

	for(ii=0;ii<j->referral_count;ii++) {
		char* p;
		char *path = NULL;
		struct dfs_StorageInfo* stor = &(dfs3->stores[ii]);
		struct referral* ref = &(j->referral_list[ii]);

		path = talloc_strdup(mem_ctx, ref->alternate_path);
		if (!path) {
			return False;
		}
		trim_char(path,'\\','\0');
		p = strrchr_m(path,'\\');
		if(p==NULL) {
			DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
			continue;
		}
		*p = '\0';
		DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
		stor->state = 2; /* set all stores as ONLINE */
		stor->server = talloc_strdup(mem_ctx, path);
		stor->share = talloc_strdup(mem_ctx, p+1);
	}
	return True;
}

static bool init_reply_dfs_info_100(TALLOC_CTX *mem_ctx, struct junction_map* j, struct dfs_Info100* dfs100)
{
	dfs100->comment = talloc_strdup(mem_ctx, j->comment);
	return True;
}

WERROR _dfs_Enum(struct pipes_struct *p, struct dfs_Enum *r)
{
	struct junction_map *jn = NULL;
	size_t num_jn = 0;
	size_t i;
	TALLOC_CTX *ctx = talloc_tos();

	jn = enum_msdfs_links(msg_ctx_to_sconn(p->msg_ctx),
			      ctx, &num_jn);
	if (!jn || num_jn == 0) {
		num_jn = 0;
		jn = NULL;
	}

	DEBUG(5,("_dfs_Enum: %u junctions found in Dfs, doing level %d\n",
				(unsigned int)num_jn, r->in.level));

	*r->out.total = num_jn;

	/* Create the return array */
	switch (r->in.level) {
	case 1:
		if (num_jn) {
			if ((r->out.info->e.info1->s = talloc_array(ctx, struct dfs_Info1, num_jn)) == NULL) {
				return WERR_NOMEM;
			}
		} else {
			r->out.info->e.info1->s = NULL;
		}
		r->out.info->e.info1->count = num_jn;
		break;
	case 2:
		if (num_jn) {
			if ((r->out.info->e.info2->s = talloc_array(ctx, struct dfs_Info2, num_jn)) == NULL) {
				return WERR_NOMEM;
			}
		} else {
			r->out.info->e.info2->s = NULL;
		}
		r->out.info->e.info2->count = num_jn;
		break;
	case 3:
		if (num_jn) {
			if ((r->out.info->e.info3->s = talloc_array(ctx, struct dfs_Info3, num_jn)) == NULL) {
				return WERR_NOMEM;
			}
		} else {
			r->out.info->e.info3->s = NULL;
		}
		r->out.info->e.info3->count = num_jn;
		break;
	default:
		return WERR_INVALID_PARAM;
	}

	for (i = 0; i < num_jn; i++) {
		switch (r->in.level) {
		case 1:
			init_reply_dfs_info_1(ctx, &jn[i], &r->out.info->e.info1->s[i]);
			break;
		case 2:
			init_reply_dfs_info_2(ctx, &jn[i], &r->out.info->e.info2->s[i]);
			break;
		case 3:
			init_reply_dfs_info_3(ctx, &jn[i], &r->out.info->e.info3->s[i]);
			break;
		default:
			return WERR_INVALID_PARAM;
		}
	}

	return WERR_OK;
}

WERROR _dfs_GetInfo(struct pipes_struct *p, struct dfs_GetInfo *r)
{
	int consumedcnt = strlen(r->in.dfs_entry_path);
	struct junction_map *jn = NULL;
	bool self_ref = False;
	TALLOC_CTX *ctx = talloc_tos();
	bool ret;

	jn = talloc_zero(ctx, struct junction_map);
	if (!jn) {
		return WERR_NOMEM;
	}

	if(!create_junction(ctx, r->in.dfs_entry_path, jn)) {
		return WERR_DFS_NO_SUCH_SERVER;
	}

	/* The following call can change the cwd. */
	if(!NT_STATUS_IS_OK(get_referred_path(ctx, r->in.dfs_entry_path,
					jn, &consumedcnt, &self_ref)) ||
			consumedcnt < strlen(r->in.dfs_entry_path)) {
		return WERR_DFS_NO_SUCH_VOL;
	}

	switch (r->in.level) {
		case 1:
			r->out.info->info1 = talloc_zero(ctx,struct dfs_Info1);
			if (!r->out.info->info1) {
				return WERR_NOMEM;
			}
			ret = init_reply_dfs_info_1(ctx, jn, r->out.info->info1);
			break;
		case 2:
			r->out.info->info2 = talloc_zero(ctx,struct dfs_Info2);
			if (!r->out.info->info2) {
				return WERR_NOMEM;
			}
			ret = init_reply_dfs_info_2(ctx, jn, r->out.info->info2);
			break;
		case 3:
			r->out.info->info3 = talloc_zero(ctx,struct dfs_Info3);
			if (!r->out.info->info3) {
				return WERR_NOMEM;
			}
			ret = init_reply_dfs_info_3(ctx, jn, r->out.info->info3);
			break;
		case 100:
			r->out.info->info100 = talloc_zero(ctx,struct dfs_Info100);
			if (!r->out.info->info100) {
				return WERR_NOMEM;
			}
			ret = init_reply_dfs_info_100(ctx, jn, r->out.info->info100);
			break;
		default:
			r->out.info->info1 = NULL;
			return WERR_INVALID_PARAM;
	}

	if (!ret)
		return WERR_INVALID_PARAM;

	return WERR_OK;
}

WERROR _dfs_SetInfo(struct pipes_struct *p, struct dfs_SetInfo *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_Rename(struct pipes_struct *p, struct dfs_Rename *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_Move(struct pipes_struct *p, struct dfs_Move *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_ManagerGetConfigInfo(struct pipes_struct *p, struct dfs_ManagerGetConfigInfo *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_ManagerSendSiteInfo(struct pipes_struct *p, struct dfs_ManagerSendSiteInfo *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_AddFtRoot(struct pipes_struct *p, struct dfs_AddFtRoot *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_RemoveFtRoot(struct pipes_struct *p, struct dfs_RemoveFtRoot *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_AddStdRoot(struct pipes_struct *p, struct dfs_AddStdRoot *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_RemoveStdRoot(struct pipes_struct *p, struct dfs_RemoveStdRoot *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_ManagerInitialize(struct pipes_struct *p, struct dfs_ManagerInitialize *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_AddStdRootForced(struct pipes_struct *p, struct dfs_AddStdRootForced *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_GetDcAddress(struct pipes_struct *p, struct dfs_GetDcAddress *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_SetDcAddress(struct pipes_struct *p, struct dfs_SetDcAddress *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_FlushFtTable(struct pipes_struct *p, struct dfs_FlushFtTable *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_Add2(struct pipes_struct *p, struct dfs_Add2 *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_Remove2(struct pipes_struct *p, struct dfs_Remove2 *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_EnumEx(struct pipes_struct *p, struct dfs_EnumEx *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}

WERROR _dfs_SetInfo2(struct pipes_struct *p, struct dfs_SetInfo2 *r)
{
	/* FIXME: Implement your code here */
	p->rng_fault_state = True;
	return WERR_NOT_SUPPORTED;
}