Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Winbind client API - SSSD version
5 :
6 : Copyright (C) Sumit Bose <sbose@redhat.com> 2014
7 :
8 : This library is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU Lesser General Public
10 : License as published by the Free Software Foundation; either
11 : version 3 of the License, or (at your option) any later version.
12 :
13 : This library is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : Library General Public License for more details.
17 :
18 : You should have received a copy of the GNU Lesser General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : /* Required Headers */
23 :
24 : #include "sss_client/idmap/sss_nss_idmap.h"
25 :
26 : #include "libwbclient.h"
27 : #include "wbc_sssd_internal.h"
28 :
29 : /* Convert a Windows SID to a Unix uid, allocating an uid if needed */
30 0 : wbcErr wbcSidToUid(const struct wbcDomainSid *sid, uid_t *puid)
31 : {
32 : int ret;
33 : char *sid_str;
34 : uint32_t id;
35 : enum sss_id_type type;
36 : wbcErr wbc_status;
37 :
38 0 : wbc_status = wbcSidToString(sid, &sid_str);
39 0 : if (!WBC_ERROR_IS_OK(wbc_status)) {
40 0 : return wbc_status;
41 : }
42 :
43 0 : ret = sss_nss_getidbysid(sid_str, &id, &type);
44 0 : wbcFreeMemory(sid_str);
45 0 : if (ret != 0) {
46 0 : return WBC_ERR_UNKNOWN_FAILURE;
47 : }
48 :
49 0 : if (type != SSS_ID_TYPE_UID && type != SSS_ID_TYPE_BOTH) {
50 0 : return WBC_ERR_UNKNOWN_GROUP;
51 : }
52 :
53 0 : *puid = (uid_t) id;
54 :
55 0 : return WBC_ERR_SUCCESS;
56 : }
57 :
58 : /* Convert a Unix uid to a Windows SID, allocating a SID if needed */
59 0 : wbcErr wbcUidToSid(uid_t uid, struct wbcDomainSid *sid)
60 : {
61 : int ret;
62 : char *str_sid;
63 : enum sss_id_type type;
64 : wbcErr wbc_status;
65 :
66 0 : ret = sss_nss_getsidbyid(uid, &str_sid, &type);
67 0 : if (ret != 0) {
68 0 : return WBC_ERR_UNKNOWN_FAILURE;
69 : }
70 :
71 0 : if (type != SSS_ID_TYPE_UID && type != SSS_ID_TYPE_BOTH) {
72 0 : free(str_sid);
73 0 : return WBC_ERR_UNKNOWN_USER;
74 : }
75 :
76 0 : wbc_status = wbcStringToSid(str_sid, sid);
77 0 : free(str_sid);
78 0 : if (!WBC_ERROR_IS_OK(wbc_status)) {
79 0 : return wbc_status;
80 : }
81 :
82 0 : return WBC_ERR_SUCCESS;
83 : }
84 :
85 : /** @brief Convert a Windows SID to a Unix gid, allocating a gid if needed
86 : *
87 : * @param *sid Pointer to the domain SID to be resolved
88 : * @param *pgid Pointer to the resolved gid_t value
89 : *
90 : * @return #wbcErr
91 : *
92 : **/
93 :
94 0 : wbcErr wbcSidToGid(const struct wbcDomainSid *sid, gid_t *pgid)
95 : {
96 : int ret;
97 : char *sid_str;
98 : uint32_t id;
99 : enum sss_id_type type;
100 : wbcErr wbc_status;
101 :
102 0 : wbc_status = wbcSidToString(sid, &sid_str);
103 0 : if (!WBC_ERROR_IS_OK(wbc_status)) {
104 0 : return wbc_status;
105 : }
106 :
107 0 : ret = sss_nss_getidbysid(sid_str, &id, &type);
108 0 : wbcFreeMemory(sid_str);
109 0 : if (ret != 0) {
110 0 : return WBC_ERR_UNKNOWN_FAILURE;
111 : }
112 :
113 0 : if (type != SSS_ID_TYPE_GID && type != SSS_ID_TYPE_BOTH) {
114 0 : return WBC_ERR_UNKNOWN_GROUP;
115 : }
116 :
117 0 : *pgid = (gid_t) id;
118 :
119 0 : return WBC_ERR_SUCCESS;
120 : }
121 :
122 : /* Convert a Unix gid to a Windows SID, allocating a SID if needed */
123 0 : wbcErr wbcGidToSid(gid_t gid, struct wbcDomainSid *sid)
124 : {
125 : int ret;
126 : char *str_sid;
127 : enum sss_id_type type;
128 : wbcErr wbc_status;
129 :
130 0 : ret = sss_nss_getsidbyid(gid, &str_sid, &type);
131 0 : if (ret != 0) {
132 0 : return WBC_ERR_UNKNOWN_FAILURE;
133 : }
134 :
135 0 : if (type != SSS_ID_TYPE_GID && type != SSS_ID_TYPE_BOTH) {
136 0 : free(str_sid);
137 0 : return WBC_ERR_UNKNOWN_USER;
138 : }
139 :
140 0 : wbc_status = wbcStringToSid(str_sid, sid);
141 0 : free(str_sid);
142 0 : if (!WBC_ERROR_IS_OK(wbc_status)) {
143 0 : return wbc_status;
144 : }
145 :
146 0 : return WBC_ERR_SUCCESS;
147 : }
148 :
149 : /* Obtain a new uid from Winbind */
150 0 : wbcErr wbcAllocateUid(uid_t *puid)
151 : {
152 : /* Not supported by SSSD */
153 0 : WBC_SSSD_NOT_IMPLEMENTED;
154 : }
155 :
156 : /* Obtain a new gid from Winbind */
157 0 : wbcErr wbcAllocateGid(gid_t *pgid)
158 : {
159 : /* Not supported by SSSD */
160 0 : WBC_SSSD_NOT_IMPLEMENTED;
161 : }
162 :
163 : /* Convert a list of SIDs */
164 0 : wbcErr wbcSidsToUnixIds(const struct wbcDomainSid *sids, uint32_t num_sids,
165 : struct wbcUnixId *ids)
166 : {
167 : int ret;
168 : char *sid_str;
169 : uint32_t id;
170 : enum sss_id_type type;
171 : size_t c;
172 : wbcErr wbc_status;
173 :
174 0 : for (c = 0; c < num_sids; c++) {
175 0 : type = SSS_ID_TYPE_NOT_SPECIFIED;
176 0 : wbc_status = wbcSidToString(&sids[c], &sid_str);
177 0 : if (WBC_ERROR_IS_OK(wbc_status)) {
178 0 : ret = sss_nss_getidbysid(sid_str, &id, &type);
179 0 : wbcFreeMemory(sid_str);
180 0 : if (ret != 0) {
181 0 : type = SSS_ID_TYPE_NOT_SPECIFIED;
182 : }
183 : }
184 :
185 0 : switch (type) {
186 : case SSS_ID_TYPE_UID:
187 0 : ids[c].type = WBC_ID_TYPE_UID;
188 0 : ids[c].id.uid = (uid_t) id;
189 0 : break;
190 : case SSS_ID_TYPE_GID:
191 0 : ids[c].type = WBC_ID_TYPE_GID;
192 0 : ids[c].id.gid = (gid_t) id;
193 0 : break;
194 : case SSS_ID_TYPE_BOTH:
195 0 : ids[c].type = WBC_ID_TYPE_BOTH;
196 0 : ids[c].id.uid = (uid_t) id;
197 0 : break;
198 : default:
199 0 : ids[c].type = WBC_ID_TYPE_NOT_SPECIFIED;
200 : }
201 : }
202 :
203 0 : return WBC_ERR_SUCCESS;
204 : }
|