skel_ -> cap_
[Samba/gebeck_regimport.git] / source3 / client / mount.cifs.c
blob6bd8c0f008300d070ebf7c519a3d572fbc4dbca8
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 } /* else if (strnicmp(data, "file_mode", 4) == 0) {
168 if (value && *value) {
169 vol->file_mode =
170 simple_strtoul(value, &value, 0);
172 } else if (strnicmp(data, "dir_mode", 3) == 0) {
173 if (value && *value) {
174 vol->dir_mode =
175 simple_strtoul(value, &value, 0);
177 } else if (strnicmp(data, "port", 4) == 0) {
178 if (value && *value) {
179 vol->port =
180 simple_strtoul(value, &value, 0);
182 } else if (strnicmp(data, "rsize", 5) == 0) {
183 if (value && *value) {
184 vol->rsize =
185 simple_strtoul(value, &value, 0);
187 } else if (strnicmp(data, "wsize", 5) == 0) {
188 if (value && *value) {
189 vol->wsize =
190 simple_strtoul(value, &value, 0);
192 } else if (strnicmp(data, "version", 3) == 0) {
194 } else if (strnicmp(data, "rw", 2) == 0) {
196 } else
197 printf("CIFS: Unknown mount option %s\n",data); */
199 return 0;
202 /* Note that caller frees the returned buffer if necessary */
203 char * parse_server(char * unc_name)
205 int length = strnlen(unc_name,1024);
206 char * share;
207 char * ipaddress_string = NULL;
208 struct hostent * host_entry;
209 struct in_addr server_ipaddr;
210 int rc,j;
211 char temp[64];
213 if(length > 1023) {
214 printf("mount error: UNC name too long");
215 return 0;
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);
223 if(length < 3) {
224 /* BB add code to find DFS root here */
225 printf("\nMounting the DFS root for domain not implemented yet");
226 return 0;
227 } else {
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);
232 return 0;
233 } else {
234 unc_name[0] = '\\';
235 unc_name[0] = '/';
236 unc_name[1] = '/';
237 unc_name += 2;
238 if ((share = strchr(unc_name, '/')) ||
239 (share = strchr(unc_name,'\\'))) {
240 *share = 0; /* temporarily terminate the string */
241 share += 1;
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);
248 return 0;
250 else {
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");
260 return 0;
262 return ipaddress_string;
264 } else {
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");
267 return 0;
273 static struct option longopts[] = {
274 { "all", 0, 0, 'a' },
275 { "help", 0, 0, 'h' },
276 { "read-only", 0, 0, 'r' },
277 { "ro", 0, 0, 'r' },
278 { "verbose", 0, 0, 'v' },
279 { "version", 0, 0, 'V' },
280 { "read-write", 0, 0, 'w' },
281 { "rw", 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 },
291 { "uid", 1, 0, 138},
292 { "gid", 1, 0, 139},
293 { "uuid",1,0,'U' },
294 { "user",1,0,140},
295 { "username",1,0,140},
296 { "dom",1,0,141},
297 { "domain",1,0,141},
298 { "password",1,0,142},
299 { NULL, 0, 0, 0 }
302 int main(int argc, char ** argv)
304 int c;
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;
310 char * uuid = NULL;
311 char * mountpoint;
312 char * options;
313 char * temp;
314 int rc,i;
315 int rsize = 0;
316 int wsize = 0;
317 int nomtab = 0;
318 int uid = 0;
319 int gid = 0;
320 int optlen = 0;
321 struct stat statbuf;
322 struct utsname sysinfo;
323 struct mntent mountent;
324 FILE * pmntfile;
326 /* setlocale(LC_ALL, "");
327 bindtextdomain(PACKAGE, LOCALEDIR);
328 textdomain(PACKAGE); */
330 if(argc && argv) {
331 thisprogram = argv[0];
333 if(thisprogram == NULL)
334 thisprogram = "mount.cifs";
336 uname(&sysinfo);
337 /* BB add workstation name and domain and pass down */
338 /*#ifdef _GNU_SOURCE
339 printf(" node: %s machine: %s\n", sysinfo.nodename,sysinfo.machine);
340 #endif*/
341 if(argc < 3)
342 mount_cifs_usage();
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) {
349 switch (c) {
350 /* case 'a':
351 ++mount_all;
352 break;
353 case 'f':
354 ++fake;
355 break;
356 case 'F':
357 ++optfork;
358 break; */
359 case 'h': /* help */
360 mount_cifs_usage ();
361 break;
362 /* case 'i':
363 external_allowed = 0;
364 break;
365 case 'l':
366 list_with_volumelabel = 1;
367 break;
368 case 'L':
369 volumelabel = optarg;
370 break; */
371 case 'n':
372 ++nomtab;
373 break;
374 case 'o':
375 if (orgoptions) {
376 orgoptions = strcat(orgoptions, ",");
377 orgoptions = strcat(orgoptions,optarg);
378 } else
379 orgoptions = strdup(optarg);
380 break;
382 /* case 'O':
383 if (test_opts)
384 test_opts = xstrconcat3(test_opts, ",", optarg);
385 else
386 test_opts = xstrdup(optarg);
387 break;*/
388 case 'r': /* mount readonly */
389 flags |= MS_RDONLY;
390 break;
391 case 'U':
392 uuid = optarg;
393 break;
394 case 'v':
395 ++verboseflag;
396 break;
397 /* case 'V':
398 printf ("mount: %s\n", version);
399 exit (0);*/
400 case 'w':
401 flags &= ~MS_RDONLY;
402 break;
403 /* case 0:
404 break;
406 case 128:
407 mounttype = MS_BIND;
408 break;
409 case 129:
410 mounttype = MS_REPLACE;
411 break;
412 case 130:
413 mounttype = MS_AFTER;
414 break;
415 case 131:
416 mounttype = MS_BEFORE;
417 break;
418 case 132:
419 mounttype = MS_OVER;
420 break;
421 case 133:
422 mounttype = MS_MOVE;
423 break;
424 case 135:
425 mounttype = (MS_BIND | MS_REC);
426 break; */
427 case 136:
428 rsize = atoi(optarg) ;
429 break;
430 case 137:
431 wsize = atoi(optarg);
432 break;
433 case 138:
434 uid = atoi(optarg);
435 break;
436 case 139:
437 gid = atoi(optarg);
438 break;
439 case 140:
440 got_user = 1;
441 user_name = optarg;
442 break;
443 case 141:
444 domain_name = optarg;
445 break;
446 case 142:
447 got_password = 1;
448 mountpassword = optarg;
449 break;
450 case '?':
451 default:
452 mount_cifs_usage ();
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);
465 return -1;
468 if (S_ISDIR(statbuf.st_mode) == 0) {
469 printf("mount error: mount point %s is not a directory\n",mountpoint);
470 return -1;
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");
476 } else {
477 printf("mount error: permission denied, not superuser and cifs.mount not installed SUID\n");
478 return -1;
482 ipaddr = parse_server(share_name);
483 /* if(share_name == NULL)
484 return 1; */
485 if (parse_options(strdup(orgoptions)))
486 return 1;
488 if(got_user == 0)
489 user_name = getusername();
491 /* check username for user%password format */
493 if(got_password == 0) {
494 if (getenv("PASSWD")) {
495 mountpassword = malloc(33);
496 if(mountpassword) {
497 strncpy(mountpassword,getenv("PASSWD"),32);
498 got_password = 1;
500 /* } else if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
501 get_password_file();
502 got_password = 1;*/ /* BB add missing function */
503 } else {
504 mountpassword = getpass("Password: "); /* BB obsolete */
505 got_password = 1;
508 /* FIXME launch daemon (handles dfs name resolution and credential change)
509 remember to clear parms and overwrite password field before launching */
510 if(orgoptions) {
511 optlen = strlen(orgoptions);
512 } else
513 optlen = 0;
514 if(share_name)
515 optlen += strlen(share_name) + 4;
516 if(user_name)
517 optlen += strlen(user_name) + 6;
518 if(ipaddr)
519 optlen += strlen(ipaddr) + 4;
520 if(mountpassword)
521 optlen += strlen(mountpassword) + 6;
522 options = malloc(optlen + 10);
524 options[0] = 0;
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)
530 *temp = '\\';
531 if(ipaddr) {
532 strncat(options,",ip=",4);
533 strcat(options,ipaddr);
535 if(user_name) {
536 strncat(options,",user=",6);
537 strcat(options,user_name);
539 if(mountpassword) {
540 strncat(options,",pass=",6);
541 strcat(options,mountpassword);
543 strncat(options,",ver=",5);
544 strcat(options,MOUNT_CIFS_VERSION);
546 if(orgoptions) {
547 strcat(options,",");
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 */
553 switch (errno) {
554 case 0:
555 printf("mount failed but no error number set\n");
556 return 0;
557 case ENODEV:
558 printf("mount error: cifs filesystem not supported by the system\n");
559 break;
560 default:
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");
564 return -1;
565 } else {
566 pmntfile = setmntent(MOUNTED, "a+");
567 if(pmntfile) {
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);
575 endmntent(pmntfile);
576 } else {
577 printf("could not update mount table\n");
580 return 0;