r23792: convert Samba4 to GPLv3
[samba.git] / source4 / libcli / wrepl / winsrepl.c
index adb2f094695fd587190812ddcb6702e822e916b4..5b2a9e1e4af443c3824f3190caf6a48cf7645ebe 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/events/events.h"
-#include "dlinklist.h"
+#include "lib/util/dlinklist.h"
 #include "lib/socket/socket.h"
 #include "libcli/wrepl/winsrepl.h"
+#include "librpc/gen_ndr/ndr_winsrepl.h"
 #include "lib/stream/packet.h"
 #include "libcli/composite/composite.h"
+#include "system/network.h"
+#include "lib/socket/netif.h"
 
 static struct wrepl_request *wrepl_request_finished(struct wrepl_request *req, NTSTATUS status);
 
@@ -35,7 +37,6 @@ static struct wrepl_request *wrepl_request_finished(struct wrepl_request *req, N
 */
 static void wrepl_socket_dead(struct wrepl_socket *wrepl_socket, NTSTATUS status)
 {
-       talloc_set_destructor(wrepl_socket, NULL);
        wrepl_socket->dead = True;
 
        if (wrepl_socket->packet) {
@@ -62,6 +63,11 @@ static void wrepl_socket_dead(struct wrepl_socket *wrepl_socket, NTSTATUS status
                DLIST_REMOVE(wrepl_socket->recv_queue, req);
                wrepl_request_finished(req, status);
        }
+
+       talloc_set_destructor(wrepl_socket, NULL);
+       if (wrepl_socket->free_skipped) {
+               talloc_free(wrepl_socket);
+       }
 }
 
 static void wrepl_request_timeout_handler(struct event_context *ev, struct timed_event *te,
@@ -80,6 +86,12 @@ static NTSTATUS wrepl_finish_recv(void *private, DATA_BLOB packet_blob_in)
        struct wrepl_request *req = wrepl_socket->recv_queue;
        DATA_BLOB blob;
 
+       if (!req) {
+               DEBUG(1,("Received unexpected WINS packet of length %u!\n", 
+                        (unsigned)packet_blob_in.length));
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
        req->packet = talloc(req, struct wrepl_packet);
        NT_STATUS_HAVE_NO_MEMORY(req->packet);
 
@@ -96,7 +108,8 @@ static NTSTATUS wrepl_finish_recv(void *private, DATA_BLOB packet_blob_in)
        }
 
        if (DEBUGLVL(10)) {
-               DEBUG(10,("Received WINS packet of length %u\n", packet_blob_in.length));
+               DEBUG(10,("Received WINS packet of length %u\n", 
+                         (unsigned)packet_blob_in.length));
                NDR_PRINT_DEBUG(wrepl_packet, req->packet);
        }
 
@@ -132,9 +145,12 @@ static void wrepl_error(void *private, NTSTATUS status)
 /*
   destroy a wrepl_socket destructor
 */
-static int wrepl_socket_destructor(void *ptr)
+static int wrepl_socket_destructor(struct wrepl_socket *sock)
 {
-       struct wrepl_socket *sock = talloc_get_type(ptr, struct wrepl_socket);
+       if (sock->dead) {
+               sock->free_skipped = True;
+               return -1;
+       }
        wrepl_socket_dead(sock, NT_STATUS_LOCAL_DISCONNECT);
        return 0;
 }
@@ -180,8 +196,8 @@ failed:
 */
 struct wrepl_socket *wrepl_socket_merge(TALLOC_CTX *mem_ctx, 
                                        struct event_context *event_ctx,
-                                       struct socket_context *socket,
-                                       struct packet_context *packet)
+                                       struct socket_context *sock,
+                                       struct packet_context *pack)
 {
        struct wrepl_socket *wrepl_socket;
 
@@ -191,7 +207,7 @@ struct wrepl_socket *wrepl_socket_merge(TALLOC_CTX *mem_ctx,
        wrepl_socket->event.ctx = talloc_reference(wrepl_socket, event_ctx);
        if (wrepl_socket->event.ctx == NULL) goto failed;
 
-       wrepl_socket->sock = socket;
+       wrepl_socket->sock = sock;
        talloc_steal(wrepl_socket, wrepl_socket->sock);
 
 
@@ -205,7 +221,7 @@ struct wrepl_socket *wrepl_socket_merge(TALLOC_CTX *mem_ctx,
                goto failed;
        }
 
-       wrepl_socket->packet = packet;
+       wrepl_socket->packet = pack;
        talloc_steal(wrepl_socket, wrepl_socket->packet);
        packet_set_private(wrepl_socket->packet, wrepl_socket);
        packet_set_socket(wrepl_socket->packet, wrepl_socket->sock);
@@ -228,9 +244,8 @@ failed:
 /*
   destroy a wrepl_request
 */
-static int wrepl_request_destructor(void *ptr)
+static int wrepl_request_destructor(struct wrepl_request *req)
 {
-       struct wrepl_request *req = talloc_get_type(ptr, struct wrepl_request);
        if (req->state == WREPL_REQUEST_RECV) {
                DLIST_REMOVE(req->wrepl_socket->recv_queue, req);
        }
@@ -298,6 +313,7 @@ struct composite_context *wrepl_connect_send(struct wrepl_socket *wrepl_socket,
 {
        struct composite_context *result;
        struct wrepl_connect_state *state;
+       struct socket_address *peer, *us;
 
        result = talloc_zero(wrepl_socket, struct composite_context);
        if (!result) return NULL;
@@ -315,8 +331,15 @@ struct composite_context *wrepl_connect_send(struct wrepl_socket *wrepl_socket,
                our_ip = iface_best_ip(peer_ip);
        }
 
-       state->creq = socket_connect_send(wrepl_socket->sock, our_ip, 0,
-                                         peer_ip, WINS_REPLICATION_PORT,
+       us = socket_address_from_strings(state, wrepl_socket->sock->backend_name, 
+                                        our_ip, 0);
+       if (composite_nomem(us, result)) return result;
+
+       peer = socket_address_from_strings(state, wrepl_socket->sock->backend_name, 
+                                          peer_ip, WINS_REPLICATION_PORT);
+       if (composite_nomem(peer, result)) return result;
+
+       state->creq = socket_connect_send(wrepl_socket->sock, us, peer,
                                          0, wrepl_socket->event.ctx);
        composite_continue(result, state->creq, wrepl_connect_handler, state);
        return result;
@@ -407,9 +430,8 @@ struct wrepl_send_ctrl_state {
        struct wrepl_socket *wrepl_sock;
 };
 
-static int wrepl_send_ctrl_destructor(void *ptr)
+static int wrepl_send_ctrl_destructor(struct wrepl_send_ctrl_state *s)
 {
-       struct wrepl_send_ctrl_state *s = talloc_get_type(ptr, struct wrepl_send_ctrl_state);
        struct wrepl_request *req = s->wrepl_sock->recv_queue;
 
        /* check if the request is still in WREPL_STATE_RECV,
@@ -468,7 +490,8 @@ struct wrepl_request *wrepl_request_send(struct wrepl_socket *wrepl_socket,
        }
 
        if (DEBUGLVL(10)) {
-               DEBUG(10,("Sending WINS packet of length %u\n", blob.length));
+               DEBUG(10,("Sending WINS packet of length %u\n", 
+                         (unsigned)blob.length));
                NDR_PRINT_DEBUG(wrepl_packet, &wrap.packet);
        }
 
@@ -542,6 +565,21 @@ struct wrepl_request *wrepl_associate_send(struct wrepl_socket *wrepl_socket,
        packet->message.start.minor_version = 2;
        packet->message.start.major_version = 5;
 
+       /*
+        * nt4 uses 41 bytes for the start_association call
+        * so do it the same and as we don't know th emeanings of this bytes
+        * we just send zeros and nt4, w2k and w2k3 seems to be happy with this
+        *
+        * if we don't do this nt4 uses an old version of the wins replication protocol
+        * and that would break nt4 <-> samba replication
+        */
+       packet->padding = data_blob_talloc(packet, NULL, 21);
+       if (packet->padding.data == NULL) {
+               talloc_free(packet);
+               return NULL;
+       }
+       memset(packet->padding.data, 0, packet->padding.length);
+
        req = wrepl_request_send(wrepl_socket, packet, NULL);
 
        talloc_free(packet);