2 Unix SMB/CIFS implementation.
4 Command backend for wbinfo -i
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/>.
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 "nsswitch/winbindd_nss.h"
29 #include "libnet/libnet_proto.h"
30 #include "param/param.h"
31 #include "libcli/security/proto.h"
33 struct cmd_getpwnam_state {
34 struct composite_context *ctx;
35 struct wbsrv_service *service;
38 struct dom_sid *group_sid;
40 struct winbindd_pw *result;
43 static void cmd_getpwnam_recv_domain(struct composite_context *ctx);
44 static void cmd_getpwnam_recv_user_info(struct composite_context *ctx);
45 static void cmd_getpwnam_recv_uid(struct composite_context *ctx);
46 static void cmd_getpwnam_recv_gid(struct composite_context *ctx);
48 struct composite_context *wb_cmd_getpwnam_send(TALLOC_CTX *mem_ctx,
49 struct wbsrv_service *service,
52 struct composite_context *result, *ctx;
53 struct cmd_getpwnam_state *state;
55 DEBUG(5, ("wb_cmd_getpwnam_send called\n"));
57 result = composite_create(mem_ctx, service->task->event_ctx);
58 if (!result) return NULL;
60 state = talloc(result, struct cmd_getpwnam_state);
61 if (composite_nomem(state, result)) return result;
63 result->private_data = state;
64 state->service = service;
65 state->name = talloc_strdup(state, name);
66 if(composite_nomem(state->name, result)) return result;
68 ctx = wb_name2domain_send(state, service, name);
69 if (composite_nomem(ctx, result)) return result;
71 composite_continue(result, ctx, cmd_getpwnam_recv_domain, state);
75 static void cmd_getpwnam_recv_domain(struct composite_context *ctx)
77 struct cmd_getpwnam_state *state = talloc_get_type(
78 ctx->async.private_data, struct cmd_getpwnam_state);
79 struct wbsrv_domain *domain;
80 struct libnet_UserInfo *user_info;
81 char *user_dom, *user_name;
84 state->ctx->status = wb_name2domain_recv(ctx, &domain);
85 if(!composite_is_ok(state->ctx)) return;
87 user_info = talloc(state, struct libnet_UserInfo);
88 if (composite_nomem(user_info, state->ctx)) return;
90 ok= wb_samba3_split_username(state, state->name, &user_dom, &user_name);
92 composite_error(state->ctx, NT_STATUS_OBJECT_NAME_INVALID);
96 user_info->in.user_name = user_name;
97 user_info->in.domain_name = domain->libnet_ctx->samr.name;
98 state->workgroup_name = talloc_strdup(state,
99 domain->libnet_ctx->samr.name);
100 if(composite_nomem(state->workgroup_name, state->ctx)) return;
102 ctx = libnet_UserInfo_send(domain->libnet_ctx, state, user_info, NULL);
104 composite_continue(state->ctx, ctx, cmd_getpwnam_recv_user_info, state);
107 static void cmd_getpwnam_recv_user_info(struct composite_context *ctx)
109 struct cmd_getpwnam_state *state = talloc_get_type(
110 ctx->async.private_data, struct cmd_getpwnam_state);
111 struct libnet_UserInfo *user_info;
112 struct winbindd_pw *pw;
114 DEBUG(5, ("cmd_getpwnam_recv_user_info called\n"));
116 user_info = talloc(state, struct libnet_UserInfo);
117 if(composite_nomem(user_info, state->ctx)) return;
119 pw = talloc(state, struct winbindd_pw);
120 if(composite_nomem(pw, state->ctx)) return;
122 state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
123 if(!composite_is_ok(state->ctx)) return;
125 WBSRV_SAMBA3_SET_STRING(pw->pw_name, user_info->out.account_name);
126 WBSRV_SAMBA3_SET_STRING(pw->pw_passwd, "*");
127 WBSRV_SAMBA3_SET_STRING(pw->pw_gecos, user_info->out.full_name);
128 WBSRV_SAMBA3_SET_STRING(pw->pw_dir, lp_template_homedir());
129 all_string_sub(pw->pw_dir, "%WORKGROUP%", state->workgroup_name,
130 sizeof(fstring) - 1);
131 all_string_sub(pw->pw_dir, "%ACCOUNTNAME%", user_info->out.account_name,
132 sizeof(fstring) - 1);
133 WBSRV_SAMBA3_SET_STRING(pw->pw_shell, lp_template_shell());
135 state->group_sid = dom_sid_dup(state, user_info->out.primary_group_sid);
136 if(composite_nomem(state->group_sid, state->ctx)) return;
140 ctx = wb_sid2uid_send(state, state->service, user_info->out.user_sid);
141 composite_continue(state->ctx, ctx, cmd_getpwnam_recv_uid, state);
144 static void cmd_getpwnam_recv_uid(struct composite_context *ctx)
146 struct cmd_getpwnam_state *state = talloc_get_type(
147 ctx->async.private_data, struct cmd_getpwnam_state);
150 DEBUG(5, ("cmd_getpwnam_recv_uid called\n"));
152 state->ctx->status = wb_sid2uid_recv(ctx, &uid);
153 if(!composite_is_ok(state->ctx)) return;
155 state->result->pw_uid = uid;
157 ctx = wb_sid2gid_send(state, state->service, state->group_sid);
158 composite_continue(state->ctx, ctx, cmd_getpwnam_recv_gid, state);
161 static void cmd_getpwnam_recv_gid(struct composite_context *ctx)
163 struct cmd_getpwnam_state *state = talloc_get_type(
164 ctx->async.private_data, struct cmd_getpwnam_state);
167 DEBUG(5, ("cmd_getpwnam_recv_gid called\n"));
169 state->ctx->status = wb_sid2gid_recv(ctx, &gid);
170 if(!composite_is_ok(state->ctx)) return;
172 state->result->pw_gid = gid;
174 composite_done(state->ctx);
177 NTSTATUS wb_cmd_getpwnam_recv(struct composite_context *ctx,
178 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
180 NTSTATUS status = composite_wait(ctx);
182 DEBUG(5, ("wb_cmd_getpwnam_recv called\n"));
184 if (NT_STATUS_IS_OK(status)) {
185 struct cmd_getpwnam_state *state =
186 talloc_get_type(ctx->private_data,
187 struct cmd_getpwnam_state);
188 *pw = talloc_steal(mem_ctx, state->result);