Register types rather than constructors, display structs as classes.
[jelmer/samba4-debian.git] / source / winbind / wb_cmd_getpwnam.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Command backend for wbinfo -i
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 "winbind/wb_async_helpers.h"
26 #include "param/param.h"
27 #include "winbind/wb_helper.h"
28 #include "smbd/service_task.h"
29 #include "libnet/libnet_proto.h"
30 #include "libcli/security/proto.h"
31
32 struct cmd_getpwnam_state {
33         struct composite_context *ctx;
34         struct wbsrv_service *service;
35         char *name;
36         char *workgroup_name;
37         struct dom_sid *group_sid;
38
39         struct winbindd_pw *result;
40 };
41
42 static void cmd_getpwnam_recv_domain(struct composite_context *ctx);
43 static void cmd_getpwnam_recv_user_info(struct composite_context *ctx);
44 static void cmd_getpwnam_recv_uid(struct composite_context *ctx);
45 static void cmd_getpwnam_recv_gid(struct composite_context *ctx);
46
47 struct composite_context *wb_cmd_getpwnam_send(TALLOC_CTX *mem_ctx,
48                                                  struct wbsrv_service *service,
49                                                  const char *name)
50 {
51         struct composite_context *result, *ctx;
52         struct cmd_getpwnam_state *state;
53
54         DEBUG(5, ("wb_cmd_getpwnam_send called\n"));
55
56         result = composite_create(mem_ctx, service->task->event_ctx);
57         if (!result) return NULL;
58
59         state = talloc(result, struct cmd_getpwnam_state);
60         if (composite_nomem(state, result)) return result;
61         state->ctx = result;
62         result->private_data = state;
63         state->service = service;
64         state->name = talloc_strdup(state, name);
65         if(composite_nomem(state->name, result)) return result;
66
67         ctx = wb_name2domain_send(state, service, name);
68         if (composite_nomem(ctx, result)) return result;
69
70         composite_continue(result, ctx, cmd_getpwnam_recv_domain, state);
71         return result;
72 }
73
74 static void cmd_getpwnam_recv_domain(struct composite_context *ctx)
75 {
76         struct cmd_getpwnam_state *state = talloc_get_type(
77                         ctx->async.private_data, struct cmd_getpwnam_state);
78         struct wbsrv_domain *domain;
79         struct libnet_UserInfo *user_info;
80         char *user_dom, *user_name;
81         bool ok;
82
83         state->ctx->status = wb_name2domain_recv(ctx, &domain);
84         if(!composite_is_ok(state->ctx)) return;
85
86         user_info = talloc(state, struct libnet_UserInfo);
87         if (composite_nomem(user_info, state->ctx)) return;
88
89         ok= wb_samba3_split_username(state, state->service->task->lp_ctx, state->name, &user_dom, &user_name);
90         if(!ok){
91                 composite_error(state->ctx, NT_STATUS_OBJECT_NAME_INVALID);
92                 return;
93         }
94
95         user_info->in.user_name = user_name;
96         user_info->in.domain_name = domain->libnet_ctx->samr.name;
97         state->workgroup_name = talloc_strdup(state,
98                         domain->libnet_ctx->samr.name);
99         if(composite_nomem(state->workgroup_name, state->ctx)) return;
100
101         ctx = libnet_UserInfo_send(domain->libnet_ctx, state, user_info, NULL);
102
103         composite_continue(state->ctx, ctx, cmd_getpwnam_recv_user_info, state);
104 }
105
106 static void cmd_getpwnam_recv_user_info(struct composite_context *ctx)
107 {
108         struct cmd_getpwnam_state *state = talloc_get_type(
109                         ctx->async.private_data, struct cmd_getpwnam_state);
110         struct libnet_UserInfo *user_info;
111         struct winbindd_pw *pw;
112
113         DEBUG(5, ("cmd_getpwnam_recv_user_info called\n"));
114
115         user_info = talloc(state, struct libnet_UserInfo);
116         if(composite_nomem(user_info, state->ctx)) return;
117
118         pw = talloc(state, struct winbindd_pw);
119         if(composite_nomem(pw, state->ctx)) return;
120
121         state->ctx->status = libnet_UserInfo_recv(ctx, state, user_info);
122         if(!composite_is_ok(state->ctx)) return;
123
124         WBSRV_SAMBA3_SET_STRING(pw->pw_name, user_info->out.account_name);
125         WBSRV_SAMBA3_SET_STRING(pw->pw_passwd, "*");
126         WBSRV_SAMBA3_SET_STRING(pw->pw_gecos, user_info->out.full_name);
127         WBSRV_SAMBA3_SET_STRING(pw->pw_dir, 
128                 lp_template_homedir(state->service->task->lp_ctx));
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, 
134                 lp_template_shell(state->service->task->lp_ctx));
135
136         state->group_sid = dom_sid_dup(state, user_info->out.primary_group_sid);
137         if(composite_nomem(state->group_sid, state->ctx)) return;
138
139         state->result = pw;
140
141         ctx = wb_sid2uid_send(state, state->service, user_info->out.user_sid);
142         composite_continue(state->ctx, ctx, cmd_getpwnam_recv_uid, state);
143 }
144
145 static void cmd_getpwnam_recv_uid(struct composite_context *ctx)
146 {
147         struct cmd_getpwnam_state *state = talloc_get_type(
148                         ctx->async.private_data, struct cmd_getpwnam_state);
149         uid_t uid;
150
151         DEBUG(5, ("cmd_getpwnam_recv_uid called\n"));
152
153         state->ctx->status = wb_sid2uid_recv(ctx, &uid);
154         if(!composite_is_ok(state->ctx)) return;
155
156         state->result->pw_uid = uid;
157
158         ctx = wb_sid2gid_send(state, state->service, state->group_sid);
159         composite_continue(state->ctx, ctx, cmd_getpwnam_recv_gid, state);
160 }
161
162 static void cmd_getpwnam_recv_gid(struct composite_context *ctx)
163 {
164         struct cmd_getpwnam_state *state = talloc_get_type(
165                         ctx->async.private_data, struct cmd_getpwnam_state);
166         gid_t gid;
167
168         DEBUG(5, ("cmd_getpwnam_recv_gid called\n"));
169
170         state->ctx->status = wb_sid2gid_recv(ctx, &gid);
171         if(!composite_is_ok(state->ctx)) return;
172
173         state->result->pw_gid = gid;
174
175         composite_done(state->ctx);
176 }
177
178 NTSTATUS wb_cmd_getpwnam_recv(struct composite_context *ctx,
179                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
180 {
181         NTSTATUS status = composite_wait(ctx);
182
183         DEBUG(5, ("wb_cmd_getpwnam_recv called\n"));
184
185         if (NT_STATUS_IS_OK(status)) {
186                 struct cmd_getpwnam_state *state =
187                         talloc_get_type(ctx->private_data,
188                                         struct cmd_getpwnam_state);
189                 *pw = talloc_steal(mem_ctx, state->result);
190         }
191         talloc_free(ctx);
192         return status;
193
194 }
195