a768b3f382e10254270ab1f374f10fdf8d9226d9
[samba.git] / source / nsswitch / 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/
20
21 #include "includes.h"
22
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_IDMAP
25
26 /*****************************************************************
27  Returns the SID mapped to the given UID.
28  If mapping is not possible returns an error.
29 *****************************************************************/  
30
31 NTSTATUS idmap_uid_to_sid(DOM_SID *sid, uid_t uid)
32 {
33         NTSTATUS ret;
34         struct id_map map;
35         struct id_map *maps[2];
36
37         DEBUG(10,("uid = [%lu]\n", (unsigned long)uid));
38
39         map.sid = sid;
40         map.xid.type = ID_TYPE_UID;
41         map.xid.id = uid;
42
43         maps[0] = ↦
44         maps[1] = NULL;
45         
46         ret = idmap_unixids_to_sids(maps);
47         if ( ! NT_STATUS_IS_OK(ret)) {
48                 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
49                 return ret;
50         }
51
52         if (map.status != ID_MAPPED) {
53                 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
54                 return NT_STATUS_NONE_MAPPED;
55         }
56
57         return NT_STATUS_OK;
58 }
59
60 /*****************************************************************
61  Returns SID mapped to the given GID.
62  If mapping is not possible returns an error.
63 *****************************************************************/  
64
65 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid)
66 {
67         NTSTATUS ret;
68         struct id_map map;
69         struct id_map *maps[2];
70
71         DEBUG(10,("gid = [%lu]\n", (unsigned long)gid));
72
73         map.sid = sid;
74         map.xid.type = ID_TYPE_GID;
75         map.xid.id = gid;
76
77         maps[0] = ↦
78         maps[1] = NULL;
79         
80         ret = idmap_unixids_to_sids(maps);
81         if ( ! NT_STATUS_IS_OK(ret)) {
82                 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
83                 return ret;
84         }
85
86         if (map.status != ID_MAPPED) {
87                 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
88                 return NT_STATUS_NONE_MAPPED;
89         }
90
91         return NT_STATUS_OK;
92 }
93
94 /*****************************************************************
95  Returns the UID mapped to the given SID.
96  If mapping is not possible or SID maps to a GID returns an error.
97 *****************************************************************/  
98
99 NTSTATUS idmap_sid_to_uid(DOM_SID *sid, uid_t *uid)
100 {
101         NTSTATUS ret;
102         struct id_map map;
103         struct id_map *maps[2];
104
105         DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_static(sid)));
106
107         map.sid = sid;
108         map.xid.type = ID_TYPE_UID;     
109         
110         maps[0] = ↦
111         maps[1] = NULL;
112         
113         ret = idmap_sids_to_unixids(maps);
114         if ( ! NT_STATUS_IS_OK(ret)) {
115                 DEBUG(10, ("error mapping sid [%s] to uid\n", 
116                            sid_string_static(sid)));
117                 return ret;
118         }
119
120         if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_UID)) {
121                 DEBUG(10, ("sid [%s] not mapped to an uid [%u,%u,%u]\n", 
122                            sid_string_static(sid), 
123                            map.status, 
124                            map.xid.type, 
125                            map.xid.id));
126                 return NT_STATUS_NONE_MAPPED;
127         }
128
129         *uid = map.xid.id;
130
131         return NT_STATUS_OK;
132 }
133
134 /*****************************************************************
135  Returns the GID mapped to the given SID.
136  If mapping is not possible or SID maps to a UID returns an error.
137 *****************************************************************/  
138
139 NTSTATUS idmap_sid_to_gid(DOM_SID *sid, gid_t *gid)
140 {
141         NTSTATUS ret;
142         struct id_map map;
143         struct id_map *maps[2];
144
145         DEBUG(10,("idmap_sid_to_gid: sid = [%s]\n", sid_string_static(sid)));
146
147         map.sid = sid;
148         map.xid.type = ID_TYPE_GID;
149         
150         maps[0] = ↦
151         maps[1] = NULL;
152         
153         ret = idmap_sids_to_unixids(maps);
154         if ( ! NT_STATUS_IS_OK(ret)) {
155                 DEBUG(10, ("error mapping sid [%s] to gid\n", 
156                            sid_string_static(sid)));
157                 return ret;
158         }
159
160         if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_GID)) {
161                 DEBUG(10, ("sid [%s] not mapped to a gid [%u,%u,%u]\n", 
162                            sid_string_static(sid), 
163                            map.status, 
164                            map.xid.type, 
165                            map.xid.id));
166                 return NT_STATUS_NONE_MAPPED;
167         }
168
169         *gid = map.xid.id;
170
171         return NT_STATUS_OK;
172 }