s3:sysquota_xfs: fix logic so that we actually use this module on Linux
[gd/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 "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         char *username_with_domain;
133
134         DEBUG(5, ("cmd_getpwuid_recv_user_info called\n"));
135
136         pw = talloc(state, struct winbindd_pw);
137         if (composite_nomem(pw, state->ctx)) return;
138
139         user_info = talloc(state, struct libnet_UserInfo);
140         if(composite_nomem(user_info, state->ctx)) return;
141
142         state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
143         if (!composite_is_ok(state->ctx)) return;
144
145         username_with_domain = talloc_asprintf(pw, "%s%s%s",
146                 state->workgroup,
147                 lpcfg_winbind_separator(state->service->task->lp_ctx),
148                 user_info->out.account_name);
149         if(composite_nomem(username_with_domain, state->ctx)) return;
150
151         WBSRV_SAMBA3_SET_STRING(pw->pw_name, username_with_domain);
152         WBSRV_SAMBA3_SET_STRING(pw->pw_passwd, "*");
153         WBSRV_SAMBA3_SET_STRING(pw->pw_gecos, user_info->out.full_name);
154         WBSRV_SAMBA3_SET_STRING(pw->pw_dir, 
155                 lpcfg_template_homedir(state->service->task->lp_ctx));
156         all_string_sub(pw->pw_dir, "%WORKGROUP%", state->workgroup,
157                         sizeof(fstring) - 1);
158         all_string_sub(pw->pw_dir, "%ACCOUNTNAME%", user_info->out.account_name,
159                         sizeof(fstring) - 1);
160         WBSRV_SAMBA3_SET_STRING(pw->pw_shell, 
161                                 lpcfg_template_shell(state->service->task->lp_ctx));
162
163         pw->pw_uid = state->uid;
164
165         state->result = pw;
166
167         ctx = wb_sid2gid_send(state, state->service,
168                         user_info->out.primary_group_sid);
169
170         composite_continue(state->ctx, ctx, cmd_getpwuid_recv_gid, state);
171 }
172
173 static void cmd_getpwuid_recv_gid(struct composite_context *ctx)
174 {
175         struct cmd_getpwuid_state *state =
176                 talloc_get_type(ctx->async.private_data,
177                                 struct cmd_getpwuid_state);
178         gid_t gid;
179
180         DEBUG(5, ("cmd_getpwuid_recv_gid called\n"));
181
182         state->ctx->status = wb_sid2gid_recv(ctx, &gid);
183         if (!composite_is_ok(state->ctx)) return;
184
185         state->result->pw_gid = gid;
186
187         composite_done(state->ctx);
188 }
189
190 NTSTATUS wb_cmd_getpwuid_recv(struct composite_context *ctx,
191                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
192 {
193         NTSTATUS status = composite_wait(ctx);
194
195         DEBUG(5, ("wb_cmd_getpwuid_recv called\n"));
196
197         if (NT_STATUS_IS_OK(status)) {
198                 struct cmd_getpwuid_state *state =
199                         talloc_get_type(ctx->private_data,
200                                         struct cmd_getpwuid_state);
201                 *pw = talloc_steal(mem_ctx, state->result);
202         }
203         talloc_free(ctx);
204         return status;
205
206 }
207