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,'\\');
53 pstrcpy(pdp->hostname,temp);
54 DEBUG(10,("parse_dfs_path: hostname: %s\n",pdp->hostname));
56 /* parse out servicename */
58 p = strchr_m(temp,'\\');
60 pstrcpy(pdp->servicename,temp);
61 pdp->reqpath[0] = '\0';
65 pstrcpy(pdp->servicename,temp);
66 DEBUG(10,("parse_dfs_path: servicename: %s\n",pdp->servicename));
69 check_path_syntax(pdp->reqpath, p+1);
71 DEBUG(10,("parse_dfs_path: rest of the path: %s\n",pdp->reqpath));
75 /**********************************************************************
76 Parse the pathname of the form /hostname/service/reqpath
77 into the dfs_path structure
78 **********************************************************************/
80 static BOOL parse_processed_dfs_path(char* pathname, struct dfs_path *pdp, BOOL allow_wcards)
82 pstring pathname_local;
85 pstrcpy(pathname_local,pathname);
86 p = temp = pathname_local;
90 trim_char(temp,'/','/');
91 DEBUG(10,("temp in parse_processed_dfs_path: .%s. after trimming \\'s\n",temp));
94 /* parse out hostname */
95 p = strchr_m(temp,'/');
100 pstrcpy(pdp->hostname,temp);
101 DEBUG(10,("parse_processed_dfs_path: hostname: %s\n",pdp->hostname));
103 /* parse out servicename */
105 p = strchr_m(temp,'/');
107 pstrcpy(pdp->servicename,temp);
108 pdp->reqpath[0] = '\0';
112 pstrcpy(pdp->servicename,temp);
113 DEBUG(10,("parse_processed_dfs_path: servicename: %s\n",pdp->servicename));
115 /* rest is reqpath */
117 BOOL path_contains_wcard;
118 check_path_syntax_wcard(pdp->reqpath, p+1, &path_contains_wcard);
120 check_path_syntax(pdp->reqpath, p+1);
123 DEBUG(10,("parse_processed_dfs_path: rest of the path: %s\n",pdp->reqpath));
127 /********************************************************
128 Fake up a connection struct for the VFS layer.
129 Note this CHANGES CWD !!!! JRA.
130 *********************************************************/
132 static BOOL create_conn_struct(connection_struct *conn, int snum, char *path)
138 conn->service = snum;
139 pstrcpy(connpath, path);
140 pstring_sub(connpath , "%S", lp_servicename(snum));
142 /* needed for smbd_vfs_init() */
144 if ( (conn->mem_ctx=talloc_init("connection_struct")) == NULL ) {
145 DEBUG(0,("talloc_init(connection_struct) failed!\n"));
149 string_set(&conn->connectpath, connpath);
151 if (!smbd_vfs_init(conn)) {
152 DEBUG(0,("create_conn_struct: smbd_vfs_init failed.\n"));
153 conn_free_internal(conn);
158 * Windows seems to insist on doing trans2getdfsreferral() calls on the IPC$
159 * share as the anonymous user. If we try to chdir as that user we will
160 * fail.... WTF ? JRA.
163 if (vfs_ChDir(conn,conn->connectpath) != 0) {
164 DEBUG(3,("create_conn_struct: Can't ChDir to new conn path %s. Error was %s\n",
165 conn->connectpath, strerror(errno) ));
166 conn_free_internal(conn);
173 /**********************************************************************
174 Parse the contents of a symlink to verify if it is an msdfs referral
175 A valid referral is of the form: msdfs:server1\share1,server2\share2
176 talloc CTX can be NULL here if preflist and refcount pointers are null.
177 **********************************************************************/
179 static BOOL parse_symlink(TALLOC_CTX *ctx, char *buf, struct referral **preflist, int *refcount)
183 char *alt_path[MAX_REFERRAL_COUNT];
185 struct referral *reflist;
189 prot = strtok(temp,":");
191 if (!strequal(prot, "msdfs")) {
195 /* No referral list requested. Just yes/no. */
201 DEBUG(0,("parse_symlink: logic error. TALLOC_CTX should not be null.\n"));
205 /* parse out the alternate paths */
206 while(((alt_path[count] = strtok(NULL,",")) != NULL) && count<MAX_REFERRAL_COUNT) {
210 DEBUG(10,("parse_symlink: count=%d\n", count));
212 reflist = *preflist = TALLOC_ARRAY(ctx, struct referral, count);
213 if(reflist == NULL) {
214 DEBUG(0,("parse_symlink: talloc failed!\n"));
218 for(i=0;i<count;i++) {
221 /* replace all /'s in the alternate path by a \ */
222 for(p = alt_path[i]; *p && ((p = strchr_m(p,'/'))!=NULL); p++) {
226 /* Remove leading '\\'s */
228 while (*p && (*p == '\\')) {
232 pstrcpy(reflist[i].alternate_path, "\\");
233 pstrcat(reflist[i].alternate_path, p);
234 reflist[i].proximity = 0;
235 reflist[i].ttl = REFERRAL_TTL;
236 DEBUG(10, ("parse_symlink: Created alt path: %s\n", reflist[i].alternate_path));
246 /**********************************************************************
247 Returns true if the unix path is a valid msdfs symlink
248 talloc CTX can be NULL here if reflistp and refcnt pointers are null.
249 **********************************************************************/
251 BOOL is_msdfs_link(TALLOC_CTX *ctx, connection_struct *conn, char *path,
252 struct referral **reflistp, int *refcnt,
253 SMB_STRUCT_STAT *sbufp)
257 int referral_len = 0;
259 if (!path || !conn) {
267 if (SMB_VFS_LSTAT(conn, path, sbufp) != 0) {
268 DEBUG(5,("is_msdfs_link: %s does not exist.\n",path));
272 if (S_ISLNK(sbufp->st_mode)) {
273 /* open the link and read it */
274 referral_len = SMB_VFS_READLINK(conn, path, referral, sizeof(pstring)-1);
275 if (referral_len == -1) {
276 DEBUG(0,("is_msdfs_link: Error reading msdfs link %s: %s\n", path, strerror(errno)));
280 referral[referral_len] = '\0';
281 DEBUG(5,("is_msdfs_link: %s -> %s\n",path,referral));
282 if (parse_symlink(ctx, referral, reflistp, refcnt)) {
289 /*****************************************************************
290 Used by other functions to decide if a dfs path is remote,
291 and to get the list of referred locations for that remote path.
293 findfirst_flag: For findfirsts, dfs links themselves are not
294 redirected, but paths beyond the links are. For normal smb calls,
295 even dfs links need to be redirected.
297 self_referralp: clients expect a dfs referral for the same share when
298 they request referrals for dfs roots on a server.
300 consumedcntp: how much of the dfs path is being redirected. the client
301 should try the remaining path on the redirected server.
303 TALLOC_CTX can be NULL here if struct referral **reflistpp, int *refcntp
305 *****************************************************************/
307 static BOOL resolve_dfs_path(TALLOC_CTX *ctx, pstring dfspath, struct dfs_path *dp,
308 connection_struct *conn, BOOL search_flag,
309 struct referral **reflistpp, int *refcntp,
310 BOOL *self_referralp, int *consumedcntp)
313 int consumed_level = 1;
315 BOOL bad_path = False;
316 SMB_STRUCT_STAT sbuf;
320 DEBUG(1,("resolve_dfs_path: NULL dfs_path* or NULL connection_struct*!\n"));
324 if (!ctx && (reflistpp || refcntp)) {
325 DEBUG(0,("resolve_dfs_path: logic error. TALLOC_CTX must not be NULL.\n"));
328 if (dp->reqpath[0] == '\0') {
329 if (self_referralp) {
330 DEBUG(6,("resolve_dfs_path: self-referral. returning False\n"));
331 *self_referralp = True;
336 DEBUG(10,("resolve_dfs_path: Conn path = %s req_path = %s\n", conn->connectpath, dp->reqpath));
338 unix_convert(dp->reqpath,conn,0,&bad_path,&sbuf);
339 /* JRA... should we strlower the last component here.... ? */
340 pstrcpy(localpath, dp->reqpath);
342 /* check if need to redirect */
343 if (is_msdfs_link(ctx, conn, localpath, reflistpp, refcntp, NULL)) {
345 DEBUG(6,("resolve_dfs_path (FindFirst) No redirection "
346 "for dfs link %s.\n", dfspath));
350 DEBUG(6,("resolve_dfs_path: %s resolves to a valid Dfs link.\n", dfspath));
352 *consumedcntp = strlen(dfspath);
357 /* redirect if any component in the path is a link */
358 pstrcpy(reqpath, dp->reqpath);
359 p = strrchr_m(reqpath, '/');
362 pstrcpy(localpath, reqpath);
363 if (is_msdfs_link(ctx, conn, localpath, reflistpp, refcntp, NULL)) {
364 DEBUG(4, ("resolve_dfs_path: Redirecting %s because parent %s is dfs link\n", dfspath, localpath));
366 /* To find the path consumed, we truncate the original
367 DFS pathname passed to use to remove the last
368 component. The length of the resulting string is
375 pstrcpy(buf, dfspath);
376 trim_char(buf, '\0', '\\');
377 for (; consumed_level; consumed_level--) {
378 q = strrchr_m(buf, '\\');
383 *consumedcntp = strlen(buf);
384 DEBUG(10, ("resolve_dfs_path: Path consumed: %s (%d)\n", buf, *consumedcntp));
389 p = strrchr_m(reqpath, '/');
396 /*****************************************************************
397 Decides if a dfs pathname should be redirected or not.
398 If not, the pathname is converted to a tcon-relative local unix path
400 search_wcard_flag: this flag performs 2 functions bother related
401 to searches. See resolve_dfs_path() and parse_processed_dfs_path()
403 *****************************************************************/
405 BOOL dfs_redirect( pstring pathname, connection_struct *conn, BOOL search_wcard_flag )
409 if (!conn || !pathname) {
413 parse_processed_dfs_path(pathname, &dp, search_wcard_flag);
415 /* if dfs pathname for a non-dfs share, convert to tcon-relative
416 path and return false */
417 if (!lp_msdfs_root(SNUM(conn))) {
418 pstrcpy(pathname, dp.reqpath);
422 if (!strequal(dp.servicename, lp_servicename(SNUM(conn)) )) {
426 if (resolve_dfs_path(NULL, pathname, &dp, conn, search_wcard_flag,
427 NULL, NULL, NULL, NULL)) {
428 DEBUG(3,("dfs_redirect: Redirecting %s\n", pathname));
431 DEBUG(3,("dfs_redirect: Not redirecting %s.\n", pathname));
433 /* Form non-dfs tcon-relative path */
434 pstrcpy(pathname, dp.reqpath);
435 DEBUG(3,("dfs_redirect: Path converted to non-dfs path %s\n", pathname));
442 /**********************************************************************
443 Return a self referral.
444 **********************************************************************/
446 static BOOL self_ref(TALLOC_CTX *ctx, char *pathname, struct junction_map *jucn,
447 int *consumedcntp, BOOL *self_referralp)
449 struct referral *ref;
451 if (self_referralp != NULL) {
452 *self_referralp = True;
455 jucn->referral_count = 1;
456 if((ref = TALLOC_P(ctx, struct referral)) == NULL) {
457 DEBUG(0,("self_ref: malloc failed for referral\n"));
461 pstrcpy(ref->alternate_path,pathname);
463 ref->ttl = REFERRAL_TTL;
464 jucn->referral_list = ref;
466 *consumedcntp = strlen(pathname);
472 /**********************************************************************
473 Gets valid referrals for a dfs path and fills up the
474 junction_map structure.
475 **********************************************************************/
477 BOOL get_referred_path(TALLOC_CTX *ctx, char *pathname, struct junction_map *jucn,
478 int *consumedcntp, BOOL *self_referralp)
482 struct connection_struct conns;
483 struct connection_struct *conn = &conns;
487 BOOL self_referral = False;
489 if (!pathname || !jucn) {
495 if (self_referralp) {
496 *self_referralp = False;
498 self_referralp = &self_referral;
501 parse_dfs_path(pathname, &dp);
503 /* Verify hostname in path */
504 if ( !strequal(get_local_machine_name(), dp.hostname) ) {
505 /* Hostname mismatch, check if one of our IP addresses */
506 if (!ismyip(*interpret_addr2(dp.hostname))) {
507 DEBUG(3, ("get_referred_path: Invalid hostname %s in path %s\n",
508 dp.hostname, pathname));
513 pstrcpy(jucn->service_name, dp.servicename);
514 pstrcpy(jucn->volume_name, dp.reqpath);
516 /* Verify the share is a dfs root */
517 snum = lp_servicenumber(jucn->service_name);
519 if ((snum = find_service(jucn->service_name)) < 0) {
524 if (!lp_msdfs_root(snum)) {
525 DEBUG(3,("get_referred_path: .%s. in dfs path %s is not a dfs root.\n",
526 dp.servicename, pathname));
531 * Self referrals are tested with a anonymous IPC connection and
532 * a GET_DFS_REFERRAL call to \\server\share. (which means dp.reqpath[0] points
533 * to an empty string). create_conn_struct cd's into the directory and will
534 * fail if it cannot (as the anonymous user). Cope with this.
537 if (dp.reqpath[0] == '\0') {
539 struct referral* ref;
541 if (*lp_msdfs_proxy(snum) == '\0') {
542 return self_ref(ctx, pathname, jucn, consumedcntp, self_referralp);
545 jucn->referral_count = 1;
546 if ((ref = TALLOC_P(ctx, struct referral)) == NULL) {
547 DEBUG(0, ("malloc failed for referral\n"));
551 pstrcpy(ref->alternate_path, lp_msdfs_proxy(snum));
552 if (dp.reqpath[0] != '\0') {
553 pstrcat(ref->alternate_path, dp.reqpath);
556 ref->ttl = REFERRAL_TTL;
557 jucn->referral_list = ref;
559 *consumedcntp = strlen(pathname);
565 pstrcpy(conn_path, lp_pathname(snum));
566 if (!create_conn_struct(conn, snum, conn_path)) {
570 /* If not remote & not a self referral, return False */
571 if (!resolve_dfs_path(ctx, pathname, &dp, conn, False,
572 &jucn->referral_list, &jucn->referral_count,
573 self_referralp, consumedcntp)) {
574 if (!*self_referralp) {
575 DEBUG(3,("get_referred_path: No valid referrals for path %s\n", pathname));
580 /* if self_referral, fill up the junction map */
581 if (*self_referralp) {
582 if (self_ref(ctx, pathname, jucn, consumedcntp, self_referralp) == False) {
591 conn_free_internal(conn);
595 static int setup_ver2_dfs_referral(char *pathname, char **ppdata,
596 struct junction_map *junction,
600 char* pdata = *ppdata;
602 unsigned char uni_requestedpath[1024];
603 int uni_reqpathoffset1,uni_reqpathoffset2;
605 int requestedpathlen=0;
610 DEBUG(10,("setting up version2 referral\nRequested path:\n"));
612 requestedpathlen = rpcstr_push(uni_requestedpath, pathname, -1,
616 dump_data(0, (const char *) uni_requestedpath,requestedpathlen);
619 DEBUG(10,("ref count = %u\n",junction->referral_count));
621 uni_reqpathoffset1 = REFERRAL_HEADER_SIZE +
622 VERSION2_REFERRAL_SIZE * junction->referral_count;
624 uni_reqpathoffset2 = uni_reqpathoffset1 + requestedpathlen;
626 uni_curroffset = uni_reqpathoffset2 + requestedpathlen;
628 reply_size = REFERRAL_HEADER_SIZE + VERSION2_REFERRAL_SIZE*junction->referral_count +
629 2 * requestedpathlen;
630 DEBUG(10,("reply_size: %u\n",reply_size));
632 /* add up the unicode lengths of all the referral paths */
633 for(i=0;i<junction->referral_count;i++) {
634 DEBUG(10,("referral %u : %s\n",i,junction->referral_list[i].alternate_path));
635 reply_size += (strlen(junction->referral_list[i].alternate_path)+1)*2;
638 DEBUG(10,("reply_size = %u\n",reply_size));
639 /* add the unexplained 0x16 bytes */
642 pdata = SMB_REALLOC(pdata,reply_size);
644 DEBUG(0,("malloc failed for Realloc!\n"));
650 /* copy in the dfs requested paths.. required for offset calculations */
651 memcpy(pdata+uni_reqpathoffset1,uni_requestedpath,requestedpathlen);
652 memcpy(pdata+uni_reqpathoffset2,uni_requestedpath,requestedpathlen);
654 /* create the header */
655 SSVAL(pdata,0,consumedcnt * 2); /* path consumed */
656 SSVAL(pdata,2,junction->referral_count); /* number of referral in this pkt */
658 SIVAL(pdata,4,DFSREF_REFERRAL_SERVER | DFSREF_STORAGE_SERVER);
660 SIVAL(pdata,4,DFSREF_STORAGE_SERVER);
664 /* add the referral elements */
665 for(i=0;i<junction->referral_count;i++) {
666 struct referral* ref = &junction->referral_list[i];
669 SSVAL(pdata,offset,2); /* version 2 */
670 SSVAL(pdata,offset+2,VERSION2_REFERRAL_SIZE);
672 SSVAL(pdata,offset+4,1);
674 SSVAL(pdata,offset+4,0);
676 SSVAL(pdata,offset+6,0); /* ref_flags :use path_consumed bytes? */
677 SIVAL(pdata,offset+8,ref->proximity);
678 SIVAL(pdata,offset+12,ref->ttl);
680 SSVAL(pdata,offset+16,uni_reqpathoffset1-offset);
681 SSVAL(pdata,offset+18,uni_reqpathoffset2-offset);
682 /* copy referred path into current offset */
683 unilen = rpcstr_push(pdata+uni_curroffset, ref->alternate_path,
686 SSVAL(pdata,offset+20,uni_curroffset-offset);
688 uni_curroffset += unilen;
689 offset += VERSION2_REFERRAL_SIZE;
691 /* add in the unexplained 22 (0x16) bytes at the end */
692 memset(pdata+uni_curroffset,'\0',0x16);
696 static int setup_ver3_dfs_referral(char *pathname, char **ppdata,
697 struct junction_map *junction,
701 char* pdata = *ppdata;
703 unsigned char uni_reqpath[1024];
704 int uni_reqpathoffset1, uni_reqpathoffset2;
711 DEBUG(10,("setting up version3 referral\n"));
713 reqpathlen = rpcstr_push(uni_reqpath, pathname, -1, STR_TERMINATE);
716 dump_data(0, (char *) uni_reqpath,reqpathlen);
719 uni_reqpathoffset1 = REFERRAL_HEADER_SIZE + VERSION3_REFERRAL_SIZE * junction->referral_count;
720 uni_reqpathoffset2 = uni_reqpathoffset1 + reqpathlen;
721 reply_size = uni_curroffset = uni_reqpathoffset2 + reqpathlen;
723 for(i=0;i<junction->referral_count;i++) {
724 DEBUG(10,("referral %u : %s\n",i,junction->referral_list[i].alternate_path));
725 reply_size += (strlen(junction->referral_list[i].alternate_path)+1)*2;
728 pdata = SMB_REALLOC(pdata,reply_size);
730 DEBUG(0,("version3 referral setup: malloc failed for Realloc!\n"));
736 /* create the header */
737 SSVAL(pdata,0,consumedcnt * 2); /* path consumed */
738 SSVAL(pdata,2,junction->referral_count); /* number of referral */
740 SIVAL(pdata,4,DFSREF_REFERRAL_SERVER | DFSREF_STORAGE_SERVER);
742 SIVAL(pdata,4,DFSREF_STORAGE_SERVER);
745 /* copy in the reqpaths */
746 memcpy(pdata+uni_reqpathoffset1,uni_reqpath,reqpathlen);
747 memcpy(pdata+uni_reqpathoffset2,uni_reqpath,reqpathlen);
750 for(i=0;i<junction->referral_count;i++) {
751 struct referral* ref = &(junction->referral_list[i]);
754 SSVAL(pdata,offset,3); /* version 3 */
755 SSVAL(pdata,offset+2,VERSION3_REFERRAL_SIZE);
757 SSVAL(pdata,offset+4,1);
759 SSVAL(pdata,offset+4,0);
762 SSVAL(pdata,offset+6,0); /* ref_flags :use path_consumed bytes? */
763 SIVAL(pdata,offset+8,ref->ttl);
765 SSVAL(pdata,offset+12,uni_reqpathoffset1-offset);
766 SSVAL(pdata,offset+14,uni_reqpathoffset2-offset);
767 /* copy referred path into current offset */
768 unilen = rpcstr_push(pdata+uni_curroffset,ref->alternate_path,
769 -1, STR_UNICODE | STR_TERMINATE);
770 SSVAL(pdata,offset+16,uni_curroffset-offset);
771 /* copy 0x10 bytes of 00's in the ServiceSite GUID */
772 memset(pdata+offset+18,'\0',16);
774 uni_curroffset += unilen;
775 offset += VERSION3_REFERRAL_SIZE;
780 /******************************************************************
781 Set up the Dfs referral for the dfs pathname
782 ******************************************************************/
784 int setup_dfs_referral(connection_struct *orig_conn, char *pathname, int max_referral_level, char **ppdata)
786 struct junction_map junction;
788 BOOL self_referral = False;
791 char *pathnamep = pathname;
794 if (!(ctx=talloc_init("setup_dfs_referral"))) {
798 ZERO_STRUCT(junction);
800 /* get the junction entry */
806 /* Trim pathname sent by client so it begins with only one backslash.
807 Two backslashes confuse some dfs clients
809 while (pathnamep[0] == '\\' && pathnamep[1] == '\\') {
813 pstrcpy(buf, pathnamep);
814 /* The following call can change cwd. */
815 if (!get_referred_path(ctx, buf, &junction, &consumedcnt, &self_referral)) {
816 vfs_ChDir(orig_conn,orig_conn->connectpath);
820 vfs_ChDir(orig_conn,orig_conn->connectpath);
822 if (!self_referral) {
823 pathnamep[consumedcnt] = '\0';
825 if( DEBUGLVL( 3 ) ) {
827 dbgtext("setup_dfs_referral: Path %s to alternate path(s):",pathnamep);
828 for(i=0;i<junction.referral_count;i++)
829 dbgtext(" %s",junction.referral_list[i].alternate_path);
834 /* create the referral depeding on version */
835 DEBUG(10,("max_referral_level :%d\n",max_referral_level));
836 if(max_referral_level<2 || max_referral_level>3) {
837 max_referral_level = 2;
840 switch(max_referral_level) {
842 reply_size = setup_ver2_dfs_referral(pathnamep, ppdata, &junction,
843 consumedcnt, self_referral);
846 reply_size = setup_ver3_dfs_referral(pathnamep, ppdata, &junction,
847 consumedcnt, self_referral);
850 DEBUG(0,("setup_dfs_referral: Invalid dfs referral version: %d\n", max_referral_level));
856 DEBUGADD(0,("DFS Referral pdata:\n"));
857 dump_data(0,*ppdata,reply_size);
864 /**********************************************************************
865 The following functions are called by the NETDFS RPC pipe functions
866 **********************************************************************/
868 /*********************************************************************
869 Creates a junction structure from a Dfs pathname
870 **********************************************************************/
872 BOOL create_junction(char *pathname, struct junction_map *jucn)
876 parse_dfs_path(pathname,&dp);
878 /* check if path is dfs : validate first token */
879 if ( !strequal(get_local_machine_name(),dp.hostname) ) {
880 /* Hostname mismatch, check if one of our IP addresses */
881 if (!ismyip(*interpret_addr2(dp.hostname))) {
882 DEBUG(4,("create_junction: Invalid hostname %s in dfs path %s\n",
883 dp.hostname, pathname));
888 /* Check for a non-DFS share */
889 if(!lp_msdfs_root(lp_servicenumber(dp.servicename))) {
890 DEBUG(4,("create_junction: %s is not an msdfs root.\n", dp.servicename));
894 pstrcpy(jucn->service_name,dp.servicename);
895 pstrcpy(jucn->volume_name,dp.reqpath);
899 /**********************************************************************
900 Forms a valid Unix pathname from the junction
901 **********************************************************************/
903 static BOOL junction_to_local_path(struct junction_map *jucn, char *path,
904 int max_pathlen, connection_struct *conn)
913 snum = lp_servicenumber(jucn->service_name);
918 safe_strcpy(path, lp_pathname(snum), max_pathlen-1);
919 safe_strcat(path, "/", max_pathlen-1);
920 safe_strcat(path, jucn->volume_name, max_pathlen-1);
922 pstrcpy(conn_path, lp_pathname(snum));
923 if (!create_conn_struct(conn, snum, conn_path)) {
930 BOOL create_msdfs_link(struct junction_map *jucn, BOOL exists)
934 connection_struct conns;
935 connection_struct *conn = &conns;
937 BOOL insert_comma = False;
942 if(!junction_to_local_path(jucn, path, sizeof(path), conn)) {
946 /* form the msdfs_link contents */
947 pstrcpy(msdfs_link, "msdfs:");
948 for(i=0; i<jucn->referral_count; i++) {
949 char* refpath = jucn->referral_list[i].alternate_path;
951 trim_char(refpath, '\\', '\\');
952 if(*refpath == '\0') {
954 insert_comma = False;
958 if (i > 0 && insert_comma) {
959 pstrcat(msdfs_link, ",");
962 pstrcat(msdfs_link, refpath);
968 DEBUG(5,("create_msdfs_link: Creating new msdfs link: %s -> %s\n", path, msdfs_link));
971 if(SMB_VFS_UNLINK(conn,path)!=0) {
976 if(SMB_VFS_SYMLINK(conn, msdfs_link, path) < 0) {
977 DEBUG(1,("create_msdfs_link: symlink failed %s -> %s\nError: %s\n",
978 path, msdfs_link, strerror(errno)));
987 conn_free_internal(conn);
991 BOOL remove_msdfs_link(struct junction_map *jucn)
994 connection_struct conns;
995 connection_struct *conn = &conns;
1000 if( junction_to_local_path(jucn, path, sizeof(path), conn) ) {
1001 if( SMB_VFS_UNLINK(conn, path) == 0 ) {
1004 talloc_destroy( conn->mem_ctx );
1007 conn_free_internal(conn);
1011 static int form_junctions(TALLOC_CTX *ctx, int snum, struct junction_map *jucn, int jn_remain)
1014 SMB_STRUCT_DIR *dirp;
1016 pstring connect_path;
1017 char* service_name = lp_servicename(snum);
1018 connection_struct conn;
1019 struct referral *ref = NULL;
1023 if (jn_remain <= 0) {
1027 pstrcpy(connect_path,lp_pathname(snum));
1029 if(*connect_path == '\0') {
1034 * Fake up a connection struct for the VFS layer.
1037 if (!create_conn_struct(&conn, snum, connect_path)) {
1041 /* form a junction for the msdfs root - convention
1042 DO NOT REMOVE THIS: NT clients will not work with us
1043 if this is not present
1045 pstrcpy(jucn[cnt].service_name, service_name);
1046 jucn[cnt].volume_name[0] = '\0';
1047 jucn[cnt].referral_count = 1;
1049 ref = jucn[cnt].referral_list = TALLOC_P(ctx, struct referral);
1050 if (jucn[cnt].referral_list == NULL) {
1051 DEBUG(0, ("Malloc failed!\n"));
1056 ref->ttl = REFERRAL_TTL;
1057 if (*lp_msdfs_proxy(snum) != '\0') {
1058 pstrcpy(ref->alternate_path, lp_msdfs_proxy(snum));
1062 slprintf(ref->alternate_path, sizeof(pstring)-1,
1063 "\\\\%s\\%s", get_local_machine_name(), service_name);
1066 /* Now enumerate all dfs links */
1067 dirp = SMB_VFS_OPENDIR(&conn, ".", NULL, 0);
1072 while ((dname = vfs_readdirname(&conn, dirp)) != NULL) {
1073 if (cnt >= jn_remain) {
1074 SMB_VFS_CLOSEDIR(&conn,dirp);
1075 DEBUG(2, ("ran out of MSDFS junction slots"));
1078 if (is_msdfs_link(ctx, &conn, dname, &jucn[cnt].referral_list,
1079 &jucn[cnt].referral_count, NULL)) {
1080 pstrcpy(jucn[cnt].service_name, service_name);
1081 pstrcpy(jucn[cnt].volume_name, dname);
1086 SMB_VFS_CLOSEDIR(&conn,dirp);
1090 conn_free_internal(&conn);
1094 int enum_msdfs_links(TALLOC_CTX *ctx, struct junction_map *jucn, int jn_max)
1099 if(!lp_host_msdfs()) {
1103 for(i=0;i < lp_numservices() && (jn_max - jn_count) > 0;i++) {
1104 if(lp_msdfs_root(i)) {
1105 jn_count += form_junctions(ctx, i,jucn,jn_max - jn_count);