Ignore:
Timestamp:
Nov 24, 2016, 1:14:11 PM (9 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to version 4.4.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/source3/libsmb/unexpected.c

    r740 r988  
    2121#include "includes.h"
    2222#include "../lib/util/tevent_ntstatus.h"
    23 #include "lib/async_req/async_sock.h"
     23#include "lib/util_tsock.h"
     24#include "lib/tsocket/tsocket.h"
    2425#include "libsmb/nmblib.h"
     26#include "lib/util/sys_rw.h"
    2527
    2628static const char *nmbd_socket_dir(void)
     
    4143        struct tevent_context *ev;
    4244        int listen_sock;
     45        struct tevent_fd *listen_fde;
    4346        int max_clients;
    4447        int num_clients;
     
    5457        char *mailslot_name;
    5558
    56         int sock;
    57         struct tevent_req *read_req;
     59        struct {
     60                uint8_t byte;
     61                struct iovec iov[1];
     62        } ack;
     63
     64        struct tstream_context *sock;
    5865        struct tevent_queue *out_queue;
    5966};
     
    7178{
    7279        struct nb_packet_server *result;
    73         struct tevent_fd *fde;
    7480        NTSTATUS status;
    75 
    76         result = TALLOC_ZERO_P(mem_ctx, struct nb_packet_server);
     81        int rc;
     82
     83        result = talloc_zero(mem_ctx, struct nb_packet_server);
    7784        if (result == NULL) {
    7885                status = NT_STATUS_NO_MEMORY;
     
    8895                goto fail;
    8996        }
     97        rc = listen(result->listen_sock, 5);
     98        if (rc < 0) {
     99                status = map_nt_error_from_unix(errno);
     100                goto fail;
     101        }
    90102        talloc_set_destructor(result, nb_packet_server_destructor);
    91103
    92         fde = tevent_add_fd(ev, result, result->listen_sock, TEVENT_FD_READ,
    93                             nb_packet_server_listener, result);
    94         if (fde == NULL) {
     104        result->listen_fde = tevent_add_fd(ev, result,
     105                                           result->listen_sock,
     106                                           TEVENT_FD_READ,
     107                                           nb_packet_server_listener,
     108                                           result);
     109        if (result->listen_fde == NULL) {
    95110                status = NT_STATUS_NO_MEMORY;
    96111                goto fail;
     
    106121static int nb_packet_server_destructor(struct nb_packet_server *s)
    107122{
     123        TALLOC_FREE(s->listen_fde);
     124
    108125        if (s->listen_sock != -1) {
    109126                close(s->listen_sock);
     
    117134                                     void *private_data);
    118135static void nb_packet_got_query(struct tevent_req *req);
     136static void nb_packet_client_ack_done(struct tevent_req *req);
    119137static void nb_packet_client_read_done(struct tevent_req *req);
    120138
     
    131149        socklen_t len;
    132150        int sock;
     151        int ret;
    133152
    134153        len = sizeof(sunaddr);
     
    141160        DEBUG(6,("accepted socket %d\n", sock));
    142161
    143         client = TALLOC_ZERO_P(server, struct nb_packet_client);
     162        client = talloc_zero(server, struct nb_packet_client);
    144163        if (client == NULL) {
    145164                DEBUG(10, ("talloc failed\n"));
     
    147166                return;
    148167        }
    149         client->sock = sock;
     168        ret = tstream_bsd_existing_socket(client, sock, &client->sock);
     169        if (ret != 0) {
     170                DEBUG(10, ("tstream_bsd_existing_socket failed\n"));
     171                close(sock);
     172                return;
     173        }
     174
    150175        client->server = server;
    151176        talloc_set_destructor(client, nb_packet_client_destructor);
     
    159184        }
    160185
    161         req = read_packet_send(client, ev, client->sock,
    162                                sizeof(struct nb_packet_query),
    163                                nb_packet_client_more, NULL);
     186        req = tstream_read_packet_send(client, ev, client->sock,
     187                                       sizeof(struct nb_packet_query),
     188                                       nb_packet_client_more, NULL);
    164189        if (req == NULL) {
    165                 DEBUG(10, ("read_packet_send failed\n"));
     190                DEBUG(10, ("tstream_read_packet_send failed\n"));
    166191                TALLOC_FREE(client);
    167192                return;
     
    201226static int nb_packet_client_destructor(struct nb_packet_client *c)
    202227{
    203         if (c->sock != -1) {
    204                 close(c->sock);
    205                 c->sock = -1;
    206         }
     228        tevent_queue_stop(c->out_queue);
     229        TALLOC_FREE(c->sock);
     230
    207231        DLIST_REMOVE(c->server->clients, c);
    208232        c->server->num_clients -= 1;
     
    216240        struct nb_packet_query q;
    217241        uint8_t *buf;
    218         ssize_t nread, nwritten;
     242        ssize_t nread;
    219243        int err;
    220         char c;
    221 
    222         nread = read_packet_recv(req, talloc_tos(), &buf, &err);
     244
     245        nread = tstream_read_packet_recv(req, talloc_tos(), &buf, &err);
    223246        TALLOC_FREE(req);
    224247        if (nread < (ssize_t)sizeof(struct nb_packet_query)) {
     
    251274        }
    252275
    253         /*
    254          * Yes, this is a blocking write of 1 byte into a unix
    255          * domain socket that has never been written to. Highly
    256          * unlikely that this actually blocks.
    257          */
    258         c = 0;
    259         nwritten = sys_write(client->sock, &c, sizeof(c));
    260         if (nwritten != sizeof(c)) {
    261                 DEBUG(10, ("Could not write success indicator to client: %s\n",
    262                            strerror(errno)));
     276        client->ack.byte = 0;
     277        client->ack.iov[0].iov_base = &client->ack.byte;
     278        client->ack.iov[0].iov_len = 1;
     279        req = tstream_writev_queue_send(client, client->server->ev,
     280                                        client->sock,
     281                                        client->out_queue,
     282                                        client->ack.iov, 1);
     283        if (req == NULL) {
     284                DEBUG(10, ("tstream_writev_queue_send failed\n"));
    263285                TALLOC_FREE(client);
    264286                return;
    265287        }
    266 
    267         client->read_req = read_packet_send(client, client->server->ev,
    268                                             client->sock, 1, NULL, NULL);
    269         if (client->read_req == NULL) {
     288        tevent_req_set_callback(req, nb_packet_client_ack_done, client);
     289
     290        req = tstream_read_packet_send(client, client->server->ev,
     291                                       client->sock, 1, NULL, NULL);
     292        if (req == NULL) {
    270293                DEBUG(10, ("Could not activate reader for client exit "
    271294                           "detection\n"));
     
    273296                return;
    274297        }
    275         tevent_req_set_callback(client->read_req, nb_packet_client_read_done,
     298        tevent_req_set_callback(req, nb_packet_client_read_done,
    276299                                client);
     300}
     301
     302static void nb_packet_client_ack_done(struct tevent_req *req)
     303{
     304        struct nb_packet_client *client = tevent_req_callback_data(
     305                req, struct nb_packet_client);
     306        ssize_t nwritten;
     307        int err;
     308
     309        nwritten = tstream_writev_queue_recv(req, &err);
     310
     311        TALLOC_FREE(req);
     312
     313        if (nwritten == -1) {
     314                DEBUG(10, ("tstream_writev_queue_recv failed: %s\n",
     315                           strerror(err)));
     316                TALLOC_FREE(client);
     317                return;
     318        }
    277319}
    278320
     
    285327        int err;
    286328
    287         nread = read_packet_recv(req, talloc_tos(), &buf, &err);
     329        nread = tstream_read_packet_recv(req, talloc_tos(), &buf, &err);
    288330        TALLOC_FREE(req);
    289331        if (nread == 1) {
     
    379421        }
    380422
    381         state = TALLOC_ZERO_P(client, struct nb_packet_client_state);
     423        state = talloc_zero(client, struct nb_packet_client_state);
    382424        if (state == NULL) {
    383425                DEBUG(10, ("talloc failed\n"));
     
    398440        state->iov[1].iov_len = state->hdr.len;
    399441
    400         TALLOC_FREE(client->read_req);
    401 
    402         req = writev_send(client, client->server->ev, client->out_queue,
    403                           client->sock, true, state->iov, 2);
     442        req = tstream_writev_queue_send(state, client->server->ev,
     443                                        client->sock,
     444                                        client->out_queue,
     445                                        state->iov, 2);
    404446        if (req == NULL) {
    405                 DEBUG(10, ("writev_send failed\n"));
     447                DEBUG(10, ("tstream_writev_queue_send failed\n"));
    406448                return;
    407449        }
     
    417459        int err;
    418460
    419         nwritten = writev_recv(req, &err);
     461        nwritten = tstream_writev_queue_recv(req, &err);
    420462
    421463        TALLOC_FREE(req);
     
    423465
    424466        if (nwritten == -1) {
    425                 DEBUG(10, ("writev failed: %s\n", strerror(err)));
     467                DEBUG(10, ("tstream_writev_queue failed: %s\n", strerror(err)));
    426468                TALLOC_FREE(client);
    427         }
    428 
    429         if (tevent_queue_length(client->out_queue) == 0) {
    430                 client->read_req = read_packet_send(client, client->server->ev,
    431                                                     client->sock, 1,
    432                                                     NULL, NULL);
    433                 if (client->read_req == NULL) {
    434                         DEBUG(10, ("Could not activate reader for client exit "
    435                                    "detection\n"));
    436                         TALLOC_FREE(client);
    437                         return;
    438                 }
    439                 tevent_req_set_callback(client->read_req,
    440                                         nb_packet_client_read_done,
    441                                         client);
     469                return;
    442470        }
    443471}
    444472
    445473struct nb_packet_reader {
    446         int sock;
     474        struct tstream_context *sock;
    447475};
    448476
    449477struct nb_packet_reader_state {
    450478        struct tevent_context *ev;
    451         struct sockaddr_un addr;
    452479        struct nb_packet_query query;
    453480        const char *mailslot_name;
     
    457484};
    458485
    459 static int nb_packet_reader_destructor(struct nb_packet_reader *r);
    460486static void nb_packet_reader_connected(struct tevent_req *subreq);
    461487static void nb_packet_reader_sent_query(struct tevent_req *subreq);
     
    470496        struct tevent_req *req, *subreq;
    471497        struct nb_packet_reader_state *state;
    472         char *path;
     498        struct tsocket_address *laddr;
     499        char *rpath;
     500        struct tsocket_address *raddr;
     501        int ret;
    473502
    474503        req = tevent_req_create(mem_ctx, &state,
     
    486515        }
    487516
    488         state->reader = TALLOC_ZERO_P(state, struct nb_packet_reader);
     517        state->reader = talloc_zero(state, struct nb_packet_reader);
    489518        if (tevent_req_nomem(state->reader, req)) {
    490519                return tevent_req_post(req, ev);
    491520        }
    492521
    493         path = talloc_asprintf(talloc_tos(), "%s/%s", nmbd_socket_dir(),
    494                                "unexpected");
    495         if (tevent_req_nomem(path, req)) {
    496                 return tevent_req_post(req, ev);
    497         }
    498         state->addr.sun_family = AF_UNIX;
    499         strlcpy(state->addr.sun_path, path, sizeof(state->addr.sun_path));
    500         TALLOC_FREE(path);
    501 
    502         state->reader->sock = socket(AF_UNIX, SOCK_STREAM, 0);
    503         if (state->reader->sock == -1) {
     522        ret = tsocket_address_unix_from_path(state, "", &laddr);
     523        if (ret != 0) {
    504524                tevent_req_nterror(req, map_nt_error_from_unix(errno));
    505525                return tevent_req_post(req, ev);
    506526        }
    507         talloc_set_destructor(state->reader, nb_packet_reader_destructor);
    508 
    509         subreq = async_connect_send(state, ev, state->reader->sock,
    510                                     (struct sockaddr *)(void *)&state->addr,
    511                                     sizeof(state->addr));
     527        rpath = talloc_asprintf(state, "%s/%s", nmbd_socket_dir(),
     528                               "unexpected");
     529        if (tevent_req_nomem(rpath, req)) {
     530                return tevent_req_post(req, ev);
     531        }
     532        ret = tsocket_address_unix_from_path(state, rpath, &raddr);
     533        if (ret != 0) {
     534                tevent_req_nterror(req, map_nt_error_from_unix(errno));
     535                return tevent_req_post(req, ev);
     536        }
     537
     538        subreq = tstream_unix_connect_send(state, ev, laddr, raddr);
    512539        if (tevent_req_nomem(subreq, req)) {
    513540                return tevent_req_post(req, ev);
     
    515542        tevent_req_set_callback(subreq, nb_packet_reader_connected, req);
    516543        return req;
    517 }
    518 
    519 static int nb_packet_reader_destructor(struct nb_packet_reader *r)
    520 {
    521         if (r->sock != -1) {
    522                 close(r->sock);
    523                 r->sock = -1;
    524         }
    525         return 0;
    526544}
    527545
     
    535553        int num_iovecs = 1;
    536554
    537         res = async_connect_recv(subreq, &err);
     555        res = tstream_unix_connect_recv(subreq, &err, state->reader,
     556                                        &state->reader->sock);
    538557        TALLOC_FREE(subreq);
    539558        if (res == -1) {
    540                 DEBUG(10, ("async_connect failed: %s\n", strerror(err)));
     559                DEBUG(10, ("tstream_unix_connect failed: %s\n", strerror(err)));
    541560                tevent_req_nterror(req, map_nt_error_from_unix(err));
    542561                return;
     
    553572        }
    554573
    555         subreq = writev_send(state, state->ev, NULL, state->reader->sock,
    556                              true, state->iov, num_iovecs);
     574        subreq = tstream_writev_send(state, state->ev, state->reader->sock,
     575                                    state->iov, num_iovecs);
    557576        if (tevent_req_nomem(subreq, req)) {
    558577                return;
     
    570589        int err;
    571590
    572         written = writev_recv(subreq, &err);
     591        written = tstream_writev_recv(subreq, &err);
    573592        TALLOC_FREE(subreq);
    574593        if (written == -1) {
     
    580599                return;
    581600        }
    582         subreq = read_packet_send(state, state->ev, state->reader->sock,
    583                                   sizeof(state->c), NULL, NULL);
     601        subreq = tstream_read_packet_send(state, state->ev,
     602                                          state->reader->sock,
     603                                          sizeof(state->c), NULL, NULL);
    584604        if (tevent_req_nomem(subreq, req)) {
    585605                return;
     
    598618        uint8_t *buf;
    599619
    600         nread = read_packet_recv(subreq, state, &buf, &err);
     620        nread = tstream_read_packet_recv(subreq, state, &buf, &err);
    601621        TALLOC_FREE(subreq);
    602622        if (nread == -1) {
     
    623643
    624644        if (tevent_req_is_nterror(req, &status)) {
     645                tevent_req_received(req);
    625646                return status;
    626647        }
    627648        *preader = talloc_move(mem_ctx, &state->reader);
     649        tevent_req_received(req);
    628650        return NT_STATUS_OK;
    629651}
     
    649671                return NULL;
    650672        }
    651         subreq = read_packet_send(state, ev, reader->sock,
    652                                   sizeof(struct nb_packet_client_header),
    653                                   nb_packet_read_more, state);
     673        subreq = tstream_read_packet_send(state, ev, reader->sock,
     674                                          sizeof(struct nb_packet_client_header),
     675                                          nb_packet_read_more, state);
    654676        if (tevent_req_nomem(subreq, req)) {
    655677                return tevent_req_post(req, ev);
     
    683705        int err;
    684706
    685         nread = read_packet_recv(subreq, state, &state->buf, &err);
     707        nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
    686708        if (nread == -1) {
    687709                tevent_req_nterror(req, map_nt_error_from_unix(err));
     
    702724
    703725        if (tevent_req_is_nterror(req, &status)) {
     726                tevent_req_received(req);
    704727                return status;
    705728        }
     
    712735                state->hdr.type, state->hdr.ip, state->hdr.port);
    713736        if (packet == NULL) {
     737                tevent_req_received(req);
    714738                return NT_STATUS_INVALID_NETWORK_RESPONSE;
    715739        }
    716740        *ppacket = packet;
     741        tevent_req_received(req);
    717742        return NT_STATUS_OK;
    718743}
Note: See TracChangeset for help on using the changeset viewer.