r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[ira/wip.git] / source3 / 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, 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         struct id_map *maps[2];
35
36         DEBUG(10,("uid = [%lu]\n", (unsigned long)uid));
37
38         map.sid = sid;
39         map.xid.type = ID_TYPE_UID;
40         map.xid.id = uid;
41
42         maps[0] = &map;
43         maps[1] = NULL;
44         
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;
49         }
50
51         if (map.status != ID_MAPPED) {
52                 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
53                 return NT_STATUS_NONE_MAPPED;
54         }
55
56         return NT_STATUS_OK;
57 }
58
59 /*****************************************************************
60  Returns SID mapped to the given GID.
61  If mapping is not possible returns an error.
62 *****************************************************************/  
63
64 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid)
65 {
66         NTSTATUS ret;
67         struct id_map map;
68         struct id_map *maps[2];
69
70         DEBUG(10,("gid = [%lu]\n", (unsigned long)gid));
71
72         map.sid = sid;
73         map.xid.type = ID_TYPE_GID;
74         map.xid.id = gid;
75
76         maps[0] = &map;
77         maps[1] = NULL;
78         
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;
83         }
84
85         if (map.status != ID_MAPPED) {
86                 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
87                 return NT_STATUS_NONE_MAPPED;
88         }
89
90         return NT_STATUS_OK;
91 }
92
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 *****************************************************************/  
97
98 NTSTATUS idmap_sid_to_uid(DOM_SID *sid, uid_t *uid)
99 {
100         NTSTATUS ret;
101         struct id_map map;
102         struct id_map *maps[2];
103
104         DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_static(sid)));
105
106         map.sid = sid;
107         map.xid.type = ID_TYPE_UID;     
108         
109         maps[0] = &map;
110         maps[1] = NULL;
111         
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_static(sid)));
116                 return ret;
117         }
118
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_static(sid), 
122                            map.status, 
123                            map.xid.type, 
124                            map.xid.id));
125                 return NT_STATUS_NONE_MAPPED;
126         }
127
128         *uid = map.xid.id;
129
130         return NT_STATUS_OK;
131 }
132
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 *****************************************************************/  
137
138 NTSTATUS idmap_sid_to_gid(DOM_SID *sid, gid_t *gid)
139 {
140         NTSTATUS ret;
141         struct id_map map;
142         struct id_map *maps[2];
143
144         DEBUG(10,("idmap_sid_to_gid: sid = [%s]\n", sid_string_static(sid)));
145
146         map.sid = sid;
147         map.xid.type = ID_TYPE_GID;
148         
149         maps[0] = &map;
150         maps[1] = NULL;
151         
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_static(sid)));
156                 return ret;
157         }
158
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_static(sid), 
162                            map.status, 
163                            map.xid.type, 
164                            map.xid.id));
165                 return NT_STATUS_NONE_MAPPED;
166         }
167
168         *gid = map.xid.id;
169
170         return NT_STATUS_OK;
171 }