9 #include <sys/utsname.h>
10 #include <sys/socket.h>
11 #include <arpa/inet.h>
18 #define MOUNT_CIFS_VERSION "1"
20 extern char *getusername(void);
24 static int got_password
= 0;
25 static int got_user
= 0;
26 static int got_domain
= 0;
27 static int got_ip
= 0;
28 static int got_unc
= 0;
29 static int got_uid
= 0;
30 static int got_gid
= 0;
31 static char * user_name
= NULL
;
32 char * mountpassword
= NULL
;
35 void mount_cifs_usage()
37 printf("\nUsage: %s remotetarget dir\n", thisprogram
);
38 printf("\nMount the remotetarget, specified as either a UNC name or ");
39 printf(" CIFS URL, to the local directory, dir.\n");
44 /* caller frees username if necessary */
45 char * getusername() {
46 char *username
= NULL
;
47 struct passwd
*password
= getpwuid(getuid());
50 username
= password
->pw_name
;
55 char * parse_cifs_url(unc_name
)
57 printf("\ncifs url %s\n",unc_name
);
60 int parse_options(char * options
)
68 while ((data
= strsep(&options
, ",")) != NULL
) {
71 if ((value
= strchr(data
, '=')) != NULL
) {
74 if (strncmp(data
, "user", 4) == 0) {
75 if (!value
|| !*value
) {
76 printf("invalid or missing username\n");
77 return 1; /* needs_arg; */
79 if (strnlen(value
, 260) < 260) {
81 /* BB add check for format user%pass */
82 /* if(strchr(username%passw) got_password = 1) */
84 printf("username too long\n");
87 } else if (strncmp(data
, "pass", 4) == 0) {
88 if (!value
|| !*value
) {
90 printf("password specified twice, ignoring second\n");
93 } else if (strnlen(value
, 17) < 17) {
96 printf("password too long\n");
99 } else if (strncmp(data
, "ip", 2) == 0) {
100 if (!value
|| !*value
) {
101 printf("target ip address argument missing");
102 } else if (strnlen(value
, 35) < 35) {
105 printf("ip address too long\n");
108 } else if ((strncmp(data
, "unc", 3) == 0)
109 || (strncmp(data
, "target", 6) == 0)
110 || (strncmp(data
, "path", 4) == 0)) {
111 if (!value
|| !*value
) {
112 printf("invalid path to network resource\n");
113 return 1; /* needs_arg; */
114 } else if(strnlen(value
,5) < 5) {
115 printf("UNC name too short");
118 if (strnlen(value
, 300) < 300) {
120 if (strncmp(value
, "//", 2) == 0) {
122 printf("unc name specified twice, ignoring second\n");
125 } else if (strncmp(value
, "\\\\", 2) != 0) {
126 printf("UNC Path does not begin with // or \\\\ \n");
130 printf("unc name specified twice, ignoring second\n");
135 printf("CIFS: UNC name too long\n");
138 } else if ((strncmp(data
, "domain", 3) == 0)
139 || (strncmp(data
, "workgroup", 5) == 0)) {
140 if (!value
|| !*value
) {
141 printf("CIFS: invalid domain name\n");
142 return 1; /* needs_arg; */
144 if (strnlen(value
, 65) < 65) {
147 printf("domain name too long\n");
150 } else if (strncmp(data
, "uid", 3) == 0) {
151 if (value
&& *value
) {
154 } else if (strncmp(data
, "gid", 3) == 0) {
155 if (value
&& *value
) {
158 } /* else if (strnicmp(data, "file_mode", 4) == 0) {
159 if (value && *value) {
161 simple_strtoul(value, &value, 0);
163 } else if (strnicmp(data, "dir_mode", 3) == 0) {
164 if (value && *value) {
166 simple_strtoul(value, &value, 0);
168 } else if (strnicmp(data, "port", 4) == 0) {
169 if (value && *value) {
171 simple_strtoul(value, &value, 0);
173 } else if (strnicmp(data, "rsize", 5) == 0) {
174 if (value && *value) {
176 simple_strtoul(value, &value, 0);
178 } else if (strnicmp(data, "wsize", 5) == 0) {
179 if (value && *value) {
181 simple_strtoul(value, &value, 0);
183 } else if (strnicmp(data, "version", 3) == 0) {
185 } else if (strnicmp(data, "rw", 2) == 0) {
188 printf("CIFS: Unknown mount option %s\n",data); */
193 /* Note that caller frees the returned buffer if necessary */
194 char * parse_server(char * unc_name
)
196 int length
= strnlen(unc_name
,1024);
198 char * ipaddress_string
= NULL
;
199 struct hostent
* host_entry
;
200 struct in_addr server_ipaddr
;
205 printf("mount error: UNC name too long");
208 if (strncasecmp("cifs://",unc_name
,7) == 0)
209 return parse_cifs_url(unc_name
+7);
210 if (strncasecmp("smb://",unc_name
,6) == 0) {
211 return parse_cifs_url(unc_name
+6);
215 /* BB add code to find DFS root here */
216 printf("\nMounting the DFS root for domain not implemented yet");
219 /* BB add support for \\\\ not just // */
220 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
221 printf("mount error: improperly formatted UNC name.");
222 printf(" %s does not begin with \\\\ or //\n",unc_name
);
228 if ((share
= strchr(unc_name
, '/')) ||
229 (share
= strchr(unc_name
,'\\'))) {
230 *share
= 0; /* temporarily terminate the string */
232 host_entry
= gethostbyname(unc_name
);
233 *(share
- 1) = '\\'; /* put the slash back */
234 /* rc = getipnodebyname(unc_name, AF_INET, AT_ADDRCONFIG ,&rc);*/
235 if(host_entry
== NULL
) {
236 printf("mount error: could not find target server. TCP name %s not found ", unc_name
);
237 printf(" rc = %d\n",rc
);
241 /* BB should we pass an alternate version of the share name as Unicode */
242 /* BB what about ipv6? BB */
243 /* BB add retries with alternate servers in list */
245 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
247 ipaddress_string
= inet_ntoa(server_ipaddr
);
248 if(ipaddress_string
== NULL
) {
249 printf("mount error: could not get valid ip address for target server\n");
252 return ipaddress_string
;
255 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
256 printf("Mounting the DFS root for a particular server not implemented yet\n");
263 static struct option longopts
[] = {
264 { "all", 0, 0, 'a' },
265 { "help", 0, 0, 'h' },
266 { "read-only", 0, 0, 'r' },
268 { "verbose", 0, 0, 'v' },
269 { "version", 0, 0, 'V' },
270 { "read-write", 0, 0, 'w' },
272 { "options", 1, 0, 'o' },
273 { "types", 1, 0, 't' },
274 { "replace", 0, 0, 129 },
275 { "after", 0, 0, 130 },
276 { "before", 0, 0, 131 },
277 { "over", 0, 0, 132 },
278 { "move", 0, 0, 133 },
279 { "rsize",1, 0, 136 },
280 { "wsize",1, 0, 137 },
285 { "username",1,0,140},
288 { "password",1,0,142},
292 int main(int argc
, char ** argv
)
295 int flags
= MS_MANDLOCK
| MS_MGC_VAL
;
296 char * orgoptions
= NULL
;
297 char * share_name
= NULL
;
298 char * domain_name
= NULL
;
299 char * ipaddr
= NULL
;
311 struct utsname sysinfo
;
312 struct mntent mountent
;
315 /* setlocale(LC_ALL, "");
316 bindtextdomain(PACKAGE, LOCALEDIR);
317 textdomain(PACKAGE); */
320 thisprogram
= argv
[0];
322 if(thisprogram
== NULL
)
323 thisprogram
= "mount.cifs";
326 /* BB add workstation name and domain and pass down */
328 printf(" node: %s machine: %s\n", sysinfo.nodename,sysinfo.machine);
332 share_name
= argv
[1];
333 mountpoint
= argv
[2];
334 /* add sharename in opts string as unc= parm */
336 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsU:vVwt:",
337 longopts
, NULL
)) != -1) {
352 external_allowed = 0;
355 list_with_volumelabel = 1;
358 volumelabel = optarg;
365 orgoptions
= strcat(orgoptions
, ",");
366 orgoptions
= strcat(orgoptions
,optarg
);
368 orgoptions
= strdup(optarg
);
373 test_opts = xstrconcat3(test_opts, ",", optarg);
375 test_opts = xstrdup(optarg);
377 case 'r': /* mount readonly */
387 printf ("mount: %s\n", version);
390 flags
&= ~MS_RDONLY
;;
399 mounttype = MS_REPLACE;
402 mounttype = MS_AFTER;
405 mounttype = MS_BEFORE;
414 mounttype = (MS_BIND | MS_REC);
417 rsize
= atoi(optarg
) ;
420 wsize
= atoi(optarg
);
433 domain_name
= optarg
;
437 mountpassword
= optarg
;
445 /* canonicalize the path in argv[1]? */
447 if(stat (mountpoint
, &statbuf
)) {
448 printf("mount error: mount point %s does not exist\n",mountpoint
);
451 if (S_ISDIR(statbuf
.st_mode
) == 0) {
452 printf("mount error: mount point %s is not a directory\n",mountpoint
);
457 printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n");
461 ipaddr
= parse_server(share_name
);
462 /* if(share_name == NULL)
464 if (parse_options(strdup(orgoptions
)))
468 user_name
= getusername();
470 /* check username for user%password format */
472 if(got_password
== 0) {
473 if (getenv("PASSWD")) {
474 mountpassword
= malloc(33);
476 strncpy(mountpassword
,getenv("PASSWD"),32);
479 /* } else if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
481 got_password = 1;*/ /* BB add missing function */
483 mountpassword
= getpass("Password: "); /* BB obsolete */
487 /* FIXME launch daemon (handles dfs name resolution and credential change)
488 remember to clear parms and overwrite password field before launching */
490 optlen
= strlen(orgoptions
);
494 optlen
+= strlen(share_name
) + 4;
496 optlen
+= strlen(user_name
) + 6;
498 optlen
+= strlen(ipaddr
) + 4;
500 optlen
+= strlen(mountpassword
) + 6;
501 options
= malloc(optlen
+ 10);
504 strncat(options
,"unc=",4);
505 strcat(options
,share_name
);
507 strncat(options
,",ip=",4);
508 strcat(options
,ipaddr
);
511 strncat(options
,",user=",6);
512 strcat(options
,user_name
);
515 strncat(options
,",pass=",6);
516 strcat(options
,mountpassword
);
518 strncat(options
,",ver=",5);
519 strcat(options
,MOUNT_CIFS_VERSION
);
523 strcat(options
,orgoptions
);
525 /* printf("\noptions %s \n",options);*/
526 if(mount(share_name
, mountpoint
, "cifs", flags
, options
)) {
527 /* remember to kill daemon on error */
530 printf("mount failed but no error number set\n");
533 printf("mount error: cifs filesystem not supported by the system\n");
536 printf("mount error %d = %s",errno
,strerror(errno
));
538 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
541 pmntfile
= setmntent(MOUNTED
, "a+");
543 mountent
.mnt_fsname
= share_name
;
544 mountent
.mnt_dir
= mountpoint
;
545 mountent
.mnt_type
= "cifs";
546 mountent
.mnt_opts
= "";
547 mountent
.mnt_freq
= 0;
548 mountent
.mnt_passno
= 0;
549 rc
= addmntent(pmntfile
,&mountent
);
552 printf("could not update mount table\n");