Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[Samba/bb.git] / source / winbindd / idmap_util.c
blobbf06cd84726e6e812e2177eb120beaa07cfc4e33
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping
4 Copyright (C) Simo Sorce 2003
5 Copyright (C) Jeremy Allison 2006
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.
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.
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/>.*/
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_IDMAP
25 /*****************************************************************
26 Returns the SID mapped to the given UID.
27 If mapping is not possible returns an error.
28 *****************************************************************/
30 NTSTATUS idmap_uid_to_sid(DOM_SID *sid, uid_t uid)
32 NTSTATUS ret;
33 struct id_map map;
34 struct id_map *maps[2];
36 DEBUG(10,("uid = [%lu]\n", (unsigned long)uid));
38 map.sid = sid;
39 map.xid.type = ID_TYPE_UID;
40 map.xid.id = uid;
42 maps[0] = &map;
43 maps[1] = NULL;
45 ret = idmap_unixids_to_sids(maps);
46 if ( ! NT_STATUS_IS_OK(ret)) {
47 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
48 return ret;
51 if (map.status != ID_MAPPED) {
52 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
53 return NT_STATUS_NONE_MAPPED;
56 return NT_STATUS_OK;
59 /*****************************************************************
60 Returns SID mapped to the given GID.
61 If mapping is not possible returns an error.
62 *****************************************************************/
64 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid)
66 NTSTATUS ret;
67 struct id_map map;
68 struct id_map *maps[2];
70 DEBUG(10,("gid = [%lu]\n", (unsigned long)gid));
72 map.sid = sid;
73 map.xid.type = ID_TYPE_GID;
74 map.xid.id = gid;
76 maps[0] = &map;
77 maps[1] = NULL;
79 ret = idmap_unixids_to_sids(maps);
80 if ( ! NT_STATUS_IS_OK(ret)) {
81 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
82 return ret;
85 if (map.status != ID_MAPPED) {
86 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
87 return NT_STATUS_NONE_MAPPED;
90 return NT_STATUS_OK;
93 /*****************************************************************
94 Returns the UID mapped to the given SID.
95 If mapping is not possible or SID maps to a GID returns an error.
96 *****************************************************************/
98 NTSTATUS idmap_sid_to_uid(DOM_SID *sid, uid_t *uid)
100 NTSTATUS ret;
101 struct id_map map;
102 struct id_map *maps[2];
104 DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_dbg(sid)));
106 map.sid = sid;
107 map.xid.type = ID_TYPE_UID;
109 maps[0] = &map;
110 maps[1] = NULL;
112 ret = idmap_sids_to_unixids(maps);
113 if ( ! NT_STATUS_IS_OK(ret)) {
114 DEBUG(10, ("error mapping sid [%s] to uid\n",
115 sid_string_dbg(sid)));
116 return ret;
119 if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_UID)) {
120 DEBUG(10, ("sid [%s] not mapped to an uid [%u,%u,%u]\n",
121 sid_string_dbg(sid),
122 map.status,
123 map.xid.type,
124 map.xid.id));
125 return NT_STATUS_NONE_MAPPED;
128 *uid = map.xid.id;
130 return NT_STATUS_OK;
133 /*****************************************************************
134 Returns the GID mapped to the given SID.
135 If mapping is not possible or SID maps to a UID returns an error.
136 *****************************************************************/
138 NTSTATUS idmap_sid_to_gid(DOM_SID *sid, gid_t *gid)
140 NTSTATUS ret;
141 struct id_map map;
142 struct id_map *maps[2];
144 DEBUG(10,("idmap_sid_to_gid: sid = [%s]\n", sid_string_dbg(sid)));
146 map.sid = sid;
147 map.xid.type = ID_TYPE_GID;
149 maps[0] = &map;
150 maps[1] = NULL;
152 ret = idmap_sids_to_unixids(maps);
153 if ( ! NT_STATUS_IS_OK(ret)) {
154 DEBUG(10, ("error mapping sid [%s] to gid\n",
155 sid_string_dbg(sid)));
156 return ret;
159 if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_GID)) {
160 DEBUG(10, ("sid [%s] not mapped to a gid [%u,%u,%u]\n",
161 sid_string_dbg(sid),
162 map.status,
163 map.xid.type,
164 map.xid.id));
165 return NT_STATUS_NONE_MAPPED;
168 *gid = map.xid.id;
170 return NT_STATUS_OK;