r16799: Fix remote smbd crash bug by removing half-implemented info level 4
[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) Shirish Kalele        2000.
5  *  Copyright (C) Jeremy Allison        2001.
6  *  Copyright (C) Jelmer Vernooij       2005.
7  *  
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 2 of the License, or
11  *  (at your option) any later version.
12  *  
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.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 /* This is the implementation of the dfs pipe. */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_MSDFS
29
30 /* This function does not return a WERROR or NTSTATUS code but rather 1 if
31    dfs exists, or 0 otherwise. */
32
33 uint32 _dfs_GetManagerVersion(pipes_struct *p, NETDFS_Q_DFS_GETMANAGERVERSION *q_u, NETDFS_R_DFS_GETMANAGERVERSION *r_u)
34 {
35         if(lp_host_msdfs()) 
36                 return 1;
37         else
38                 return 0;
39 }
40
41 WERROR _dfs_Add(pipes_struct *p, NETDFS_Q_DFS_ADD* q_u, NETDFS_R_DFS_ADD *r_u)
42 {
43         struct current_user user;
44         struct junction_map jn;
45         struct referral* old_referral_list = NULL;
46         BOOL exists = False;
47
48         pstring dfspath, servername, sharename;
49         pstring altpath;
50
51         get_current_user(&user,p);
52
53         if (user.ut.uid != 0) {
54                 DEBUG(10,("_dfs_add: uid != 0. Access denied.\n"));
55                 return WERR_ACCESS_DENIED;
56         }
57
58         unistr2_to_ascii(dfspath, &q_u->path, sizeof(dfspath)-1);
59         unistr2_to_ascii(servername, &q_u->server, sizeof(servername)-1);
60         unistr2_to_ascii(sharename, &q_u->share, sizeof(sharename)-1);
61
62         DEBUG(5,("init_reply_dfs_add: Request to add %s -> %s\\%s.\n",
63                 dfspath, servername, sharename));
64
65         pstrcpy(altpath, servername);
66         pstrcat(altpath, "\\");
67         pstrcat(altpath, sharename);
68
69         /* The following call can change the cwd. */
70         if(get_referred_path(p->mem_ctx, dfspath, &jn, NULL, NULL)) {
71                 exists = True;
72                 jn.referral_count += 1;
73                 old_referral_list = jn.referral_list;
74         } else {
75                 jn.referral_count = 1;
76         }
77
78         vfs_ChDir(p->conn,p->conn->connectpath);
79
80         jn.referral_list = TALLOC_ARRAY(p->mem_ctx, struct referral, jn.referral_count);
81         if(jn.referral_list == NULL) {
82                 DEBUG(0,("init_reply_dfs_add: talloc failed for referral list!\n"));
83                 return WERR_DFS_INTERNAL_ERROR;
84         }
85
86         if(old_referral_list) {
87                 memcpy(jn.referral_list, old_referral_list, sizeof(struct referral)*jn.referral_count-1);
88         }
89   
90         jn.referral_list[jn.referral_count-1].proximity = 0;
91         jn.referral_list[jn.referral_count-1].ttl = REFERRAL_TTL;
92
93         pstrcpy(jn.referral_list[jn.referral_count-1].alternate_path, altpath);
94   
95         if(!create_msdfs_link(&jn, exists)) {
96                 vfs_ChDir(p->conn,p->conn->connectpath);
97                 return WERR_DFS_CANT_CREATE_JUNCT;
98         }
99         vfs_ChDir(p->conn,p->conn->connectpath);
100
101         return WERR_OK;
102 }
103
104 WERROR _dfs_Remove(pipes_struct *p, NETDFS_Q_DFS_REMOVE *q_u, 
105                    NETDFS_R_DFS_REMOVE *r_u)
106 {
107         struct current_user user;
108         struct junction_map jn;
109         BOOL found = False;
110
111         pstring dfspath, servername, sharename;
112         pstring altpath;
113
114         get_current_user(&user,p);
115
116         if (user.ut.uid != 0) {
117                 DEBUG(10,("_dfs_remove: uid != 0. Access denied.\n"));
118                 return WERR_ACCESS_DENIED;
119         }
120
121         unistr2_to_ascii(dfspath, &q_u->path, sizeof(dfspath)-1);
122         if(q_u->ptr0_server) {
123                 unistr2_to_ascii(servername, &q_u->server, sizeof(servername)-1);
124         }
125
126         if(q_u->ptr0_share) {
127                 unistr2_to_ascii(sharename, &q_u->share, sizeof(sharename)-1);
128         }
129
130         if(q_u->ptr0_server && q_u->ptr0_share) {
131                 pstrcpy(altpath, servername);
132                 pstrcat(altpath, "\\");
133                 pstrcat(altpath, sharename);
134                 strlower_m(altpath);
135         }
136
137         DEBUG(5,("init_reply_dfs_remove: Request to remove %s -> %s\\%s.\n",
138                 dfspath, servername, sharename));
139
140         if(!get_referred_path(p->mem_ctx, dfspath, &jn, NULL, NULL)) {
141                 return WERR_DFS_NO_SUCH_VOL;
142         }
143
144         /* if no server-share pair given, remove the msdfs link completely */
145         if(!q_u->ptr0_server && !q_u->ptr0_share) {
146                 if(!remove_msdfs_link(&jn)) {
147                         vfs_ChDir(p->conn,p->conn->connectpath);
148                         return WERR_DFS_NO_SUCH_VOL;
149                 }
150                 vfs_ChDir(p->conn,p->conn->connectpath);
151         } else {
152                 int i=0;
153                 /* compare each referral in the list with the one to remove */
154                 DEBUG(10,("altpath: .%s. refcnt: %d\n", altpath, jn.referral_count));
155                 for(i=0;i<jn.referral_count;i++) {
156                         pstring refpath;
157                         pstrcpy(refpath,jn.referral_list[i].alternate_path);
158                         trim_char(refpath, '\\', '\\');
159                         DEBUG(10,("_dfs_remove:  refpath: .%s.\n", refpath));
160                         if(strequal(refpath, altpath)) {
161                                 *(jn.referral_list[i].alternate_path)='\0';
162                                 DEBUG(10,("_dfs_remove: Removal request matches referral %s\n",
163                                         refpath));
164                                 found = True;
165                         }
166                 }
167
168                 if(!found) {
169                         return WERR_DFS_NO_SUCH_SHARE;
170                 }
171
172                 /* Only one referral, remove it */
173                 if(jn.referral_count == 1) {
174                         if(!remove_msdfs_link(&jn)) {
175                                 vfs_ChDir(p->conn,p->conn->connectpath);
176                                 return WERR_DFS_NO_SUCH_VOL;
177                         }
178                 } else {
179                         if(!create_msdfs_link(&jn, True)) { 
180                                 vfs_ChDir(p->conn,p->conn->connectpath);
181                                 return WERR_DFS_CANT_CREATE_JUNCT;
182                         }
183                 }
184                 vfs_ChDir(p->conn,p->conn->connectpath);
185         }
186
187         return WERR_OK;
188 }
189
190 static BOOL init_reply_dfs_info_1(struct junction_map* j, NETDFS_DFS_INFO1* dfs1)
191 {
192         pstring str;
193         dfs1->ptr0_path = 1;
194         slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname(), 
195                 j->service_name, j->volume_name);
196         DEBUG(5,("init_reply_dfs_info_1: initing entrypath: %s\n",str));
197         init_unistr2(&dfs1->path,str,UNI_STR_TERMINATE);
198         return True;
199 }
200
201 static BOOL init_reply_dfs_info_2(struct junction_map* j, NETDFS_DFS_INFO2* dfs2)
202 {
203         pstring str;
204         dfs2->ptr0_path = 1;
205         slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname(),
206                 j->service_name, j->volume_name);
207         init_unistr2(&dfs2->path, str, UNI_STR_TERMINATE);
208         dfs2->ptr0_comment = 0;
209         dfs2->state = 1; /* set up state of dfs junction as OK */
210         dfs2->num_stores = j->referral_count;
211         return True;
212 }
213
214 static BOOL init_reply_dfs_info_3(TALLOC_CTX *ctx, struct junction_map* j, NETDFS_DFS_INFO3* dfs3)
215 {
216         int ii;
217         pstring str;
218         dfs3->ptr0_path = 1;
219         if (j->volume_name[0] == '\0')
220                 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s",
221                         global_myname(), j->service_name);
222         else
223                 slprintf(str, sizeof(pstring)-1, "\\\\%s\\%s\\%s", global_myname(),
224                         j->service_name, j->volume_name);
225
226         init_unistr2(&dfs3->path, str, UNI_STR_TERMINATE);
227         dfs3->ptr0_comment = 1;
228         init_unistr2(&dfs3->comment, "", UNI_STR_TERMINATE);
229         dfs3->state = 1;
230         dfs3->num_stores = dfs3->size_stores = j->referral_count;
231         dfs3->ptr0_stores = 1;
232     
233         /* also enumerate the stores */
234         dfs3->stores = TALLOC_ARRAY(ctx, NETDFS_DFS_STORAGEINFO, j->referral_count);
235         if (!dfs3->stores)
236                 return False;
237
238         memset(dfs3->stores, '\0', j->referral_count * sizeof(NETDFS_DFS_STORAGEINFO));
239
240         for(ii=0;ii<j->referral_count;ii++) {
241                 char* p; 
242                 pstring path;
243                 NETDFS_DFS_STORAGEINFO* stor = &(dfs3->stores[ii]);
244                 struct referral* ref = &(j->referral_list[ii]);
245   
246                 pstrcpy(path, ref->alternate_path);
247                 trim_char(path,'\\','\0');
248                 p = strrchr_m(path,'\\');
249                 if(p==NULL) {
250                         DEBUG(4,("init_reply_dfs_info_3: invalid path: no \\ found in %s\n",path));
251                         continue;
252                 }
253                 *p = '\0';
254                 DEBUG(5,("storage %d: %s.%s\n",ii,path,p+1));
255                 stor->state = 2; /* set all stores as ONLINE */
256                 init_unistr2(&stor->server, path, UNI_STR_TERMINATE);
257                 init_unistr2(&stor->share,  p+1, UNI_STR_TERMINATE);
258                 stor->ptr0_server = stor->ptr0_share = 1;
259         }
260         return True;
261 }
262
263 WERROR _dfs_Enum(pipes_struct *p, NETDFS_Q_DFS_ENUM *q_u, NETDFS_R_DFS_ENUM *r_u)
264 {
265         uint32 level = q_u->level;
266         struct junction_map jn[MAX_MSDFS_JUNCTIONS];
267         int num_jn = 0;
268         int i;
269
270         num_jn = enum_msdfs_links(p->mem_ctx, jn, ARRAY_SIZE(jn));
271         vfs_ChDir(p->conn,p->conn->connectpath);
272     
273         DEBUG(5,("make_reply_dfs_enum: %d junctions found in Dfs, doing level %d\n", num_jn, level));
274
275         r_u->ptr0_info = q_u->ptr0_info;
276         r_u->ptr0_total = q_u->ptr0_total;
277         r_u->total = num_jn;
278
279         r_u->info = q_u->info;
280
281         /* Create the return array */
282         switch (level) {
283         case 1:
284                 if ((r_u->info.e.u.info1.s = TALLOC_ARRAY(p->mem_ctx, NETDFS_DFS_INFO1, num_jn)) == NULL) {
285                         return WERR_NOMEM;
286                 }
287                 r_u->info.e.u.info1.count = num_jn;
288                 r_u->info.e.u.info1.ptr0_s = 1;
289                 r_u->info.e.u.info1.size_s = num_jn;
290                 break;
291         case 2:
292                 if ((r_u->info.e.u.info2.s = TALLOC_ARRAY(p->mem_ctx, NETDFS_DFS_INFO2, num_jn)) == NULL) {
293                         return WERR_NOMEM;
294                 }
295                 r_u->info.e.u.info2.count = num_jn;
296                 r_u->info.e.u.info2.ptr0_s = 1;
297                 r_u->info.e.u.info2.size_s = num_jn;
298                 break;
299         case 3:
300                 if ((r_u->info.e.u.info3.s = TALLOC_ARRAY(p->mem_ctx, NETDFS_DFS_INFO3, num_jn)) == NULL) {
301                         return WERR_NOMEM;
302                 }
303                 r_u->info.e.u.info3.count = num_jn;
304                 r_u->info.e.u.info3.ptr0_s = 1;
305                 r_u->info.e.u.info3.size_s = num_jn;
306                 break;
307         default:
308                 return WERR_INVALID_PARAM;
309         }
310
311         for (i = 0; i < num_jn; i++) {
312                 switch (level) {
313                 case 1: 
314                         init_reply_dfs_info_1(&jn[i], &r_u->info.e.u.info1.s[i]);
315                         break;
316                 case 2:
317                         init_reply_dfs_info_2(&jn[i], &r_u->info.e.u.info2.s[i]);
318                         break;
319                 case 3:
320                         init_reply_dfs_info_3(p->mem_ctx, &jn[i], &r_u->info.e.u.info3.s[i]);
321                         break;
322                 default:
323                         return WERR_INVALID_PARAM;
324                 }
325         }
326   
327         r_u->status = WERR_OK;
328
329         return r_u->status;
330 }
331       
332 WERROR _dfs_GetInfo(pipes_struct *p, NETDFS_Q_DFS_GETINFO *q_u, 
333                      NETDFS_R_DFS_GETINFO *r_u)
334 {
335         UNISTR2* uni_path = &q_u->path;
336         uint32 level = q_u->level;
337         int consumedcnt = sizeof(pstring);
338         pstring path;
339         BOOL ret;
340         struct junction_map jn;
341
342         unistr2_to_ascii(path, uni_path, sizeof(path)-1);
343         if(!create_junction(path, &jn))
344                 return WERR_DFS_NO_SUCH_SERVER;
345   
346         /* The following call can change the cwd. */
347         if(!get_referred_path(p->mem_ctx, path, &jn, &consumedcnt, NULL) || consumedcnt < strlen(path)) {
348                 vfs_ChDir(p->conn,p->conn->connectpath);
349                 return WERR_DFS_NO_SUCH_VOL;
350         }
351
352         vfs_ChDir(p->conn,p->conn->connectpath);
353         r_u->info.switch_value = level;
354         r_u->info.ptr0 = 1;
355         r_u->status = WERR_OK;
356
357         switch (level) {
358                 case 1: ret = init_reply_dfs_info_1(&jn, &r_u->info.u.info1); break;
359                 case 2: ret = init_reply_dfs_info_2(&jn, &r_u->info.u.info2); break;
360                 case 3: ret = init_reply_dfs_info_3(p->mem_ctx, &jn, &r_u->info.u.info3); break;
361                 default:
362                         ret = False;
363                         break;
364         }
365
366         if (!ret) 
367                 r_u->status = WERR_INVALID_PARAM;
368   
369         return r_u->status;
370 }
371
372 WERROR _dfs_SetInfo(pipes_struct *p, NETDFS_Q_DFS_SETINFO *q_u, NETDFS_R_DFS_SETINFO *r_u)
373 {
374         /* FIXME: Implement your code here */
375         return WERR_NOT_SUPPORTED;
376 }
377
378 WERROR _dfs_Rename(pipes_struct *p, NETDFS_Q_DFS_RENAME *q_u, NETDFS_R_DFS_RENAME *r_u)
379 {
380         /* FIXME: Implement your code here */
381         return WERR_NOT_SUPPORTED;
382 }
383
384 WERROR _dfs_Move(pipes_struct *p, NETDFS_Q_DFS_MOVE *q_u, NETDFS_R_DFS_MOVE *r_u)
385 {
386         /* FIXME: Implement your code here */
387         return WERR_NOT_SUPPORTED;
388 }
389
390 WERROR _dfs_ManagerGetConfigInfo(pipes_struct *p, NETDFS_Q_DFS_MANAGERGETCONFIGINFO *q_u, NETDFS_R_DFS_MANAGERGETCONFIGINFO *r_u)
391 {
392         /* FIXME: Implement your code here */
393         return WERR_NOT_SUPPORTED;
394 }
395
396 WERROR _dfs_ManagerSendSiteInfo(pipes_struct *p, NETDFS_Q_DFS_MANAGERSENDSITEINFO *q_u, NETDFS_R_DFS_MANAGERSENDSITEINFO *r_u)
397 {
398         /* FIXME: Implement your code here */
399         return WERR_NOT_SUPPORTED;
400 }
401
402 WERROR _dfs_AddFtRoot(pipes_struct *p, NETDFS_Q_DFS_ADDFTROOT *q_u, NETDFS_R_DFS_ADDFTROOT *r_u)
403 {
404         /* FIXME: Implement your code here */
405         return WERR_NOT_SUPPORTED;
406 }
407
408 WERROR _dfs_RemoveFtRoot(pipes_struct *p, NETDFS_Q_DFS_REMOVEFTROOT *q_u, NETDFS_R_DFS_REMOVEFTROOT *r_u)
409 {
410         /* FIXME: Implement your code here */
411         return WERR_NOT_SUPPORTED;
412 }
413
414 WERROR _dfs_AddStdRoot(pipes_struct *p, NETDFS_Q_DFS_ADDSTDROOT *q_u, NETDFS_R_DFS_ADDSTDROOT *r_u)
415 {
416         /* FIXME: Implement your code here */
417         return WERR_NOT_SUPPORTED;
418 }
419
420 WERROR _dfs_RemoveStdRoot(pipes_struct *p, NETDFS_Q_DFS_REMOVESTDROOT *q_u, NETDFS_R_DFS_REMOVESTDROOT *r_u)
421 {
422         /* FIXME: Implement your code here */
423         return WERR_NOT_SUPPORTED;
424 }
425
426 WERROR _dfs_ManagerInitialize(pipes_struct *p, NETDFS_Q_DFS_MANAGERINITIALIZE *q_u, NETDFS_R_DFS_MANAGERINITIALIZE *r_u)
427 {
428         /* FIXME: Implement your code here */
429         return WERR_NOT_SUPPORTED;
430 }
431
432 WERROR _dfs_AddStdRootForced(pipes_struct *p, NETDFS_Q_DFS_ADDSTDROOTFORCED *q_u, NETDFS_R_DFS_ADDSTDROOTFORCED *r_u)
433 {
434         /* FIXME: Implement your code here */
435         return WERR_NOT_SUPPORTED;
436 }
437
438 WERROR _dfs_GetDcAddress(pipes_struct *p, NETDFS_Q_DFS_GETDCADDRESS *q_u, NETDFS_R_DFS_GETDCADDRESS *r_u)
439 {
440         /* FIXME: Implement your code here */
441         return WERR_NOT_SUPPORTED;
442 }
443
444 WERROR _dfs_SetDcAddress(pipes_struct *p, NETDFS_Q_DFS_SETDCADDRESS *q_u, NETDFS_R_DFS_SETDCADDRESS *r_u)
445 {
446         /* FIXME: Implement your code here */
447         return WERR_NOT_SUPPORTED;
448 }
449
450 WERROR _dfs_FlushFtTable(pipes_struct *p, NETDFS_Q_DFS_FLUSHFTTABLE *q_u, NETDFS_R_DFS_FLUSHFTTABLE *r_u)
451 {
452         /* FIXME: Implement your code here */
453         return WERR_NOT_SUPPORTED;
454 }
455
456 WERROR _dfs_Add2(pipes_struct *p, NETDFS_Q_DFS_ADD2 *q_u, NETDFS_R_DFS_ADD2 *r_u)
457 {
458         /* FIXME: Implement your code here */
459         return WERR_NOT_SUPPORTED;
460 }
461
462 WERROR _dfs_Remove2(pipes_struct *p, NETDFS_Q_DFS_REMOVE2 *q_u, NETDFS_R_DFS_REMOVE2 *r_u)
463 {
464         /* FIXME: Implement your code here */
465         return WERR_NOT_SUPPORTED;
466 }
467
468 WERROR _dfs_EnumEx(pipes_struct *p, NETDFS_Q_DFS_ENUMEX *q_u, NETDFS_R_DFS_ENUMEX *r_u)
469 {
470         /* FIXME: Implement your code here */
471         return WERR_NOT_SUPPORTED;
472 }
473
474 WERROR _dfs_SetInfo2(pipes_struct *p, NETDFS_Q_DFS_SETINFO2 *q_u, NETDFS_R_DFS_SETINFO2 *r_u)
475 {
476         /* FIXME: Implement your code here */
477         return WERR_NOT_SUPPORTED;
478 }
479