s4: ran minimal_includes.pl on source4/winbind
[kai/samba.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 "smbd/service_task.h"
26 #include "param/param.h"
27
28 struct cmd_getpwuid_state {
29         struct composite_context *ctx;
30         struct wbsrv_service *service;
31         uid_t uid;
32         struct dom_sid *sid;
33         char *workgroup;
34         struct wbsrv_domain *domain;
35
36         struct winbindd_pw *result;
37 };
38
39 static void cmd_getpwuid_recv_sid(struct composite_context *ctx);
40 static void cmd_getpwuid_recv_domain(struct composite_context *ctx);
41 static void cmd_getpwuid_recv_user_info(struct composite_context *ctx);
42 static void cmd_getpwuid_recv_gid(struct composite_context *ctx);
43
44 /* Get the SID using the uid */
45
46 struct composite_context *wb_cmd_getpwuid_send(TALLOC_CTX *mem_ctx,
47                                                  struct wbsrv_service *service,
48                                                  uid_t uid)
49 {
50         struct composite_context *ctx, *result;
51         struct cmd_getpwuid_state *state;
52
53         DEBUG(5, ("wb_cmd_getpwuid_send called\n"));
54
55         result = composite_create(mem_ctx, service->task->event_ctx);
56         if (!result) return NULL;
57
58         state = talloc(result, struct cmd_getpwuid_state);
59         if (composite_nomem(state, result)) return result;
60         state->ctx = result;
61         result->private_data = state;
62         state->service = service;
63         state->uid = uid;
64
65         ctx = wb_uid2sid_send(state, service, uid);
66         if (composite_nomem(ctx, state->ctx)) return result;
67
68         composite_continue(result, ctx, cmd_getpwuid_recv_sid, state);
69         return result;
70 }
71
72
73 /* Receive the sid and get the domain structure with it */
74
75 static void cmd_getpwuid_recv_sid(struct composite_context *ctx)
76 {
77         struct cmd_getpwuid_state *state =
78                 talloc_get_type(ctx->async.private_data,
79                                 struct cmd_getpwuid_state);
80
81         DEBUG(5, ("cmd_getpwuid_recv_sid called %p\n", ctx->private_data));
82
83         state->ctx->status = wb_uid2sid_recv(ctx, state, &state->sid);
84         if (!composite_is_ok(state->ctx)) return;
85
86         ctx = wb_sid2domain_send(state, state->service, state->sid);
87
88         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_domain, state);
89 }
90
91 /* Receive the domain struct and call libnet to get the user info struct */
92
93 static void cmd_getpwuid_recv_domain(struct composite_context *ctx)
94 {
95         struct cmd_getpwuid_state *state =
96                 talloc_get_type(ctx->async.private_data,
97                                 struct cmd_getpwuid_state);
98         struct libnet_UserInfo *user_info;
99
100         DEBUG(5, ("cmd_getpwuid_recv_domain called\n"));
101
102         state->ctx->status = wb_sid2domain_recv(ctx, &state->domain);
103         if (!composite_is_ok(state->ctx)) return;
104
105         user_info = talloc(state, struct libnet_UserInfo);
106         if (composite_nomem(user_info, state->ctx)) return;
107
108         user_info->in.level = USER_INFO_BY_SID;
109         user_info->in.data.user_sid = state->sid;
110         user_info->in.domain_name = state->domain->libnet_ctx->samr.name;
111
112         /* We need the workgroup later, so copy it  */
113         state->workgroup = talloc_strdup(state,
114                         state->domain->libnet_ctx->samr.name);
115         if (composite_nomem(state->workgroup, state->ctx)) return;
116
117         ctx = libnet_UserInfo_send(state->domain->libnet_ctx, state, user_info,
118                         NULL);
119
120         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_user_info, state);
121 }
122
123 /* Receive the user info struct and get the gid for the user */
124
125 static void cmd_getpwuid_recv_user_info(struct composite_context *ctx)
126 {
127         struct cmd_getpwuid_state *state =
128                 talloc_get_type(ctx->async.private_data,
129                                 struct cmd_getpwuid_state);
130         struct libnet_UserInfo *user_info;
131         struct winbindd_pw *pw;
132
133         DEBUG(5, ("cmd_getpwuid_recv_user_info called\n"));
134
135         pw = talloc(state, struct winbindd_pw);
136         if (composite_nomem(pw, state->ctx)) return;
137
138         user_info = talloc(state, struct libnet_UserInfo);
139         if(composite_nomem(user_info, state->ctx)) return;
140
141         state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
142         if (!composite_is_ok(state->ctx)) return;
143
144         WBSRV_SAMBA3_SET_STRING(pw->pw_name, user_info->out.account_name);
145         WBSRV_SAMBA3_SET_STRING(pw->pw_passwd, "*");
146         WBSRV_SAMBA3_SET_STRING(pw->pw_gecos, user_info->out.full_name);
147         WBSRV_SAMBA3_SET_STRING(pw->pw_dir, 
148                 lp_template_homedir(state->service->task->lp_ctx));
149         all_string_sub(pw->pw_dir, "%WORKGROUP%", state->workgroup,
150                         sizeof(fstring) - 1);
151         all_string_sub(pw->pw_dir, "%ACCOUNTNAME%", user_info->out.account_name,
152                         sizeof(fstring) - 1);
153         WBSRV_SAMBA3_SET_STRING(pw->pw_shell, 
154                                 lp_template_shell(state->service->task->lp_ctx));
155
156         pw->pw_uid = state->uid;
157
158         state->result = pw;
159
160         ctx = wb_sid2gid_send(state, state->service,
161                         user_info->out.primary_group_sid);
162
163         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_gid, state);
164 }
165
166 static void cmd_getpwuid_recv_gid(struct composite_context *ctx)
167 {
168         struct cmd_getpwuid_state *state =
169                 talloc_get_type(ctx->async.private_data,
170                                 struct cmd_getpwuid_state);
171         gid_t gid;
172
173         DEBUG(5, ("cmd_getpwuid_recv_gid called\n"));
174
175         state->ctx->status = wb_sid2gid_recv(ctx, &gid);
176         if (!composite_is_ok(state->ctx)) return;
177
178         state->result->pw_gid = gid;
179
180         composite_done(state->ctx);
181 }
182
183 NTSTATUS wb_cmd_getpwuid_recv(struct composite_context *ctx,
184                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
185 {
186         NTSTATUS status = composite_wait(ctx);
187
188         DEBUG(5, ("wb_cmd_getpwuid_recv called\n"));
189
190         if (NT_STATUS_IS_OK(status)) {
191                 struct cmd_getpwuid_state *state =
192                         talloc_get_type(ctx->private_data,
193                                         struct cmd_getpwuid_state);
194                 *pw = talloc_steal(mem_ctx, state->result);
195         }
196         talloc_free(ctx);
197         return status;
198
199 }
200