2 Unix SMB/Netbios implementation.
4 MSDfs services for Samba
5 Copyright (C) Shirish Kalele 2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DBGC_CLASS DBGC_MSDFS
26 extern uint32 global_client_caps;
28 /**********************************************************************
29 Parse the pathname of the form \hostname\service\reqpath
30 into the dfs_path structure
31 **********************************************************************/
33 static BOOL parse_dfs_path(char* pathname, struct dfs_path* pdp)
35 pstring pathname_local;
38 pstrcpy(pathname_local,pathname);
39 p = temp = pathname_local;
43 trim_char(temp,'\\','\\');
44 DEBUG(10,("temp in parse_dfs_path: .%s. after trimming \\'s\n",temp));
47 /* parse out hostname */
48 p = strchr_m(temp,'\\');
52 pstrcpy(pdp->hostname,temp);
53 DEBUG(10,("parse_dfs_path: hostname: %s\n",pdp->hostname));
55 /* parse out servicename */
57 p = strchr_m(temp,'\\');
59 pstrcpy(pdp->servicename,temp);
60 pdp->reqpath[0] = '\0';
64 pstrcpy(pdp->servicename,temp);
65 DEBUG(10,("parse_dfs_path: servicename: %s\n",pdp->servicename));
68 check_path_syntax(pdp->reqpath, p+1);
70 DEBUG(10,("parse_dfs_path: rest of the path: %s\n",pdp->reqpath));
74 /**********************************************************************
75 Parse the pathname of the form /hostname/service/reqpath
76 into the dfs_path structure
77 **********************************************************************/
79 static BOOL parse_processed_dfs_path(char* pathname, struct dfs_path* pdp, BOOL allow_wcards)
81 pstring pathname_local;
84 pstrcpy(pathname_local,pathname);
85 p = temp = pathname_local;
89 trim_char(temp,'/','/');
90 DEBUG(10,("temp in parse_processed_dfs_path: .%s. after trimming \\'s\n",temp));
93 /* parse out hostname */
94 p = strchr_m(temp,'/');
98 pstrcpy(pdp->hostname,temp);
99 DEBUG(10,("parse_processed_dfs_path: hostname: %s\n",pdp->hostname));
101 /* parse out servicename */
103 p = strchr_m(temp,'/');
105 pstrcpy(pdp->servicename,temp);
106 pdp->reqpath[0] = '\0';
110 pstrcpy(pdp->servicename,temp);
111 DEBUG(10,("parse_processed_dfs_path: servicename: %s\n",pdp->servicename));
113 /* rest is reqpath */
115 check_path_syntax_wcard(pdp->reqpath, p+1);
117 check_path_syntax(pdp->reqpath, p+1);
120 DEBUG(10,("parse_processed_dfs_path: rest of the path: %s\n",pdp->reqpath));
124 /********************************************************
125 Fake up a connection struct for the VFS layer.
126 Note this CHANGES CWD !!!! JRA.
127 *********************************************************/
129 static BOOL create_conn_struct( connection_struct *conn, int snum, char *path)
134 conn->service = snum;
135 pstrcpy(connpath, path);
136 pstring_sub(connpath , "%S", lp_servicename(snum));
138 string_set(&conn->connectpath, connpath);
140 /* needed for smbd_vfs_init() */
142 if ( (conn->mem_ctx=talloc_init("connection_struct")) == NULL ) {
143 DEBUG(0,("talloc_init(connection_struct) failed!\n"));
147 if (!smbd_vfs_init(conn)) {
148 DEBUG(0,("create_conn_struct: smbd_vfs_init failed.\n"));
149 talloc_destroy( conn->mem_ctx );
154 * Windows seems to insist on doing trans2getdfsreferral() calls on the IPC$
155 * share as the anonymous user. If we try to chdir as that user we will
156 * fail.... WTF ? JRA.
159 if (vfs_ChDir(conn,conn->connectpath) != 0) {
160 DEBUG(3,("create_conn_struct: Can't ChDir to new conn path %s. Error was %s\n",
161 conn->connectpath, strerror(errno) ));
162 talloc_destroy( conn->mem_ctx );
169 /**********************************************************************
170 Parse the contents of a symlink to verify if it is an msdfs referral
171 A valid referral is of the form: msdfs:server1\share1,server2\share2
172 **********************************************************************/
174 static BOOL parse_symlink(char* buf,struct referral** preflist,
179 char* alt_path[MAX_REFERRAL_COUNT];
181 struct referral* reflist;
185 prot = strtok(temp,":");
187 if (!strequal(prot, "msdfs"))
190 /* No referral list requested. Just yes/no. */
194 /* parse out the alternate paths */
195 while(((alt_path[count] = strtok(NULL,",")) != NULL) && count<MAX_REFERRAL_COUNT)
198 DEBUG(10,("parse_symlink: count=%d\n", count));
200 reflist = *preflist = SMB_MALLOC_ARRAY(struct referral, count);
201 if(reflist == NULL) {
202 DEBUG(0,("parse_symlink: Malloc failed!\n"));
206 for(i=0;i<count;i++) {
209 /* replace all /'s in the alternate path by a \ */
210 for(p = alt_path[i]; *p && ((p = strchr_m(p,'/'))!=NULL); p++) {
214 /* Remove leading '\\'s */
216 while (*p && (*p == '\\')) {
220 pstrcpy(reflist[i].alternate_path, "\\");
221 pstrcat(reflist[i].alternate_path, p);
222 reflist[i].proximity = 0;
223 reflist[i].ttl = REFERRAL_TTL;
224 DEBUG(10, ("parse_symlink: Created alt path: %s\n", reflist[i].alternate_path));
233 /**********************************************************************
234 Returns true if the unix path is a valid msdfs symlink
235 **********************************************************************/
237 BOOL is_msdfs_link(connection_struct* conn, char * path,
238 struct referral** reflistp, int* refcnt,
239 SMB_STRUCT_STAT *sbufp)
243 int referral_len = 0;
251 if (SMB_VFS_LSTAT(conn, path, sbufp) != 0) {
252 DEBUG(5,("is_msdfs_link: %s does not exist.\n",path));
256 if (S_ISLNK(sbufp->st_mode)) {
257 /* open the link and read it */
258 referral_len = SMB_VFS_READLINK(conn, path, referral, sizeof(pstring)-1);
259 if (referral_len == -1) {
260 DEBUG(0,("is_msdfs_link: Error reading msdfs link %s: %s\n", path, strerror(errno)));
264 referral[referral_len] = '\0';
265 DEBUG(5,("is_msdfs_link: %s -> %s\n",path,referral));
266 if (parse_symlink(referral, reflistp, refcnt))
272 /*****************************************************************
273 Used by other functions to decide if a dfs path is remote,
274 and to get the list of referred locations for that remote path.
276 findfirst_flag: For findfirsts, dfs links themselves are not
277 redirected, but paths beyond the links are. For normal smb calls,
278 even dfs links need to be redirected.
280 self_referralp: clients expect a dfs referral for the same share when
281 they request referrals for dfs roots on a server.
283 consumedcntp: how much of the dfs path is being redirected. the client
284 should try the remaining path on the redirected server.
286 *****************************************************************/
288 static BOOL resolve_dfs_path(pstring dfspath, struct dfs_path* dp,
289 connection_struct* conn, BOOL search_flag,
290 struct referral** reflistpp, int* refcntp,
291 BOOL* self_referralp, int* consumedcntp)
294 int consumed_level = 1;
296 BOOL bad_path = False;
297 SMB_STRUCT_STAT sbuf;
301 DEBUG(1,("resolve_dfs_path: NULL dfs_path* or NULL connection_struct*!\n"));
305 if (dp->reqpath[0] == '\0') {
306 if (self_referralp) {
307 DEBUG(6,("resolve_dfs_path: self-referral. returning False\n"));
308 *self_referralp = True;
313 DEBUG(10,("resolve_dfs_path: Conn path = %s req_path = %s\n", conn->connectpath, dp->reqpath));
315 unix_convert(dp->reqpath,conn,0,&bad_path,&sbuf);
316 /* JRA... should we strlower the last component here.... ? */
317 pstrcpy(localpath, dp->reqpath);
319 /* check if need to redirect */
320 if (is_msdfs_link(conn, localpath, reflistpp, refcntp, NULL)) {
322 DEBUG(6,("resolve_dfs_path (FindFirst) No redirection "
323 "for dfs link %s.\n", dfspath));
327 DEBUG(6,("resolve_dfs_path: %s resolves to a valid Dfs link.\n", dfspath));
329 *consumedcntp = strlen(dfspath);
333 /* redirect if any component in the path is a link */
334 pstrcpy(reqpath, dp->reqpath);
335 p = strrchr_m(reqpath, '/');
338 pstrcpy(localpath, reqpath);
339 if (is_msdfs_link(conn, localpath, reflistpp, refcntp, NULL)) {
341 DEBUG(4, ("resolve_dfs_path: Redirecting %s because parent %s is dfs link\n", dfspath, localpath));
343 /* To find the path consumed, we truncate the original
344 DFS pathname passed to use to remove the last
345 component. The length of the resulting string is
352 pstrcpy(buf, dfspath);
353 trim_char(buf, '\0', '\\');
354 for (; consumed_level; consumed_level--) {
355 q = strrchr_m(buf, '\\');
359 *consumedcntp = strlen(buf);
360 DEBUG(10, ("resolve_dfs_path: Path consumed: %s (%d)\n", buf, *consumedcntp));
365 p = strrchr_m(reqpath, '/');
372 /*****************************************************************
373 Decides if a dfs pathname should be redirected or not.
374 If not, the pathname is converted to a tcon-relative local unix path
376 search_wcard_flag: this flag performs 2 functions bother related
377 to searches. See resolve_dfs_path() and parse_processed_dfs_path()
379 *****************************************************************/
381 BOOL dfs_redirect( pstring pathname, connection_struct* conn, BOOL search_wcard_flag )
385 if (!conn || !pathname)
388 parse_processed_dfs_path(pathname, &dp, search_wcard_flag);
390 /* if dfs pathname for a non-dfs share, convert to tcon-relative
391 path and return false */
392 if (!lp_msdfs_root(SNUM(conn))) {
393 pstrcpy(pathname, dp.reqpath);
397 if (!strequal(dp.servicename, lp_servicename(SNUM(conn)) ))
400 if (resolve_dfs_path(pathname, &dp, conn, search_wcard_flag,
401 NULL, NULL, NULL, NULL)) {
402 DEBUG(3,("dfs_redirect: Redirecting %s\n", pathname));
405 DEBUG(3,("dfs_redirect: Not redirecting %s.\n", pathname));
407 /* Form non-dfs tcon-relative path */
408 pstrcpy(pathname, dp.reqpath);
409 DEBUG(3,("dfs_redirect: Path converted to non-dfs path %s\n",
417 /**********************************************************************
418 Return a self referral.
419 **********************************************************************/
421 static BOOL self_ref(char *pathname, struct junction_map *jucn,
422 int *consumedcntp, BOOL *self_referralp)
424 struct referral *ref;
426 if (self_referralp != NULL)
427 *self_referralp = True;
429 jucn->referral_count = 1;
430 if((ref = SMB_MALLOC_P(struct referral)) == NULL) {
431 DEBUG(0,("self_ref: malloc failed for referral\n"));
435 pstrcpy(ref->alternate_path,pathname);
437 ref->ttl = REFERRAL_TTL;
438 jucn->referral_list = ref;
440 *consumedcntp = strlen(pathname);
445 /**********************************************************************
446 Gets valid referrals for a dfs path and fills up the
447 junction_map structure
448 **********************************************************************/
450 BOOL get_referred_path(char *pathname, struct junction_map *jucn,
451 int *consumedcntp, BOOL *self_referralp)
455 struct connection_struct conns;
456 struct connection_struct* conn = &conns;
460 BOOL self_referral = False;
462 if (!pathname || !jucn)
468 *self_referralp = False;
470 self_referralp = &self_referral;
472 parse_dfs_path(pathname, &dp);
474 /* Verify hostname in path */
475 if ( !strequal(get_local_machine_name(), dp.hostname) ) {
476 /* Hostname mismatch, check if one of our IP addresses */
477 if (!ismyip(*interpret_addr2(dp.hostname))) {
478 DEBUG(3, ("get_referred_path: Invalid hostname %s in path %s\n",
479 dp.hostname, pathname));
484 pstrcpy(jucn->service_name, dp.servicename);
485 pstrcpy(jucn->volume_name, dp.reqpath);
487 /* Verify the share is a dfs root */
488 snum = lp_servicenumber(jucn->service_name);
490 if ((snum = find_service(jucn->service_name)) < 0)
494 if (!lp_msdfs_root(snum)) {
495 DEBUG(3,("get_referred_path: .%s. in dfs path %s is not a dfs root.\n",
496 dp.servicename, pathname));
501 * Self referrals are tested with a anonymous IPC connection and
502 * a GET_DFS_REFERRAL call to \\server\share. (which means dp.reqpath[0] points
503 * to an empty string). create_conn_struct cd's into the directory and will
504 * fail if it cannot (as the anonymous user). Cope with this.
507 if (dp.reqpath[0] == '\0') {
509 struct referral* ref;
511 if (*lp_msdfs_proxy(snum) == '\0')
512 return self_ref(pathname, jucn, consumedcntp,
515 jucn->referral_count = 1;
516 if ((ref = SMB_MALLOC_P(struct referral)) == NULL) {
517 DEBUG(0, ("malloc failed for referral\n"));
521 pstrcpy(ref->alternate_path, lp_msdfs_proxy(snum));
522 if (dp.reqpath[0] != '\0')
523 pstrcat(ref->alternate_path, dp.reqpath);
525 ref->ttl = REFERRAL_TTL;
526 jucn->referral_list = ref;
528 *consumedcntp = strlen(pathname);
533 pstrcpy(conn_path, lp_pathname(snum));
534 if (!create_conn_struct(conn, snum, conn_path))
537 /* If not remote & not a self referral, return False */
538 if (!resolve_dfs_path(pathname, &dp, conn, False,
539 &jucn->referral_list, &jucn->referral_count,
540 self_referralp, consumedcntp)) {
541 if (!*self_referralp) {
542 DEBUG(3,("get_referred_path: No valid referrals for path %s\n", pathname));
547 /* if self_referral, fill up the junction map */
548 if (*self_referralp) {
549 if (self_ref(pathname, jucn, consumedcntp, self_referralp) == False) {
558 talloc_destroy( conn->mem_ctx );
563 static int setup_ver2_dfs_referral(char* pathname, char** ppdata,
564 struct junction_map* junction,
568 char* pdata = *ppdata;
570 unsigned char uni_requestedpath[1024];
571 int uni_reqpathoffset1,uni_reqpathoffset2;
573 int requestedpathlen=0;
578 DEBUG(10,("setting up version2 referral\nRequested path:\n"));
580 requestedpathlen = rpcstr_push(uni_requestedpath, pathname, -1,
584 dump_data(0, (const char *) uni_requestedpath,requestedpathlen);
587 DEBUG(10,("ref count = %u\n",junction->referral_count));
589 uni_reqpathoffset1 = REFERRAL_HEADER_SIZE +
590 VERSION2_REFERRAL_SIZE * junction->referral_count;
592 uni_reqpathoffset2 = uni_reqpathoffset1 + requestedpathlen;
594 uni_curroffset = uni_reqpathoffset2 + requestedpathlen;
596 reply_size = REFERRAL_HEADER_SIZE + VERSION2_REFERRAL_SIZE*junction->referral_count +
597 2 * requestedpathlen;
598 DEBUG(10,("reply_size: %u\n",reply_size));
600 /* add up the unicode lengths of all the referral paths */
601 for(i=0;i<junction->referral_count;i++) {
602 DEBUG(10,("referral %u : %s\n",i,junction->referral_list[i].alternate_path));
603 reply_size += (strlen(junction->referral_list[i].alternate_path)+1)*2;
606 DEBUG(10,("reply_size = %u\n",reply_size));
607 /* add the unexplained 0x16 bytes */
610 pdata = SMB_REALLOC(pdata,reply_size);
612 DEBUG(0,("malloc failed for Realloc!\n"));
617 /* copy in the dfs requested paths.. required for offset calculations */
618 memcpy(pdata+uni_reqpathoffset1,uni_requestedpath,requestedpathlen);
619 memcpy(pdata+uni_reqpathoffset2,uni_requestedpath,requestedpathlen);
621 /* create the header */
622 SSVAL(pdata,0,consumedcnt * 2); /* path consumed */
623 SSVAL(pdata,2,junction->referral_count); /* number of referral in this pkt */
625 SIVAL(pdata,4,DFSREF_REFERRAL_SERVER | DFSREF_STORAGE_SERVER);
627 SIVAL(pdata,4,DFSREF_STORAGE_SERVER);
630 /* add the referral elements */
631 for(i=0;i<junction->referral_count;i++) {
632 struct referral* ref = &junction->referral_list[i];
635 SSVAL(pdata,offset,2); /* version 2 */
636 SSVAL(pdata,offset+2,VERSION2_REFERRAL_SIZE);
638 SSVAL(pdata,offset+4,1);
640 SSVAL(pdata,offset+4,0);
641 SSVAL(pdata,offset+6,0); /* ref_flags :use path_consumed bytes? */
642 SIVAL(pdata,offset+8,ref->proximity);
643 SIVAL(pdata,offset+12,ref->ttl);
645 SSVAL(pdata,offset+16,uni_reqpathoffset1-offset);
646 SSVAL(pdata,offset+18,uni_reqpathoffset2-offset);
647 /* copy referred path into current offset */
648 unilen = rpcstr_push(pdata+uni_curroffset, ref->alternate_path,
651 SSVAL(pdata,offset+20,uni_curroffset-offset);
653 uni_curroffset += unilen;
654 offset += VERSION2_REFERRAL_SIZE;
656 /* add in the unexplained 22 (0x16) bytes at the end */
657 memset(pdata+uni_curroffset,'\0',0x16);
661 static int setup_ver3_dfs_referral(char* pathname, char** ppdata,
662 struct junction_map* junction,
666 char* pdata = *ppdata;
668 unsigned char uni_reqpath[1024];
669 int uni_reqpathoffset1, uni_reqpathoffset2;
676 DEBUG(10,("setting up version3 referral\n"));
678 reqpathlen = rpcstr_push(uni_reqpath, pathname, -1, STR_TERMINATE);
681 dump_data(0, (char *) uni_reqpath,reqpathlen);
684 uni_reqpathoffset1 = REFERRAL_HEADER_SIZE + VERSION3_REFERRAL_SIZE * junction->referral_count;
685 uni_reqpathoffset2 = uni_reqpathoffset1 + reqpathlen;
686 reply_size = uni_curroffset = uni_reqpathoffset2 + reqpathlen;
688 for(i=0;i<junction->referral_count;i++) {
689 DEBUG(10,("referral %u : %s\n",i,junction->referral_list[i].alternate_path));
690 reply_size += (strlen(junction->referral_list[i].alternate_path)+1)*2;
693 pdata = SMB_REALLOC(pdata,reply_size);
695 DEBUG(0,("version3 referral setup: malloc failed for Realloc!\n"));
700 /* create the header */
701 SSVAL(pdata,0,consumedcnt * 2); /* path consumed */
702 SSVAL(pdata,2,junction->referral_count); /* number of referral */
704 SIVAL(pdata,4,DFSREF_REFERRAL_SERVER | DFSREF_STORAGE_SERVER);
706 SIVAL(pdata,4,DFSREF_STORAGE_SERVER);
708 /* copy in the reqpaths */
709 memcpy(pdata+uni_reqpathoffset1,uni_reqpath,reqpathlen);
710 memcpy(pdata+uni_reqpathoffset2,uni_reqpath,reqpathlen);
713 for(i=0;i<junction->referral_count;i++) {
714 struct referral* ref = &(junction->referral_list[i]);
717 SSVAL(pdata,offset,3); /* version 3 */
718 SSVAL(pdata,offset+2,VERSION3_REFERRAL_SIZE);
720 SSVAL(pdata,offset+4,1);
722 SSVAL(pdata,offset+4,0);
724 SSVAL(pdata,offset+6,0); /* ref_flags :use path_consumed bytes? */
725 SIVAL(pdata,offset+8,ref->ttl);
727 SSVAL(pdata,offset+12,uni_reqpathoffset1-offset);
728 SSVAL(pdata,offset+14,uni_reqpathoffset2-offset);
729 /* copy referred path into current offset */
730 unilen = rpcstr_push(pdata+uni_curroffset,ref->alternate_path,
731 -1, STR_UNICODE | STR_TERMINATE);
732 SSVAL(pdata,offset+16,uni_curroffset-offset);
733 /* copy 0x10 bytes of 00's in the ServiceSite GUID */
734 memset(pdata+offset+18,'\0',16);
736 uni_curroffset += unilen;
737 offset += VERSION3_REFERRAL_SIZE;
742 /******************************************************************
743 * Set up the Dfs referral for the dfs pathname
744 ******************************************************************/
746 int setup_dfs_referral(connection_struct *orig_conn, char *pathname, int max_referral_level, char** ppdata)
748 struct junction_map junction;
750 BOOL self_referral = False;
753 char *pathnamep = pathname;
755 ZERO_STRUCT(junction);
757 /* get the junction entry */
761 /* Trim pathname sent by client so it begins with only one backslash.
762 Two backslashes confuse some dfs clients
764 while (pathnamep[0] == '\\' && pathnamep[1] == '\\')
767 pstrcpy(buf, pathnamep);
768 /* The following call can change cwd. */
769 if (!get_referred_path(buf, &junction, &consumedcnt, &self_referral)) {
770 vfs_ChDir(orig_conn,orig_conn->connectpath);
773 vfs_ChDir(orig_conn,orig_conn->connectpath);
775 if (!self_referral) {
776 pathnamep[consumedcnt] = '\0';
778 if( DEBUGLVL( 3 ) ) {
780 dbgtext("setup_dfs_referral: Path %s to alternate path(s):",pathnamep);
781 for(i=0;i<junction.referral_count;i++)
782 dbgtext(" %s",junction.referral_list[i].alternate_path);
787 /* create the referral depeding on version */
788 DEBUG(10,("max_referral_level :%d\n",max_referral_level));
789 if(max_referral_level<2 || max_referral_level>3)
790 max_referral_level = 2;
792 switch(max_referral_level) {
794 reply_size = setup_ver2_dfs_referral(pathnamep, ppdata, &junction,
795 consumedcnt, self_referral);
796 SAFE_FREE(junction.referral_list);
799 reply_size = setup_ver3_dfs_referral(pathnamep, ppdata, &junction,
800 consumedcnt, self_referral);
801 SAFE_FREE(junction.referral_list);
804 DEBUG(0,("setup_dfs_referral: Invalid dfs referral version: %d\n", max_referral_level));
809 DEBUGADD(0,("DFS Referral pdata:\n"));
810 dump_data(0,*ppdata,reply_size);
816 /**********************************************************************
817 The following functions are called by the NETDFS RPC pipe functions
818 **********************************************************************/
820 /**********************************************************************
821 Creates a junction structure from a Dfs pathname
822 **********************************************************************/
824 BOOL create_junction(char* pathname, struct junction_map* jucn)
828 parse_dfs_path(pathname,&dp);
830 /* check if path is dfs : validate first token */
831 if ( !strequal(get_local_machine_name(),dp.hostname) ) {
833 /* Hostname mismatch, check if one of our IP addresses */
834 if (!ismyip(*interpret_addr2(dp.hostname))) {
835 DEBUG(4,("create_junction: Invalid hostname %s in dfs path %s\n",
836 dp.hostname, pathname));
841 /* Check for a non-DFS share */
842 if(!lp_msdfs_root(lp_servicenumber(dp.servicename))) {
843 DEBUG(4,("create_junction: %s is not an msdfs root.\n",
848 pstrcpy(jucn->service_name,dp.servicename);
849 pstrcpy(jucn->volume_name,dp.reqpath);
853 /**********************************************************************
854 Forms a valid Unix pathname from the junction
855 **********************************************************************/
857 static BOOL junction_to_local_path(struct junction_map* jucn, char* path,
858 int max_pathlen, connection_struct *conn)
866 snum = lp_servicenumber(jucn->service_name);
870 safe_strcpy(path, lp_pathname(snum), max_pathlen-1);
871 safe_strcat(path, "/", max_pathlen-1);
872 safe_strcat(path, jucn->volume_name, max_pathlen-1);
874 pstrcpy(conn_path, lp_pathname(snum));
875 if (!create_conn_struct(conn, snum, conn_path))
881 BOOL create_msdfs_link(struct junction_map *jucn, BOOL exists)
885 connection_struct conns;
886 connection_struct *conn = &conns;
888 BOOL insert_comma = False;
891 if(!junction_to_local_path(jucn, path, sizeof(path), conn))
894 /* form the msdfs_link contents */
895 pstrcpy(msdfs_link, "msdfs:");
896 for(i=0; i<jucn->referral_count; i++) {
897 char* refpath = jucn->referral_list[i].alternate_path;
899 trim_char(refpath, '\\', '\\');
900 if(*refpath == '\0') {
902 insert_comma = False;
905 if (i > 0 && insert_comma)
906 pstrcat(msdfs_link, ",");
908 pstrcat(msdfs_link, refpath);
914 DEBUG(5,("create_msdfs_link: Creating new msdfs link: %s -> %s\n", path, msdfs_link));
917 if(SMB_VFS_UNLINK(conn,path)!=0)
920 if(SMB_VFS_SYMLINK(conn, msdfs_link, path) < 0) {
921 DEBUG(1,("create_msdfs_link: symlink failed %s -> %s\nError: %s\n",
922 path, msdfs_link, strerror(errno)));
930 talloc_destroy( conn->mem_ctx );
934 BOOL remove_msdfs_link(struct junction_map* jucn)
937 connection_struct conns;
938 connection_struct *conn = &conns;
941 if( junction_to_local_path(jucn, path, sizeof(path), conn) ) {
942 if( SMB_VFS_UNLINK(conn, path) == 0 )
945 talloc_destroy( conn->mem_ctx );
951 static int form_junctions(TALLOC_CTX *ctx, int snum, struct junction_map* jucn, int jn_remain)
956 pstring connect_path;
957 char* service_name = lp_servicename(snum);
958 connection_struct conn;
959 struct referral *ref = NULL;
964 pstrcpy(connect_path,lp_pathname(snum));
966 if(*connect_path == '\0')
970 * Fake up a connection struct for the VFS layer.
973 if (!create_conn_struct(&conn, snum, connect_path))
976 /* form a junction for the msdfs root - convention
977 DO NOT REMOVE THIS: NT clients will not work with us
978 if this is not present
980 pstrcpy(jucn[cnt].service_name, service_name);
981 jucn[cnt].volume_name[0] = '\0';
982 jucn[cnt].referral_count = 1;
984 ref = jucn[cnt].referral_list = TALLOC_P(ctx, struct referral);
985 if (jucn[cnt].referral_list == NULL) {
986 DEBUG(0, ("Malloc failed!\n"));
991 ref->ttl = REFERRAL_TTL;
992 if (*lp_msdfs_proxy(snum) != '\0') {
993 pstrcpy(ref->alternate_path, lp_msdfs_proxy(snum));
997 slprintf(ref->alternate_path, sizeof(pstring)-1,
998 "\\\\%s\\%s", get_local_machine_name(), service_name);
1001 /* Now enumerate all dfs links */
1002 dirp = SMB_VFS_OPENDIR(&conn, ".", NULL, 0);
1006 while ((dname = vfs_readdirname(&conn, dirp)) != NULL) {
1007 if (cnt >= jn_remain) {
1008 SMB_VFS_CLOSEDIR(&conn,dirp);
1009 DEBUG(2, ("ran out of MSDFS junction slots"));
1012 if (is_msdfs_link(&conn, dname, &(jucn[cnt].referral_list),
1013 &(jucn[cnt].referral_count), NULL)) {
1014 pstrcpy(jucn[cnt].service_name, service_name);
1015 pstrcpy(jucn[cnt].volume_name, dname);
1020 SMB_VFS_CLOSEDIR(&conn,dirp);
1026 int enum_msdfs_links(TALLOC_CTX *ctx, struct junction_map* jucn, int jn_max)
1031 if(!lp_host_msdfs())
1034 for(i=0;i < lp_numservices() && (jn_max - jn_count) > 0;i++) {
1035 if(lp_msdfs_root(i))
1036 jn_count += form_junctions(ctx, i,jucn,jn_max - jn_count);