7312ab46c81e22f6234a6faa170d7739dbba99d3
[samba.git] / source3 / nsswitch / idmap_passdb.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 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_IDMAP
27
28 /*****************************
29  Initialise idmap database. 
30 *****************************/
31
32 static NTSTATUS idmap_pdb_init(struct idmap_domain *dom)
33 {       
34         dom->initialized = True;
35         return NT_STATUS_OK;
36 }
37
38 /**********************************
39  lookup a set of unix ids. 
40 **********************************/
41
42 static NTSTATUS idmap_pdb_unixids_to_sids(struct idmap_domain *dom, struct id_map **ids)
43 {
44         int i;
45
46         if (! dom->initialized) {
47                 return NT_STATUS_UNSUCCESSFUL;
48         }
49
50         for (i = 0; ids[i]; i++) {
51
52                 /* unmapped by default */
53                 ids[i]->status = ID_UNMAPPED;
54
55                 switch (ids[i]->xid.type) {
56                 case ID_TYPE_UID:
57                         if (pdb_uid_to_sid((uid_t)ids[i]->xid.id, ids[i]->sid)) {
58                                 ids[i]->status = ID_MAPPED;
59                         }
60                         break;
61                 case ID_TYPE_GID:
62                         if (pdb_gid_to_sid((gid_t)ids[i]->xid.id, ids[i]->sid)) {
63                                 ids[i]->status = ID_MAPPED;
64                         }
65                         break;
66                 default: /* ?? */
67                         ids[i]->status = ID_UNKNOWN;
68                 }
69         }
70
71         return NT_STATUS_OK;
72 }
73
74 /**********************************
75  lookup a set of sids. 
76 **********************************/
77
78 static NTSTATUS idmap_pdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
79 {
80         int i;
81
82         if (! dom->initialized) {
83                 return NT_STATUS_UNSUCCESSFUL;
84         }
85
86         for (i = 0; ids[i]; i++) {
87                 enum lsa_SidType type;
88                 union unid_t id;
89                 
90                 if (pdb_sid_to_id(ids[i]->sid, &id, &type)) {
91                         switch (type) {
92                         case SID_NAME_USER:
93                                 ids[i]->xid.id = id.uid;
94                                 ids[i]->xid.type = ID_TYPE_UID;
95                                 ids[i]->status = ID_MAPPED;
96                                 break;
97
98                         case SID_NAME_DOM_GRP:
99                         case SID_NAME_ALIAS:
100                         case SID_NAME_WKN_GRP:
101                                 ids[i]->xid.id = id.gid;
102                                 ids[i]->xid.type = ID_TYPE_GID;
103                                 ids[i]->status = ID_MAPPED;
104                                 break;
105
106                         default: /* ?? */
107                                 /* make sure it is marked as unmapped */
108                                 ids[i]->status = ID_UNKNOWN;
109                                 break;
110                         }
111                 } else {
112                         /* Query Failed */
113                         ids[i]->status = ID_UNMAPPED;
114                 }
115         }
116
117         return NT_STATUS_OK;
118 }
119
120 /**********************************
121  Close the idmap tdb instance
122 **********************************/
123
124 static NTSTATUS idmap_pdb_close(struct idmap_domain *dom)
125 {
126         return NT_STATUS_OK;
127 }
128
129 static struct idmap_methods passdb_methods = {
130
131         .init = idmap_pdb_init,
132         .unixids_to_sids = idmap_pdb_unixids_to_sids,
133         .sids_to_unixids = idmap_pdb_sids_to_unixids,
134         .close_fn =idmap_pdb_close
135 };
136
137 NTSTATUS idmap_passdb_init(void)
138 {
139         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "passdb", &passdb_methods);
140 }