s3:winbind: Convert WINBINDD_LIST_USERS to the new API
[sfrench/samba-autobuild/.git] / source3 / winbindd / winbindd_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - user related functions
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Jeremy Allison 2001.
8    Copyright (C) Gerald (Jerry) Carter 2003.
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "winbindd.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 bool fillup_pw_field(const char *lp_template,
31                             const char *username,
32                             const char *domname,
33                             uid_t uid,
34                             gid_t gid,
35                             const char *in,
36                             fstring out)
37 {
38         char *templ;
39
40         if (out == NULL)
41                 return False;
42
43         /* The substitution of %U and %D in the 'template
44            homedir' is done by talloc_sub_specified() below.
45            If we have an in string (which means the value has already
46            been set in the nss_info backend), then use that.
47            Otherwise use the template value passed in. */
48
49         if ( in && !strequal(in,"") && lp_security() == SEC_ADS ) {
50                 templ = talloc_sub_specified(NULL, in,
51                                              username, domname,
52                                      uid, gid);
53         } else {
54                 templ = talloc_sub_specified(NULL, lp_template,
55                                              username, domname,
56                                              uid, gid);
57         }
58
59         if (!templ)
60                 return False;
61
62         safe_strcpy(out, templ, sizeof(fstring) - 1);
63         TALLOC_FREE(templ);
64
65         return True;
66
67 }
68
69 /* Wrapper for domain->methods->query_user, only on the parent->child pipe */
70
71 enum winbindd_result winbindd_dual_userinfo(struct winbindd_domain *domain,
72                                             struct winbindd_cli_state *state)
73 {
74         DOM_SID sid;
75         struct wbint_userinfo user_info;
76         NTSTATUS status;
77
78         /* Ensure null termination */
79         state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
80
81         DEBUG(3, ("[%5lu]: lookupsid %s\n", (unsigned long)state->pid,
82                   state->request->data.sid));
83
84         if (!string_to_sid(&sid, state->request->data.sid)) {
85                 DEBUG(5, ("%s not a SID\n", state->request->data.sid));
86                 return WINBINDD_ERROR;
87         }
88
89         status = domain->methods->query_user(domain, state->mem_ctx,
90                                              &sid, &user_info);
91         if (!NT_STATUS_IS_OK(status)) {
92                 DEBUG(1, ("error getting user info for sid %s\n",
93                           sid_string_dbg(&sid)));
94                 return WINBINDD_ERROR;
95         }
96
97         fstrcpy(state->response->data.user_info.acct_name,
98                 user_info.acct_name);
99         fstrcpy(state->response->data.user_info.full_name,
100                 user_info.full_name);
101         fstrcpy(state->response->data.user_info.homedir, user_info.homedir);
102         fstrcpy(state->response->data.user_info.shell, user_info.shell);
103         state->response->data.user_info.primary_gid = user_info.primary_gid;
104         if (!sid_peek_check_rid(&domain->sid, &user_info.group_sid,
105                                 &state->response->data.user_info.group_rid)) {
106                 DEBUG(1, ("Could not extract group rid out of %s\n",
107                           sid_string_dbg(&sid)));
108                 return WINBINDD_ERROR;
109         }
110
111         return WINBINDD_OK;
112 }