r26268: Avoid more use of global_loadparm - put lp_ctx in smb_server and wbsrv_connec...
[kai/samba-autobuild/.git] / source4 / winbind / wb_cmd_getpwuid.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Backend for getpwuid
5
6    Copyright (C) Kai Blin 2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libcli/composite/composite.h"
24 #include "winbind/wb_server.h"
25 #include "winbind/wb_async_helpers.h"
26 #include "winbind/wb_helper.h"
27 #include "smbd/service_task.h"
28 #include "libnet/libnet_proto.h"
29 #include "param/param.h"
30 #include "libcli/security/proto.h"
31 #include "auth/credentials/credentials.h"
32
33 struct cmd_getpwuid_state {
34         struct composite_context *ctx;
35         struct wbsrv_service *service;
36         uid_t uid;
37         char *workgroup;
38         struct wbsrv_domain *domain;
39
40         struct winbindd_pw *result;
41 };
42
43 static void cmd_getpwuid_recv_sid(struct composite_context *ctx);
44 static void cmd_getpwuid_recv_domain(struct composite_context *ctx);
45 static void cmd_getpwuid_recv_user_info(struct composite_context *ctx);
46 static void cmd_getpwuid_recv_gid(struct composite_context *ctx);
47
48 /* Get the SID using the uid */
49
50 struct composite_context *wb_cmd_getpwuid_send(TALLOC_CTX *mem_ctx,
51                                                  struct wbsrv_service *service,
52                                                  uid_t uid)
53 {
54         struct composite_context *ctx, *result;
55         struct cmd_getpwuid_state *state;
56
57         DEBUG(5, ("wb_cmd_getpwnam_send called\n"));
58
59         result = composite_create(mem_ctx, service->task->event_ctx);
60         if (!result) return NULL;
61
62         state = talloc(result, struct cmd_getpwuid_state);
63         if (composite_nomem(state, result)) return result;
64         state->ctx = result;
65         result->private_data = state;
66         state->service = service;
67         state->uid = uid;
68
69         ctx = wb_uid2sid_send(state, service, uid);
70         if (composite_nomem(ctx, state->ctx)) return result;
71
72         composite_continue(result, ctx, cmd_getpwuid_recv_sid, state);
73         return result;
74 }
75
76
77 /* Receive the sid and get the domain structure with it */
78
79 static void cmd_getpwuid_recv_sid(struct composite_context *ctx)
80 {
81         struct cmd_getpwuid_state *state =
82                 talloc_get_type(ctx->async.private_data,
83                                 struct cmd_getpwuid_state);
84         struct dom_sid *sid;
85
86         DEBUG(5, ("cmd_getpwuid_recv_sid called %p\n", ctx->private_data));
87
88         state->ctx->status = wb_uid2sid_recv(ctx, state, &sid);
89         if (!composite_is_ok(state->ctx)) return;
90
91         ctx = wb_sid2domain_send(state, state->service, sid);
92
93         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_domain, state);
94 }
95
96 /* Receive the domain struct and call libnet to get the user info struct */
97
98 static void cmd_getpwuid_recv_domain(struct composite_context *ctx)
99 {
100         struct cmd_getpwuid_state *state =
101                 talloc_get_type(ctx->async.private_data,
102                                 struct cmd_getpwuid_state);
103         struct libnet_UserInfo *user_info;
104
105         DEBUG(5, ("cmd_getpwuid_recv_domain called\n"));
106
107         state->ctx->status = wb_sid2domain_recv(ctx, &state->domain);
108         if (!composite_is_ok(state->ctx)) return;
109
110         user_info = talloc(state, struct libnet_UserInfo);
111         if (composite_nomem(user_info, state->ctx)) return;
112
113         user_info->in.user_name = state->domain->libnet_ctx->cred->username;
114         user_info->in.domain_name = state->domain->libnet_ctx->samr.name;
115
116         /* We need the workgroup later, so copy it  */
117         state->workgroup = talloc_strdup(state,
118                         state->domain->libnet_ctx->samr.name);
119         if (composite_nomem(state->workgroup, state->ctx)) return;
120
121         ctx = libnet_UserInfo_send(state->domain->libnet_ctx, state, user_info,
122                         NULL);
123
124         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_user_info, state);
125 }
126
127 /* Receive the user info struct and get the gid for the user */
128
129 static void cmd_getpwuid_recv_user_info(struct composite_context *ctx)
130 {
131         struct cmd_getpwuid_state *state =
132                 talloc_get_type(ctx->async.private_data,
133                                 struct cmd_getpwuid_state);
134         struct libnet_UserInfo *user_info;
135         struct winbindd_pw *pw;
136
137         DEBUG(5, ("cmd_getpwuid_recv_user_info called\n"));
138
139         pw = talloc(state, struct winbindd_pw);
140         if (composite_nomem(pw, state->ctx)) return;
141
142         user_info = talloc(state, struct libnet_UserInfo);
143         if(composite_nomem(user_info, state->ctx)) return;
144
145         state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
146         if (!composite_is_ok(state->ctx)) return;
147
148         WBSRV_SAMBA3_SET_STRING(pw->pw_name, user_info->out.account_name);
149         WBSRV_SAMBA3_SET_STRING(pw->pw_passwd, "*");
150         WBSRV_SAMBA3_SET_STRING(pw->pw_gecos, user_info->out.full_name);
151         WBSRV_SAMBA3_SET_STRING(pw->pw_dir, 
152                 lp_template_homedir(state->service->task->lp_ctx));
153         all_string_sub(pw->pw_dir, "%WORKGROUP%", state->workgroup,
154                         sizeof(fstring) - 1);
155         all_string_sub(pw->pw_dir, "%ACCOUNTNAME%", user_info->out.account_name,
156                         sizeof(fstring) - 1);
157         WBSRV_SAMBA3_SET_STRING(pw->pw_shell, 
158                                 lp_template_shell(state->service->task->lp_ctx));
159
160         pw->pw_uid = state->uid;
161
162         state->result = pw;
163
164         ctx = wb_sid2gid_send(state, state->service,
165                         user_info->out.primary_group_sid);
166
167         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_gid, state);
168 }
169
170 static void cmd_getpwuid_recv_gid(struct composite_context *ctx)
171 {
172         struct cmd_getpwuid_state *state =
173                 talloc_get_type(ctx->async.private_data,
174                                 struct cmd_getpwuid_state);
175         gid_t gid;
176
177         DEBUG(5, ("cmd_getpwuid_recv_gid called\n"));
178
179         state->ctx->status = wb_sid2gid_recv(ctx, &gid);
180         if (!composite_is_ok(state->ctx)) return;
181
182         state->result->pw_gid = gid;
183
184         composite_done(state->ctx);
185 }
186
187 NTSTATUS wb_cmd_getpwuid_recv(struct composite_context *ctx,
188                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
189 {
190         NTSTATUS status = composite_wait(ctx);
191
192         DEBUG(5, ("wb_cmd_getpwnam_recv called\n"));
193
194         if (NT_STATUS_IS_OK(status)) {
195                 struct cmd_getpwuid_state *state =
196                         talloc_get_type(ctx->private_data,
197                                         struct cmd_getpwuid_state);
198                 *pw = talloc_steal(mem_ctx, state->result);
199         }
200         talloc_free(ctx);
201         return status;
202
203 }
204