s3:tests: clean up old log directories of the registry.roundtrip test
[Samba.git] / source4 / winbind / wb_cmd_getpwuid.c
blobf6f3cb282e77be726c697d6bf547979c62b90c87
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;
133 DEBUG(5, ("cmd_getpwuid_recv_user_info called\n"));
135 pw = talloc(state, struct winbindd_pw);
136 if (composite_nomem(pw, state->ctx)) return;
138 user_info = talloc(state, struct libnet_UserInfo);
139 if(composite_nomem(user_info, state->ctx)) return;
141 state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
142 if (!composite_is_ok(state->ctx)) return;
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 lpcfg_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 lpcfg_template_shell(state->service->task->lp_ctx));
156 pw->pw_uid = state->uid;
158 state->result = pw;
160 ctx = wb_sid2gid_send(state, state->service,
161 user_info->out.primary_group_sid);
163 composite_continue(state->ctx, ctx, cmd_getpwuid_recv_gid, state);
166 static void cmd_getpwuid_recv_gid(struct composite_context *ctx)
168 struct cmd_getpwuid_state *state =
169 talloc_get_type(ctx->async.private_data,
170 struct cmd_getpwuid_state);
171 gid_t gid;
173 DEBUG(5, ("cmd_getpwuid_recv_gid called\n"));
175 state->ctx->status = wb_sid2gid_recv(ctx, &gid);
176 if (!composite_is_ok(state->ctx)) return;
178 state->result->pw_gid = gid;
180 composite_done(state->ctx);
183 NTSTATUS wb_cmd_getpwuid_recv(struct composite_context *ctx,
184 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
186 NTSTATUS status = composite_wait(ctx);
188 DEBUG(5, ("wb_cmd_getpwuid_recv called\n"));
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);
196 talloc_free(ctx);
197 return status;