* fix to display correct form information in REG_BINARY information
[Samba.git] / source / rpc_server / srv_dfs_nt.c
blob65e387176d3a69ad94083dace585c5667eb39c39
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines for Dfs
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Shirish Kalele 2000.
7 * Copyright (C) Jeremy Allison 2001.
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 2 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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This is the implementation of the dfs pipe. */
26 #include "includes.h"
27 #include "nterr.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_RPC_SRV
32 extern pstring global_myname;
34 #define MAX_MSDFS_JUNCTIONS 256
36 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
37 dfs exists, or 0 otherwise. */
39 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
41 if(lp_host_msdfs())
42 return 1;
43 else
44 return 0;
47 WERROR _dfs_add(pipes_struct *p, DFS_Q_DFS_ADD* q_u, DFS_R_DFS_ADD *r_u)
49 struct current_user user;
50 struct junction_map jn;
51 struct referral* old_referral_list = NULL;
52 BOOL exists = False;
54 pstring dfspath, servername, sharename;
55 pstring altpath;
57 get_current_user(&user,p);
59 if (user.uid != 0) {
60 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
61 return WERR_ACCESS_DENIED;
64 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
65 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
66 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
68 DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
69 dfspath, servername, sharename));
71 pstrcpy(altpath, servername);
72 pstrcat(altpath, "\\");
73 pstrcat(altpath, sharename);
75 if(get_referred_path(dfspath, &jn, NULL, NULL))
77 exists = True;
78 jn.referral_count += 1;
79 old_referral_list = jn.referral_list;
81 else
82 jn.referral_count = 1;
84 jn.referral_list = (struct referral*) talloc(p->mem_ctx, jn.referral_count
85 * sizeof(struct referral));
87 if(jn.referral_list == NULL)
89 DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
90 return WERR_DFS_INTERNAL_ERROR;
93 if(old_referral_list)
95 memcpy(jn.referral_list, old_referral_list,
96 sizeof(struct referral)*jn.referral_count-1);
97 SAFE_FREE(old_referral_list);
100 jn.referral_list[jn.referral_count-1].proximity = 0;
101 jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
103 pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
105 if(!create_msdfs_link(&jn, exists))
106 return WERR_DFS_CANT_CREATE_JUNCT;
108 return WERR_OK;
111 WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u,
112 DFS_R_DFS_REMOVE *r_u)
114 struct current_user user;
115 struct junction_map jn;
116 BOOL found = False;
118 pstring dfspath, servername, sharename;
119 pstring altpath;
121 get_current_user(&user,p);
123 if (user.uid != 0) {
124 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
125 return WERR_ACCESS_DENIED;
128 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
129 if(q_u->ptr_ServerName)
130 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
132 if(q_u->ptr_ShareName)
133 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
135 if(q_u->ptr_ServerName && q_u->ptr_ShareName)
137 pstrcpy(altpath, servername);
138 pstrcat(altpath, "\\");
139 pstrcat(altpath, sharename);
140 strlower(altpath);
143 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
144 dfspath, servername, sharename));
146 if(!get_referred_path(dfspath, &jn, NULL, NULL))
147 return WERR_DFS_NO_SUCH_VOL;
149 /* if no server-share pair given, remove the msdfs link completely */
150 if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
152 if(!remove_msdfs_link(&jn))
153 return WERR_DFS_NO_SUCH_VOL;
155 else
157 int i=0;
158 /* compare each referral in the list with the one to remove */
159 DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn.referral_count));
160 for(i=0;i<jn.referral_count;i++)
162 pstring refpath;
163 pstrcpy(refpath,jn.referral_list[i].alternate_path);
164 trim_string(refpath, "\\", "\\");
165 DEBUG(10,("_dfs_remove: refpath: .%s.\n", refpath));
166 if(strequal(refpath, altpath))
168 *(jn.referral_list[i].alternate_path)='\0';
169 DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
170 refpath));
171 found = True;
174 if(!found)
175 return WERR_DFS_NO_SUCH_SHARE;
177 /* Only one referral, remove it */
178 if(jn.referral_count == 1)
180 if(!remove_msdfs_link(&jn))
181 return WERR_DFS_NO_SUCH_VOL;
183 else
185 if(!create_msdfs_link(&jn, True))
186 return WERR_DFS_CANT_CREATE_JUNCT;
190 return WERR_OK;
193 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
195 int i=0;
196 for(i=0;i<num_j;i++)
198 pstring str;
199 dfs1[i].ptr_entrypath = 1;
200 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
201 j[i].service_name, j[i].volume_name);
202 DEBUG(5,("init_reply_dfs_info_1: %d) initing entrypath: %s\n",i,str));
203 init_unistr2(&dfs1[i].entrypath,str,strlen(str)+1);
205 return True;
208 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
210 int i=0;
211 for(i=0;i<num_j;i++)
213 pstring str;
214 dfs2[i].ptr_entrypath = 1;
215 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
216 j[i].service_name, j[i].volume_name);
217 init_unistr2(&dfs2[i].entrypath, str, strlen(str)+1);
218 dfs2[i].ptr_comment = 0;
219 dfs2[i].state = 1; /* set up state of dfs junction as OK */
220 dfs2[i].num_storages = j[i].referral_count;
222 return True;
225 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
227 int i=0,ii=0;
228 for(i=0;i<num_j;i++)
230 pstring str;
231 dfs3[i].ptr_entrypath = 1;
232 if (j[i].volume_name[0] == '\0')
233 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s",
234 global_myname, j[i].service_name);
235 else
236 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
237 j[i].service_name, j[i].volume_name);
239 init_unistr2(&dfs3[i].entrypath, str, strlen(str)+1);
240 dfs3[i].ptr_comment = 1;
241 init_unistr2(&dfs3[i].comment, "", 1);
242 dfs3[i].state = 1;
243 dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
244 dfs3[i].ptr_storages = 1;
246 /* also enumerate the storages */
247 dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count *
248 sizeof(DFS_STORAGE_INFO));
249 if (!dfs3[i].storages)
250 return False;
252 memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
254 for(ii=0;ii<j[i].referral_count;ii++)
256 char* p;
257 pstring path;
258 DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
259 struct referral* ref = &(j[i].referral_list[ii]);
261 pstrcpy(path, ref->alternate_path);
262 trim_string(path,"\\","");
263 p = strrchr_m(path,'\\');
264 if(p==NULL)
266 DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
267 continue;
269 *p = '\0';
270 DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
271 stor->state = 2; /* set all storages as ONLINE */
272 init_unistr2(&stor->servername, path, strlen(path)+1);
273 init_unistr2(&stor->sharename, p+1, strlen(p+1)+1);
274 stor->ptr_servername = stor->ptr_sharename = 1;
277 return True;
280 static WERROR init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level,
281 DFS_INFO_CTR* ctr, struct junction_map* jn,
282 int num_jn)
284 /* do the levels */
285 switch(level)
287 case 1:
289 DFS_INFO_1* dfs1;
290 dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
291 if (!dfs1)
292 return WERR_NOMEM;
293 init_reply_dfs_info_1(jn, dfs1, num_jn);
294 ctr->dfs.info1 = dfs1;
295 break;
297 case 2:
299 DFS_INFO_2* dfs2;
300 dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
301 if (!dfs2)
302 return WERR_NOMEM;
303 init_reply_dfs_info_2(jn, dfs2, num_jn);
304 ctr->dfs.info2 = dfs2;
305 break;
307 case 3:
309 DFS_INFO_3* dfs3;
310 dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
311 if (!dfs3)
312 return WERR_NOMEM;
313 init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
314 ctr->dfs.info3 = dfs3;
315 break;
317 default:
318 return WERR_INVALID_PARAM;
320 return WERR_OK;
323 WERROR _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
325 uint32 level = q_u->level;
326 struct junction_map jn[MAX_MSDFS_JUNCTIONS];
327 int num_jn = 0;
329 num_jn = enum_msdfs_links(jn);
331 DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
333 r_u->ptr_buffer = level;
334 r_u->level = r_u->level2 = level;
335 r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
336 r_u->num_entries = r_u->num_entries2 = num_jn;
337 r_u->reshnd.ptr_hnd = 1;
338 r_u->reshnd.handle = num_jn;
340 r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
341 if (!r_u->ctr)
342 return WERR_NOMEM;
343 ZERO_STRUCTP(r_u->ctr);
344 r_u->ctr->switch_value = level;
345 r_u->ctr->num_entries = num_jn;
346 r_u->ctr->ptr_dfs_ctr = 1;
348 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
350 return r_u->status;
353 WERROR _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u,
354 DFS_R_DFS_GET_INFO *r_u)
356 UNISTR2* uni_path = &q_u->uni_path;
357 uint32 level = q_u->level;
358 pstring path;
359 struct junction_map jn;
361 unistr2_to_ascii(path, uni_path, sizeof(path)-1);
362 if(!create_junction(path, &jn))
363 return WERR_DFS_NO_SUCH_SERVER;
365 if(!get_referred_path(path, &jn, NULL, NULL))
366 return WERR_DFS_NO_SUCH_VOL;
368 r_u->level = level;
369 r_u->ptr_ctr = 1;
370 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
372 return r_u->status;