Apply the changes that Derrell Lipman supplied ...
[Samba/gebeck_regimport.git] / source3 / client / mount.cifs.c
blob43b20e9d502cdbd00002065acfeb62a5c752ea1a
1 #define _GNU_SOURCE
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <pwd.h>
6 #include <sys/types.h>
7 #include <sys/mount.h>
8 #include <sys/stat.h>
9 #include <sys/utsname.h>
10 #include <sys/socket.h>
11 #include <arpa/inet.h>
12 #include <getopt.h>
13 #include <errno.h>
14 #include <netdb.h>
15 #include <string.h>
16 #include <mntent.h>
18 #define MOUNT_CIFS_VERSION "1"
20 extern char *getusername(void);
22 char * thisprogram;
23 int verboseflag = 0;
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 /* BB finish BB
37 cifs_umount
38 open nofollow - avoid symlink exposure?
39 get owner of dir see if matches self or if root
40 call system(umount argv) etc.
42 BB end finish BB */
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");
50 exit(1);
53 /* caller frees username if necessary */
54 char * getusername() {
55 char *username = NULL;
56 struct passwd *password = getpwuid(getuid());
58 if (password) {
59 username = password->pw_name;
61 return username;
64 char * parse_cifs_url(unc_name)
66 printf("\ncifs url %s\n",unc_name);
69 int parse_options(char * options)
71 char * data;
72 char * value = 0;
74 if (!options)
75 return 1;
77 while ((data = strsep(&options, ",")) != NULL) {
78 if (!*data)
79 continue;
80 if ((value = strchr(data, '=')) != NULL) {
81 *value++ = '\0';
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) {
89 got_user=1;
90 /* BB add check for format user%pass */
91 /* if(strchr(username%passw) got_password = 1) */
92 } else {
93 printf("username too long\n");
94 return 1;
96 } else if (strncmp(data, "pass", 4) == 0) {
97 if (!value || !*value) {
98 if(got_password) {
99 printf("password specified twice, ignoring second\n");
100 } else
101 got_password = 1;
102 } else if (strnlen(value, 17) < 17) {
103 got_password = 1;
104 } else {
105 printf("password too long\n");
106 return 1;
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) {
112 got_ip = 1;
113 } else {
114 printf("ip address too long\n");
115 return 1;
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) {
128 got_unc = 1;
129 if (strncmp(value, "//", 2) == 0) {
130 if(got_unc)
131 printf("unc name specified twice, ignoring second\n");
132 else
133 got_unc = 1;
134 } else if (strncmp(value, "\\\\", 2) != 0) {
135 printf("UNC Path does not begin with // or \\\\ \n");
136 return 1;
137 } else {
138 if(got_unc)
139 printf("unc name specified twice, ignoring second\n");
140 else
141 got_unc = 1;
143 } else {
144 printf("CIFS: UNC name too long\n");
145 return 1;
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) {
154 got_domain = 1;
155 } else {
156 printf("domain name too long\n");
157 return 1;
159 } else if (strncmp(data, "uid", 3) == 0) {
160 if (value && *value) {
161 got_uid = 1;
163 } else if (strncmp(data, "gid", 3) == 0) {
164 if (value && *value) {
165 got_gid = 1;
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);
171 return 1;
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");
180 data = "file_mode";
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);
185 return 1;
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");
194 data = "dir_mode";
196 } /* else if (strnicmp(data, "port", 4) == 0) {
197 if (value && *value) {
198 vol->port =
199 simple_strtoul(value, &value, 0);
201 } else if (strnicmp(data, "rsize", 5) == 0) {
202 if (value && *value) {
203 vol->rsize =
204 simple_strtoul(value, &value, 0);
206 } else if (strnicmp(data, "wsize", 5) == 0) {
207 if (value && *value) {
208 vol->wsize =
209 simple_strtoul(value, &value, 0);
211 } else if (strnicmp(data, "version", 3) == 0) {
213 } else if (strnicmp(data, "rw", 2) == 0) {
215 } else
216 printf("CIFS: Unknown mount option %s\n",data); */
218 return 0;
221 /* Note that caller frees the returned buffer if necessary */
222 char * parse_server(char * unc_name)
224 int length = strnlen(unc_name,1024);
225 char * share;
226 char * ipaddress_string = NULL;
227 struct hostent * host_entry;
228 struct in_addr server_ipaddr;
229 int rc,j;
230 char temp[64];
232 if(length > 1023) {
233 printf("mount error: UNC name too long");
234 return 0;
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);
242 if(length < 3) {
243 /* BB add code to find DFS root here */
244 printf("\nMounting the DFS root for domain not implemented yet");
245 return 0;
246 } else {
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);
251 return 0;
252 } else {
253 unc_name[0] = '\\';
254 unc_name[0] = '/';
255 unc_name[1] = '/';
256 unc_name += 2;
257 if ((share = strchr(unc_name, '/')) ||
258 (share = strchr(unc_name,'\\'))) {
259 *share = 0; /* temporarily terminate the string */
260 share += 1;
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);
267 return 0;
269 else {
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");
279 return 0;
281 return ipaddress_string;
283 } else {
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");
286 return 0;
292 static struct option longopts[] = {
293 { "all", 0, 0, 'a' },
294 { "help", 0, 0, 'h' },
295 { "read-only", 0, 0, 'r' },
296 { "ro", 0, 0, 'r' },
297 { "verbose", 0, 0, 'v' },
298 { "version", 0, 0, 'V' },
299 { "read-write", 0, 0, 'w' },
300 { "rw", 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 },
310 { "uid", 1, 0, 138},
311 { "gid", 1, 0, 139},
312 { "uuid",1,0,'U' },
313 { "user",1,0,140},
314 { "username",1,0,140},
315 { "dom",1,0,141},
316 { "domain",1,0,141},
317 { "password",1,0,142},
318 { NULL, 0, 0, 0 }
321 int main(int argc, char ** argv)
323 int c;
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;
329 char * uuid = NULL;
330 char * mountpoint;
331 char * options;
332 char * temp;
333 int rc,i;
334 int rsize = 0;
335 int wsize = 0;
336 int nomtab = 0;
337 int uid = 0;
338 int gid = 0;
339 int optlen = 0;
340 struct stat statbuf;
341 struct utsname sysinfo;
342 struct mntent mountent;
343 FILE * pmntfile;
345 /* setlocale(LC_ALL, "");
346 bindtextdomain(PACKAGE, LOCALEDIR);
347 textdomain(PACKAGE); */
349 if(argc && argv) {
350 thisprogram = argv[0];
352 if(thisprogram == NULL)
353 thisprogram = "mount.cifs";
355 uname(&sysinfo);
356 /* BB add workstation name and domain and pass down */
357 /*#ifdef _GNU_SOURCE
358 printf(" node: %s machine: %s\n", sysinfo.nodename,sysinfo.machine);
359 #endif*/
360 if(argc < 3)
361 mount_cifs_usage();
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) {
368 switch (c) {
369 /* case 'a':
370 ++mount_all;
371 break;
372 case 'f':
373 ++fake;
374 break;
375 case 'F':
376 ++optfork;
377 break; */
378 case 'h': /* help */
379 mount_cifs_usage ();
380 break;
381 /* case 'i':
382 external_allowed = 0;
383 break;
384 case 'l':
385 list_with_volumelabel = 1;
386 break;
387 case 'L':
388 volumelabel = optarg;
389 break; */
390 case 'n':
391 ++nomtab;
392 break;
393 case 'o':
394 if (orgoptions) {
395 orgoptions = strcat(orgoptions, ",");
396 orgoptions = strcat(orgoptions,optarg);
397 } else
398 orgoptions = strdup(optarg);
399 break;
401 /* case 'O':
402 if (test_opts)
403 test_opts = xstrconcat3(test_opts, ",", optarg);
404 else
405 test_opts = xstrdup(optarg);
406 break;*/
407 case 'r': /* mount readonly */
408 flags |= MS_RDONLY;
409 break;
410 case 'U':
411 uuid = optarg;
412 break;
413 case 'v':
414 ++verboseflag;
415 break;
416 /* case 'V':
417 printf ("mount: %s\n", version);
418 exit (0);*/
419 case 'w':
420 flags &= ~MS_RDONLY;
421 break;
422 /* case 0:
423 break;
425 case 128:
426 mounttype = MS_BIND;
427 break;
428 case 129:
429 mounttype = MS_REPLACE;
430 break;
431 case 130:
432 mounttype = MS_AFTER;
433 break;
434 case 131:
435 mounttype = MS_BEFORE;
436 break;
437 case 132:
438 mounttype = MS_OVER;
439 break;
440 case 133:
441 mounttype = MS_MOVE;
442 break;
443 case 135:
444 mounttype = (MS_BIND | MS_REC);
445 break; */
446 case 136:
447 rsize = atoi(optarg) ;
448 break;
449 case 137:
450 wsize = atoi(optarg);
451 break;
452 case 138:
453 uid = atoi(optarg);
454 break;
455 case 139:
456 gid = atoi(optarg);
457 break;
458 case 140:
459 got_user = 1;
460 user_name = optarg;
461 break;
462 case 141:
463 domain_name = optarg;
464 break;
465 case 142:
466 got_password = 1;
467 mountpassword = optarg;
468 break;
469 case '?':
470 default:
471 mount_cifs_usage ();
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);
484 return -1;
487 if (S_ISDIR(statbuf.st_mode) == 0) {
488 printf("mount error: mount point %s is not a directory\n",mountpoint);
489 return -1;
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");
495 } else {
496 printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n");
497 return -1;
501 ipaddr = parse_server(share_name);
502 /* if(share_name == NULL)
503 return 1; */
504 if (orgoptions && parse_options(strdup(orgoptions)))
505 return 1;
507 if(got_user == 0)
508 user_name = getusername();
510 /* check username for user%password format */
512 if(got_password == 0) {
513 if (getenv("PASSWD")) {
514 mountpassword = malloc(33);
515 if(mountpassword) {
516 strncpy(mountpassword,getenv("PASSWD"),32);
517 got_password = 1;
519 /* } else if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
520 get_password_file();
521 got_password = 1;*/ /* BB add missing function */
522 } else {
523 mountpassword = getpass("Password: "); /* BB obsolete */
524 got_password = 1;
527 /* FIXME launch daemon (handles dfs name resolution and credential change)
528 remember to clear parms and overwrite password field before launching */
529 if(orgoptions) {
530 optlen = strlen(orgoptions);
531 } else
532 optlen = 0;
533 if(share_name)
534 optlen += strlen(share_name) + 4;
535 if(user_name)
536 optlen += strlen(user_name) + 6;
537 if(ipaddr)
538 optlen += strlen(ipaddr) + 4;
539 if(mountpassword)
540 optlen += strlen(mountpassword) + 6;
541 options = malloc(optlen + 10);
543 options[0] = 0;
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)
549 *temp = '\\';
550 if(ipaddr) {
551 strncat(options,",ip=",4);
552 strcat(options,ipaddr);
554 if(user_name) {
555 strncat(options,",user=",6);
556 strcat(options,user_name);
558 if(mountpassword) {
559 strncat(options,",pass=",6);
560 strcat(options,mountpassword);
562 strncat(options,",ver=",5);
563 strcat(options,MOUNT_CIFS_VERSION);
565 if(orgoptions) {
566 strcat(options,",");
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 */
572 switch (errno) {
573 case 0:
574 printf("mount failed but no error number set\n");
575 return 0;
576 case ENODEV:
577 printf("mount error: cifs filesystem not supported by the system\n");
578 break;
579 default:
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");
583 return -1;
584 } else {
585 pmntfile = setmntent(MOUNTED, "a+");
586 if(pmntfile) {
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);
594 endmntent(pmntfile);
595 } else {
596 printf("could not update mount table\n");
599 return 0;