s4-idmap: Add mapping using uidNumber and gidNumber like idmap_ad
[samba.git] / source4 / 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 "param/param.h"
26 #include "winbind/wb_helper.h"
27 #include "smbd/service_task.h"
28 #include "libcli/security/security.h"
29
30 struct cmd_getpwnam_state {
31         struct composite_context *ctx;
32         struct wbsrv_service *service;
33         char *name;
34         char *workgroup_name;
35         struct dom_sid *group_sid;
36
37         struct winbindd_pw *result;
38 };
39
40 static void cmd_getpwnam_recv_domain(struct composite_context *ctx);
41 static void cmd_getpwnam_recv_user_info(struct composite_context *ctx);
42 static void cmd_getpwnam_recv_uid(struct composite_context *ctx);
43 static void cmd_getpwnam_recv_gid(struct composite_context *ctx);
44
45 struct composite_context *wb_cmd_getpwnam_send(TALLOC_CTX *mem_ctx,
46                                                  struct wbsrv_service *service,
47                                                  const char *name)
48 {
49         struct composite_context *result, *ctx;
50         struct cmd_getpwnam_state *state;
51
52         DEBUG(5, ("wb_cmd_getpwnam_send called\n"));
53
54         result = composite_create(mem_ctx, service->task->event_ctx);
55         if (!result) return NULL;
56
57         state = talloc(result, struct cmd_getpwnam_state);
58         if (composite_nomem(state, result)) return result;
59         state->ctx = result;
60         result->private_data = state;
61         state->service = service;
62         state->name = talloc_strdup(state, name);
63         if(composite_nomem(state->name, result)) return result;
64
65         ctx = wb_name2domain_send(state, service, name);
66         if (composite_nomem(ctx, result)) return result;
67
68         composite_continue(result, ctx, cmd_getpwnam_recv_domain, state);
69         return result;
70 }
71
72 static void cmd_getpwnam_recv_domain(struct composite_context *ctx)
73 {
74         struct cmd_getpwnam_state *state = talloc_get_type(
75                         ctx->async.private_data, struct cmd_getpwnam_state);
76         struct wbsrv_domain *domain;
77         struct libnet_UserInfo *user_info;
78         char *user_dom, *user_name;
79         bool ok;
80
81         state->ctx->status = wb_name2domain_recv(ctx, &domain);
82         if(!composite_is_ok(state->ctx)) return;
83
84         user_info = talloc(state, struct libnet_UserInfo);
85         if (composite_nomem(user_info, state->ctx)) return;
86
87         ok= wb_samba3_split_username(state, state->service->task->lp_ctx, state->name, &user_dom, &user_name);
88         if(!ok){
89                 composite_error(state->ctx, NT_STATUS_OBJECT_NAME_INVALID);
90                 return;
91         }
92
93         user_info->in.level = USER_INFO_BY_NAME;
94         user_info->in.data.user_name = user_name;
95         user_info->in.domain_name = domain->libnet_ctx->samr.name;
96         state->workgroup_name = talloc_strdup(state,
97                         domain->libnet_ctx->samr.name);
98         if(composite_nomem(state->workgroup_name, state->ctx)) return;
99
100         ctx = libnet_UserInfo_send(domain->libnet_ctx, state, user_info, NULL);
101
102         composite_continue(state->ctx, ctx, cmd_getpwnam_recv_user_info, state);
103 }
104
105 static void cmd_getpwnam_recv_user_info(struct composite_context *ctx)
106 {
107         struct cmd_getpwnam_state *state = talloc_get_type(
108                         ctx->async.private_data, struct cmd_getpwnam_state);
109         struct libnet_UserInfo *user_info;
110         struct winbindd_pw *pw;
111         char *username_with_domain;
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         username_with_domain = talloc_asprintf(pw, "%s%s%s",
125                 state->workgroup_name,
126                 lpcfg_winbind_separator(state->service->task->lp_ctx),
127                 user_info->out.account_name);
128         if(composite_nomem(username_with_domain, state->ctx)) return;
129
130         WBSRV_SAMBA3_SET_STRING(pw->pw_name, username_with_domain);
131         WBSRV_SAMBA3_SET_STRING(pw->pw_passwd, "*");
132         WBSRV_SAMBA3_SET_STRING(pw->pw_gecos, user_info->out.full_name);
133         WBSRV_SAMBA3_SET_STRING(pw->pw_dir, 
134                 lpcfg_template_homedir(state->service->task->lp_ctx));
135         all_string_sub(pw->pw_dir, "%WORKGROUP%", state->workgroup_name,
136                         sizeof(fstring) - 1);
137         all_string_sub(pw->pw_dir, "%ACCOUNTNAME%", user_info->out.account_name,
138                         sizeof(fstring) - 1);
139         WBSRV_SAMBA3_SET_STRING(pw->pw_shell, 
140                 lpcfg_template_shell(state->service->task->lp_ctx));
141
142         state->group_sid = dom_sid_dup(state, user_info->out.primary_group_sid);
143         if(composite_nomem(state->group_sid, state->ctx)) return;
144
145         state->result = pw;
146
147         ctx = wb_sid2uid_send(state, state->service, user_info->out.user_sid);
148         composite_continue(state->ctx, ctx, cmd_getpwnam_recv_uid, state);
149 }
150
151 static void cmd_getpwnam_recv_uid(struct composite_context *ctx)
152 {
153         struct cmd_getpwnam_state *state = talloc_get_type(
154                         ctx->async.private_data, struct cmd_getpwnam_state);
155         uid_t uid;
156
157         DEBUG(5, ("cmd_getpwnam_recv_uid called\n"));
158
159         state->ctx->status = wb_sid2uid_recv(ctx, &uid);
160         if(!composite_is_ok(state->ctx)) return;
161
162         state->result->pw_uid = uid;
163
164         ctx = wb_sid2gid_send(state, state->service, state->group_sid);
165         composite_continue(state->ctx, ctx, cmd_getpwnam_recv_gid, state);
166 }
167
168 static void cmd_getpwnam_recv_gid(struct composite_context *ctx)
169 {
170         struct cmd_getpwnam_state *state = talloc_get_type(
171                         ctx->async.private_data, struct cmd_getpwnam_state);
172         gid_t gid;
173
174         DEBUG(5, ("cmd_getpwnam_recv_gid called\n"));
175
176         state->ctx->status = wb_sid2gid_recv(ctx, &gid);
177         if(!composite_is_ok(state->ctx)) return;
178
179         state->result->pw_gid = gid;
180
181         composite_done(state->ctx);
182 }
183
184 NTSTATUS wb_cmd_getpwnam_recv(struct composite_context *ctx,
185                 TALLOC_CTX *mem_ctx, struct winbindd_pw **pw)
186 {
187         NTSTATUS status = composite_wait(ctx);
188
189         DEBUG(5, ("wb_cmd_getpwnam_recv called\n"));
190
191         if (NT_STATUS_IS_OK(status)) {
192                 struct cmd_getpwnam_state *state =
193                         talloc_get_type(ctx->private_data,
194                                         struct cmd_getpwnam_state);
195                 *pw = talloc_steal(mem_ctx, state->result);
196         }
197         talloc_free(ctx);
198         return status;
199
200 }
201