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
;
38 open nofollow - avoid symlink exposure?
39 get owner of dir see if matches self or if root
40 call system(umount argv) etc.
44 void mount_cifs_usage()
46 printf("\nUsage: %s remotetarget dir\n", thisprogram
);
47 printf("\nMount the remotetarget, specified as either a UNC name or ");
48 printf(" CIFS URL, to the local directory, dir.\n");
53 /* caller frees username if necessary */
54 char * getusername() {
55 char *username
= NULL
;
56 struct passwd
*password
= getpwuid(getuid());
59 username
= password
->pw_name
;
64 char * parse_cifs_url(unc_name
)
66 printf("\ncifs url %s\n",unc_name
);
69 int parse_options(char * options
)
77 while ((data
= strsep(&options
, ",")) != NULL
) {
80 if ((value
= strchr(data
, '=')) != NULL
) {
83 if (strncmp(data
, "user", 4) == 0) {
84 if (!value
|| !*value
) {
85 printf("invalid or missing username\n");
86 return 1; /* needs_arg; */
88 if (strnlen(value
, 260) < 260) {
90 /* BB add check for format user%pass */
91 /* if(strchr(username%passw) got_password = 1) */
93 printf("username too long\n");
96 } else if (strncmp(data
, "pass", 4) == 0) {
97 if (!value
|| !*value
) {
99 printf("password specified twice, ignoring second\n");
102 } else if (strnlen(value
, 17) < 17) {
105 printf("password too long\n");
108 } else if (strncmp(data
, "ip", 2) == 0) {
109 if (!value
|| !*value
) {
110 printf("target ip address argument missing");
111 } else if (strnlen(value
, 35) < 35) {
114 printf("ip address too long\n");
117 } else if ((strncmp(data
, "unc", 3) == 0)
118 || (strncmp(data
, "target", 6) == 0)
119 || (strncmp(data
, "path", 4) == 0)) {
120 if (!value
|| !*value
) {
121 printf("invalid path to network resource\n");
122 return 1; /* needs_arg; */
123 } else if(strnlen(value
,5) < 5) {
124 printf("UNC name too short");
127 if (strnlen(value
, 300) < 300) {
129 if (strncmp(value
, "//", 2) == 0) {
131 printf("unc name specified twice, ignoring second\n");
134 } else if (strncmp(value
, "\\\\", 2) != 0) {
135 printf("UNC Path does not begin with // or \\\\ \n");
139 printf("unc name specified twice, ignoring second\n");
144 printf("CIFS: UNC name too long\n");
147 } else if ((strncmp(data
, "domain", 3) == 0)
148 || (strncmp(data
, "workgroup", 5) == 0)) {
149 if (!value
|| !*value
) {
150 printf("CIFS: invalid domain name\n");
151 return 1; /* needs_arg; */
153 if (strnlen(value
, 65) < 65) {
156 printf("domain name too long\n");
159 } else if (strncmp(data
, "uid", 3) == 0) {
160 if (value
&& *value
) {
163 } else if (strncmp(data
, "gid", 3) == 0) {
164 if (value
&& *value
) {
167 } /* else if (strnicmp(data, "file_mode", 4) == 0) {
168 if (value && *value) {
170 simple_strtoul(value, &value, 0);
172 } else if (strnicmp(data, "dir_mode", 3) == 0) {
173 if (value && *value) {
175 simple_strtoul(value, &value, 0);
177 } else if (strnicmp(data, "port", 4) == 0) {
178 if (value && *value) {
180 simple_strtoul(value, &value, 0);
182 } else if (strnicmp(data, "rsize", 5) == 0) {
183 if (value && *value) {
185 simple_strtoul(value, &value, 0);
187 } else if (strnicmp(data, "wsize", 5) == 0) {
188 if (value && *value) {
190 simple_strtoul(value, &value, 0);
192 } else if (strnicmp(data, "version", 3) == 0) {
194 } else if (strnicmp(data, "rw", 2) == 0) {
197 printf("CIFS: Unknown mount option %s\n",data); */
202 /* Note that caller frees the returned buffer if necessary */
203 char * parse_server(char * unc_name
)
205 int length
= strnlen(unc_name
,1024);
207 char * ipaddress_string
= NULL
;
208 struct hostent
* host_entry
;
209 struct in_addr server_ipaddr
;
214 printf("mount error: UNC name too long");
217 if (strncasecmp("cifs://",unc_name
,7) == 0)
218 return parse_cifs_url(unc_name
+7);
219 if (strncasecmp("smb://",unc_name
,6) == 0) {
220 return parse_cifs_url(unc_name
+6);
224 /* BB add code to find DFS root here */
225 printf("\nMounting the DFS root for domain not implemented yet");
228 /* BB add support for \\\\ not just // */
229 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
230 printf("mount error: improperly formatted UNC name.");
231 printf(" %s does not begin with \\\\ or //\n",unc_name
);
238 if ((share
= strchr(unc_name
, '/')) ||
239 (share
= strchr(unc_name
,'\\'))) {
240 *share
= 0; /* temporarily terminate the string */
242 host_entry
= gethostbyname(unc_name
);
243 *(share
- 1) = '/'; /* put the slash back */
244 /* rc = getipnodebyname(unc_name, AF_INET, AT_ADDRCONFIG ,&rc);*/
245 if(host_entry
== NULL
) {
246 printf("mount error: could not find target server. TCP name %s not found ", unc_name
);
247 printf(" rc = %d\n",rc
);
251 /* BB should we pass an alternate version of the share name as Unicode */
252 /* BB what about ipv6? BB */
253 /* BB add retries with alternate servers in list */
255 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
257 ipaddress_string
= inet_ntoa(server_ipaddr
);
258 if(ipaddress_string
== NULL
) {
259 printf("mount error: could not get valid ip address for target server\n");
262 return ipaddress_string
;
265 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
266 printf("Mounting the DFS root for a particular server not implemented yet\n");
273 static struct option longopts
[] = {
274 { "all", 0, 0, 'a' },
275 { "help", 0, 0, 'h' },
276 { "read-only", 0, 0, 'r' },
278 { "verbose", 0, 0, 'v' },
279 { "version", 0, 0, 'V' },
280 { "read-write", 0, 0, 'w' },
282 { "options", 1, 0, 'o' },
283 { "types", 1, 0, 't' },
284 { "replace", 0, 0, 129 },
285 { "after", 0, 0, 130 },
286 { "before", 0, 0, 131 },
287 { "over", 0, 0, 132 },
288 { "move", 0, 0, 133 },
289 { "rsize",1, 0, 136 },
290 { "wsize",1, 0, 137 },
295 { "username",1,0,140},
298 { "password",1,0,142},
302 int main(int argc
, char ** argv
)
305 int flags
= MS_MANDLOCK
| MS_MGC_VAL
;
306 char * orgoptions
= NULL
;
307 char * share_name
= NULL
;
308 char * domain_name
= NULL
;
309 char * ipaddr
= NULL
;
322 struct utsname sysinfo
;
323 struct mntent mountent
;
326 /* setlocale(LC_ALL, "");
327 bindtextdomain(PACKAGE, LOCALEDIR);
328 textdomain(PACKAGE); */
331 thisprogram
= argv
[0];
333 if(thisprogram
== NULL
)
334 thisprogram
= "mount.cifs";
337 /* BB add workstation name and domain and pass down */
339 printf(" node: %s machine: %s\n", sysinfo.nodename,sysinfo.machine);
343 share_name
= argv
[1];
344 mountpoint
= argv
[2];
345 /* add sharename in opts string as unc= parm */
347 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsU:vVwt:",
348 longopts
, NULL
)) != -1) {
363 external_allowed = 0;
366 list_with_volumelabel = 1;
369 volumelabel = optarg;
376 orgoptions
= strcat(orgoptions
, ",");
377 orgoptions
= strcat(orgoptions
,optarg
);
379 orgoptions
= strdup(optarg
);
384 test_opts = xstrconcat3(test_opts, ",", optarg);
386 test_opts = xstrdup(optarg);
388 case 'r': /* mount readonly */
398 printf ("mount: %s\n", version);
410 mounttype = MS_REPLACE;
413 mounttype = MS_AFTER;
416 mounttype = MS_BEFORE;
425 mounttype = (MS_BIND | MS_REC);
428 rsize
= atoi(optarg
) ;
431 wsize
= atoi(optarg
);
444 domain_name
= optarg
;
448 mountpassword
= optarg
;
456 /* canonicalize the path in argv[1]? */
458 /* BB save off path and pop after mount returns */
459 if(chdir(mountpoint
)) {
460 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
463 if(stat (mountpoint
, &statbuf
)) {
464 printf("mount error: mount point %s does not exist\n",mountpoint
);
468 if (S_ISDIR(statbuf
.st_mode
) == 0) {
469 printf("mount error: mount point %s is not a directory\n",mountpoint
);
473 if((getuid() != 0) && (geteuid() == 0)) {
474 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== statbuf
.st_mode
& S_IRWXU
)) {
475 printf("setuid mount allowed\n");
477 printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n");
482 ipaddr
= parse_server(share_name
);
483 /* if(share_name == NULL)
485 if (parse_options(strdup(orgoptions
)))
489 user_name
= getusername();
491 /* check username for user%password format */
493 if(got_password
== 0) {
494 if (getenv("PASSWD")) {
495 mountpassword
= malloc(33);
497 strncpy(mountpassword
,getenv("PASSWD"),32);
500 /* } else if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
502 got_password = 1;*/ /* BB add missing function */
504 mountpassword
= getpass("Password: "); /* BB obsolete */
508 /* FIXME launch daemon (handles dfs name resolution and credential change)
509 remember to clear parms and overwrite password field before launching */
511 optlen
= strlen(orgoptions
);
515 optlen
+= strlen(share_name
) + 4;
517 optlen
+= strlen(user_name
) + 6;
519 optlen
+= strlen(ipaddr
) + 4;
521 optlen
+= strlen(mountpassword
) + 6;
522 options
= malloc(optlen
+ 10);
525 strncat(options
,"unc=",4);
526 strcat(options
,share_name
);
527 /* scan backwards and reverse direction of slash */
528 temp
= strrchr(options
, '/');
529 if(temp
> options
+ 6)
532 strncat(options
,",ip=",4);
533 strcat(options
,ipaddr
);
536 strncat(options
,",user=",6);
537 strcat(options
,user_name
);
540 strncat(options
,",pass=",6);
541 strcat(options
,mountpassword
);
543 strncat(options
,",ver=",5);
544 strcat(options
,MOUNT_CIFS_VERSION
);
548 strcat(options
,orgoptions
);
550 /* printf("\noptions %s \n",options);*/
551 if(mount(share_name
, mountpoint
, "cifs", flags
, options
)) {
552 /* remember to kill daemon on error */
555 printf("mount failed but no error number set\n");
558 printf("mount error: cifs filesystem not supported by the system\n");
561 printf("mount error %d = %s\n",errno
,strerror(errno
));
563 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
566 pmntfile
= setmntent(MOUNTED
, "a+");
568 mountent
.mnt_fsname
= share_name
;
569 mountent
.mnt_dir
= mountpoint
;
570 mountent
.mnt_type
= "cifs";
571 mountent
.mnt_opts
= "";
572 mountent
.mnt_freq
= 0;
573 mountent
.mnt_passno
= 0;
574 rc
= addmntent(pmntfile
,&mountent
);
577 printf("could not update mount table\n");