s3-pysmbd: Fix error message
[Samba/gebeck_regimport.git] / source4 / winbind / wb_cmd_getpwuid.c
blob73bbb59bc3d2fb2d21b5fe984560b52fb5efe1a0
1 /*
2 Unix SMB/CIFS implementation.
4 Backend for getpwuid
6 Copyright (C) Kai Blin 2007
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.
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.
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/>.
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"
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;
36 struct winbindd_pw *result;
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);
44 /* Get the SID using the uid */
46 struct composite_context *wb_cmd_getpwuid_send(TALLOC_CTX *mem_ctx,
47 struct wbsrv_service *service,
48 uid_t uid)
50 struct composite_context *ctx, *result;
51 struct cmd_getpwuid_state *state;
53 DEBUG(5, ("wb_cmd_getpwuid_send called\n"));
55 result = composite_create(mem_ctx, service->task->event_ctx);
56 if (!result) return NULL;
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;
65 ctx = wb_uid2sid_send(state, service, uid);
66 if (composite_nomem(ctx, state->ctx)) return result;
68 composite_continue(result, ctx, cmd_getpwuid_recv_sid, state);
69 return result;
73 /* Receive the sid and get the domain structure with it */
75 static void cmd_getpwuid_recv_sid(struct composite_context *ctx)
77 struct cmd_getpwuid_state *state =
78 talloc_get_type(ctx->async.private_data,
79 struct cmd_getpwuid_state);
81 DEBUG(5, ("cmd_getpwuid_recv_sid called %p\n", ctx->private_data));
83 state->ctx->status = wb_uid2sid_recv(ctx, state, &state->sid);
84 if (!composite_is_ok(state->ctx)) return;
86 ctx = wb_sid2domain_send(state, state->service, state->sid);
88 composite_continue(state->ctx, ctx, cmd_getpwuid_recv_domain, state);
91 /* Receive the domain struct and call libnet to get the user info struct */
93 static void cmd_getpwuid_recv_domain(struct composite_context *ctx)
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;
100 DEBUG(5, ("cmd_getpwuid_recv_domain called\n"));
102 state->ctx->status = wb_sid2domain_recv(ctx, &state->domain);
103 if (!composite_is_ok(state->ctx)) return;
105 user_info = talloc(state, struct libnet_UserInfo);
106 if (composite_nomem(user_info, state->ctx)) return;
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;
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;
117 ctx = libnet_UserInfo_send(state->domain->libnet_ctx, state, user_info,
118 NULL);
120 composite_continue(state->ctx, ctx, cmd_getpwuid_recv_user_info, state);
123 /* Receive the user info struct and get the gid for the user */
125 static void cmd_getpwuid_recv_user_info(struct composite_context *ctx)
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;
134 DEBUG(5, ("cmd_getpwuid_recv_user_info called\n"));
136 pw = talloc(state, struct winbindd_pw);
137 if (composite_nomem(pw, state->ctx)) return;
139 user_info = talloc(state, struct libnet_UserInfo);
140 if(composite_nomem(user_info, state->ctx)) return;
142 state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
143 if (!composite_is_ok(state->ctx)) return;
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;
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));
163 pw->pw_uid = state->uid;
165 state->result = pw;
167 ctx = wb_sid2gid_send(state, state->service,
168 user_info->out.primary_group_sid);
170 composite_continue(state->ctx, ctx, cmd_getpwuid_recv_gid, state);
173 static void cmd_getpwuid_recv_gid(struct composite_context *ctx)
175 struct cmd_getpwuid_state *state =
176 talloc_get_type(ctx->async.private_data,
177 struct cmd_getpwuid_state);
178 gid_t gid;
180 DEBUG(5, ("cmd_getpwuid_recv_gid called\n"));
182 state->ctx->status = wb_sid2gid_recv(ctx, &gid);
183 if (!composite_is_ok(state->ctx)) return;
185 state->result->pw_gid = gid;
187 composite_done(state->ctx);
190 NTSTATUS wb_cmd_getpwuid_recv(struct composite_context *ctx,
191 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
193 NTSTATUS status = composite_wait(ctx);
195 DEBUG(5, ("wb_cmd_getpwuid_recv called\n"));
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);
203 talloc_free(ctx);
204 return status;