Make use of ADD_TO_ARRAY
[samba.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         struct id_map *maps;
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         maps = &map;
42
43         ret = idmap_unixids_to_sids(&maps, 1);
44         if ( ! NT_STATUS_IS_OK(ret)) {
45                 DEBUG(10, ("error mapping uid [%lu]\n", (unsigned long)uid));
46                 return ret;
47         }
48
49         if (map.status != ID_MAPPED) {
50                 DEBUG(10, ("uid [%lu] not mapped\n", (unsigned long)uid));
51                 return NT_STATUS_NONE_MAPPED;
52         }
53
54         return NT_STATUS_OK;
55 }
56
57 /*****************************************************************
58  Returns SID mapped to the given GID.
59  If mapping is not possible returns an error.
60 *****************************************************************/  
61
62 NTSTATUS idmap_gid_to_sid(DOM_SID *sid, gid_t gid)
63 {
64         NTSTATUS ret;
65         struct id_map map;
66         struct id_map *maps;
67
68         DEBUG(10,("gid = [%lu]\n", (unsigned long)gid));
69
70         map.sid = sid;
71         map.xid.type = ID_TYPE_GID;
72         map.xid.id = gid;
73         maps = &map;
74
75         ret = idmap_unixids_to_sids(&maps, 1);
76         if ( ! NT_STATUS_IS_OK(ret)) {
77                 DEBUG(10, ("error mapping gid [%lu]\n", (unsigned long)gid));
78                 return ret;
79         }
80
81         if (map.status != ID_MAPPED) {
82                 DEBUG(10, ("gid [%lu] not mapped\n", (unsigned long)gid));
83                 return NT_STATUS_NONE_MAPPED;
84         }
85
86         return NT_STATUS_OK;
87 }
88
89 /*****************************************************************
90  Returns the UID mapped to the given SID.
91  If mapping is not possible or SID maps to a GID returns an error.
92 *****************************************************************/  
93
94 NTSTATUS idmap_sid_to_uid(DOM_SID *sid, uid_t *uid)
95 {
96         NTSTATUS ret;
97         struct id_map map;
98         struct id_map *maps;
99
100         DEBUG(10,("idmap_sid_to_uid: sid = [%s]\n", sid_string_dbg(sid)));
101
102         map.sid = sid;
103         map.xid.type = ID_TYPE_UID;
104         maps = &map;
105
106         ret = idmap_sids_to_unixids(&maps, 1);
107         if ( ! NT_STATUS_IS_OK(ret)) {
108                 DEBUG(10, ("error mapping sid [%s] to uid\n", 
109                            sid_string_dbg(sid)));
110                 return ret;
111         }
112
113         if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_UID)) {
114                 DEBUG(10, ("sid [%s] not mapped to an uid [%u,%u,%u]\n", 
115                            sid_string_dbg(sid),
116                            map.status, 
117                            map.xid.type, 
118                            map.xid.id));
119                 return NT_STATUS_NONE_MAPPED;
120         }
121
122         *uid = map.xid.id;
123
124         return NT_STATUS_OK;
125 }
126
127 /*****************************************************************
128  Returns the GID mapped to the given SID.
129  If mapping is not possible or SID maps to a UID returns an error.
130 *****************************************************************/  
131
132 NTSTATUS idmap_sid_to_gid(DOM_SID *sid, gid_t *gid)
133 {
134         NTSTATUS ret;
135         struct id_map map;
136         struct id_map *maps;
137
138         DEBUG(10,("idmap_sid_to_gid: sid = [%s]\n", sid_string_dbg(sid)));
139
140         map.sid = sid;
141         map.xid.type = ID_TYPE_GID;
142         maps = &map;
143
144         ret = idmap_sids_to_unixids(&maps, 1);
145         if ( ! NT_STATUS_IS_OK(ret)) {
146                 DEBUG(10, ("error mapping sid [%s] to gid\n", 
147                            sid_string_dbg(sid)));
148                 return ret;
149         }
150
151         if ((map.status != ID_MAPPED) || (map.xid.type != ID_TYPE_GID)) {
152                 DEBUG(10, ("sid [%s] not mapped to a gid [%u,%u]\n", 
153                            sid_string_dbg(sid),
154                            map.status, 
155                            map.xid.type));
156                 return NT_STATUS_NONE_MAPPED;
157         }
158
159         *gid = map.xid.id;
160
161         return NT_STATUS_OK;
162 }