Directly call backends from idmap_[ugs]_to_[ugs]id
[ira/wip.git] / source3 / winbindd / idmap_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ID Mapping
4    Copyright (C) Simo Sorce 2003
5    Copyright (C) Jeremy Allison 2006
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
19
20 #include "includes.h"
21
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_IDMAP
24
25 /*****************************************************************
26  Returns the SID mapped to the given UID.
27  If mapping is not possible returns an error.
28 *****************************************************************/  
29
30 NTSTATUS idmap_uid_to_sid(DOM_SID *sid, uid_t uid)
31 {
32         NTSTATUS ret;
33         struct id_map map;
34
35         DEBUG(10,("uid = [%lu]\n", (unsigned long)uid));
36
37         map.sid = sid;
38         map.xid.type = ID_TYPE_UID;
39         map.xid.id = uid;
40
41         ret = idmap_backends_unixid_to_sid(&map);
42         if ( ! NT_STATUS_IS_OK(ret)) {
43                 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
44                 return ret;
45         }
46
47         if (map.status != ID_MAPPED) {
48                 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
49                 return NT_STATUS_NONE_MAPPED;
50         }
51
52         return NT_STATUS_OK;
53 }
54
55 /*****************************************************************
56  Returns SID mapped to the given GID.
57  If mapping is not possible returns an error.
58 *****************************************************************/  
59
60 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid)
61 {
62         NTSTATUS ret;
63         struct id_map map;
64
65         DEBUG(10,("gid = [%lu]\n", (unsigned long)gid));
66
67         map.sid = sid;
68         map.xid.type = ID_TYPE_GID;
69         map.xid.id = gid;
70
71         ret = idmap_backends_unixid_to_sid(&map);
72         if ( ! NT_STATUS_IS_OK(ret)) {
73                 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
74                 return ret;
75         }
76
77         if (map.status != ID_MAPPED) {
78                 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
79                 return NT_STATUS_NONE_MAPPED;
80         }
81
82         return NT_STATUS_OK;
83 }
84
85 /*****************************************************************
86  Returns the UID mapped to the given SID.
87  If mapping is not possible or SID maps to a GID returns an error.
88 *****************************************************************/  
89
90 NTSTATUS idmap_sid_to_uid(DOM_SID *sid, uid_t *uid)
91 {
92         NTSTATUS ret;
93         struct id_map map;
94
95         DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_dbg(sid)));
96
97         map.sid = sid;
98         map.xid.type = ID_TYPE_UID;     
99
100         ret = idmap_backends_sid_to_unixid(&map);
101         if ( ! NT_STATUS_IS_OK(ret)) {
102                 DEBUG(10, ("error mapping sid [%s] to uid\n", 
103                            sid_string_dbg(sid)));
104                 return ret;
105         }
106
107         if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_UID)) {
108                 DEBUG(10, ("sid [%s] not mapped to an uid [%u,%u,%u]\n", 
109                            sid_string_dbg(sid),
110                            map.status, 
111                            map.xid.type, 
112                            map.xid.id));
113                 return NT_STATUS_NONE_MAPPED;
114         }
115
116         *uid = map.xid.id;
117
118         return NT_STATUS_OK;
119 }
120
121 /*****************************************************************
122  Returns the GID mapped to the given SID.
123  If mapping is not possible or SID maps to a UID returns an error.
124 *****************************************************************/  
125
126 NTSTATUS idmap_sid_to_gid(DOM_SID *sid, gid_t *gid)
127 {
128         NTSTATUS ret;
129         struct id_map map;
130
131         DEBUG(10,("idmap_sid_to_gid: sid = [%s]\n", sid_string_dbg(sid)));
132
133         map.sid = sid;
134         map.xid.type = ID_TYPE_GID;
135
136         ret = idmap_backends_sid_to_unixid(&map);
137         if ( ! NT_STATUS_IS_OK(ret)) {
138                 DEBUG(10, ("error mapping sid [%s] to gid\n", 
139                            sid_string_dbg(sid)));
140                 return ret;
141         }
142
143         if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_GID)) {
144                 DEBUG(10, ("sid [%s] not mapped to a gid [%u,%u]\n", 
145                            sid_string_dbg(sid),
146                            map.status, 
147                            map.xid.type));
148                 return NT_STATUS_NONE_MAPPED;
149         }
150
151         *gid = map.xid.id;
152
153         return NT_STATUS_OK;
154 }