pdb-interface: Do not use unid_t here
[rusty/samba.git] / source3 / winbindd / 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "idmap.h"
24 #include "passdb.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
28
29 /*****************************
30  Initialise idmap database. 
31 *****************************/
32
33 static NTSTATUS idmap_pdb_init(struct idmap_domain *dom)
34 {       
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         for (i = 0; ids[i]; i++) {
47
48                 /* unmapped by default */
49                 ids[i]->status = ID_UNMAPPED;
50
51                 switch (ids[i]->xid.type) {
52                 case ID_TYPE_UID:
53                         if (pdb_uid_to_sid((uid_t)ids[i]->xid.id, ids[i]->sid)) {
54                                 ids[i]->status = ID_MAPPED;
55                         }
56                         break;
57                 case ID_TYPE_GID:
58                         if (pdb_gid_to_sid((gid_t)ids[i]->xid.id, ids[i]->sid)) {
59                                 ids[i]->status = ID_MAPPED;
60                         }
61                         break;
62                 default: /* ?? */
63                         ids[i]->status = ID_UNKNOWN;
64                 }
65         }
66
67         return NT_STATUS_OK;
68 }
69
70 /**********************************
71  lookup a set of sids. 
72 **********************************/
73
74 static NTSTATUS idmap_pdb_sids_to_unixids(struct idmap_domain *dom, struct id_map **ids)
75 {
76         int i;
77
78         for (i = 0; ids[i]; i++) {
79                 enum lsa_SidType type;
80                 uid_t uid;
81                 gid_t gid;
82
83                 if (pdb_sid_to_id(ids[i]->sid, &uid, &gid, &type)) {
84                         switch (type) {
85                         case SID_NAME_USER:
86                                 ids[i]->xid.id = uid;
87                                 ids[i]->xid.type = ID_TYPE_UID;
88                                 ids[i]->status = ID_MAPPED;
89                                 break;
90
91                         case SID_NAME_DOM_GRP:
92                         case SID_NAME_ALIAS:
93                         case SID_NAME_WKN_GRP:
94                                 ids[i]->xid.id = gid;
95                                 ids[i]->xid.type = ID_TYPE_GID;
96                                 ids[i]->status = ID_MAPPED;
97                                 break;
98
99                         default: /* ?? */
100                                 /* make sure it is marked as unmapped */
101                                 ids[i]->status = ID_UNKNOWN;
102                                 break;
103                         }
104                 } else {
105                         /* Query Failed */
106                         ids[i]->status = ID_UNMAPPED;
107                 }
108         }
109
110         return NT_STATUS_OK;
111 }
112
113 /**********************************
114  Close the idmap tdb instance
115 **********************************/
116
117 static struct idmap_methods passdb_methods = {
118
119         .init = idmap_pdb_init,
120         .unixids_to_sids = idmap_pdb_unixids_to_sids,
121         .sids_to_unixids = idmap_pdb_sids_to_unixids,
122 };
123
124 NTSTATUS idmap_passdb_init(void)
125 {
126         return smb_register_idmap(SMB_IDMAP_INTERFACE_VERSION, "passdb", &passdb_methods);
127 }