]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Removed all invocations of atoi().
authorStephan Bosch <stephan@rename-it.nl>
Sat, 29 Aug 2015 11:30:37 +0000 (14:30 +0300)
committerStephan Bosch <stephan@rename-it.nl>
Sat, 29 Aug 2015 11:30:37 +0000 (14:30 +0300)
17 files changed:
src/auth/db-ldap.c
src/auth/mech-digest-md5.c
src/auth/passdb-blocking.c
src/dict/dict-connection.c
src/director/director-connection.c
src/doveadm/doveadm-dump-index.c
src/doveadm/doveadm-who.c
src/imap-login/client.c
src/lib-dict/dict.h
src/lib-master/master-service.c
src/lib-sql/driver-mysql.c
src/lib-sql/driver-pgsql.c
src/lib-storage/mail-storage-service.c
src/lmtp/commands.c
src/login-common/client-common-auth.c
src/master/main.c
src/util/script.c

index 1476fa96126c51e59a14b495ff3eeaa7187b7f36..08a1c596f62dec36a9379d6498265d28837d72ff 100644 (file)
@@ -1103,8 +1103,7 @@ static void db_ldap_set_options(struct ldap_connection *conn)
        db_ldap_set_opt(conn, conn->ld, LDAP_OPT_DEREF, &conn->set.ldap_deref,
                        "deref", conn->set.deref);
 #ifdef LDAP_OPT_DEBUG_LEVEL
-       value = atoi(conn->set.debug_level);
-       if (value != 0) {
+       if (str_to_int(conn->set.debug_level, &value) >= 0 && value != 0) {
                db_ldap_set_opt(conn, NULL, LDAP_OPT_DEBUG_LEVEL, &value,
                                "debug_level", conn->set.debug_level);
        }
@@ -1143,10 +1142,15 @@ static void db_ldap_init_ld(struct ldap_connection *conn)
 
 int db_ldap_connect(struct ldap_connection *conn)
 {
-       bool debug = atoi(conn->set.debug_level) > 0;
+       int debug_level;
+       bool debug;
        struct timeval start, end;
        int ret;
 
+       debug = FALSE;
+       if (str_to_int(conn->set.debug_level, &debug_level) >= 0)
+               debug = debug_level > 0;
+
        if (conn->conn_state != LDAP_CONN_STATE_DISCONNECTED)
                return 0;
 
index 237817cb7c52cd689d5090abd78571b190bc7e9a..98f73265494274210543b28911596ecf8f712f3a 100644 (file)
@@ -334,12 +334,19 @@ static bool auth_handle_response(struct digest_auth_request *request,
        }
 
        if (strcmp(key, "nc") == 0) {
+               unsigned int nc;
+
                if (request->nonce_count != NULL) {
                        *error = "nonce-count must not exist more than once";
                        return FALSE;
                }
 
-               if (atoi(value) != 1) {
+               if (str_to_uint(value, &nc) < 0) {
+                       *error = "nonce-count value invalid";
+                       return FALSE;
+               }
+
+               if (nc != 1) {
                        *error = "re-auth not supported currently";
                        return FALSE;
                }
index 6cc4eb33bc0c82b5d898d648808b12de7680730f..27841302c8a1b092c044fd5dcb9b9df0036d2d84 100644 (file)
@@ -38,20 +38,25 @@ auth_worker_reply_parse(struct auth_request *request, const char *reply)
        }
 
        if (strcmp(*args, "FAIL") == 0 && args[1] != NULL) {
+               int result;
                /* FAIL \t result [\t user \t password [\t extra]] */
-               ret = atoi(args[1]);
-               if (ret == PASSDB_RESULT_OK) {
+               if (str_to_int(args[1], &result) < 0) {
                        /* shouldn't happen */
-               } else if (args[2] == NULL) {
-                       /* internal failure most likely */
-                       return ret;
-               } else if (args[3] != NULL) {
-                       if (*args[2] != '\0') {
-                               auth_request_set_field(request, "user",
-                                                      args[2], NULL);
+               } else {
+                       ret = (enum passdb_result)result;
+                       if (ret == PASSDB_RESULT_OK) {
+                               /* shouldn't happen */
+                       } else if (args[2] == NULL) {
+                               /* internal failure most likely */
+                               return ret;
+                       } else if (args[3] != NULL) {
+                               if (*args[2] != '\0') {
+                                       auth_request_set_field(request, "user",
+                                                              args[2], NULL);
+                               }
+                               auth_worker_reply_parse_args(request, args + 3);
+                               return ret;
                        }
-                       auth_worker_reply_parse_args(request, args + 3);
-                       return ret;
                }
        }
 
index 81b95de4d16f0fea239c7ae80703cc50b739cb9e..a9860cfa1c942175abf39d73935a7814f5906570 100644 (file)
@@ -21,6 +21,7 @@ static int dict_connection_parse_handshake(struct dict_connection *conn,
                                           const char *line)
 {
        const char *username, *name, *value_type;
+       unsigned int value_type_num;
 
        if (*line++ != DICT_PROTOCOL_CMD_HELLO)
                return -1;
@@ -42,7 +43,11 @@ static int dict_connection_parse_handshake(struct dict_connection *conn,
 
        if (*line++ != '\t')
                return -1;
-       conn->value_type = atoi(t_strdup_until(value_type, line - 1));
+       if (str_to_uint(t_strdup_until(value_type, line - 1), &value_type_num) < 0)
+               return -1;
+       if (value_type_num >= DICT_DATA_TYPE_LAST)
+               return -1;
+       conn->value_type = (enum dict_data_type)value_type_num;
 
        /* get username */
        username = line;
index 06714169e99df6da4c459880c8f644c681dcb232..a068fcb030554c0e49424b2d713b7fe8f87192a7 100644 (file)
@@ -44,7 +44,6 @@
 #include "user-directory.h"
 #include "director-connection.h"
 
-#include <stdlib.h>
 #include <unistd.h>
 
 #define MAX_INBUF_SIZE 1024
@@ -1145,6 +1144,8 @@ static int
 director_connection_handle_handshake(struct director_connection *conn,
                                     const char *cmd, const char *const *args)
 {
+       unsigned int major_version;
+
        /* both incoming and outgoing connections get VERSION and ME */
        if (strcmp(cmd, "VERSION") == 0 && str_array_length(args) >= 3) {
                if (strcmp(args[0], DIRECTOR_VERSION_NAME) != 0) {
@@ -1152,13 +1153,17 @@ director_connection_handle_handshake(struct director_connection *conn,
                                "(%s vs %s)",
                                conn->name, args[0], DIRECTOR_VERSION_NAME);
                        return -1;
-               } else if (atoi(args[1]) != DIRECTOR_VERSION_MAJOR) {
+               } else if (str_to_uint(args[1], &major_version) < 0 ||
+                       str_to_uint(args[2], &conn->minor_version) < 0) {
+                       i_error("director(%s): Invalid protocol version: "
+                               "%s.%s", conn->name, args[1], args[2]);
+                       return -1;
+               } else if (major_version != DIRECTOR_VERSION_MAJOR) {
                        i_error("director(%s): Incompatible protocol version: "
-                               "%u vs %u", conn->name, atoi(args[1]),
+                               "%u vs %u", conn->name, major_version,
                                DIRECTOR_VERSION_MAJOR);
                        return -1;
                }
-               conn->minor_version = atoi(args[2]);
                conn->version_received = TRUE;
                if (conn->done_pending) {
                        if (director_connection_send_done(conn) < 0)
@@ -1306,7 +1311,10 @@ static bool director_connection_sync(struct director_connection *conn,
                return FALSE;
        }
        if (args[3] != NULL) {
-               minor_version = atoi(args[3]);
+               if (str_to_uint(args[3], &minor_version) < 0) {
+                       director_cmd_error(conn, "Invalid parameters");
+                       return FALSE;
+               }
                if (args[4] != NULL && str_to_uint(args[4], &timestamp) < 0) {
                        director_cmd_error(conn, "Invalid parameters");
                        return FALSE;
index d0ccc7a0f705ab998279875cd031633330691564..a87ae0d03295850ceb0c01fbbe9ea7c5ff8a5871 100644 (file)
@@ -668,8 +668,10 @@ static void cmd_dump_index(int argc ATTR_UNUSED, char *argv[])
        if (index == NULL ||
            mail_index_open(index, MAIL_INDEX_OPEN_FLAG_READONLY) <= 0)
                i_fatal("Couldn't open index %s", argv[1]);
-       if (argv[2] != NULL)
-               uid = atoi(argv[2]);
+       if (argv[2] != NULL) {
+               if (str_to_uint(argv[2], &uid) < 0)
+                       i_fatal("Invalid uid number %s", argv[2]);
+       }
 
        view = mail_index_view_open(index);
        cache_view = mail_cache_view_open(index->cache, view);
index b237bfc7a0dd11f9bab02b3189cd02efdf0ccb45..2e689fbbb6c854eb9e565380eda661aeb509e11d 100644 (file)
@@ -68,7 +68,8 @@ static int who_parse_line(const char *line, struct who_line *line_r)
        line_r->username = strchr(p, '/');
        if (line_r->username == NULL)
                return -1;
-       line_r->refcount = atoi(refcount_str);
+       if (str_to_uint(refcount_str, &line_r->refcount) < 0)
+               return -1;
        ip_str = t_strdup_until(p, line_r->username++);
        (void)net_addr2ip(ip_str, &line_r->ip);
        return 0;
index 26ffc4cfa448c63fe360318e20db57a52ae47013..fa7745f8b88c3edde6aa37eedfdcb8c48eee5544 100644 (file)
@@ -21,8 +21,6 @@
 #include "imap-proxy.h"
 #include "imap-login-settings.h"
 
-#include <stdlib.h>
-
 #if LOGIN_MAX_INBUF_SIZE < 1024+2
 #  error LOGIN_MAX_INBUF_SIZE too short to fit all ID command parameters
 #endif
@@ -158,7 +156,9 @@ client_update_info(struct imap_client *client,
        } else if (strcasecmp(key, "x-connected-port") == 0) {
                (void)net_str2port(value, &client->common.local_port);
        }       else if (strcasecmp(key, "x-proxy-ttl") == 0) {
-               client->common.proxy_ttl = atoi(value);
+               if (str_to_uint(value, &client->common.proxy_ttl) < 0) {
+                       /* nothing */
+               }
        } else if (strcasecmp(key, "x-session-id") == 0 ||
                 strcasecmp(key, "x-session-ext-id") == 0) {
                if (strlen(value) <= LOGIN_MAX_SESSION_ID_LEN) {
index b7afc72ead79b828604fd1701e94741cdd9d9c92..66759fe63a25baff9f30bf94437193ca0f74308c 100644 (file)
@@ -23,7 +23,8 @@ enum dict_iterate_flags {
 
 enum dict_data_type {
        DICT_DATA_TYPE_STRING = 0,
-       DICT_DATA_TYPE_UINT32
+       DICT_DATA_TYPE_UINT32,
+       DICT_DATA_TYPE_LAST
 };
 
 struct dict_settings {
index c7d87bb6aaef1d538e6ca6bdcb5f96e1857655c0..296e5b0faf23778dcc81f3f1dfd0c6645a5875bd 100644 (file)
@@ -164,7 +164,8 @@ master_service_init(const char *name, enum master_service_flags flags,
                int count;
 
                value = getenv("SOCKET_COUNT");
-               count = value == NULL ? 0 : atoi(value);
+               if (value == NULL || str_to_uint(value, &count) < 0)
+                       count = 0;
                fd_debug_verify_leaks(MASTER_LISTEN_FD_FIRST + count, 1024);
        }
 #endif
@@ -222,8 +223,8 @@ master_service_init(const char *name, enum master_service_flags flags,
 
        /* listener configuration */
        value = getenv("SOCKET_COUNT");
-       if (value != NULL)
-               service->socket_count = atoi(value);
+       if (value != NULL && str_to_uint(value, &service->socket_count) < 0)
+               i_fatal("Invalid SOCKET_COUNT environment");
        T_BEGIN {
                master_service_init_socket_listeners(service);
        } T_END;
index 348e840c6a1f99b5774190f2cd0518632e6541ee..1285367bb64890433e7085367215d1ef4fbff31d 100644 (file)
@@ -184,9 +184,10 @@ static void driver_mysql_parse_connect_string(struct mysql_db *db,
                else if (strcmp(name, "port") == 0) {
                        if (net_str2port(value, &db->port) < 0)
                                i_fatal("mysql: Invalid port number: %s", value);
-               } else if (strcmp(name, "client_flags") == 0)
-                       db->client_flags = atoi(value);
-               else if (strcmp(name, "ssl_cert") == 0)
+               } else if (strcmp(name, "client_flags") == 0) {
+                       if (str_to_uint(value, &db->client_flags) < 9)
+                               i_fatal("mysql: Invalid client flags: %s", value);
+               } else if (strcmp(name, "ssl_cert") == 0)
                        field = &db->ssl_cert;
                else if (strcmp(name, "ssl_key") == 0)
                        field = &db->ssl_key;
index 9b0e22f7a6ee2ee930af3fcc55153a0acac2aea9..95fa0052be094e3b404bb331a79b7c176f083492 100644 (file)
@@ -898,7 +898,9 @@ transaction_update_callback(struct sql_result *result,
        } else if (query->affected_rows != NULL) {
                struct pgsql_result *pg_result = (struct pgsql_result *)result;
 
-               *query->affected_rows = atoi(PQcmdTuples(pg_result->pgres));
+               if (str_to_uint(PQcmdTuples(pg_result->pgres),
+                               query->affected_rows) < 0)
+                       i_unreached();
        }
        driver_pgsql_transaction_unref(ctx);
 }
@@ -969,8 +971,9 @@ driver_pgsql_transaction_commit_multi(struct pgsql_transaction_context *ctx)
                        struct pgsql_result *pg_result =
                                (struct pgsql_result *)result;
 
-                       *query->affected_rows =
-                               atoi(PQcmdTuples(pg_result->pgres));
+                       if (str_to_uint(PQcmdTuples(pg_result->pgres),
+                                       query->affected_rows) < 0)
+                               i_unreached();
                }
                sql_result_unref(result);
        }
@@ -1010,8 +1013,9 @@ driver_pgsql_try_commit_s(struct pgsql_transaction_context *ctx,
                        struct pgsql_result *pg_result =
                                (struct pgsql_result *)result;
 
-                       *single_query->affected_rows =
-                               atoi(PQcmdTuples(pg_result->pgres));
+                       if (str_to_uint(PQcmdTuples(pg_result->pgres),
+                                       single_query->affected_rows) < 0)
+                               i_unreached();
                }
        }
        if (result != NULL)
index 5c82f972b2e74ab47c88622655ede3ee1561a059..9a9c94e618ae85af7dbac1d86e9cada0c0e336aa 100644 (file)
@@ -270,9 +270,11 @@ user_reply_handle(struct mail_storage_service_ctx *ctx,
                                p_strdup(user->pool, line + 19);
                } else if (strncmp(line, "nice=", 5) == 0) {
 #ifdef HAVE_SETPRIORITY
-                       int n = atoi(line + 5);
-
-                       if (n != 0) {
+                       int n;
+                       if (str_to_int(line + 5, &n) < 0) {
+                               i_error("userdb returned invalid nice value %s",
+                                       line + 5);
+                       } else if (n != 0) {
                                if (setpriority(PRIO_PROCESS, 0, n) < 0)
                                        i_error("setpriority(%d) failed: %m", n);
                        }
index 9fdab4e2407490c2301cde319355bfa12a9969c5..bc6643629602e529997372d999bd0a0a2827deca 100644 (file)
@@ -34,8 +34,6 @@
 #include "commands.h"
 #include "lmtp-proxy.h"
 
-#include <stdlib.h>
-
 #define ERRSTR_TEMP_MAILBOX_FAIL "451 4.3.0 <%s> Temporary internal error"
 #define ERRSTR_TEMP_USERDB_FAIL_PREFIX "451 4.3.0 <%s> "
 #define ERRSTR_TEMP_USERDB_FAIL \
@@ -245,9 +243,13 @@ client_proxy_rcpt_parse_fields(struct lmtp_proxy_rcpt_settings *set,
                                return FALSE;
                        }
                        port_set = TRUE;
-               } else if (strcmp(key, "proxy_timeout") == 0)
-                       set->timeout_msecs = atoi(value)*1000;
-               else if (strcmp(key, "protocol") == 0) {
+               } else if (strcmp(key, "proxy_timeout") == 0) {
+                       if (str_to_uint(value, &set->timeout_msecs) < 0) {
+                               i_error("proxy: Invalid proxy_timeout value %s", value);
+                               return FALSE;
+                       }
+                       set->timeout_msecs *= 1000;
+               } else if (strcmp(key, "protocol") == 0) {
                        if (strcmp(value, "lmtp") == 0)
                                set->protocol = LMTP_CLIENT_PROTOCOL_LMTP;
                        else if (strcmp(value, "smtp") == 0) {
index 677c1935a80dd2a6137e3b39660be604e3711596..90941c56d00594899c97802aaceb0eae4bc3d81a 100644 (file)
@@ -14,8 +14,6 @@
 #include "master-service-ssl-settings.h"
 #include "client-common.h"
 
-#include <stdlib.h>
-
 #define PROXY_FAILURE_MSG "Account is temporarily unavailable."
 #define PROXY_DEFAULT_TIMEOUT_MSECS (1000*30)
 
@@ -107,11 +105,18 @@ static void client_auth_parse_args(struct client *client,
                        reply_r->destuser = value;
                else if (strcmp(key, "pass") == 0)
                        reply_r->password = value;
-               else if (strcmp(key, "proxy_timeout") == 0)
-                       reply_r->proxy_timeout_msecs = 1000*atoi(value);
-               else if (strcmp(key, "proxy_refresh") == 0)
-                       reply_r->proxy_refresh_secs = atoi(value);
-               else if (strcmp(key, "proxy_mech") == 0)
+               else if (strcmp(key, "proxy_timeout") == 0) {
+                       if (str_to_uint(value, &reply_r->proxy_timeout_msecs) < 0) {
+                               i_error("BUG: Auth service returned invalid "
+                                       "proxy_timeout value: %s", value);
+                       }
+                       reply_r->proxy_timeout_msecs *= 1000;
+               } else if (strcmp(key, "proxy_refresh") == 0) {
+                       if (str_to_uint(value, &reply_r->proxy_refresh_secs) < 0) {
+                               i_error("BUG: Auth service returned invalid "
+                                       "proxy_refresh value: %s", value);
+                       }
+               } else if (strcmp(key, "proxy_mech") == 0)
                        reply_r->proxy_mech = value;
                else if (strcmp(key, "proxy_nopipelining") == 0)
                        reply_r->proxy_nopipelining = TRUE;
index af21c92c2743feb9a6acf0a7e6168ee2939c06df..eb300bf43e6967c9cee3ebd9a325b772605d0824 100644 (file)
@@ -245,10 +245,13 @@ static bool pid_file_read(const char *path, pid_t *pid_r)
                if (buf[ret-1] == '\n')
                        ret--;
                buf[ret] = '\0';
-               *pid_r = atoi(buf);
-
-               found = !(*pid_r == getpid() ||
-                         (kill(*pid_r, 0) < 0 && errno == ESRCH));
+               if (str_to_pid(buf, pid_r) < 0) {
+                       i_error("PID file contains invalid PID value");
+                       found = FALSE;
+               } else {
+                       found = !(*pid_r == getpid() ||
+                                 (kill(*pid_r, 0) < 0 && errno == ESRCH));
+               }
        }
        i_close_fd(&fd);
        return found;
index 72f41bed3836f11ade845615ffe8c5d9025496cd..15d9ed624c45f4dc68bbe66e81af9dc142517a11 100644 (file)
@@ -144,7 +144,10 @@ static bool client_exec_script(struct master_service_connection *conn)
        script_verify_version(*args); args++;
        if (*args != NULL) {
                if (strncmp(*args, "alarm=", 6) == 0) {
-                       alarm(atoi(*args + 6));
+                       unsigned int seconds;
+                       if (str_to_uint(*args + 6, &seconds) < 0)
+                               i_fatal("invalid alarm option");
+                       alarm(seconds);
                        args++;
                }
                if (strcmp(*args, "noreply") == 0) {