This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[kai/samba.git] / source3 / rpc_server / srv_dfs_nt.c
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.
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, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 /* This is the implementation of the dfs pipe. */
25
26 #include "includes.h"
27 #include "nterr.h"
28
29 extern pstring global_myname;
30
31 #define MAX_MSDFS_JUNCTIONS 256
32
33 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
34    dfs exists, or 0 otherwise. */
35
36 uint32 _dfs_exist(pipes_struct *p, DFS_Q_DFS_EXIST *q_u, DFS_R_DFS_EXIST *r_u)
37 {
38         if(lp_host_msdfs()) 
39                 return 1;
40         else
41                 return 0;
42 }
43
44 WERROR _dfs_add(pipes_struct *p, DFS_Q_DFS_ADD* q_u, DFS_R_DFS_ADD *r_u)
45 {
46   struct current_user user;
47   struct junction_map jn;
48   struct referral* old_referral_list = NULL;
49   BOOL exists = False;
50
51   pstring dfspath, servername, sharename;
52   pstring altpath;
53
54   get_current_user(&user,p);
55
56   if (user.uid != 0) {
57         DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
58         return WERR_ACCESS_DENIED;
59   }
60
61   unistr2_to_ascii(dfspath, &q_u->DfsEntryPath, sizeof(dfspath)-1);
62   unistr2_to_ascii(servername, &q_u->ServerName, sizeof(servername)-1);
63   unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
64
65   DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
66            dfspath, servername, sharename));
67
68   pstrcpy(altpath, servername);
69   pstrcat(altpath, "\\");
70   pstrcat(altpath, sharename);
71
72   if(!create_junction(dfspath, &jn))
73     return WERR_DFS_NO_SUCH_SERVER;
74
75   if(get_referred_path(&jn))
76     {
77       exists = True;
78       jn.referral_count += 1;
79       old_referral_list = jn.referral_list;
80     }
81   else
82     jn.referral_count = 1;
83
84   jn.referral_list = (struct referral*) talloc(p->mem_ctx, jn.referral_count 
85                                                * sizeof(struct referral));
86
87   if(jn.referral_list == NULL)
88     {
89       DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
90       return WERR_DFS_INTERNAL_ERROR;
91     }
92
93   if(old_referral_list)
94     {
95       memcpy(jn.referral_list, old_referral_list, 
96              sizeof(struct referral)*jn.referral_count-1);
97       SAFE_FREE(old_referral_list);
98     }
99   
100   jn.referral_list[jn.referral_count-1].proximity = 0;
101   jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
102
103   pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
104   
105   if(!create_msdfs_link(&jn, exists))
106     return WERR_DFS_CANT_CREATE_JUNCT;
107
108   return WERR_OK;
109 }
110
111 WERROR _dfs_remove(pipes_struct *p, DFS_Q_DFS_REMOVE *q_u, 
112                    DFS_R_DFS_REMOVE *r_u)
113 {
114   struct current_user user;
115   struct junction_map jn;
116   BOOL found = False;
117
118   pstring dfspath, servername, sharename;
119   pstring altpath;
120
121   get_current_user(&user,p);
122
123   if (user.uid != 0) {
124         DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
125         return WERR_ACCESS_DENIED;
126   }
127
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);
131
132   if(q_u->ptr_ShareName)
133     unistr2_to_ascii(sharename, &q_u->ShareName, sizeof(sharename)-1);
134
135   if(q_u->ptr_ServerName && q_u->ptr_ShareName)
136     {
137       pstrcpy(altpath, servername);
138       pstrcat(altpath, "\\");
139       pstrcat(altpath, sharename);
140     }
141
142   DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
143            dfspath, servername, sharename));
144
145   if(!create_junction(dfspath, &jn))
146     return WERR_DFS_NO_SUCH_SERVER;
147
148   if(!get_referred_path(&jn))
149     return WERR_DFS_NO_SUCH_VOL;
150
151   /* if no server-share pair given, remove the msdfs link completely */
152   if(!q_u->ptr_ServerName && !q_u->ptr_ShareName)
153     {
154       if(!remove_msdfs_link(&jn))
155         return WERR_DFS_NO_SUCH_VOL;
156     }
157   else
158     {
159       int i=0;
160       /* compare each referral in the list with the one to remove */
161       for(i=0;i<jn.referral_count;i++)
162         {
163           pstring refpath;
164           pstrcpy(refpath,jn.referral_list[i].alternate_path);
165           trim_string(refpath, "\\", "\\");
166           if(strequal(refpath, altpath))
167             {
168               *(jn.referral_list[i].alternate_path)='\0';
169               found = True;
170             }
171         }
172       if(!found)
173         return WERR_DFS_NO_SUCH_SHARE;
174       
175       /* Only one referral, remove it */
176       if(jn.referral_count == 1)
177         {
178           if(!remove_msdfs_link(&jn))
179             return WERR_DFS_NO_SUCH_VOL;
180         }
181       else
182         {
183           if(!create_msdfs_link(&jn, True))
184             return WERR_DFS_CANT_CREATE_JUNCT;
185         }
186     }
187
188   return WERR_OK;
189 }
190
191 static BOOL init_reply_dfs_info_1(struct junction_map* j, DFS_INFO_1* dfs1, int num_j)
192 {
193   int i=0;
194   for(i=0;i<num_j;i++) 
195     {
196       pstring str;
197       dfs1[i].ptr_entrypath = 1;
198       slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname, 
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);
202     }
203   return True;
204 }
205
206 static BOOL init_reply_dfs_info_2(struct junction_map* j, DFS_INFO_2* dfs2, int num_j)
207 {
208   int i=0;
209   for(i=0;i<num_j;i++)
210     {
211       pstring str;
212       dfs2[i].ptr_entrypath = 1;
213       slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
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;
219     }
220   return True;
221 }
222
223 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, DFS_INFO_3* dfs3, int num_j)
224 {
225   int i=0,ii=0;
226   for(i=0;i<num_j;i++)
227     {
228       pstring str;
229       dfs3[i].ptr_entrypath = 1;
230       slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname,
231                j[i].service_name, j[i].volume_name);
232       init_unistr2(&dfs3[i].entrypath, str, strlen(str)+1);
233       dfs3[i].ptr_comment = 1;
234       init_unistr2(&dfs3[i].comment, "", 1); 
235       dfs3[i].state = 1;
236       dfs3[i].num_storages = dfs3[i].num_storage_infos = j[i].referral_count;
237       dfs3[i].ptr_storages = 1;
238      
239       /* also enumerate the storages */
240       dfs3[i].storages = (DFS_STORAGE_INFO*) talloc(ctx, j[i].referral_count * 
241                                                     sizeof(DFS_STORAGE_INFO));
242       if (!dfs3[i].storages)
243         return False;
244
245       memset(dfs3[i].storages, '\0', j[i].referral_count * sizeof(DFS_STORAGE_INFO));
246
247       for(ii=0;ii<j[i].referral_count;ii++)
248         {
249           char* p; 
250           pstring path;
251           DFS_STORAGE_INFO* stor = &(dfs3[i].storages[ii]);
252           struct referral* ref = &(j[i].referral_list[ii]);
253           
254           pstrcpy(path, ref->alternate_path);
255           trim_string(path,"\\","");
256           p = strrchr_m(path,'\\');
257           if(p==NULL)
258             {
259               DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
260               continue;
261             }
262           *p = '\0';
263           DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
264           stor->state = 2; /* set all storages as ONLINE */
265           init_unistr2(&stor->servername, path, strlen(path)+1);
266           init_unistr2(&stor->sharename,  p+1, strlen(p+1)+1);
267           stor->ptr_servername = stor->ptr_sharename = 1;
268         }
269     }
270   return True;
271 }
272
273 static WERROR init_reply_dfs_ctr(TALLOC_CTX *ctx, uint32 level, 
274                                    DFS_INFO_CTR* ctr, struct junction_map* jn,
275                                    int num_jn)
276 {
277   /* do the levels */
278   switch(level)
279     {
280     case 1:
281       {
282         DFS_INFO_1* dfs1;
283         dfs1 = (DFS_INFO_1*) talloc(ctx, num_jn * sizeof(DFS_INFO_1));
284         if (!dfs1)
285                 return WERR_NOMEM;
286         init_reply_dfs_info_1(jn, dfs1, num_jn);
287         ctr->dfs.info1 = dfs1;
288         break;
289       }
290     case 2:
291       {
292         DFS_INFO_2* dfs2;
293         dfs2 = (DFS_INFO_2*) talloc(ctx, num_jn * sizeof(DFS_INFO_2));
294         if (!dfs2)
295                 return WERR_NOMEM;
296         init_reply_dfs_info_2(jn, dfs2, num_jn);
297         ctr->dfs.info2 = dfs2;
298         break;
299       }
300     case 3:
301       {
302         DFS_INFO_3* dfs3;
303         dfs3 = (DFS_INFO_3*) talloc(ctx, num_jn * sizeof(DFS_INFO_3));
304         if (!dfs3)
305                 return WERR_NOMEM;
306         init_reply_dfs_info_3(ctx, jn, dfs3, num_jn);
307         ctr->dfs.info3 = dfs3;
308         break;
309       }
310         default:
311                 return WERR_INVALID_PARAM;
312     }
313   return WERR_OK;
314 }
315       
316 WERROR _dfs_enum(pipes_struct *p, DFS_Q_DFS_ENUM *q_u, DFS_R_DFS_ENUM *r_u)
317 {
318   uint32 level = q_u->level;
319   struct junction_map jn[MAX_MSDFS_JUNCTIONS];
320   int num_jn = 0;
321
322   num_jn = enum_msdfs_links(jn);
323   
324   DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
325
326   r_u->ptr_buffer = level;
327   r_u->level = r_u->level2 = level;
328   r_u->ptr_num_entries = r_u->ptr_num_entries2 = 1;
329   r_u->num_entries = r_u->num_entries2 = num_jn;
330   r_u->reshnd.ptr_hnd = 1;
331   r_u->reshnd.handle = num_jn;
332   
333   r_u->ctr = (DFS_INFO_CTR*)talloc(p->mem_ctx, sizeof(DFS_INFO_CTR));
334   if (!r_u->ctr)
335     return WERR_NOMEM;
336   ZERO_STRUCTP(r_u->ctr);
337   r_u->ctr->switch_value = level;
338   r_u->ctr->num_entries = num_jn;
339   r_u->ctr->ptr_dfs_ctr = 1;
340   
341   r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, r_u->ctr, jn, num_jn);
342
343   return r_u->status;
344 }
345       
346 WERROR _dfs_get_info(pipes_struct *p, DFS_Q_DFS_GET_INFO *q_u, 
347                      DFS_R_DFS_GET_INFO *r_u)
348 {
349   UNISTR2* uni_path = &q_u->uni_path;
350   uint32 level = q_u->level;
351   pstring path;
352   struct junction_map jn;
353
354   unistr2_to_ascii(path, uni_path, sizeof(path)-1);
355   if(!create_junction(path, &jn))
356      return WERR_DFS_NO_SUCH_SERVER;
357   
358   if(!get_referred_path(&jn))
359     return WERR_DFS_NO_SUCH_VOL;
360
361   r_u->level = level;
362   r_u->ptr_ctr = 1;
363   r_u->status = init_reply_dfs_ctr(p->mem_ctx, level, &r_u->ctr, &jn, 1);
364   
365   return r_u->status;
366 }