don't dereference null pointer
[Samba/gebeck_regimport.git] / source4 / rpc_server / srv_dfs_nt.c
blobbb9ed87a48ea37ca1dd34b0addd7076bbabc2616
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 #define MAX_MSDFS_JUNCTIONS 256
34 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
35 dfs exists, or 0 otherwise. */
37 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
39 if(lp_host_msdfs())
40 return 1;
41 else
42 return 0;
45 WERROR _dfs_add(pipes_struct *p, DFS_Q_DFS_ADD* q_u, DFS_R_DFS_ADD *r_u)
47 struct current_user user;
48 struct junction_map jn;
49 struct referral* old_referral_list = NULL;
50 BOOL exists = False;
52 pstring dfspath, servername, sharename;
53 pstring altpath;
55 get_current_user(&user,p);
57 if (user.uid != 0) {
58 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
59 return WERR_ACCESS_DENIED;
62 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
63 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
64 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
66 DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
67 dfspath, servername, sharename));
69 pstrcpy(altpath, servername);
70 pstrcat(altpath, "\\");
71 pstrcat(altpath, sharename);
73 if(get_referred_path(dfspath, &jn, NULL, NULL))
75 exists = True;
76 jn.referral_count += 1;
77 old_referral_list = jn.referral_list;
79 else
80 jn.referral_count = 1;
82 jn.referral_list = (struct referral*) talloc(p->mem_ctx, jn.referral_count
83 * sizeof(struct referral));
85 if(jn.referral_list == NULL)
87 DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
88 return WERR_DFS_INTERNAL_ERROR;
91 if(old_referral_list)
93 memcpy(jn.referral_list, old_referral_list,
94 sizeof(struct referral)*jn.referral_count-1);
95 SAFE_FREE(old_referral_list);
98 jn.referral_list[jn.referral_count-1].proximity = 0;
99 jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
101 pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
103 if(!create_msdfs_link(&jn, exists))
104 return WERR_DFS_CANT_CREATE_JUNCT;
106 return WERR_OK;
109 WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u,
110 DFS_R_DFS_REMOVE *r_u)
112 struct current_user user;
113 struct junction_map jn;
114 BOOL found = False;
116 pstring dfspath, servername, sharename;
117 pstring altpath;
119 get_current_user(&user,p);
121 if (user.uid != 0) {
122 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
123 return WERR_ACCESS_DENIED;
126 unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
127 if(q_u->ptr_ServerName)
128 unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
130 if(q_u->ptr_ShareName)
131 unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
133 if(q_u->ptr_ServerName && q_u->ptr_ShareName)
135 pstrcpy(altpath, servername);
136 pstrcat(altpath, "\\");
137 pstrcat(altpath, sharename);
138 strlower(altpath);
141 DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
142 dfspath, servername, sharename));
144 if(!get_referred_path(dfspath, &jn, NULL, NULL))
145 return WERR_DFS_NO_SUCH_VOL;
147 /* if no server-share pair given, remove the msdfs link completely */
148 if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
150 if(!remove_msdfs_link(&jn))
151 return WERR_DFS_NO_SUCH_VOL;
153 else
155 int i=0;
156 /* compare each referral in the list with the one to remove */
157 DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn.referral_count));
158 for(i=0;i<jn.referral_count;i++)
160 pstring refpath;
161 pstrcpy(refpath,jn.referral_list[i].alternate_path);
162 trim_string(refpath, "\\", "\\");
163 DEBUG(10,("_dfs_remove: refpath: .%s.\n", refpath));
164 if(strequal(refpath, altpath))
166 *(jn.referral_list[i].alternate_path)='\0';
167 DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
168 refpath));
169 found = True;
172 if(!found)
173 return WERR_DFS_NO_SUCH_SHARE;
175 /* Only one referral, remove it */
176 if(jn.referral_count == 1)
178 if(!remove_msdfs_link(&jn))
179 return WERR_DFS_NO_SUCH_VOL;
181 else
183 if(!create_msdfs_link(&jn, True))
184 return WERR_DFS_CANT_CREATE_JUNCT;
188 return WERR_OK;
191 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
193 int i=0;
194 for(i=0;i<num_j;i++)
196 pstring str;
197 dfs1[i].ptr_entrypath = 1;
198 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", lp_netbios_name(),
199 j[i].service_name, j[i].volume_name);
200 DEBUG(5,("init_reply_dfs_info_1: %d) initing entrypath: %s\n",i,str));
201 init_unistr2(&dfs1[i].entrypath,str,strlen(str)+1);
203 return True;
206 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
208 int i=0;
209 for(i=0;i<num_j;i++)
211 pstring str;
212 dfs2[i].ptr_entrypath = 1;
213 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", lp_netbios_name(),
214 j[i].service_name, j[i].volume_name);
215 init_unistr2(&dfs2[i].entrypath, str, strlen(str)+1);
216 dfs2[i].ptr_comment = 0;
217 dfs2[i].state = 1; /* set up state of dfs junction as OK */
218 dfs2[i].num_storages = j[i].referral_count;
220 return True;
223 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
225 int i=0,ii=0;
226 for(i=0;i<num_j;i++)
228 pstring str;
229 dfs3[i].ptr_entrypath = 1;
230 if (j[i].volume_name[0] == '\0')
231 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s",
232 lp_netbios_name(), j[i].service_name);
233 else
234 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", lp_netbios_name(),
235 j[i].service_name, j[i].volume_name);
237 init_unistr2(&dfs3[i].entrypath, str, strlen(str)+1);
238 dfs3[i].ptr_comment = 1;
239 init_unistr2(&dfs3[i].comment, "", 1);
240 dfs3[i].state = 1;
241 dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
242 dfs3[i].ptr_storages = 1;
244 /* also enumerate the storages */
245 dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count *
246 sizeof(DFS_STORAGE_INFO));
247 if (!dfs3[i].storages)
248 return False;
250 memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
252 for(ii=0;ii<j[i].referral_count;ii++)
254 char* p;
255 pstring path;
256 DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
257 struct referral* ref = &(j[i].referral_list[ii]);
259 pstrcpy(path, ref->alternate_path);
260 trim_string(path,"\\","");
261 p = strrchr_m(path,'\\');
262 if(p==NULL)
264 DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
265 continue;
267 *p = '\0';
268 DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
269 stor->state = 2; /* set all storages as ONLINE */
270 init_unistr2(&stor->servername, path, strlen(path)+1);
271 init_unistr2(&stor->sharename, p+1, strlen(p+1)+1);
272 stor->ptr_servername = stor->ptr_sharename = 1;
275 return True;
278 static WERROR init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level,
279 DFS_INFO_CTR* ctr, struct junction_map* jn,
280 int num_jn)
282 /* do the levels */
283 switch(level)
285 case 1:
287 DFS_INFO_1* dfs1;
288 dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
289 if (!dfs1)
290 return WERR_NOMEM;
291 init_reply_dfs_info_1(jn, dfs1, num_jn);
292 ctr->dfs.info1 = dfs1;
293 break;
295 case 2:
297 DFS_INFO_2* dfs2;
298 dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
299 if (!dfs2)
300 return WERR_NOMEM;
301 init_reply_dfs_info_2(jn, dfs2, num_jn);
302 ctr->dfs.info2 = dfs2;
303 break;
305 case 3:
307 DFS_INFO_3* dfs3;
308 dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
309 if (!dfs3)
310 return WERR_NOMEM;
311 init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
312 ctr->dfs.info3 = dfs3;
313 break;
315 default:
316 return WERR_INVALID_PARAM;
318 return WERR_OK;
321 WERROR _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
323 uint32 level = q_u->level;
324 struct junction_map jn[MAX_MSDFS_JUNCTIONS];
325 int num_jn = 0;
327 num_jn = enum_msdfs_links(jn);
329 DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
331 r_u->ptr_buffer = level;
332 r_u->level = r_u->level2 = level;
333 r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
334 r_u->num_entries = r_u->num_entries2 = num_jn;
335 r_u->reshnd.ptr_hnd = 1;
336 r_u->reshnd.handle = num_jn;
338 r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
339 if (!r_u->ctr)
340 return WERR_NOMEM;
341 ZERO_STRUCTP(r_u->ctr);
342 r_u->ctr->switch_value = level;
343 r_u->ctr->num_entries = num_jn;
344 r_u->ctr->ptr_dfs_ctr = 1;
346 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
348 return r_u->status;
351 WERROR _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u,
352 DFS_R_DFS_GET_INFO *r_u)
354 UNISTR2* uni_path = &q_u->uni_path;
355 uint32 level = q_u->level;
356 pstring path;
357 struct junction_map jn;
359 unistr2_to_ascii(path, uni_path, sizeof(path)-1);
360 if(!create_junction(path, &jn))
361 return WERR_DFS_NO_SUCH_SERVER;
363 if(!get_referred_path(path, &jn, NULL, NULL))
364 return WERR_DFS_NO_SUCH_VOL;
366 r_u->level = level;
367 r_u->ptr_ctr = 1;
368 r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
370 return r_u->status;