ctdb-tests: nfs_iterate_test() marks RPC service down
[samba4-gss.git] / nsswitch / libwbclient / wbc_sid.c
blob747addb1f5b23f1f2dcb7b01139c6d030b08799d
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind client API
6 Copyright (C) Gerald (Jerry) Carter 2007
7 Copyright (C) Volker Lendecke 2010
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* Required Headers */
26 #include "replace.h"
27 #include "libwbclient.h"
28 #include "../winbind_client.h"
29 #include "lib/util/smb_strtox.h"
31 /* Convert a sid to a string into a buffer. Return the string
32 * length. If buflen is too small, return the string length that would
33 * result if it was long enough. */
34 _PUBLIC_
35 int wbcSidToStringBuf(const struct wbcDomainSid *sid, char *buf, int buflen)
37 uint64_t id_auth;
38 int i, ofs;
40 if (!sid) {
41 strlcpy(buf, "(NULL SID)", buflen);
42 return 10; /* strlen("(NULL SID)") */
45 id_auth = (uint64_t)sid->id_auth[5] +
46 ((uint64_t)sid->id_auth[4] << 8) +
47 ((uint64_t)sid->id_auth[3] << 16) +
48 ((uint64_t)sid->id_auth[2] << 24) +
49 ((uint64_t)sid->id_auth[1] << 32) +
50 ((uint64_t)sid->id_auth[0] << 40);
52 ofs = snprintf(buf, buflen, "S-%hhu-", (unsigned char)sid->sid_rev_num);
53 if (id_auth >= UINT32_MAX) {
54 ofs += snprintf(buf + ofs, MAX(buflen - ofs, 0), "0x%llx",
55 (unsigned long long)id_auth);
56 } else {
57 ofs += snprintf(buf + ofs, MAX(buflen - ofs, 0), "%llu",
58 (unsigned long long)id_auth);
61 for (i = 0; i < sid->num_auths; i++) {
62 ofs += snprintf(buf + ofs, MAX(buflen - ofs, 0), "-%u",
63 (unsigned int)sid->sub_auths[i]);
65 return ofs;
68 /* Convert a binary SID to a character string */
69 _PUBLIC_
70 wbcErr wbcSidToString(const struct wbcDomainSid *sid,
71 char **sid_string)
73 char buf[WBC_SID_STRING_BUFLEN];
74 char *result;
75 int len;
77 if (!sid) {
78 return WBC_ERR_INVALID_SID;
81 len = wbcSidToStringBuf(sid, buf, sizeof(buf));
83 if (len >= WBC_SID_STRING_BUFLEN) {
84 return WBC_ERR_INVALID_SID;
87 result = (char *)wbcAllocateMemory(len+1, 1, NULL);
88 if (result == NULL) {
89 return WBC_ERR_NO_MEMORY;
91 memcpy(result, buf, len+1);
93 *sid_string = result;
94 return WBC_ERR_SUCCESS;
97 #define AUTHORITY_MASK (~(0xffffffffffffULL))
99 /* Convert a character string to a binary SID */
100 _PUBLIC_
101 wbcErr wbcStringToSid(const char *str,
102 struct wbcDomainSid *sid)
104 const char *p;
105 char *q;
106 int error = 0;
107 uint64_t x;
108 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
110 if (!sid) {
111 wbc_status = WBC_ERR_INVALID_PARAM;
112 BAIL_ON_WBC_ERROR(wbc_status);
115 /* Sanity check for either "S-" or "s-" */
117 if (!str
118 || (str[0]!='S' && str[0]!='s')
119 || (str[1]!='-'))
121 wbc_status = WBC_ERR_INVALID_PARAM;
122 BAIL_ON_WBC_ERROR(wbc_status);
125 /* Get the SID revision number */
127 p = str+2;
128 x = (uint64_t)smb_strtoul(p, &q, 10, &error, SMB_STR_STANDARD);
129 if (x == 0 || x > UINT8_MAX || !q || *q != '-' || error != 0) {
130 wbc_status = WBC_ERR_INVALID_SID;
131 BAIL_ON_WBC_ERROR(wbc_status);
133 sid->sid_rev_num = (uint8_t)x;
136 * Next the Identifier Authority. This is stored big-endian in a
137 * 6 byte array. If the authority value is >= UINT_MAX, then it should
138 * be expressed as a hex value, according to MS-DTYP.
140 p = q+1;
141 x = smb_strtoull(p, &q, 0, &error, SMB_STR_STANDARD);
142 if (!q || *q != '-' || (x & AUTHORITY_MASK) || error != 0) {
143 wbc_status = WBC_ERR_INVALID_SID;
144 BAIL_ON_WBC_ERROR(wbc_status);
146 sid->id_auth[5] = (x & 0x0000000000ffULL);
147 sid->id_auth[4] = (x & 0x00000000ff00ULL) >> 8;
148 sid->id_auth[3] = (x & 0x000000ff0000ULL) >> 16;
149 sid->id_auth[2] = (x & 0x0000ff000000ULL) >> 24;
150 sid->id_auth[1] = (x & 0x00ff00000000ULL) >> 32;
151 sid->id_auth[0] = (x & 0xff0000000000ULL) >> 40;
153 /* now read the the subauthorities */
154 p = q +1;
155 sid->num_auths = 0;
156 while (sid->num_auths < WBC_MAXSUBAUTHS) {
157 x = smb_strtoull(p, &q, 10, &error, SMB_STR_ALLOW_NO_CONVERSION);
158 if (p == q)
159 break;
160 if (x > UINT32_MAX || error != 0) {
161 wbc_status = WBC_ERR_INVALID_SID;
162 BAIL_ON_WBC_ERROR(wbc_status);
164 sid->sub_auths[sid->num_auths++] = x;
166 if (*q != '-') {
167 break;
169 p = q + 1;
172 /* IF we ended early, then the SID could not be converted */
174 if (q && *q!='\0') {
175 wbc_status = WBC_ERR_INVALID_SID;
176 BAIL_ON_WBC_ERROR(wbc_status);
179 wbc_status = WBC_ERR_SUCCESS;
181 done:
182 return wbc_status;
187 /* Convert a domain and name to SID */
188 _PUBLIC_
189 wbcErr wbcCtxLookupName(struct wbcContext *ctx,
190 const char *domain,
191 const char *name,
192 struct wbcDomainSid *sid,
193 enum wbcSidType *name_type)
195 struct winbindd_request request;
196 struct winbindd_response response;
197 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
199 if (!sid || !name_type) {
200 wbc_status = WBC_ERR_INVALID_PARAM;
201 BAIL_ON_WBC_ERROR(wbc_status);
204 /* Initialize request */
206 ZERO_STRUCT(request);
207 ZERO_STRUCT(response);
209 /* dst is already null terminated from the memset above */
211 strncpy(request.data.name.dom_name, domain,
212 sizeof(request.data.name.dom_name)-1);
213 strncpy(request.data.name.name, name,
214 sizeof(request.data.name.name)-1);
216 wbc_status = wbcRequestResponse(ctx, WINBINDD_LOOKUPNAME,
217 &request,
218 &response);
219 BAIL_ON_WBC_ERROR(wbc_status);
221 *name_type = (enum wbcSidType)response.data.sid.type;
222 if (*name_type == WBC_SID_NAME_UNKNOWN) {
223 return WBC_ERR_NOT_MAPPED;
226 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
227 BAIL_ON_WBC_ERROR(wbc_status);
229 wbc_status = WBC_ERR_SUCCESS;
231 done:
232 return wbc_status;
235 _PUBLIC_
236 wbcErr wbcLookupName(const char *domain,
237 const char *name,
238 struct wbcDomainSid *sid,
239 enum wbcSidType *name_type)
241 return wbcCtxLookupName(NULL, domain, name, sid, name_type);
245 /* Convert a SID to a domain and name */
246 _PUBLIC_
247 wbcErr wbcCtxLookupSid(struct wbcContext *ctx,
248 const struct wbcDomainSid *sid,
249 char **pdomain,
250 char **pname,
251 enum wbcSidType *pname_type)
253 struct winbindd_request request;
254 struct winbindd_response response;
255 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
256 char *domain, *name;
258 if (!sid) {
259 return WBC_ERR_INVALID_PARAM;
262 /* Initialize request */
264 ZERO_STRUCT(request);
265 ZERO_STRUCT(response);
267 wbcSidToStringBuf(sid, request.data.sid, sizeof(request.data.sid));
269 /* Make request */
271 wbc_status = wbcRequestResponse(ctx, WINBINDD_LOOKUPSID,
272 &request,
273 &response);
274 if (!WBC_ERROR_IS_OK(wbc_status)) {
275 return wbc_status;
278 /* Copy out result */
280 wbc_status = WBC_ERR_NO_MEMORY;
281 domain = NULL;
282 name = NULL;
284 domain = wbcStrDup(response.data.name.dom_name);
285 if (domain == NULL) {
286 goto done;
288 name = wbcStrDup(response.data.name.name);
289 if (name == NULL) {
290 goto done;
292 if (pdomain != NULL) {
293 *pdomain = domain;
294 domain = NULL;
296 if (pname != NULL) {
297 *pname = name;
298 name = NULL;
300 if (pname_type != NULL) {
301 *pname_type = (enum wbcSidType)response.data.name.type;
303 wbc_status = WBC_ERR_SUCCESS;
304 done:
305 wbcFreeMemory(name);
306 wbcFreeMemory(domain);
307 return wbc_status;
310 _PUBLIC_
311 wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
312 char **pdomain,
313 char **pname,
314 enum wbcSidType *pname_type)
316 return wbcCtxLookupSid(NULL, sid, pdomain, pname, pname_type);
319 static void wbcDomainInfosDestructor(void *ptr)
321 struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
323 while (i->short_name != NULL) {
324 wbcFreeMemory(i->short_name);
325 wbcFreeMemory(i->dns_name);
326 i += 1;
330 static void wbcTranslatedNamesDestructor(void *ptr)
332 struct wbcTranslatedName *n = (struct wbcTranslatedName *)ptr;
334 while (n->name != NULL) {
335 wbcFreeMemory(n->name);
336 n += 1;
340 _PUBLIC_
341 wbcErr wbcCtxLookupSids(struct wbcContext *ctx,
342 const struct wbcDomainSid *sids, int num_sids,
343 struct wbcDomainInfo **pdomains, int *pnum_domains,
344 struct wbcTranslatedName **pnames)
346 struct winbindd_request request;
347 struct winbindd_response response;
348 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
349 int buflen, i, extra_len, num_domains, num_names;
350 char *sidlist, *p, *q, *extra_data;
351 struct wbcDomainInfo *domains = NULL;
352 struct wbcTranslatedName *names = NULL;
353 int error = 0;
355 buflen = num_sids * (WBC_SID_STRING_BUFLEN + 1) + 1;
357 sidlist = (char *)malloc(buflen);
358 if (sidlist == NULL) {
359 return WBC_ERR_NO_MEMORY;
362 p = sidlist;
364 for (i=0; i<num_sids; i++) {
365 int remaining;
366 int len;
368 remaining = buflen - (p - sidlist);
370 len = wbcSidToStringBuf(&sids[i], p, remaining);
371 if (len > remaining) {
372 free(sidlist);
373 return WBC_ERR_UNKNOWN_FAILURE;
376 p += len;
377 *p++ = '\n';
379 *p++ = '\0';
381 ZERO_STRUCT(request);
382 ZERO_STRUCT(response);
384 request.extra_data.data = sidlist;
385 request.extra_len = p - sidlist;
387 wbc_status = wbcRequestResponse(ctx, WINBINDD_LOOKUPSIDS,
388 &request, &response);
389 free(sidlist);
390 if (!WBC_ERROR_IS_OK(wbc_status)) {
391 return wbc_status;
394 extra_len = response.length - sizeof(struct winbindd_response);
395 extra_data = (char *)response.extra_data.data;
397 if ((extra_len <= 0) || (extra_data[extra_len-1] != '\0')) {
398 goto wbc_err_invalid;
401 p = extra_data;
403 num_domains = smb_strtoul(p, &q, 10, &error, SMB_STR_STANDARD);
404 if (*q != '\n' || error != 0) {
405 goto wbc_err_invalid;
407 p = q+1;
409 domains = (struct wbcDomainInfo *)wbcAllocateMemory(
410 num_domains+1, sizeof(struct wbcDomainInfo),
411 wbcDomainInfosDestructor);
412 if (domains == NULL) {
413 wbc_status = WBC_ERR_NO_MEMORY;
414 goto fail;
417 for (i=0; i<num_domains; i++) {
419 q = strchr(p, ' ');
420 if (q == NULL) {
421 goto wbc_err_invalid;
423 *q = '\0';
424 wbc_status = wbcStringToSid(p, &domains[i].sid);
425 if (!WBC_ERROR_IS_OK(wbc_status)) {
426 goto fail;
428 p = q+1;
430 q = strchr(p, '\n');
431 if (q == NULL) {
432 goto wbc_err_invalid;
434 *q = '\0';
435 domains[i].short_name = wbcStrDup(p);
436 if (domains[i].short_name == NULL) {
437 wbc_status = WBC_ERR_NO_MEMORY;
438 goto fail;
440 p = q+1;
443 num_names = smb_strtoul(p, &q, 10, &error, SMB_STR_STANDARD);
444 if (*q != '\n' || error != 0) {
445 goto wbc_err_invalid;
447 p = q+1;
449 if (num_names != num_sids) {
450 goto wbc_err_invalid;
453 names = (struct wbcTranslatedName *)wbcAllocateMemory(
454 num_names+1, sizeof(struct wbcTranslatedName),
455 wbcTranslatedNamesDestructor);
456 if (names == NULL) {
457 wbc_status = WBC_ERR_NO_MEMORY;
458 goto fail;
461 for (i=0; i<num_names; i++) {
463 names[i].domain_index = smb_strtoul(p,
466 &error,
467 SMB_STR_STANDARD);
468 if (names[i].domain_index < 0 || error != 0) {
469 goto wbc_err_invalid;
471 if (names[i].domain_index >= num_domains) {
472 goto wbc_err_invalid;
475 if (*q != ' ') {
476 goto wbc_err_invalid;
478 p = q+1;
480 names[i].type = smb_strtoul(p, &q, 10, &error, SMB_STR_STANDARD);
481 if (*q != ' ' || error != 0) {
482 goto wbc_err_invalid;
484 p = q+1;
486 q = strchr(p, '\n');
487 if (q == NULL) {
488 goto wbc_err_invalid;
490 *q = '\0';
491 names[i].name = wbcStrDup(p);
492 if (names[i].name == NULL) {
493 wbc_status = WBC_ERR_NO_MEMORY;
494 goto fail;
496 p = q+1;
498 if (*p != '\0') {
499 goto wbc_err_invalid;
502 *pdomains = domains;
503 *pnames = names;
504 winbindd_free_response(&response);
505 return WBC_ERR_SUCCESS;
507 wbc_err_invalid:
508 wbc_status = WBC_ERR_INVALID_RESPONSE;
509 fail:
510 winbindd_free_response(&response);
511 wbcFreeMemory(domains);
512 wbcFreeMemory(names);
513 return wbc_status;
516 _PUBLIC_
517 wbcErr wbcLookupSids(const struct wbcDomainSid *sids, int num_sids,
518 struct wbcDomainInfo **pdomains, int *pnum_domains,
519 struct wbcTranslatedName **pnames)
521 return wbcCtxLookupSids(NULL, sids, num_sids, pdomains,
522 pnum_domains, pnames);
525 /* Translate a collection of RIDs within a domain to names */
527 _PUBLIC_
528 wbcErr wbcCtxLookupRids(struct wbcContext *ctx, struct wbcDomainSid *dom_sid,
529 int num_rids,
530 uint32_t *rids,
531 const char **pp_domain_name,
532 const char ***pnames,
533 enum wbcSidType **ptypes)
535 int i;
536 size_t len, ridbuf_size;
537 char *ridlist;
538 char *p;
539 int error = 0;
540 struct winbindd_request request;
541 struct winbindd_response response;
542 char *domain_name = NULL;
543 const char **names = NULL;
544 enum wbcSidType *types = NULL;
545 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
547 /* Initialise request */
549 ZERO_STRUCT(request);
550 ZERO_STRUCT(response);
552 if (!dom_sid || (num_rids == 0)) {
553 wbc_status = WBC_ERR_INVALID_PARAM;
554 BAIL_ON_WBC_ERROR(wbc_status);
557 wbcSidToStringBuf(dom_sid, request.data.sid, sizeof(request.data.sid));
559 /* Even if all the Rids were of maximum 32bit values,
560 we would only have 11 bytes per rid in the final array
561 ("4294967296" + \n). Add one more byte for the
562 terminating '\0' */
564 ridbuf_size = (sizeof(char)*11) * num_rids + 1;
566 ridlist = (char *)malloc(ridbuf_size);
567 BAIL_ON_PTR_ERROR(ridlist, wbc_status);
569 len = 0;
570 for (i=0; i<num_rids; i++) {
571 len += snprintf(ridlist + len, ridbuf_size - len, "%u\n",
572 rids[i]);
574 ridlist[len] = '\0';
575 len += 1;
577 request.extra_data.data = ridlist;
578 request.extra_len = len;
580 wbc_status = wbcRequestResponse(ctx, WINBINDD_LOOKUPRIDS,
581 &request,
582 &response);
583 free(ridlist);
584 BAIL_ON_WBC_ERROR(wbc_status);
586 domain_name = wbcStrDup(response.data.domain_name);
587 BAIL_ON_PTR_ERROR(domain_name, wbc_status);
589 names = wbcAllocateStringArray(num_rids);
590 BAIL_ON_PTR_ERROR(names, wbc_status);
592 types = (enum wbcSidType *)wbcAllocateMemory(
593 num_rids, sizeof(enum wbcSidType), NULL);
594 BAIL_ON_PTR_ERROR(types, wbc_status);
596 p = (char *)response.extra_data.data;
598 for (i=0; i<num_rids; i++) {
599 char *q;
601 if (*p == '\0') {
602 wbc_status = WBC_ERR_INVALID_RESPONSE;
603 goto done;
606 types[i] = (enum wbcSidType)smb_strtoul(p,
609 &error,
610 SMB_STR_STANDARD);
612 if (*q != ' ' || error != 0) {
613 wbc_status = WBC_ERR_INVALID_RESPONSE;
614 goto done;
617 p = q+1;
619 if ((q = strchr(p, '\n')) == NULL) {
620 wbc_status = WBC_ERR_INVALID_RESPONSE;
621 goto done;
624 *q = '\0';
626 names[i] = strdup(p);
627 BAIL_ON_PTR_ERROR(names[i], wbc_status);
629 p = q+1;
632 if (*p != '\0') {
633 wbc_status = WBC_ERR_INVALID_RESPONSE;
634 goto done;
637 wbc_status = WBC_ERR_SUCCESS;
639 done:
640 winbindd_free_response(&response);
642 if (WBC_ERROR_IS_OK(wbc_status)) {
643 *pp_domain_name = domain_name;
644 *pnames = names;
645 *ptypes = types;
647 else {
648 wbcFreeMemory(domain_name);
649 wbcFreeMemory(names);
650 wbcFreeMemory(types);
653 return wbc_status;
656 _PUBLIC_
657 wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
658 int num_rids,
659 uint32_t *rids,
660 const char **pp_domain_name,
661 const char ***pnames,
662 enum wbcSidType **ptypes)
664 return wbcCtxLookupRids(NULL, dom_sid, num_rids, rids,
665 pp_domain_name, pnames, ptypes);
668 /* Get the groups a user belongs to */
669 _PUBLIC_
670 wbcErr wbcCtxLookupUserSids(struct wbcContext *ctx,
671 const struct wbcDomainSid *user_sid,
672 bool domain_groups_only,
673 uint32_t *num_sids,
674 struct wbcDomainSid **_sids)
676 uint32_t i;
677 const char *s;
678 struct winbindd_request request;
679 struct winbindd_response response;
680 struct wbcDomainSid *sids = NULL;
681 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
682 int cmd;
684 /* Initialise request */
686 ZERO_STRUCT(request);
687 ZERO_STRUCT(response);
689 if (!user_sid) {
690 wbc_status = WBC_ERR_INVALID_PARAM;
691 BAIL_ON_WBC_ERROR(wbc_status);
694 wbcSidToStringBuf(user_sid, request.data.sid, sizeof(request.data.sid));
696 if (domain_groups_only) {
697 cmd = WINBINDD_GETUSERDOMGROUPS;
698 } else {
699 cmd = WINBINDD_GETUSERSIDS;
702 wbc_status = wbcRequestResponse(ctx, cmd,
703 &request,
704 &response);
705 BAIL_ON_WBC_ERROR(wbc_status);
707 if (response.data.num_entries &&
708 !response.extra_data.data) {
709 wbc_status = WBC_ERR_INVALID_RESPONSE;
710 BAIL_ON_WBC_ERROR(wbc_status);
713 sids = (struct wbcDomainSid *)wbcAllocateMemory(
714 response.data.num_entries, sizeof(struct wbcDomainSid),
715 NULL);
716 BAIL_ON_PTR_ERROR(sids, wbc_status);
718 s = (const char *)response.extra_data.data;
719 for (i = 0; i < response.data.num_entries; i++) {
720 char *n = strchr(s, '\n');
721 if (n) {
722 *n = '\0';
724 wbc_status = wbcStringToSid(s, &sids[i]);
725 BAIL_ON_WBC_ERROR(wbc_status);
726 s += strlen(s) + 1;
729 *num_sids = response.data.num_entries;
730 *_sids = sids;
731 sids = NULL;
732 wbc_status = WBC_ERR_SUCCESS;
734 done:
735 winbindd_free_response(&response);
736 if (sids) {
737 wbcFreeMemory(sids);
740 return wbc_status;
743 _PUBLIC_
744 wbcErr wbcLookupUserSids(const struct wbcDomainSid *user_sid,
745 bool domain_groups_only,
746 uint32_t *num_sids,
747 struct wbcDomainSid **_sids)
749 return wbcCtxLookupUserSids(NULL, user_sid, domain_groups_only,
750 num_sids, _sids);
753 static inline
754 wbcErr _sid_to_rid(struct wbcDomainSid *sid, uint32_t *rid)
756 if (sid->num_auths < 1) {
757 return WBC_ERR_INVALID_RESPONSE;
759 *rid = sid->sub_auths[sid->num_auths - 1];
761 return WBC_ERR_SUCCESS;
764 /* Get alias membership for sids */
765 _PUBLIC_
766 wbcErr wbcCtxGetSidAliases(struct wbcContext *ctx,
767 const struct wbcDomainSid *dom_sid,
768 struct wbcDomainSid *sids,
769 uint32_t num_sids,
770 uint32_t **alias_rids,
771 uint32_t *num_alias_rids)
773 uint32_t i;
774 const char *s;
775 struct winbindd_request request;
776 struct winbindd_response response;
777 ssize_t extra_data_len = 0;
778 char * extra_data = NULL;
779 ssize_t buflen = 0;
780 struct wbcDomainSid sid;
781 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
782 uint32_t * rids = NULL;
784 /* Initialise request */
786 ZERO_STRUCT(request);
787 ZERO_STRUCT(response);
789 if (!dom_sid) {
790 wbc_status = WBC_ERR_INVALID_PARAM;
791 goto done;
794 wbcSidToStringBuf(dom_sid, request.data.sid, sizeof(request.data.sid));
796 /* Lets assume each sid is around 57 characters
797 * S-1-5-21-AAAAAAAAAAA-BBBBBBBBBBB-CCCCCCCCCCC-DDDDDDDDDDD\n */
798 buflen = 57 * num_sids;
799 extra_data = (char *)malloc(buflen);
800 if (!extra_data) {
801 wbc_status = WBC_ERR_NO_MEMORY;
802 goto done;
805 /* Build the sid list */
806 for (i=0; i<num_sids; i++) {
807 char sid_str[WBC_SID_STRING_BUFLEN];
808 size_t sid_len;
810 sid_len = wbcSidToStringBuf(&sids[i], sid_str, sizeof(sid_str));
812 if (buflen < extra_data_len + sid_len + 2) {
813 char * tmp_data = NULL;
814 buflen *= 2;
815 tmp_data = (char *)realloc(extra_data, buflen);
816 if (!tmp_data) {
817 wbc_status = WBC_ERR_NO_MEMORY;
818 BAIL_ON_WBC_ERROR(wbc_status);
820 extra_data = tmp_data;
823 strncpy(&extra_data[extra_data_len], sid_str,
824 buflen - extra_data_len);
825 extra_data_len += sid_len;
826 extra_data[extra_data_len++] = '\n';
827 extra_data[extra_data_len] = '\0';
829 extra_data_len += 1;
831 request.extra_data.data = extra_data;
832 request.extra_len = extra_data_len;
834 wbc_status = wbcRequestResponse(ctx, WINBINDD_GETSIDALIASES,
835 &request,
836 &response);
837 BAIL_ON_WBC_ERROR(wbc_status);
839 if (response.data.num_entries &&
840 !response.extra_data.data) {
841 wbc_status = WBC_ERR_INVALID_RESPONSE;
842 goto done;
845 rids = (uint32_t *)wbcAllocateMemory(response.data.num_entries,
846 sizeof(uint32_t), NULL);
847 BAIL_ON_PTR_ERROR(rids, wbc_status);
849 s = (const char *)response.extra_data.data;
850 for (i = 0; i < response.data.num_entries; i++) {
851 char *n = strchr(s, '\n');
852 if (n) {
853 *n = '\0';
855 wbc_status = wbcStringToSid(s, &sid);
856 BAIL_ON_WBC_ERROR(wbc_status);
857 wbc_status = _sid_to_rid(&sid, &rids[i]);
858 BAIL_ON_WBC_ERROR(wbc_status);
859 s += strlen(s) + 1;
862 *num_alias_rids = response.data.num_entries;
863 *alias_rids = rids;
864 rids = NULL;
865 wbc_status = WBC_ERR_SUCCESS;
867 done:
868 free(extra_data);
869 winbindd_free_response(&response);
870 wbcFreeMemory(rids);
871 return wbc_status;
874 _PUBLIC_
875 wbcErr wbcGetSidAliases(const struct wbcDomainSid *dom_sid,
876 struct wbcDomainSid *sids,
877 uint32_t num_sids,
878 uint32_t **alias_rids,
879 uint32_t *num_alias_rids)
881 return wbcCtxGetSidAliases(NULL, dom_sid, sids, num_sids,
882 alias_rids, num_alias_rids);
886 /* Lists Users */
887 _PUBLIC_
888 wbcErr wbcCtxListUsers(struct wbcContext *ctx,
889 const char *domain_name,
890 uint32_t *_num_users,
891 const char ***_users)
893 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
894 struct winbindd_request request;
895 struct winbindd_response response;
896 uint32_t num_users = 0;
897 const char **users = NULL;
898 const char *next;
900 /* Initialise request */
902 ZERO_STRUCT(request);
903 ZERO_STRUCT(response);
905 if (domain_name) {
906 strncpy(request.domain_name, domain_name,
907 sizeof(request.domain_name)-1);
910 wbc_status = wbcRequestResponse(ctx, WINBINDD_LIST_USERS,
911 &request,
912 &response);
913 BAIL_ON_WBC_ERROR(wbc_status);
915 users = wbcAllocateStringArray(response.data.num_entries);
916 if (users == NULL) {
917 return WBC_ERR_NO_MEMORY;
920 /* Look through extra data */
922 next = (const char *)response.extra_data.data;
923 while (next) {
924 const char *current;
925 char *k;
927 if (num_users >= response.data.num_entries) {
928 wbc_status = WBC_ERR_INVALID_RESPONSE;
929 goto done;
932 current = next;
933 k = strchr(next, ',');
935 if (k) {
936 k[0] = '\0';
937 next = k+1;
938 } else {
939 next = NULL;
942 users[num_users] = strdup(current);
943 BAIL_ON_PTR_ERROR(users[num_users], wbc_status);
944 num_users += 1;
946 if (num_users != response.data.num_entries) {
947 wbc_status = WBC_ERR_INVALID_RESPONSE;
948 goto done;
951 *_num_users = response.data.num_entries;
952 *_users = users;
953 users = NULL;
954 wbc_status = WBC_ERR_SUCCESS;
956 done:
957 winbindd_free_response(&response);
958 wbcFreeMemory(users);
959 return wbc_status;
962 _PUBLIC_
963 wbcErr wbcListUsers(const char *domain_name,
964 uint32_t *_num_users,
965 const char ***_users)
967 return wbcCtxListUsers(NULL, domain_name, _num_users, _users);
970 /* Lists Groups */
971 _PUBLIC_
972 wbcErr wbcCtxListGroups(struct wbcContext *ctx,
973 const char *domain_name,
974 uint32_t *_num_groups,
975 const char ***_groups)
977 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
978 struct winbindd_request request;
979 struct winbindd_response response;
980 uint32_t num_groups = 0;
981 const char **groups = NULL;
982 const char *next;
984 /* Initialise request */
986 ZERO_STRUCT(request);
987 ZERO_STRUCT(response);
989 if (domain_name) {
990 strncpy(request.domain_name, domain_name,
991 sizeof(request.domain_name)-1);
994 wbc_status = wbcRequestResponse(ctx, WINBINDD_LIST_GROUPS,
995 &request,
996 &response);
997 BAIL_ON_WBC_ERROR(wbc_status);
999 groups = wbcAllocateStringArray(response.data.num_entries);
1000 if (groups == NULL) {
1001 return WBC_ERR_NO_MEMORY;
1004 /* Look through extra data */
1006 next = (const char *)response.extra_data.data;
1007 while (next) {
1008 const char *current;
1009 char *k;
1011 if (num_groups >= response.data.num_entries) {
1012 wbc_status = WBC_ERR_INVALID_RESPONSE;
1013 goto done;
1016 current = next;
1017 k = strchr(next, ',');
1019 if (k) {
1020 k[0] = '\0';
1021 next = k+1;
1022 } else {
1023 next = NULL;
1026 groups[num_groups] = strdup(current);
1027 BAIL_ON_PTR_ERROR(groups[num_groups], wbc_status);
1028 num_groups += 1;
1030 if (num_groups != response.data.num_entries) {
1031 wbc_status = WBC_ERR_INVALID_RESPONSE;
1032 goto done;
1035 *_num_groups = response.data.num_entries;
1036 *_groups = groups;
1037 groups = NULL;
1038 wbc_status = WBC_ERR_SUCCESS;
1040 done:
1041 winbindd_free_response(&response);
1042 wbcFreeMemory(groups);
1043 return wbc_status;
1046 _PUBLIC_
1047 wbcErr wbcListGroups(const char *domain_name,
1048 uint32_t *_num_groups,
1049 const char ***_groups)
1051 return wbcCtxListGroups(NULL, domain_name, _num_groups, _groups);
1054 _PUBLIC_
1055 wbcErr wbcCtxGetDisplayName(struct wbcContext *ctx,
1056 const struct wbcDomainSid *sid,
1057 char **pdomain,
1058 char **pfullname,
1059 enum wbcSidType *pname_type)
1061 wbcErr wbc_status;
1062 char *domain = NULL;
1063 char *name = NULL;
1064 enum wbcSidType name_type;
1066 wbc_status = wbcCtxLookupSid(ctx, sid, &domain, &name, &name_type);
1067 BAIL_ON_WBC_ERROR(wbc_status);
1069 if (name_type == WBC_SID_NAME_USER) {
1070 uid_t uid;
1071 struct passwd *pwd;
1073 wbc_status = wbcCtxSidToUid(ctx, sid, &uid);
1074 BAIL_ON_WBC_ERROR(wbc_status);
1076 wbc_status = wbcCtxGetpwuid(ctx, uid, &pwd);
1077 BAIL_ON_WBC_ERROR(wbc_status);
1079 wbcFreeMemory(name);
1081 name = wbcStrDup(pwd->pw_gecos);
1082 wbcFreeMemory(pwd);
1083 BAIL_ON_PTR_ERROR(name, wbc_status);
1086 wbc_status = WBC_ERR_SUCCESS;
1088 done:
1089 if (WBC_ERROR_IS_OK(wbc_status)) {
1090 *pdomain = domain;
1091 *pfullname = name;
1092 *pname_type = name_type;
1093 } else {
1094 wbcFreeMemory(domain);
1095 wbcFreeMemory(name);
1098 return wbc_status;
1101 _PUBLIC_
1102 wbcErr wbcGetDisplayName(const struct wbcDomainSid *sid,
1103 char **pdomain,
1104 char **pfullname,
1105 enum wbcSidType *pname_type)
1107 return wbcCtxGetDisplayName(NULL, sid, pdomain, pfullname, pname_type);
1110 _PUBLIC_
1111 const char* wbcSidTypeString(enum wbcSidType type)
1113 switch (type) {
1114 case WBC_SID_NAME_USE_NONE: return "SID_NONE";
1115 case WBC_SID_NAME_USER: return "SID_USER";
1116 case WBC_SID_NAME_DOM_GRP: return "SID_DOM_GROUP";
1117 case WBC_SID_NAME_DOMAIN: return "SID_DOMAIN";
1118 case WBC_SID_NAME_ALIAS: return "SID_ALIAS";
1119 case WBC_SID_NAME_WKN_GRP: return "SID_WKN_GROUP";
1120 case WBC_SID_NAME_DELETED: return "SID_DELETED";
1121 case WBC_SID_NAME_INVALID: return "SID_INVALID";
1122 case WBC_SID_NAME_UNKNOWN: return "SID_UNKNOWN";
1123 case WBC_SID_NAME_COMPUTER: return "SID_COMPUTER";
1124 case WBC_SID_NAME_LABEL: return "SID_LABEL";
1125 default: return "Unknown type";