From 3aa147f8d20bc5124dca1f4746da678a55247b82 Mon Sep 17 00:00:00 2001 From: Jeff Layton Date: Thu, 24 Sep 2009 20:55:43 -0400 Subject: mount.cifs: print output to stderr When a mount fails, mount.cifs often prints an error message. In most cases, this error goes to stdout instead of stderr like it should. Fix it to print errors to stderr instead. Reported-by: Jan Engelhardt Signed-off-by: Jeff Layton --- client/mount.cifs.c | 209 +++++++++++++++++++++++++--------------------------- 1 file changed, 101 insertions(+), 108 deletions(-) (limited to 'client') diff --git a/client/mount.cifs.c b/client/mount.cifs.c index 4387f5945a..1c04e13c8d 100644 --- a/client/mount.cifs.c +++ b/client/mount.cifs.c @@ -273,31 +273,34 @@ BB end finish BB */ static char * check_for_domain(char **); -static void mount_cifs_usage(void) +static void mount_cifs_usage(FILE *stream) { - printf("\nUsage: %s -o \n", thisprogram); - printf("\nMount the remote target, specified as a UNC name,"); - printf(" to a local directory.\n\nOptions:\n"); - printf("\tuser=\n\tpass=\n\tdom=\n"); - printf("\nLess commonly used options:"); - printf("\n\tcredentials=,guest,perm,noperm,setuids,nosetuids,rw,ro,"); - printf("\n\tsep=,iocharset=,suid,nosuid,exec,noexec,serverino,"); - printf("\n\tmapchars,nomapchars,nolock,servernetbiosname="); - printf("\n\tdirectio,nounix,cifsacl,sec=,sign"); - printf("\n\nOptions not needed for servers supporting CIFS Unix extensions"); - printf("\n\t(e.g. unneeded for mounts to most Samba versions):"); - printf("\n\tuid=,gid=,dir_mode=,file_mode=,sfu"); - printf("\n\nRarely used options:"); - printf("\n\tport=,rsize=,wsize=,unc=,ip=,"); - printf("\n\tdev,nodev,nouser_xattr,netbiosname=,hard,soft,intr,"); - printf("\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=,nobrl"); - printf("\n\tin6_addr"); - printf("\n\nOptions are described in more detail in the manual page"); - printf("\n\tman 8 mount.cifs\n"); - printf("\nTo display the version number of the mount helper:"); - printf("\n\t%s -V\n",thisprogram); + fprintf(stream, "\nUsage: %s -o \n", thisprogram); + fprintf(stream, "\nMount the remote target, specified as a UNC name,"); + fprintf(stream, " to a local directory.\n\nOptions:\n"); + fprintf(stream, "\tuser=\n\tpass=\n\tdom=\n"); + fprintf(stream, "\nLess commonly used options:"); + fprintf(stream, "\n\tcredentials=,guest,perm,noperm,setuids,nosetuids,rw,ro,"); + fprintf(stream, "\n\tsep=,iocharset=,suid,nosuid,exec,noexec,serverino,"); + fprintf(stream, "\n\tmapchars,nomapchars,nolock,servernetbiosname="); + fprintf(stream, "\n\tdirectio,nounix,cifsacl,sec=,sign"); + fprintf(stream, "\n\nOptions not needed for servers supporting CIFS Unix extensions"); + fprintf(stream, "\n\t(e.g. unneeded for mounts to most Samba versions):"); + fprintf(stream, "\n\tuid=,gid=,dir_mode=,file_mode=,sfu"); + fprintf(stream, "\n\nRarely used options:"); + fprintf(stream, "\n\tport=,rsize=,wsize=,unc=,ip=,"); + fprintf(stream, "\n\tdev,nodev,nouser_xattr,netbiosname=,hard,soft,intr,"); + fprintf(stream, "\n\tnointr,ignorecase,noposixpaths,noacl,prefixpath=,nobrl"); + fprintf(stream, "\n\nOptions are described in more detail in the manual page"); + fprintf(stream, "\n\tman 8 mount.cifs\n"); + fprintf(stream, "\nTo display the version number of the mount helper:"); + fprintf(stream, "\n\t%s -V\n",thisprogram); SAFE_FREE(mountpassword); + + if (stream == stderr) + exit(EX_USAGE); + exit(0); } /* caller frees username if necessary */ @@ -348,7 +351,7 @@ static int open_cred_file(char * file_name) } } if(length > 4086) { - printf("mount.cifs failed due to malformed username in credentials file"); + fprintf(stderr, "mount.cifs failed due to malformed username in credentials file\n"); memset(line_buf,0,4096); exit(EX_USAGE); } else { @@ -372,7 +375,7 @@ static int open_cred_file(char * file_name) } } if(length > MOUNT_PASSWD_SIZE) { - printf("mount.cifs failed: password in credentials file too long\n"); + fprintf(stderr, "mount.cifs failed: password in credentials file too long\n"); memset(line_buf,0, 4096); exit(EX_USAGE); } else { @@ -392,7 +395,7 @@ static int open_cred_file(char * file_name) /* go past equals sign */ temp_val++; if(verboseflag) - printf("\nDomain %s\n",temp_val); + fprintf(stderr, "\nDomain %s\n",temp_val); for(length = 0;length DOMAIN_SIZE) { - printf("mount.cifs failed: domain in credentials file too long\n"); + fprintf(stderr, "mount.cifs failed: domain in credentials file too long\n"); exit(EX_USAGE); } else { if(domain_name == NULL) { @@ -434,14 +437,14 @@ static int get_password_from_file(int file_descript, char * filename) memset(mountpassword, 0, MOUNT_PASSWD_SIZE); if (mountpassword == NULL) { - printf("malloc failed\n"); + fprintf(stderr, "malloc failed\n"); exit(EX_SYSERR); } if(filename != NULL) { file_descript = open(filename, O_RDONLY); if(file_descript < 0) { - printf("mount.cifs failed. %s attempting to open password file %s\n", + fprintf(stderr, "mount.cifs failed. %s attempting to open password file %s\n", strerror(errno),filename); exit(EX_SYSERR); } @@ -451,14 +454,14 @@ static int get_password_from_file(int file_descript, char * filename) for(i=0;ipw_uid); @@ -704,7 +707,7 @@ static int parse_options(char ** optionsp, unsigned long * filesys_flags) struct group *gr; if (!(gr = getgrnam(value))) { - printf("bad group name \"%s\"\n", value); + fprintf(stderr, "bad group name \"%s\"\n", value); exit(EX_USAGE); } snprintf(group, sizeof(group), "%u", gr->gr_gid); @@ -716,32 +719,32 @@ static int parse_options(char ** optionsp, unsigned long * filesys_flags) /* fmask and dmask synonyms for people used to smbfs syntax */ } else if (strcmp(data, "file_mode") == 0 || strcmp(data, "fmask")==0) { if (!value || !*value) { - printf ("Option '%s' requires a numerical argument\n", data); + fprintf(stderr, "Option '%s' requires a numerical argument\n", data); SAFE_FREE(out); return 1; } if (value[0] != '0') { - printf ("WARNING: '%s' not expressed in octal.\n", data); + fprintf(stderr, "WARNING: '%s' not expressed in octal.\n", data); } if (strcmp (data, "fmask") == 0) { - printf ("WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n"); + fprintf(stderr, "WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.\n"); data = "file_mode"; /* BB fix this */ } } else if (strcmp(data, "dir_mode") == 0 || strcmp(data, "dmask")==0) { if (!value || !*value) { - printf ("Option '%s' requires a numerical argument\n", data); + fprintf(stderr, "Option '%s' requires a numerical argument\n", data); SAFE_FREE(out); return 1; } if (value[0] != '0') { - printf ("WARNING: '%s' not expressed in octal.\n", data); + fprintf(stderr, "WARNING: '%s' not expressed in octal.\n", data); } if (strcmp (data, "dmask") == 0) { - printf ("WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n"); + fprintf(stderr, "WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead.\n"); data = "dir_mode"; } /* the following eight mount options should be @@ -794,7 +797,7 @@ static int parse_options(char ** optionsp, unsigned long * filesys_flags) } } else if (strnicmp(data, "version", 3) == 0) { } else { - printf("CIFS: Unknown mount option %s\n",data); + fprintf(stderr, "CIFS: Unknown mount option %s\n",data); } */ /* nothing to do on those four mount options above. Just pass to kernel and ignore them here */ @@ -888,7 +891,7 @@ static void check_for_comma(char ** ppasswrd) return; if(number_of_commas > MOUNT_PASSWD_SIZE) { /* would otherwise overflow the mount options buffer */ - printf("\nInvalid password. Password contains too many commas.\n"); + fprintf(stderr, "\nInvalid password. Password contains too many commas.\n"); return; } @@ -942,7 +945,7 @@ static char * check_for_domain(char **ppuser) } if(got_domain) { - printf("Domain name specified twice. Username probably malformed\n"); + fprintf(stderr, "Domain name specified twice. Username probably malformed\n"); return NULL; } @@ -951,7 +954,7 @@ static char * check_for_domain(char **ppuser) if (domainnm[0] != 0) { got_domain = 1; } else { - printf("null domain\n"); + fprintf(stderr, "null domain\n"); } len = strlen(domainnm); /* reset domainm to new buffer, and copy @@ -1010,18 +1013,18 @@ parse_server(char ** punc_name) int rc; if(length > (MAX_UNC_LEN - 1)) { - printf("mount error: UNC name too long"); + fprintf(stderr, "mount error: UNC name too long"); return NULL; } if ((strncasecmp("cifs://", unc_name, 7) == 0) || (strncasecmp("smb://", unc_name, 6) == 0)) { - printf("\nMounting cifs URL not implemented yet. Attempt to mount %s\n", unc_name); + fprintf(stderr, "\nMounting cifs URL not implemented yet. Attempt to mount %s\n", unc_name); return NULL; } if(length < 3) { /* BB add code to find DFS root here */ - printf("\nMounting the DFS root for domain not implemented yet\n"); + fprintf(stderr, "\nMounting the DFS root for domain not implemented yet\n"); return NULL; } else { if(strncmp(unc_name,"//",2) && strncmp(unc_name,"\\\\",2)) { @@ -1042,8 +1045,8 @@ parse_server(char ** punc_name) unc_name[length+2] = 0; goto continue_unc_parsing; } else { - printf("mount error: improperly formatted UNC name."); - printf(" %s does not begin with \\\\ or //\n",unc_name); + fprintf(stderr, "mount error: improperly formatted UNC name."); + fprintf(stderr, " %s does not begin with \\\\ or //\n",unc_name); return NULL; } } else { @@ -1059,7 +1062,7 @@ continue_unc_parsing: if(got_ip == 0) { rc = getaddrinfo(unc_name, NULL, NULL, &addrlist); if (rc != 0) { - printf("mount error: could not resolve address for %s: %s\n", + fprintf(stderr, "mount error: could not resolve address for %s: %s\n", unc_name, gai_strerror(rc)); addrlist = NULL; } @@ -1074,7 +1077,7 @@ continue_unc_parsing: } if(got_ip) { if(verboseflag) - printf("ip address specified explicitly\n"); + fprintf(stderr, "ip address specified explicitly\n"); return NULL; } /* BB should we pass an alternate version of the share name as Unicode */ @@ -1082,7 +1085,7 @@ continue_unc_parsing: return addrlist; } else { /* BB add code to find DFS root (send null path on get DFS Referral to specified server here */ - printf("Mounting the DFS root for a particular server not implemented yet\n"); + fprintf(stderr, "Mounting the DFS root for a particular server not implemented yet\n"); return NULL; } } @@ -1181,12 +1184,10 @@ int main(int argc, char ** argv) bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); */ - if(argc && argv) { + if(argc && argv) thisprogram = argv[0]; - } else { - mount_cifs_usage(); - exit(EX_USAGE); - } + else + mount_cifs_usage(stderr); if(thisprogram == NULL) thisprogram = "mount.cifs"; @@ -1195,7 +1196,7 @@ int main(int argc, char ** argv) /* BB add workstation name and domain and pass down */ /* #ifdef _GNU_SOURCE - printf(" node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname); + fprintf(stderr, " node: %s machine: %s sysname %s domain %s\n", sysinfo.nodename,sysinfo.machine,sysinfo.sysname,sysinfo.domainname); #endif */ if(argc > 2) { dev_name = argv[1]; @@ -1216,16 +1217,11 @@ int main(int argc, char ** argv) if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "--help") == 0)) - { - mount_cifs_usage(); - exit(0); - } + mount_cifs_usage(stdout); - mount_cifs_usage(); - exit(EX_USAGE); + mount_cifs_usage(stderr); } else { - mount_cifs_usage(); - exit(EX_USAGE); + mount_cifs_usage(stderr); } @@ -1246,8 +1242,7 @@ int main(int argc, char ** argv) case '?': case 'h': /* help */ - mount_cifs_usage (); - exit(0); + mount_cifs_usage(stdout); case 'n': ++nomtab; break; @@ -1297,14 +1292,14 @@ int main(int argc, char ** argv) uid = strtoul(optarg, &ep, 10); if (*ep) { - printf("bad uid value \"%s\"\n", optarg); + fprintf(stderr, "bad uid value \"%s\"\n", optarg); exit(EX_USAGE); } } else { struct passwd *pw; if (!(pw = getpwnam(optarg))) { - printf("bad user name \"%s\"\n", optarg); + fprintf(stderr, "bad user name \"%s\"\n", optarg); exit(EX_USAGE); } uid = pw->pw_uid; @@ -1317,14 +1312,14 @@ int main(int argc, char ** argv) gid = strtoul(optarg, &ep, 10); if (*ep) { - printf("bad gid value \"%s\"\n", optarg); + fprintf(stderr, "bad gid value \"%s\"\n", optarg); exit(EX_USAGE); } } else { struct group *gr; if (!(gr = getgrnam(optarg))) { - printf("bad user name \"%s\"\n", optarg); + fprintf(stderr, "bad user name \"%s\"\n", optarg); exit(EX_USAGE); } gid = gr->gr_gid; @@ -1356,15 +1351,13 @@ int main(int argc, char ** argv) ++fakemnt; break; default: - printf("unknown mount option %c\n",c); - mount_cifs_usage(); - exit(EX_USAGE); + fprintf(stderr, "unknown mount option %c\n",c); + mount_cifs_usage(stderr); } } if((argc < 3) || (dev_name == NULL) || (mountpoint == NULL)) { - mount_cifs_usage(); - exit(EX_USAGE); + mount_cifs_usage(stderr); } /* make sure mountpoint is legit */ @@ -1423,7 +1416,7 @@ int main(int argc, char ** argv) addrhead = addr = parse_server(&share_name); if((addrhead == NULL) && (got_ip == 0)) { - printf("No ip address specified and hostname not found\n"); + fprintf(stderr, "No ip address specified and hostname not found\n"); rc = EX_USAGE; goto mount_exit; } @@ -1453,7 +1446,7 @@ int main(int argc, char ** argv) no good replacement yet. */ mountpassword = (char *)calloc(MOUNT_PASSWD_SIZE+1,1); if (!tmp_pass || !mountpassword) { - printf("Password not entered, exiting\n"); + fprintf(stderr, "Password not entered, exiting\n"); exit(EX_USAGE); } strlcpy(mountpassword, tmp_pass, MOUNT_PASSWD_SIZE+1); @@ -1469,8 +1462,8 @@ int main(int argc, char ** argv) if(share_name) optlen += strlen(share_name) + 4; else { - printf("No server share name specified\n"); - printf("\nMounting the DFS root for server not implemented yet\n"); + fprintf(stderr, "No server share name specified\n"); + fprintf(stderr, "\nMounting the DFS root for server not implemented yet\n"); exit(EX_USAGE); } if(user_name) @@ -1484,7 +1477,7 @@ mount_retry: options = (char *)malloc(options_size /* space for commas in password */ + 8 /* space for domain= , domain name itself was counted as part of the length username string above */); if(options == NULL) { - printf("Could not allocate memory for mount options\n"); + fprintf(stderr, "Could not allocate memory for mount options\n"); exit(EX_SYSERR); } @@ -1530,7 +1523,7 @@ mount_retry: strlcat(options,prefixpath,options_size); /* no need to cat the / */ } if(verboseflag) - printf("\nmount.cifs kernel mount options %s \n",options); + fprintf(stderr, "\nmount.cifs kernel mount options %s \n",options); /* convert all '\\' to '/' in share portion so that /proc/mounts looks pretty */ replace_char(dev_name, '\\', '/', strlen(share_name)); @@ -1583,7 +1576,7 @@ mount_retry: } break; case ENODEV: - printf("mount error: cifs filesystem not supported by the system\n"); + fprintf(stderr, "mount error: cifs filesystem not supported by the system\n"); break; case ENXIO: if(retry == 0) { @@ -1591,13 +1584,13 @@ mount_retry: if (uppercase_string(dev_name) && uppercase_string(share_name) && uppercase_string(prefixpath)) { - printf("retrying with upper case share name\n"); + fprintf(stderr, "retrying with upper case share name\n"); goto mount_retry; } } } - printf("mount error(%d): %s\n", errno, strerror(errno)); - printf("Refer to the mount.cifs(8) manual page (e.g. man " + fprintf(stderr, "mount error(%d): %s\n", errno, strerror(errno)); + fprintf(stderr, "Refer to the mount.cifs(8) manual page (e.g. man " "mount.cifs)\n"); rc = EX_FAIL; goto mount_exit; @@ -1608,12 +1601,12 @@ mount_retry: atexit(unlock_mtab); rc = lock_mtab(); if (rc) { - printf("cannot lock mtab"); + fprintf(stderr, "cannot lock mtab"); goto mount_exit; } pmntfile = setmntent(MOUNTED, "a+"); if (!pmntfile) { - printf("could not update mount table\n"); + fprintf(stderr, "could not update mount table\n"); unlock_mtab(); rc = EX_FILEIO; goto mount_exit; -- cgit