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 /* fmask and dmask synonyms for people used to smbfs syntax */
168 } else if (strcmp(data
, "file_mode") == 0 || strcmp(data
, "fmask")==0) {
169 if (!value
|| !*value
) {
170 printf ("Option '%s' requires a numerical argument\n", data
);
174 if (value
[0] != '0') {
175 printf ("WARNING: '%s' not expressed in octal.\n", data
);
178 if (strcmp (data
, "fmask") == 0) {
179 printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n");
182 } else if (strcmp(data
, "dir_mode") == 0 || strcmp(data
, "dmask")==0) {
183 if (!value
|| !*value
) {
184 printf ("Option '%s' requires a numerical argument\n", data
);
188 if (value
[0] != '0') {
189 printf ("WARNING: '%s' not expressed in octal.\n", data
);
192 if (strcmp (data
, "dmask") == 0) {
193 printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n");
196 } /* else if (strnicmp(data, "port", 4) == 0) {
197 if (value && *value) {
199 simple_strtoul(value, &value, 0);
201 } else if (strnicmp(data, "rsize", 5) == 0) {
202 if (value && *value) {
204 simple_strtoul(value, &value, 0);
206 } else if (strnicmp(data, "wsize", 5) == 0) {
207 if (value && *value) {
209 simple_strtoul(value, &value, 0);
211 } else if (strnicmp(data, "version", 3) == 0) {
213 } else if (strnicmp(data, "rw", 2) == 0) {
216 printf("CIFS: Unknown mount option %s\n",data); */
221 /* Note that caller frees the returned buffer if necessary */
222 char * parse_server(char * unc_name
)
224 int length
= strnlen(unc_name
,1024);
226 char * ipaddress_string
= NULL
;
227 struct hostent
* host_entry
;
228 struct in_addr server_ipaddr
;
233 printf("mount error: UNC name too long");
236 if (strncasecmp("cifs://",unc_name
,7) == 0)
237 return parse_cifs_url(unc_name
+7);
238 if (strncasecmp("smb://",unc_name
,6) == 0) {
239 return parse_cifs_url(unc_name
+6);
243 /* BB add code to find DFS root here */
244 printf("\nMounting the DFS root for domain not implemented yet");
247 /* BB add support for \\\\ not just // */
248 if(strncmp(unc_name
,"//",2) && strncmp(unc_name
,"\\\\",2)) {
249 printf("mount error: improperly formatted UNC name.");
250 printf(" %s does not begin with \\\\ or //\n",unc_name
);
257 if ((share
= strchr(unc_name
, '/')) ||
258 (share
= strchr(unc_name
,'\\'))) {
259 *share
= 0; /* temporarily terminate the string */
261 host_entry
= gethostbyname(unc_name
);
262 *(share
- 1) = '/'; /* put the slash back */
263 /* rc = getipnodebyname(unc_name, AF_INET, AT_ADDRCONFIG ,&rc);*/
264 if(host_entry
== NULL
) {
265 printf("mount error: could not find target server. TCP name %s not found ", unc_name
);
266 printf(" rc = %d\n",rc
);
270 /* BB should we pass an alternate version of the share name as Unicode */
271 /* BB what about ipv6? BB */
272 /* BB add retries with alternate servers in list */
274 memcpy(&server_ipaddr
.s_addr
, host_entry
->h_addr
, 4);
276 ipaddress_string
= inet_ntoa(server_ipaddr
);
277 if(ipaddress_string
== NULL
) {
278 printf("mount error: could not get valid ip address for target server\n");
281 return ipaddress_string
;
284 /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */
285 printf("Mounting the DFS root for a particular server not implemented yet\n");
292 static struct option longopts
[] = {
293 { "all", 0, 0, 'a' },
294 { "help", 0, 0, 'h' },
295 { "read-only", 0, 0, 'r' },
297 { "verbose", 0, 0, 'v' },
298 { "version", 0, 0, 'V' },
299 { "read-write", 0, 0, 'w' },
301 { "options", 1, 0, 'o' },
302 { "types", 1, 0, 't' },
303 { "replace", 0, 0, 129 },
304 { "after", 0, 0, 130 },
305 { "before", 0, 0, 131 },
306 { "over", 0, 0, 132 },
307 { "move", 0, 0, 133 },
308 { "rsize",1, 0, 136 },
309 { "wsize",1, 0, 137 },
314 { "username",1,0,140},
317 { "password",1,0,142},
321 int main(int argc
, char ** argv
)
324 int flags
= MS_MANDLOCK
| MS_MGC_VAL
;
325 char * orgoptions
= NULL
;
326 char * share_name
= NULL
;
327 char * domain_name
= NULL
;
328 char * ipaddr
= NULL
;
341 struct utsname sysinfo
;
342 struct mntent mountent
;
345 /* setlocale(LC_ALL, "");
346 bindtextdomain(PACKAGE, LOCALEDIR);
347 textdomain(PACKAGE); */
350 thisprogram
= argv
[0];
352 if(thisprogram
== NULL
)
353 thisprogram
= "mount.cifs";
356 /* BB add workstation name and domain and pass down */
358 printf(" node: %s machine: %s\n", sysinfo.nodename,sysinfo.machine);
362 share_name
= argv
[1];
363 mountpoint
= argv
[2];
364 /* add sharename in opts string as unc= parm */
366 while ((c
= getopt_long (argc
, argv
, "afFhilL:no:O:rsU:vVwt:",
367 longopts
, NULL
)) != -1) {
382 external_allowed = 0;
385 list_with_volumelabel = 1;
388 volumelabel = optarg;
395 orgoptions
= strcat(orgoptions
, ",");
396 orgoptions
= strcat(orgoptions
,optarg
);
398 orgoptions
= strdup(optarg
);
403 test_opts = xstrconcat3(test_opts, ",", optarg);
405 test_opts = xstrdup(optarg);
407 case 'r': /* mount readonly */
417 printf ("mount: %s\n", version);
429 mounttype = MS_REPLACE;
432 mounttype = MS_AFTER;
435 mounttype = MS_BEFORE;
444 mounttype = (MS_BIND | MS_REC);
447 rsize
= atoi(optarg
) ;
450 wsize
= atoi(optarg
);
463 domain_name
= optarg
;
467 mountpassword
= optarg
;
475 /* canonicalize the path in argv[1]? */
477 /* BB save off path and pop after mount returns */
478 if(chdir(mountpoint
)) {
479 printf("mount error: can not change directory into mount target %s\n",mountpoint
);
482 if(stat (mountpoint
, &statbuf
)) {
483 printf("mount error: mount point %s does not exist\n",mountpoint
);
487 if (S_ISDIR(statbuf
.st_mode
) == 0) {
488 printf("mount error: mount point %s is not a directory\n",mountpoint
);
492 if((getuid() != 0) && (geteuid() == 0)) {
493 if((statbuf
.st_uid
== getuid()) && (S_IRWXU
== statbuf
.st_mode
& S_IRWXU
)) {
494 printf("setuid mount allowed\n");
496 printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n");
501 ipaddr
= parse_server(share_name
);
502 /* if(share_name == NULL)
504 if (orgoptions
&& parse_options(strdup(orgoptions
)))
508 user_name
= getusername();
510 /* check username for user%password format */
512 if(got_password
== 0) {
513 if (getenv("PASSWD")) {
514 mountpassword
= malloc(33);
516 strncpy(mountpassword
,getenv("PASSWD"),32);
519 /* } else if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
521 got_password = 1;*/ /* BB add missing function */
523 mountpassword
= getpass("Password: "); /* BB obsolete */
527 /* FIXME launch daemon (handles dfs name resolution and credential change)
528 remember to clear parms and overwrite password field before launching */
530 optlen
= strlen(orgoptions
);
534 optlen
+= strlen(share_name
) + 4;
536 optlen
+= strlen(user_name
) + 6;
538 optlen
+= strlen(ipaddr
) + 4;
540 optlen
+= strlen(mountpassword
) + 6;
541 options
= malloc(optlen
+ 10);
544 strncat(options
,"unc=",4);
545 strcat(options
,share_name
);
546 /* scan backwards and reverse direction of slash */
547 temp
= strrchr(options
, '/');
548 if(temp
> options
+ 6)
551 strncat(options
,",ip=",4);
552 strcat(options
,ipaddr
);
555 strncat(options
,",user=",6);
556 strcat(options
,user_name
);
559 strncat(options
,",pass=",6);
560 strcat(options
,mountpassword
);
562 strncat(options
,",ver=",5);
563 strcat(options
,MOUNT_CIFS_VERSION
);
567 strcat(options
,orgoptions
);
569 /* printf("\noptions %s \n",options);*/
570 if(mount(share_name
, mountpoint
, "cifs", flags
, options
)) {
571 /* remember to kill daemon on error */
574 printf("mount failed but no error number set\n");
577 printf("mount error: cifs filesystem not supported by the system\n");
580 printf("mount error %d = %s\n",errno
,strerror(errno
));
582 printf("Refer to the mount.cifs(8) manual page (e.g.man mount.cifs)\n");
585 pmntfile
= setmntent(MOUNTED
, "a+");
587 mountent
.mnt_fsname
= share_name
;
588 mountent
.mnt_dir
= mountpoint
;
589 mountent
.mnt_type
= "cifs";
590 mountent
.mnt_opts
= "";
591 mountent
.mnt_freq
= 0;
592 mountent
.mnt_passno
= 0;
593 rc
= addmntent(pmntfile
,&mountent
);
596 printf("could not update mount table\n");