mount.cifs: make local versions of strlcat and strlcpy static
[sfrench/samba-autobuild/.git] / source3 / client / mount.cifs.c
index 7432dac5be27cc94a4947170c3f8f23ab7ff1efe..3b56e5f861a6a2cbb05721ed17b982f0b9e80b75 100644 (file)
@@ -94,7 +94,7 @@ char * prefixpath = NULL;
 
 /* like strncpy but does not 0 fill the buffer and always null
  *    terminates. bufsize is the size of the destination buffer */
-size_t strlcpy(char *d, const char *s, size_t bufsize)
+static size_t strlcpy(char *d, const char *s, size_t bufsize)
 {
        size_t len = strlen(s);
        size_t ret = len;
@@ -108,13 +108,16 @@ size_t strlcpy(char *d, const char *s, size_t bufsize)
 /* like strncat but does not 0 fill the buffer and always null
  *    terminates. bufsize is the length of the buffer, which should
  *       be one more than the maximum resulting string length */
-size_t strlcat(char *d, const char *s, size_t bufsize)
+static size_t strlcat(char *d, const char *s, size_t bufsize)
 {
        size_t len1 = strlen(d);
        size_t len2 = strlen(s);
        size_t ret = len1 + len2;
 
        if (len1+len2 >= bufsize) {
+               if (bufsize < (len1+1)) {
+                       return ret;
+               }
                len2 = bufsize - (len1+1);
        }
        if (len2 > 0) {
@@ -160,9 +163,6 @@ static void mount_cifs_usage(void)
        printf("\nTo display the version number of the mount helper:");
        printf("\n\t%s -V\n",thisprogram);
 
-       if(mountpassword) {
-               memset(mountpassword,0,MOUNT_PASSWD_SIZE);
-       }
        SAFE_FREE(mountpassword);
        exit(1);
 }
@@ -196,7 +196,7 @@ static int open_cred_file(char * file_name)
        line_buf = (char *)malloc(4096);
        if(line_buf == NULL) {
                fclose(fs);
-               return -ENOMEM;
+               return ENOMEM;
        }
 
        while(fgets(line_buf,4096,fs)) {
@@ -216,15 +216,13 @@ static int open_cred_file(char * file_name)
                                for(length = 0;length<4087;length++) {
                                        if ((temp_val[length] == '\n')
                                            || (temp_val[length] == '\0')) {
+                                               temp_val[length] = '\0';
                                                break;
                                        }
                                }
                                if(length > 4086) {
                                        printf("mount.cifs failed due to malformed username in credentials file");
                                        memset(line_buf,0,4096);
-                                       if(mountpassword) {
-                                               memset(mountpassword,0,MOUNT_PASSWD_SIZE);
-                                       }
                                        exit(1);
                                } else {
                                        got_user = 1;
@@ -242,6 +240,7 @@ static int open_cred_file(char * file_name)
                                for(length = 0;length<MOUNT_PASSWD_SIZE+1;length++) {
                                        if ((temp_val[length] == '\n')
                                            || (temp_val[length] == '\0')) {
+                                               temp_val[length] = '\0';
                                                break;
                                        }
                                }
@@ -270,6 +269,7 @@ static int open_cred_file(char * file_name)
                                 for(length = 0;length<DOMAIN_SIZE+1;length++) {
                                        if ((temp_val[length] == '\n')
                                            || (temp_val[length] == '\0')) {
+                                               temp_val[length] = '\0';
                                                break;
                                        }
                                 }
@@ -291,9 +291,6 @@ static int open_cred_file(char * file_name)
 
        }
        fclose(fs);
-       if(line_buf) {
-               memset(line_buf,0,4096);
-       }
        SAFE_FREE(line_buf);
        return 0;
 }
@@ -328,7 +325,6 @@ static int get_password_from_file(int file_descript, char * filename)
                rc = read(file_descript,&c,1);
                if(rc < 0) {
                        printf("mount.cifs failed. Error %s reading password file\n",strerror(errno));
-                       memset(mountpassword,0,MOUNT_PASSWD_SIZE);
                        if(filename != NULL)
                                close(file_descript);
                        exit(1);
@@ -340,6 +336,7 @@ static int get_password_from_file(int file_descript, char * filename)
                        break;
                } else /* read valid character */ {
                        if((c == 0) || (c == '\n')) {
+                               mountpassword[i] = '\0';
                                break;
                        } else 
                                mountpassword[i] = c;
@@ -476,7 +473,8 @@ static int parse_options(char ** optionsp, int * filesys_flags)
                        }
                } else if (strncmp(data, "sec", 3) == 0) {
                        if (value) {
-                               if (!strcmp(value, "none"))
+                               if (!strncmp(value, "none", 4) ||
+                                   !strncmp(value, "krb5", 4))
                                        got_password = 1;
                        }
                } else if (strncmp(data, "ip", 2) == 0) {
@@ -520,13 +518,16 @@ static int parse_options(char ** optionsp, int * filesys_flags)
                                printf("CIFS: UNC name too long\n");
                                return 1;
                        }
-               } else if ((strncmp(data, "domain", 3) == 0)
-                          || (strncmp(data, "workgroup", 5) == 0)) {
+               } else if ((strncmp(data, "dom" /* domain */, 3) == 0)
+                          || (strncmp(data, "workg", 5) == 0)) {
+                       /* note this allows for synonyms of "domain"
+                          such as "DOM" and "dom" and "workgroup"
+                          and "WORKGRP" etc. */
                        if (!value || !*value) {
                                printf("CIFS: invalid domain name\n");
                                return 1;       /* needs_arg; */
                        }
-                       if (strnlen(value, 65) < 65) {
+                       if (strnlen(value, DOMAIN_SIZE+1) < DOMAIN_SIZE+1) {
                                got_domain = 1;
                        } else {
                                printf("domain name too long\n");
@@ -536,7 +537,8 @@ static int parse_options(char ** optionsp, int * filesys_flags)
                        if (value && *value) {
                                rc = open_cred_file(value);
                                if(rc) {
-                                       printf("error %d opening credential file %s\n",rc, value);
+                                       printf("error %d (%s) opening credential file %s\n",
+                                               rc, strerror(rc), value);
                                        return 1;
                                }
                        } else {
@@ -1119,9 +1121,6 @@ int main(int argc, char ** argv)
                        MOUNT_CIFS_VERSION_MAJOR,
                        MOUNT_CIFS_VERSION_MINOR,
                        MOUNT_CIFS_VENDOR_SUFFIX);
-                       if(mountpassword) {
-                               memset(mountpassword,0,MOUNT_PASSWD_SIZE);
-                       }
                        exit (0);
                case 'w':
                        flags &= ~MS_RDONLY;
@@ -1209,7 +1208,7 @@ int main(int argc, char ** argv)
                if(mountpassword == NULL)
                        mountpassword = (char *)calloc(MOUNT_PASSWD_SIZE+1,1);
                if(mountpassword) {
-                       strlcpy(mountpassword,getenv("PASSWD"),MOUNT_PASSWD_SIZE);
+                       strlcpy(mountpassword,getenv("PASSWD"),MOUNT_PASSWD_SIZE+1);
                        got_password = 1;
                }
        } else if (getenv("PASSWD_FD")) {
@@ -1442,16 +1441,8 @@ mount_exit:
                SAFE_FREE(mountpassword);
        }
 
-       if(options) {
-               memset(options,0,optlen);
-               SAFE_FREE(options);
-       }
-
-       if(orgoptions) {
-               memset(orgoptions,0,orgoptlen);
-               SAFE_FREE(orgoptions);
-       }
-
+       SAFE_FREE(options);
+       SAFE_FREE(orgoptions);
        SAFE_FREE(resolved_path);
        SAFE_FREE(share_name);
        return rc;