9 #include <sys/utsname.h>
10 #include <sys/socket.h>
11 #include <arpa/inet.h>
17 #define MOUNT_CIFS_VERSION "1"
19 extern char *getusername(void);
23 static int got_password
= 0;
24 static int got_user
= 0;
25 static char * user_name
= NULL
;
26 char * mountpassword
= NULL
;
29 void mount_cifs_usage()
31 printf("\nUsage: %s remotetarget dir\n", thisprogram
);
32 printf("\nMount the remotetarget, specified as either a UNC name or ");
33 printf(" CIFS URL, to the local directory, dir.\n");
38 /* caller frees username if necessary */
39 char * getusername() {
40 char *username
= NULL
;
41 struct passwd
*password
= getpwuid(getuid());
44 username
= password
->pw_name
;
49 char * parse_cifs_url(unc_name
)
51 printf("\ncifs url %s\n",unc_name
);
54 char * parse_options(char * options
)
56 /* BB add missing code BB */
59 /* Note that caller frees the returned buffer if necessary */
60 char * parse_server(char * unc_name
)
62 int length
= strnlen(unc_name
,1024);
64 char * ipaddress_string
= NULL
;
65 struct hostent
* host_entry
;
66 struct in_addr server_ipaddr
;
72 printf("mount error: UNC name too long");
75 if (strncasecmp("cifs://",unc_name
,7) == 0)
76 return parse_cifs_url(unc_name
+7);
77 if (strncasecmp("smb://",unc_name
,6) == 0) {
78 return parse_cifs_url(unc_name
+6);
82 /* BB add code to find DFS root here */
83 printf("\nMounting the DFS root for domain not implemented yet");
86 /* BB add support for \\\\ not just // */
87 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
88 printf("mount error: improperly formatted UNC name.");
89 printf(" %s does not begin with \\\\ or //\n",unc_name
);
93 if (share
= strchr(unc_name
, '/')) {
94 *share
= 0; /* temporarily terminate the string */
96 host_entry
= gethostbyname(unc_name
);
97 *(share
- 1) = '\\'; /* put the slash back */
98 /* rc = getipnodebyname(unc_name, AF_INET, AT_ADDRCONFIG ,&rc);*/
99 if(host_entry
== NULL
) {
100 printf("mount error: could not find target server. TCP name %s not found ", unc_name
);
101 printf(" rc = %d\n",rc
);
105 printf("Target server %s %x found\n",host_entry
->h_name
,host_entry
->h_addr
); /* BB removeme */
106 /* BB should we pass an alternate version of the share name as Unicode */
107 /* BB what about ipv6? BB */
108 /* BB add retries with alternate servers in list */
110 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
112 ipaddress_string
= inet_ntoa(server_ipaddr
);
113 if(ipaddress_string
== NULL
) {
114 printf("mount error: could not get valid ip address for target server\n");
117 return ipaddress_string
;
120 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
121 printf("Mounting the DFS root for a particular server not implemented yet\n");
128 static struct option longopts
[] = {
129 { "all", 0, 0, 'a' },
130 { "help", 0, 0, 'h' },
131 { "read-only", 0, 0, 'r' },
133 { "verbose", 0, 0, 'v' },
134 { "version", 0, 0, 'V' },
135 { "read-write", 0, 0, 'w' },
137 { "options", 1, 0, 'o' },
138 { "types", 1, 0, 't' },
139 { "replace", 0, 0, 129 },
140 { "after", 0, 0, 130 },
141 { "before", 0, 0, 131 },
142 { "over", 0, 0, 132 },
143 { "move", 0, 0, 133 },
144 { "rsize",1, 0, 136 },
145 { "wsize",1, 0, 137 },
150 { "username",1,0,140},
153 { "password",1,0,142},
157 int main(int argc
, char ** argv
)
160 int flags
= MS_MANDLOCK
| MS_MGC_VAL
;
161 char * orgoptions
= NULL
;
164 char * domain_name
= NULL
;
176 struct utsname sysinfo
;
178 /* setlocale(LC_ALL, "");
179 bindtextdomain(PACKAGE, LOCALEDIR);
180 textdomain(PACKAGE); */
183 thisprogram
= argv
[0];
185 if(thisprogram
== NULL
)
186 thisprogram
= "mount.cifs";
190 printf(" node: %s machine: %s\n", sysinfo
.nodename
,sysinfo
.machine
);
193 share_name
= argv
[1];
194 mount_point
= argv
[2];
195 /* add sharename in opts string as unc= parm */
197 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsU:vVwt:",
198 longopts
, NULL
)) != -1) {
213 external_allowed = 0;
216 list_with_volumelabel = 1;
219 volumelabel = optarg;
226 orgoptions
= strcat(orgoptions
, ",");
227 orgoptions
= strcat(orgoptions
,optarg
);
229 orgoptions
= strdup(optarg
);
234 test_opts = xstrconcat3(test_opts, ",", optarg);
236 test_opts = xstrdup(optarg);
238 case 'r': /* mount readonly */
248 printf ("mount: %s\n", version);
251 flags
&= ~MS_RDONLY
;;
260 mounttype = MS_REPLACE;
263 mounttype = MS_AFTER;
266 mounttype = MS_BEFORE;
275 mounttype = (MS_BIND | MS_REC);
278 rsize
= atoi(optarg
) ;
281 wsize
= atoi(optarg
);
294 domain_name
= optarg
;
298 mountpassword
= optarg
;
306 for(i
= 0;i
< argc
;i
++) /* BB remove */
307 printf("\narg %d is %s",i
,argv
[i
]); /* BB remove */
308 printf("\n"); /* BB removeme */
310 /* canonicalize the path in argv[1]? */
312 if(stat (mount_point
, &statbuf
)) {
313 printf("mount error: mount point %s does not exist\n",mount_point
);
316 if (S_ISDIR(statbuf
.st_mode
) == 0) {
317 printf("mount error: mount point %s is not a directory\n",mount_point
);
322 printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n");
326 ipaddr
= parse_server(share_name
);
327 /* if(share_name == NULL)
329 parse_options(orgoptions
);
332 user_name
= getusername();
334 /* check username for user%password format */
336 if(got_password
== 0) {
337 if (getenv("PASSWD")) {
338 mountpassword
= malloc(33);
340 strncpy(mountpassword
,getenv("PASSWD"),32);
343 /* } else if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
345 got_password = 1;*/ /* BB add missing function */
347 mountpassword
= getpass("Password: "); /* BB obsolete */
352 /* launch daemon (handles dfs name resolution and credential change) */
354 optlen
= strlen(orgoptions
);
357 options
= malloc(optlen
+ 25 + strlen(share_name
) + strlen(user_name
)
358 + strlen(ipaddr
) + 1);
359 strcpy(options
,"unc=");
360 strcat(options
,share_name
);
361 strncat(options
,",ip=",4);
362 strcat(options
,ipaddr
);
363 strncat(options
,",user=",6);
364 strcat(options
,user_name
);
365 strncat(options
,",pass=",6);
366 strcat(options
,mountpassword
);
367 strncat(options
,",ver=",5);
368 strcat(options
,MOUNT_CIFS_VERSION
);
370 strcat(options
,orgoptions
);
371 printf("\noptions %s \n",options
);
372 if(mount(share_name
, mount_point
, "cifs", flags
, options
)) {
373 /* remember to kill daemon on error */
376 printf(" success\n"); /* BB removeme */
379 printf("mount error: cifs filesystem not supported by the system\n");
382 printf("mount error %d = %s",errno
,strerror(errno
));
384 printf("\nRefer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
387 printf(" mount succeeded\n"); /* BB removeme */