r20774: I thought I committed this before Xmas holidays ...
[tprouty/samba.git] / source / nsswitch / idmap_nss.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    idmap PASSDB backend
5
6    Copyright (C) Simo Sorce 2006
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
28
29 /*****************************
30  Initialise idmap database. 
31 *****************************/
32
33 static NTSTATUS idmap_nss_int_init(struct idmap_domain *dom, const char *compat_params)
34 {       
35         return NT_STATUS_OK;
36 }
37
38 /**********************************
39  lookup a set of unix ids. 
40 **********************************/
41
42 static NTSTATUS idmap_nss_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
43 {
44         TALLOC_CTX *ctx;
45         int i;
46
47         ctx = talloc_new(dom);
48         if ( ! ctx) {
49                 DEBUG(0, ("Out of memory!\n"));
50                 return NT_STATUS_NO_MEMORY;
51         }
52
53         for (i = 0; ids[i]; i++) {
54                 struct passwd *pw;
55                 struct group *gr;
56                 const char *name;
57                 enum lsa_SidType type;
58                 BOOL ret;
59                 
60                 switch (ids[i]->xid.type) {
61                 case ID_TYPE_UID:
62                         pw = getpwuid((uid_t)ids[i]->xid.id);
63
64                         if (!pw) {
65                                 ids[i]->status = ID_UNMAPPED;
66                                 continue;
67                         }
68                         name = pw->pw_name;
69                         break;
70                 case ID_TYPE_GID:
71                         gr = getgrgid((gid_t)ids[i]->xid.id);
72
73                         if (!gr) {
74                                 ids[i]->status = ID_UNMAPPED;
75                                 continue;
76                         }
77                         name = gr->gr_name;
78                         break;
79                 default: /* ?? */
80                         ids[i]->status = ID_UNKNOWN;
81                         continue;
82                 }
83
84                 /* by default calls to winbindd are disabled
85                    the following call will not recurse so this is safe */
86                 winbind_on();
87                 /* Lookup name from PDC using lsa_lookup_names() */
88                 ret = winbind_lookup_name(dom->name, name, ids[i]->sid, &type);
89                 winbind_off();
90
91                 if (!ret) {
92                         /* TODO: how do we know if the name is really not mapped,
93                          * or something just failed ? */
94                         ids[i]->status = ID_UNMAPPED;
95                         continue;
96                 }
97
98                 switch (type) {
99                 case SID_NAME_USER:
100                         if (ids[i]->xid.type == ID_TYPE_UID) {
101                                 ids[i]->status = ID_MAPPED;
102                         }
103                         break;
104
105                 case SID_NAME_DOM_GRP:
106                 case SID_NAME_ALIAS:
107                 case SID_NAME_WKN_GRP:
108                         if (ids[i]->xid.type == ID_TYPE_GID) {
109                                 ids[i]->status = ID_MAPPED;
110                         }
111                         break;
112
113                 default:
114                         ids[i]->status = ID_UNKNOWN;
115                         break;
116                 }
117         }
118
119
120         talloc_free(ctx);
121         return NT_STATUS_OK;
122 }
123
124 /**********************************
125  lookup a set of sids. 
126 **********************************/
127
128 static NTSTATUS idmap_nss_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
129 {
130         TALLOC_CTX *ctx;
131         int i;
132
133         ctx = talloc_new(dom);
134         if ( ! ctx) {
135                 DEBUG(0, ("Out of memory!\n"));
136                 return NT_STATUS_NO_MEMORY;
137         }
138
139         for (i = 0; ids[i]; i++) {
140                 struct passwd *pw;
141                 struct group *gr;
142                 enum lsa_SidType type;
143                 const char *dom_name = NULL;
144                 const char *name = NULL;
145                 BOOL ret;
146
147                 /* by default calls to winbindd are disabled
148                    the following call will not recurse so this is safe */
149                 winbind_on();
150                 ret = winbind_lookup_sid(ctx, ids[i]->sid, &dom_name, &name, &type);
151                 winbind_off();
152
153                 if (!ret) {
154                         /* TODO: how do we know if the name is really not mapped,
155                          * or something just failed ? */
156                         ids[i]->status = ID_UNMAPPED;
157                         continue;
158                 }
159
160                 switch (type) {
161                 case SID_NAME_USER:
162
163                         /* this will find also all lower case name and use username level */
164                         
165                         pw = Get_Pwnam(name);
166                         if (pw) {
167                                 ids[i]->xid.id = pw->pw_uid;
168                                 ids[i]->xid.type = ID_TYPE_UID;
169                                 ids[i]->status = ID_MAPPED;
170                         }
171                         break;
172
173                 case SID_NAME_DOM_GRP:
174                 case SID_NAME_ALIAS:
175                 case SID_NAME_WKN_GRP:
176
177                         gr = getgrnam(name);
178                         if (gr) {
179                                 ids[i]->xid.id = gr->gr_gid;
180                                 ids[i]->xid.type = ID_TYPE_GID;
181                                 ids[i]->status = ID_MAPPED;
182                         }
183                         break;
184
185                 default:
186                         ids[i]->status = ID_UNKNOWN;
187                         break;
188                 }
189         }
190
191         talloc_free(ctx);
192         return NT_STATUS_OK;
193 }
194
195 /**********************************
196  Close the idmap tdb instance
197 **********************************/
198
199 static NTSTATUS idmap_nss_close(struct idmap_domain *dom)
200 {
201         return NT_STATUS_OK;
202 }
203
204 static struct idmap_methods nss_methods = {
205
206         .init = idmap_nss_int_init,
207         .unixids_to_sids = idmap_nss_unixids_to_sids,
208         .sids_to_unixids = idmap_nss_sids_to_unixids,
209         .close_fn = idmap_nss_close
210 };
211
212 NTSTATUS idmap_nss_init(void)
213 {
214         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "nss", &nss_methods);
215 }