pyldb: avoid segfault when adding an element with no name
[nivanova/samba-autobuild/.git] / source3 / winbindd / winbindd_domain_info.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * async implementation of WINBINDD_DOMAIN_INFO
4  * Copyright (C) Volker Lendecke 2018
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "winbindd.h"
22
23 struct winbindd_domain_info_state {
24         struct winbindd_domain *domain;
25         struct winbindd_request ping_request;
26 };
27
28 static void winbindd_domain_info_done(struct tevent_req *subreq);
29
30 struct tevent_req *winbindd_domain_info_send(
31         TALLOC_CTX *mem_ctx,
32         struct tevent_context *ev,
33         struct winbindd_cli_state *cli,
34         struct winbindd_request *request)
35 {
36         struct tevent_req *req, *subreq;
37         struct winbindd_domain_info_state *state;
38
39         req = tevent_req_create(mem_ctx, &state,
40                                 struct winbindd_domain_info_state);
41         if (req == NULL) {
42                 return NULL;
43         }
44
45         DEBUG(3, ("[%5lu]: domain_info [%s]\n", (unsigned long)cli->pid,
46                   cli->request->domain_name));
47
48         state->domain = find_domain_from_name_noinit(
49                 cli->request->domain_name);
50
51         if (state->domain == NULL) {
52                 DEBUG(3, ("Did not find domain [%s]\n",
53                           cli->request->domain_name));
54                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
55                 return tevent_req_post(req, ev);
56         }
57
58         if (state->domain->initialized) {
59                 tevent_req_done(req);
60                 return tevent_req_post(req, ev);
61         }
62
63         state->ping_request.cmd = WINBINDD_PING;
64
65         /*
66          * Send a ping down. This implicitly initializes the domain.
67          */
68
69         subreq = wb_domain_request_send(state, global_event_context(),
70                                         state->domain, &state->ping_request);
71         if (tevent_req_nomem(subreq, req)) {
72                 return tevent_req_post(req, ev);
73         }
74         tevent_req_set_callback(subreq, winbindd_domain_info_done, req);
75
76         return req;
77 }
78
79 static void winbindd_domain_info_done(struct tevent_req *subreq)
80 {
81         struct tevent_req *req = tevent_req_callback_data(
82                 subreq, struct tevent_req);
83         struct winbindd_domain_info_state *state = tevent_req_data(
84                 req, struct winbindd_domain_info_state);
85         struct winbindd_response *response;
86         int ret, err;
87
88         ret = wb_domain_request_recv(subreq, state, &response, &err);
89         TALLOC_FREE(subreq);
90         if (ret == -1) {
91                 DBG_DEBUG("wb_domain_request failed: %s\n", strerror(err));
92                 tevent_req_nterror(req, map_nt_error_from_unix(err));
93                 return;
94         }
95
96         if (!state->domain->initialized) {
97                 DBG_INFO("wb_domain_request did not initialize domain %s\n",
98                          state->domain->name);
99                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
100                 return;
101         }
102
103         tevent_req_done(req);
104 }
105
106 NTSTATUS winbindd_domain_info_recv(struct tevent_req *req,
107                                    struct winbindd_response *response)
108 {
109         struct winbindd_domain_info_state *state = tevent_req_data(
110                 req, struct winbindd_domain_info_state);
111         struct winbindd_domain *domain = state->domain;
112         NTSTATUS status;
113
114         if (tevent_req_is_nterror(req, &status)) {
115                 DBG_DEBUG("winbindd_domain_info failed: %s\n",
116                           nt_errstr(status));
117                 return status;
118         }
119
120         fstrcpy(response->data.domain_info.name, domain->name);
121         fstrcpy(response->data.domain_info.alt_name, domain->alt_name);
122         sid_to_fstring(response->data.domain_info.sid, &domain->sid);
123
124         response->data.domain_info.native_mode = domain->native_mode;
125         response->data.domain_info.active_directory = domain->active_directory;
126         response->data.domain_info.primary = domain->primary;
127
128         return NT_STATUS_OK;
129 }