Fix denial of service - memory corruption.
[samba.git] / source3 / libads / cldap.c
index 73b28b523acc18b79e356176b5cca7eebccdffa5..ac4e2cb3012ee4f872ba6c459930007c7df623e5 100644 (file)
@@ -9,12 +9,12 @@
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 */
 /*
   do a cldap netlogon query
 */
-static int send_cldap_netlogon(int sock, const char *domain, 
+static int send_cldap_netlogon(TALLOC_CTX *mem_ctx, int sock, const char *domain,
                               const char *hostname, unsigned ntversion)
 {
-       ASN1_DATA data;
+       ASN1_DATA *data;
        char ntver[4];
 #ifdef CLDAP_USER_QUERY
        char aac[4];
@@ -36,102 +36,96 @@ static int send_cldap_netlogon(int sock, const char *domain,
 #endif
        SIVAL(ntver, 0, ntversion);
 
-       memset(&data, 0, sizeof(data));
+       data = asn1_init(mem_ctx);
+       if (data == NULL) {
+               return -1;
+       }
 
-       asn1_push_tag(&data,ASN1_SEQUENCE(0));
-       asn1_write_Integer(&data, 4);
-       asn1_push_tag(&data, ASN1_APPLICATION(3));
-       asn1_write_OctetString(&data, NULL, 0);
-       asn1_write_enumerated(&data, 0);
-       asn1_write_enumerated(&data, 0);
-       asn1_write_Integer(&data, 0);
-       asn1_write_Integer(&data, 0);
-       asn1_write_BOOLEAN2(&data, False);
-       asn1_push_tag(&data, ASN1_CONTEXT(0));
+       asn1_push_tag(data,ASN1_SEQUENCE(0));
+       asn1_write_Integer(data, 4);
+       asn1_push_tag(data, ASN1_APPLICATION(3));
+       asn1_write_OctetString(data, NULL, 0);
+       asn1_write_enumerated(data, 0);
+       asn1_write_enumerated(data, 0);
+       asn1_write_Integer(data, 0);
+       asn1_write_Integer(data, 0);
+       asn1_write_BOOLEAN(data, False);
+       asn1_push_tag(data, ASN1_CONTEXT(0));
 
        if (domain) {
-               asn1_push_tag(&data, ASN1_CONTEXT(3));
-               asn1_write_OctetString(&data, "DnsDomain", 9);
-               asn1_write_OctetString(&data, domain, strlen(domain));
-               asn1_pop_tag(&data);
+               asn1_push_tag(data, ASN1_CONTEXT(3));
+               asn1_write_OctetString(data, "DnsDomain", 9);
+               asn1_write_OctetString(data, domain, strlen(domain));
+               asn1_pop_tag(data);
        }
 
-       asn1_push_tag(&data, ASN1_CONTEXT(3));
-       asn1_write_OctetString(&data, "Host", 4);
-       asn1_write_OctetString(&data, hostname, strlen(hostname));
-       asn1_pop_tag(&data);
+       asn1_push_tag(data, ASN1_CONTEXT(3));
+       asn1_write_OctetString(data, "Host", 4);
+       asn1_write_OctetString(data, hostname, strlen(hostname));
+       asn1_pop_tag(data);
 
 #ifdef CLDAP_USER_QUERY
-       asn1_push_tag(&data, ASN1_CONTEXT(3));
-       asn1_write_OctetString(&data, "User", 4);
-       asn1_write_OctetString(&data, "SAMBA$", 6);
-       asn1_pop_tag(&data);
-
-       asn1_push_tag(&data, ASN1_CONTEXT(3));
-       asn1_write_OctetString(&data, "AAC", 4);
-       asn1_write_OctetString(&data, aac, 4);
-       asn1_pop_tag(&data);
+       asn1_push_tag(data, ASN1_CONTEXT(3));
+       asn1_write_OctetString(data, "User", 4);
+       asn1_write_OctetString(data, "SAMBA$", 6);
+       asn1_pop_tag(data);
+
+       asn1_push_tag(data, ASN1_CONTEXT(3));
+       asn1_write_OctetString(data, "AAC", 4);
+       asn1_write_OctetString(data, aac, 4);
+       asn1_pop_tag(data);
 #endif
 
-       asn1_push_tag(&data, ASN1_CONTEXT(3));
-       asn1_write_OctetString(&data, "NtVer", 5);
-       asn1_write_OctetString(&data, ntver, 4);
-       asn1_pop_tag(&data);
+       asn1_push_tag(data, ASN1_CONTEXT(3));
+       asn1_write_OctetString(data, "NtVer", 5);
+       asn1_write_OctetString(data, ntver, 4);
+       asn1_pop_tag(data);
 
-       asn1_pop_tag(&data);
+       asn1_pop_tag(data);
 
-       asn1_push_tag(&data,ASN1_SEQUENCE(0));
-       asn1_write_OctetString(&data, "NetLogon", 8);
-       asn1_pop_tag(&data);
-       asn1_pop_tag(&data);
-       asn1_pop_tag(&data);
+       asn1_push_tag(data,ASN1_SEQUENCE(0));
+       asn1_write_OctetString(data, "NetLogon", 8);
+       asn1_pop_tag(data);
+       asn1_pop_tag(data);
+       asn1_pop_tag(data);
 
-       if (data.has_error) {
-               DEBUG(2,("Failed to build cldap netlogon at offset %d\n", (int)data.ofs));
-               asn1_free(&data);
+       if (data->has_error) {
+               DEBUG(2,("Failed to build cldap netlogon at offset %d\n", (int)data->ofs));
+               asn1_free(data);
                return -1;
        }
 
-       if (write(sock, data.data, data.length) != (ssize_t)data.length) {
+       if (write(sock, data->data, data->length) != (ssize_t)data->length) {
                DEBUG(2,("failed to send cldap query (%s)\n", strerror(errno)));
-               asn1_free(&data);
+               asn1_free(data);
                return -1;
        }
 
-       asn1_free(&data);
+       asn1_free(data);
 
        return 0;
 }
 
-static SIG_ATOMIC_T gotalarm;
-                                                                                                                   
-/***************************************************************
- Signal function to tell us we timed out.
-****************************************************************/
-                                                                                                                   
-static void gotalarm_sig(void)
-{
-       gotalarm = 1;
-}
-                                                                                                                   
 /*
   receive a cldap netlogon reply
 */
 static int recv_cldap_netlogon(TALLOC_CTX *mem_ctx,
                               int sock,
-                              uint32_t *nt_version,
-                              union nbt_cldap_netlogon **reply)
+                              uint32_t nt_version,
+                              struct netlogon_samlogon_response **reply)
 {
        int ret;
-       ASN1_DATA data;
+       ASN1_DATA *data;
        DATA_BLOB blob = data_blob_null;
        DATA_BLOB os1 = data_blob_null;
        DATA_BLOB os2 = data_blob_null;
        DATA_BLOB os3 = data_blob_null;
        int i1;
-       /* half the time of a regular ldap timeout, not less than 3 seconds. */
-       unsigned int al_secs = MAX(3,lp_ldap_timeout()/2);
-       union nbt_cldap_netlogon *r = NULL;
+       struct netlogon_samlogon_response *r = NULL;
+       NTSTATUS status;
+
+       fd_set r_fds;
+       struct timeval timeout;
 
        blob = data_blob(NULL, 8192);
        if (blob.data == NULL) {
@@ -140,76 +134,108 @@ static int recv_cldap_netlogon(TALLOC_CTX *mem_ctx,
                return -1;
        }
 
-       /* Setup timeout */
-       gotalarm = 0;
-       CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
-       alarm(al_secs);
-       /* End setup timeout. */
-       ret = read(sock, blob.data, blob.length);
+       if (sock < 0 || sock >= FD_SETSIZE) {
+               errno = EBADF;
+               return -1;
+       }
+
+       FD_ZERO(&r_fds);
+       FD_SET(sock, &r_fds);
 
-       /* Teardown timeout. */
-       CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
-       alarm(0);
+       /*
+        * half the time of a regular ldap timeout, not less than 3 seconds.
+        */
+       timeout.tv_sec = MAX(3,lp_ldap_timeout()/2);
+       timeout.tv_usec = 0;
 
+       ret = sys_select(sock+1, &r_fds, NULL, NULL, &timeout);
+       if (ret == -1) {
+               DEBUG(10, ("select failed: %s\n", strerror(errno)));
+               data_blob_free(&blob);
+               return -1;
+       }
+
+       if (ret == 0) {
+               DEBUG(1,("no reply received to cldap netlogon "
+                       "(select timeout %u sec)\n",
+                       (unsigned int)timeout.tv_sec));
+               data_blob_free(&blob);
+               return -1;
+       }
+
+       ret = read(sock, blob.data, blob.length);
        if (ret <= 0) {
-               DEBUG(1,("no reply received to cldap netlogon\n"));
+               DEBUG(1,("no reply received to cldap netlogon "
+                       "(ret = %d: Error = %s)\n",
+                       ret,
+                       ret == -1 ? strerror(errno) : "" ));
                data_blob_free(&blob);
                return -1;
        }
        blob.length = ret;
 
-       asn1_load(&data, blob);
-       asn1_start_tag(&data, ASN1_SEQUENCE(0));
-       asn1_read_Integer(&data, &i1);
-       asn1_start_tag(&data, ASN1_APPLICATION(4));
-       asn1_read_OctetString(&data, &os1);
-       asn1_start_tag(&data, ASN1_SEQUENCE(0));
-       asn1_start_tag(&data, ASN1_SEQUENCE(0));
-       asn1_read_OctetString(&data, &os2);
-       asn1_start_tag(&data, ASN1_SET);
-       asn1_read_OctetString(&data, &os3);
-       asn1_end_tag(&data);
-       asn1_end_tag(&data);
-       asn1_end_tag(&data);
-       asn1_end_tag(&data);
-       asn1_end_tag(&data);
-
-       if (data.has_error) {
+       data = asn1_init(mem_ctx);
+       if (data == NULL) {
+               data_blob_free(&blob);
+               return -1;
+       }
+
+       asn1_load(data, blob);
+       asn1_start_tag(data, ASN1_SEQUENCE(0));
+       asn1_read_Integer(data, &i1);
+       asn1_start_tag(data, ASN1_APPLICATION(4));
+       asn1_read_OctetString(data, NULL, &os1);
+       asn1_start_tag(data, ASN1_SEQUENCE(0));
+       asn1_start_tag(data, ASN1_SEQUENCE(0));
+       asn1_read_OctetString(data, NULL, &os2);
+       asn1_start_tag(data, ASN1_SET);
+       asn1_read_OctetString(data, NULL, &os3);
+       asn1_end_tag(data);
+       asn1_end_tag(data);
+       asn1_end_tag(data);
+       asn1_end_tag(data);
+       asn1_end_tag(data);
+
+       if (data->has_error) {
                data_blob_free(&blob);
                data_blob_free(&os1);
                data_blob_free(&os2);
                data_blob_free(&os3);
-               asn1_free(&data);
+               asn1_free(data);
                DEBUG(1,("Failed to parse cldap reply\n"));
                return -1;
        }
 
-       r = TALLOC_ZERO_P(mem_ctx, union nbt_cldap_netlogon);
+       r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
        if (!r) {
                errno = ENOMEM;
                data_blob_free(&os1);
                data_blob_free(&os2);
                data_blob_free(&os3);
                data_blob_free(&blob);
+               asn1_free(data);
                return -1;
        }
 
-       if (!pull_mailslot_cldap_reply(mem_ctx, &os3, r, nt_version)) {
+       status = pull_netlogon_samlogon_response(&os3, mem_ctx, NULL, r);
+       if (!NT_STATUS_IS_OK(status)) {
                data_blob_free(&os1);
                data_blob_free(&os2);
                data_blob_free(&os3);
                data_blob_free(&blob);
+               asn1_free(data);
                TALLOC_FREE(r);
                return -1;
        }
 
+       map_netlogon_samlogon_response(r);
+
        data_blob_free(&os1);
        data_blob_free(&os2);
        data_blob_free(&os3);
        data_blob_free(&blob);
-       
-       asn1_free(&data);
+
+       asn1_free(data);
 
        if (reply) {
                *reply = r;
@@ -227,20 +253,22 @@ static int recv_cldap_netlogon(TALLOC_CTX *mem_ctx,
 bool ads_cldap_netlogon(TALLOC_CTX *mem_ctx,
                        const char *server,
                        const char *realm,
-                       uint32_t *nt_version,
-                       union nbt_cldap_netlogon **reply)
+                       uint32_t nt_version,
+                       struct netlogon_samlogon_response **reply)
 {
        int sock;
        int ret;
 
        sock = open_udp_socket(server, LDAP_PORT );
        if (sock == -1) {
-               DEBUG(2,("ads_cldap_netlogon: Failed to open udp socket to %s\n", 
-                        server));
+               DEBUG(2,("ads_cldap_netlogon: Failed to open udp socket to %s. "
+                       "Error %s\n",
+                       server,
+                       strerror(errno) ));
                return False;
        }
 
-       ret = send_cldap_netlogon(sock, realm, global_myname(), *nt_version);
+       ret = send_cldap_netlogon(mem_ctx, sock, realm, global_myname(), nt_version);
        if (ret != 0) {
                close(sock);
                return False;
@@ -262,107 +290,24 @@ bool ads_cldap_netlogon(TALLOC_CTX *mem_ctx,
 bool ads_cldap_netlogon_5(TALLOC_CTX *mem_ctx,
                          const char *server,
                          const char *realm,
-                         struct nbt_cldap_netlogon_5 *reply5)
+                         struct NETLOGON_SAM_LOGON_RESPONSE_EX *reply5)
 {
-       uint32_t nt_version = NETLOGON_VERSION_5 | NETLOGON_VERSION_5EX;
-       union nbt_cldap_netlogon *reply = NULL;
+       uint32_t nt_version = NETLOGON_NT_VERSION_5 | NETLOGON_NT_VERSION_5EX;
+       struct netlogon_samlogon_response *reply = NULL;
        bool ret;
 
-       ret = ads_cldap_netlogon(mem_ctx, server, realm, &nt_version, &reply);
+       ret = ads_cldap_netlogon(mem_ctx, server, realm, nt_version, &reply);
        if (!ret) {
                return false;
        }
 
-       if (nt_version != (NETLOGON_VERSION_5 | NETLOGON_VERSION_5EX)) {
+       if (reply->ntver != NETLOGON_NT_VERSION_5EX) {
+               DEBUG(0,("ads_cldap_netlogon_5: nt_version mismatch: 0x%08x\n",
+                       reply->ntver));
                return false;
        }
 
-       *reply5 = reply->logon5;
-
-       return true;
-}
-
-/****************************************************************
-****************************************************************/
-
-bool pull_mailslot_cldap_reply(TALLOC_CTX *mem_ctx,
-                              const DATA_BLOB *blob,
-                              union nbt_cldap_netlogon *r,
-                              uint32_t *nt_version)
-{
-       enum ndr_err_code ndr_err;
-       uint32_t nt_version_query = ((*nt_version) & 0x0000001f);
-       uint16_t command = 0;
-
-       ndr_err = ndr_pull_struct_blob(blob, mem_ctx, NULL, &command,
-                       (ndr_pull_flags_fn_t)ndr_pull_uint16);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               return false;
-       }
-
-       switch (command) {
-               case 0x13: /* 19 */
-               case 0x15: /* 21 */
-               case 0x17: /* 23 */
-               case 0x19: /* 25 */
-                        break;
-               default:
-                       DEBUG(1,("got unexpected command: %d (0x%08x)\n",
-                               command, command));
-                       return false;
-       }
-
-       ndr_err = ndr_pull_union_blob_all(blob, mem_ctx, r, nt_version_query,
-                      (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
-       if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               goto done;
-       }
-
-       /* when the caller requested just those nt_version bits that the server
-        * was able to reply to, we are fine and all done. otherwise we need to
-        * assume downgraded replies which are painfully parsed here - gd */
-
-       if (nt_version_query & NETLOGON_VERSION_WITH_CLOSEST_SITE) {
-               nt_version_query &= ~NETLOGON_VERSION_WITH_CLOSEST_SITE;
-       }
-       ndr_err = ndr_pull_union_blob_all(blob, mem_ctx, r, nt_version_query,
-                      (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
-       if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               goto done;
-       }
-       if (nt_version_query & NETLOGON_VERSION_5EX_WITH_IP) {
-               nt_version_query &= ~NETLOGON_VERSION_5EX_WITH_IP;
-       }
-       ndr_err = ndr_pull_union_blob_all(blob, mem_ctx, r, nt_version_query,
-                      (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
-       if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               goto done;
-       }
-       if (nt_version_query & NETLOGON_VERSION_5EX) {
-               nt_version_query &= ~NETLOGON_VERSION_5EX;
-       }
-       ndr_err = ndr_pull_union_blob_all(blob, mem_ctx, r, nt_version_query,
-                      (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
-       if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               goto done;
-       }
-       if (nt_version_query & NETLOGON_VERSION_5) {
-               nt_version_query &= ~NETLOGON_VERSION_5;
-       }
-       ndr_err = ndr_pull_union_blob_all(blob, mem_ctx, r, nt_version_query,
-                      (ndr_pull_flags_fn_t)ndr_pull_nbt_cldap_netlogon);
-       if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               goto done;
-       }
-
-       return false;
-
- done:
-       if (DEBUGLEVEL >= 10) {
-               NDR_PRINT_UNION_DEBUG(nbt_cldap_netlogon, nt_version_query, r);
-       }
-
-       *nt_version = nt_version_query;
+       *reply5 = reply->data.nt5_ex;
 
        return true;
 }