s3:utils: let smbstatus report anonymous signing/encryption explicitly
[samba.git] / source3 / winbindd / wb_query_user_list.c
1 /*
2    Unix SMB/CIFS implementation.
3    async query_user_list
4    Copyright (C) Volker Lendecke 2009
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 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "lib/util/strv.h"
24
25 struct wb_query_user_list_state {
26         struct tevent_context *ev;
27         struct winbindd_domain *domain;
28         struct wbint_RidArray rids;
29         const char *domain_name;
30         struct wbint_Principals names;
31         char *users;
32 };
33
34 static void wb_query_user_list_gotrids(struct tevent_req *subreq);
35 static void wb_query_user_list_done(struct tevent_req *subreq);
36
37 struct tevent_req *wb_query_user_list_send(TALLOC_CTX *mem_ctx,
38                                            struct tevent_context *ev,
39                                            struct winbindd_domain *domain)
40 {
41         struct tevent_req *req, *subreq;
42         struct wb_query_user_list_state *state;
43
44         req = tevent_req_create(mem_ctx, &state,
45                                 struct wb_query_user_list_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49
50         D_INFO("WB command user_list start.\nQuery users in domain %s.\n",
51                domain->name);
52         state->ev = ev;
53         state->domain = domain;
54
55         subreq = dcerpc_wbint_QueryUserRidList_send(
56                 state, ev, dom_child_handle(domain), &state->rids);
57         if (tevent_req_nomem(subreq, req)) {
58                 return tevent_req_post(req, ev);
59         }
60         tevent_req_set_callback(subreq, wb_query_user_list_gotrids, req);
61         return req;
62 }
63
64 static void wb_query_user_list_gotrids(struct tevent_req *subreq)
65 {
66         struct tevent_req *req = tevent_req_callback_data(
67                 subreq, struct tevent_req);
68         struct wb_query_user_list_state *state = tevent_req_data(
69                 req, struct wb_query_user_list_state);
70         NTSTATUS status, result;
71
72         status = dcerpc_wbint_QueryUserRidList_recv(subreq, state, &result);
73         TALLOC_FREE(subreq);
74         if (any_nt_status_not_ok(status, result, &status)) {
75                 tevent_req_nterror(req, status);
76                 return;
77         }
78
79         D_DEBUG("dcerpc_wbint_QueryUserRidList returned %"PRIu32" users\n",
80                 state->rids.num_rids);
81
82         subreq = dcerpc_wbint_LookupRids_send(
83                 state, state->ev, dom_child_handle(state->domain),
84                 &state->domain->sid, &state->rids,
85                 &state->domain_name, &state->names);
86         if (tevent_req_nomem(subreq, req)) {
87                 return;
88         }
89         tevent_req_set_callback(subreq, wb_query_user_list_done, req);
90 }
91
92 static void wb_query_user_list_done(struct tevent_req *subreq)
93 {
94         struct tevent_req *req = tevent_req_callback_data(
95                 subreq, struct tevent_req);
96         struct wb_query_user_list_state *state = tevent_req_data(
97                 req, struct wb_query_user_list_state);
98         NTSTATUS status, result;
99         uint32_t i;
100
101         status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
102         TALLOC_FREE(subreq);
103         if (any_nt_status_not_ok(status, result, &status)) {
104                 tevent_req_nterror(req, status);
105                 return;
106         }
107         D_DEBUG("Processing %"PRIu32" principal(s).\n",
108                 state->names.num_principals);
109         for (i=0; i<state->names.num_principals; i++) {
110                 struct wbint_Principal *p = &state->names.principals[i];
111                 const char *name;
112                 int ret;
113
114                 name = fill_domain_username_talloc(state, state->domain_name, p->name, true);
115                 if (name == NULL) {
116                         tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
117                         return;
118                 }
119                 ret = strv_add(state, &state->users, name);
120                 if (ret != 0) {
121                         tevent_req_nterror(req, map_nt_error_from_unix(ret));
122                         return;
123                 }
124                 D_DEBUG("%"PRIu32": Adding user %s\n", i, name);
125         }
126
127         tevent_req_done(req);
128 }
129
130 NTSTATUS wb_query_user_list_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
131                                  char **users)
132 {
133         struct wb_query_user_list_state *state = tevent_req_data(
134                 req, struct wb_query_user_list_state);
135         NTSTATUS status;
136
137         D_INFO("WB command user_list end.\n");
138         if (tevent_req_is_nterror(req, &status)) {
139                 D_WARNING("Failed with: %s\n", nt_errstr(status));
140                 return status;
141         }
142
143         *users = talloc_move(mem_ctx, &state->users);
144
145         return NT_STATUS_OK;
146 }