s3-smbd: Don't segfault if user specified ports out for range.
authorAndreas Schneider <asn@samba.org>
Tue, 25 Sep 2012 12:28:22 +0000 (14:28 +0200)
committerKarolin Seeger <kseeger@samba.org>
Thu, 1 Nov 2012 08:10:35 +0000 (09:10 +0100)
(cherry picked from commit 50d324b7e070de4672eff3fb6231923e6dca807a)

Signed-off-by: Andreas Schneider <asn@samba.org>
Fix bug #9218 - Samba panics if a user specifies an invalid port number.
(cherry picked from commit 60b15f3b646d10e027e8288132db5b942261de8f)

source3/smbd/server.c

index 63a9869dcef367fe653afc8747b138e1f9ee1ae9..f34d9f6fdf6d13904873958f3977f999cee9d93b 100644 (file)
@@ -560,6 +560,8 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
        int num_interfaces = iface_count();
        int i;
        char *ports;
+       char *tok;
+       const char *ptr;
        unsigned dns_port = 0;
 
 #ifdef HAVE_ATEXIT
@@ -581,6 +583,16 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                ports = talloc_strdup(talloc_tos(), smb_ports);
        }
 
+       for (ptr = ports;
+            next_token_talloc(talloc_tos(),&ptr, &tok, " \t,");) {
+               unsigned port = atoi(tok);
+
+               if (port == 0 || port > 0xffff) {
+                       exit_server_cleanly("Invalid port in the config or on "
+                                           "the commandline specified!");
+               }
+       }
+
        if (lp_interfaces() && lp_bind_interfaces_only()) {
                /* We have been given an interfaces line, and been
                   told to only bind to those interfaces. Create a
@@ -592,8 +604,6 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                for(i = 0; i < num_interfaces; i++) {
                        const struct sockaddr_storage *ifss =
                                        iface_n_sockaddr_storage(i);
-                       char *tok;
-                       const char *ptr;
 
                        if (ifss == NULL) {
                                DEBUG(0,("open_sockets_smbd: "
@@ -605,9 +615,6 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                        for (ptr=ports;
                             next_token_talloc(talloc_tos(),&ptr, &tok, " \t,");) {
                                unsigned port = atoi(tok);
-                               if (port == 0 || port > 0xffff) {
-                                       continue;
-                               }
 
                                /* Keep the first port for mDNS service
                                 * registration.
@@ -625,8 +632,6 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                /* Just bind to 0.0.0.0 - accept connections
                   from anywhere. */
 
-               char *tok;
-               const char *ptr;
                const char *sock_addr = lp_socket_address();
                char *sock_tok;
                const char *sock_ptr;
@@ -644,11 +649,7 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                     next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,"); ) {
                        for (ptr=ports; next_token_talloc(talloc_tos(), &ptr, &tok, " \t,"); ) {
                                struct sockaddr_storage ss;
-
                                unsigned port = atoi(tok);
-                               if (port == 0 || port > 0xffff) {
-                                       continue;
-                               }
 
                                /* Keep the first port for mDNS service
                                 * registration.