debug: Set close-on-exec for the main log file FD
[Samba.git] / nsswitch / libwbclient / wbc_sid.c
blob82ac339865e3cd0a3ab3e640bf244c5332e5a93c
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"
30 /* Convert a sid to a string into a buffer. Return the string
31 * length. If buflen is too small, return the string length that would
32 * result if it was long enough. */
33 int wbcSidToStringBuf(const struct wbcDomainSid *sid, char *buf, int buflen)
35 uint32_t id_auth;
36 int i, ofs;
38 if (!sid) {
39 strlcpy(buf, "(NULL SID)", buflen);
40 return 10; /* strlen("(NULL SID)") */
44 * BIG NOTE: this function only does SIDS where the identauth is not
45 * >= ^32 in a range of 2^48.
48 id_auth = sid->id_auth[5] +
49 (sid->id_auth[4] << 8) +
50 (sid->id_auth[3] << 16) +
51 (sid->id_auth[2] << 24);
53 ofs = snprintf(buf, buflen, "S-%u-%lu",
54 (unsigned int)sid->sid_rev_num, (unsigned long)id_auth);
56 for (i = 0; i < sid->num_auths; i++) {
57 ofs += snprintf(buf + ofs, MAX(buflen - ofs, 0), "-%lu",
58 (unsigned long)sid->sub_auths[i]);
60 return ofs;
63 /* Convert a binary SID to a character string */
64 wbcErr wbcSidToString(const struct wbcDomainSid *sid,
65 char **sid_string)
67 char buf[WBC_SID_STRING_BUFLEN];
68 char *result;
69 int len;
71 if (!sid) {
72 return WBC_ERR_INVALID_SID;
75 len = wbcSidToStringBuf(sid, buf, sizeof(buf));
77 if (len+1 > sizeof(buf)) {
78 return WBC_ERR_INVALID_SID;
81 result = (char *)wbcAllocateMemory(len+1, 1, NULL);
82 if (result == NULL) {
83 return WBC_ERR_NO_MEMORY;
85 memcpy(result, buf, len+1);
87 *sid_string = result;
88 return WBC_ERR_SUCCESS;
91 /* Convert a character string to a binary SID */
92 wbcErr wbcStringToSid(const char *str,
93 struct wbcDomainSid *sid)
95 const char *p;
96 char *q;
97 uint32_t x;
98 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
100 if (!sid) {
101 wbc_status = WBC_ERR_INVALID_PARAM;
102 BAIL_ON_WBC_ERROR(wbc_status);
105 /* Sanity check for either "S-" or "s-" */
107 if (!str
108 || (str[0]!='S' && str[0]!='s')
109 || (str[1]!='-'))
111 wbc_status = WBC_ERR_INVALID_PARAM;
112 BAIL_ON_WBC_ERROR(wbc_status);
115 /* Get the SID revision number */
117 p = str+2;
118 x = (uint32_t)strtol(p, &q, 10);
119 if (x==0 || !q || *q!='-') {
120 wbc_status = WBC_ERR_INVALID_SID;
121 BAIL_ON_WBC_ERROR(wbc_status);
123 sid->sid_rev_num = (uint8_t)x;
125 /* Next the Identifier Authority. This is stored in big-endian
126 in a 6 byte array. */
128 p = q+1;
129 x = (uint32_t)strtol(p, &q, 10);
130 if (!q || *q!='-') {
131 wbc_status = WBC_ERR_INVALID_SID;
132 BAIL_ON_WBC_ERROR(wbc_status);
134 sid->id_auth[5] = (x & 0x000000ff);
135 sid->id_auth[4] = (x & 0x0000ff00) >> 8;
136 sid->id_auth[3] = (x & 0x00ff0000) >> 16;
137 sid->id_auth[2] = (x & 0xff000000) >> 24;
138 sid->id_auth[1] = 0;
139 sid->id_auth[0] = 0;
141 /* now read the the subauthorities */
143 p = q +1;
144 sid->num_auths = 0;
145 while (sid->num_auths < WBC_MAXSUBAUTHS) {
146 x=(uint32_t)strtoul(p, &q, 10);
147 if (p == q)
148 break;
149 if (q == NULL) {
150 wbc_status = WBC_ERR_INVALID_SID;
151 BAIL_ON_WBC_ERROR(wbc_status);
153 sid->sub_auths[sid->num_auths++] = x;
155 if (*q != '-') {
156 break;
158 p = q + 1;
161 /* IF we ended early, then the SID could not be converted */
163 if (q && *q!='\0') {
164 wbc_status = WBC_ERR_INVALID_SID;
165 BAIL_ON_WBC_ERROR(wbc_status);
168 wbc_status = WBC_ERR_SUCCESS;
170 done:
171 return wbc_status;
176 /* Convert a domain and name to SID */
177 wbcErr wbcLookupName(const char *domain,
178 const char *name,
179 struct wbcDomainSid *sid,
180 enum wbcSidType *name_type)
182 struct winbindd_request request;
183 struct winbindd_response response;
184 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
186 if (!sid || !name_type) {
187 wbc_status = WBC_ERR_INVALID_PARAM;
188 BAIL_ON_WBC_ERROR(wbc_status);
191 /* Initialize request */
193 ZERO_STRUCT(request);
194 ZERO_STRUCT(response);
196 /* dst is already null terminated from the memset above */
198 strncpy(request.data.name.dom_name, domain,
199 sizeof(request.data.name.dom_name)-1);
200 strncpy(request.data.name.name, name,
201 sizeof(request.data.name.name)-1);
203 wbc_status = wbcRequestResponse(WINBINDD_LOOKUPNAME,
204 &request,
205 &response);
206 BAIL_ON_WBC_ERROR(wbc_status);
208 wbc_status = wbcStringToSid(response.data.sid.sid, sid);
209 BAIL_ON_WBC_ERROR(wbc_status);
211 *name_type = (enum wbcSidType)response.data.sid.type;
213 wbc_status = WBC_ERR_SUCCESS;
215 done:
216 return wbc_status;
220 /* Convert a SID to a domain and name */
221 wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
222 char **pdomain,
223 char **pname,
224 enum wbcSidType *pname_type)
226 struct winbindd_request request;
227 struct winbindd_response response;
228 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
229 char *domain, *name;
231 if (!sid) {
232 return WBC_ERR_INVALID_PARAM;
235 /* Initialize request */
237 ZERO_STRUCT(request);
238 ZERO_STRUCT(response);
240 wbcSidToStringBuf(sid, request.data.sid, sizeof(request.data.sid));
242 /* Make request */
244 wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSID, &request,
245 &response);
246 if (!WBC_ERROR_IS_OK(wbc_status)) {
247 return wbc_status;
250 /* Copy out result */
252 wbc_status = WBC_ERR_NO_MEMORY;
253 domain = NULL;
254 name = NULL;
256 domain = wbcStrDup(response.data.name.dom_name);
257 if (domain == NULL) {
258 goto done;
260 name = wbcStrDup(response.data.name.name);
261 if (name == NULL) {
262 goto done;
264 if (pdomain != NULL) {
265 *pdomain = domain;
266 domain = NULL;
268 if (pname != NULL) {
269 *pname = name;
270 name = NULL;
272 if (pname_type != NULL) {
273 *pname_type = (enum wbcSidType)response.data.name.type;
275 wbc_status = WBC_ERR_SUCCESS;
276 done:
277 wbcFreeMemory(name);
278 wbcFreeMemory(domain);
279 return wbc_status;
282 static void wbcDomainInfosDestructor(void *ptr)
284 struct wbcDomainInfo *i = (struct wbcDomainInfo *)ptr;
286 while (i->short_name != NULL) {
287 wbcFreeMemory(i->short_name);
288 wbcFreeMemory(i->dns_name);
289 i += 1;
293 static void wbcTranslatedNamesDestructor(void *ptr)
295 struct wbcTranslatedName *n = (struct wbcTranslatedName *)ptr;
297 while (n->name != NULL) {
298 wbcFreeMemory(n->name);
299 n += 1;
303 wbcErr wbcLookupSids(const struct wbcDomainSid *sids, int num_sids,
304 struct wbcDomainInfo **pdomains, int *pnum_domains,
305 struct wbcTranslatedName **pnames)
307 struct winbindd_request request;
308 struct winbindd_response response;
309 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
310 int buflen, i, extra_len, num_domains, num_names;
311 char *sidlist, *p, *q, *extra_data;
312 struct wbcDomainInfo *domains = NULL;
313 struct wbcTranslatedName *names = NULL;
315 buflen = num_sids * (WBC_SID_STRING_BUFLEN + 1) + 1;
317 sidlist = (char *)malloc(buflen);
318 if (sidlist == NULL) {
319 return WBC_ERR_NO_MEMORY;
322 p = sidlist;
324 for (i=0; i<num_sids; i++) {
325 int remaining;
326 int len;
328 remaining = buflen - (p - sidlist);
330 len = wbcSidToStringBuf(&sids[i], p, remaining);
331 if (len > remaining) {
332 free(sidlist);
333 return WBC_ERR_UNKNOWN_FAILURE;
336 p += len;
337 *p++ = '\n';
339 *p++ = '\0';
341 ZERO_STRUCT(request);
342 ZERO_STRUCT(response);
344 request.extra_data.data = sidlist;
345 request.extra_len = p - sidlist;
347 wbc_status = wbcRequestResponse(WINBINDD_LOOKUPSIDS,
348 &request, &response);
349 free(sidlist);
350 if (!WBC_ERROR_IS_OK(wbc_status)) {
351 return wbc_status;
354 extra_len = response.length - sizeof(struct winbindd_response);
355 extra_data = (char *)response.extra_data.data;
357 if ((extra_len <= 0) || (extra_data[extra_len-1] != '\0')) {
358 goto wbc_err_invalid;
361 p = extra_data;
363 num_domains = strtoul(p, &q, 10);
364 if (*q != '\n') {
365 goto wbc_err_invalid;
367 p = q+1;
369 domains = (struct wbcDomainInfo *)wbcAllocateMemory(
370 num_domains+1, sizeof(struct wbcDomainInfo),
371 wbcDomainInfosDestructor);
372 if (domains == NULL) {
373 wbc_status = WBC_ERR_NO_MEMORY;
374 goto fail;
377 for (i=0; i<num_domains; i++) {
379 q = strchr(p, ' ');
380 if (q == NULL) {
381 goto wbc_err_invalid;
383 *q = '\0';
384 wbc_status = wbcStringToSid(p, &domains[i].sid);
385 if (!WBC_ERROR_IS_OK(wbc_status)) {
386 goto fail;
388 p = q+1;
390 q = strchr(p, '\n');
391 if (q == NULL) {
392 goto wbc_err_invalid;
394 *q = '\0';
395 domains[i].short_name = wbcStrDup(p);
396 if (domains[i].short_name == NULL) {
397 wbc_status = WBC_ERR_NO_MEMORY;
398 goto fail;
400 p = q+1;
403 num_names = strtoul(p, &q, 10);
404 if (*q != '\n') {
405 goto wbc_err_invalid;
407 p = q+1;
409 if (num_names != num_sids) {
410 goto wbc_err_invalid;
413 names = (struct wbcTranslatedName *)wbcAllocateMemory(
414 num_names+1, sizeof(struct wbcTranslatedName),
415 wbcTranslatedNamesDestructor);
416 if (names == NULL) {
417 wbc_status = WBC_ERR_NO_MEMORY;
418 goto fail;
421 for (i=0; i<num_names; i++) {
423 names[i].domain_index = strtoul(p, &q, 10);
424 if (names[i].domain_index < 0) {
425 goto wbc_err_invalid;
427 if (names[i].domain_index >= num_domains) {
428 goto wbc_err_invalid;
431 if (*q != ' ') {
432 goto wbc_err_invalid;
434 p = q+1;
436 names[i].type = strtoul(p, &q, 10);
437 if (*q != ' ') {
438 goto wbc_err_invalid;
440 p = q+1;
442 q = strchr(p, '\n');
443 if (q == NULL) {
444 goto wbc_err_invalid;
446 *q = '\0';
447 names[i].name = wbcStrDup(p);
448 if (names[i].name == NULL) {
449 wbc_status = WBC_ERR_NO_MEMORY;
450 goto fail;
452 p = q+1;
454 if (*p != '\0') {
455 goto wbc_err_invalid;
458 *pdomains = domains;
459 *pnames = names;
460 winbindd_free_response(&response);
461 return WBC_ERR_SUCCESS;
463 wbc_err_invalid:
464 wbc_status = WBC_ERR_INVALID_RESPONSE;
465 fail:
466 winbindd_free_response(&response);
467 wbcFreeMemory(domains);
468 wbcFreeMemory(names);
469 return wbc_status;
472 /* Translate a collection of RIDs within a domain to names */
474 wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
475 int num_rids,
476 uint32_t *rids,
477 const char **pp_domain_name,
478 const char ***pnames,
479 enum wbcSidType **ptypes)
481 size_t i, len, ridbuf_size;
482 char *ridlist;
483 char *p;
484 struct winbindd_request request;
485 struct winbindd_response response;
486 char *domain_name = NULL;
487 const char **names = NULL;
488 enum wbcSidType *types = NULL;
489 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
491 /* Initialise request */
493 ZERO_STRUCT(request);
494 ZERO_STRUCT(response);
496 if (!dom_sid || (num_rids == 0)) {
497 wbc_status = WBC_ERR_INVALID_PARAM;
498 BAIL_ON_WBC_ERROR(wbc_status);
501 wbcSidToStringBuf(dom_sid, request.data.sid, sizeof(request.data.sid));
503 /* Even if all the Rids were of maximum 32bit values,
504 we would only have 11 bytes per rid in the final array
505 ("4294967296" + \n). Add one more byte for the
506 terminating '\0' */
508 ridbuf_size = (sizeof(char)*11) * num_rids + 1;
510 ridlist = (char *)malloc(ridbuf_size);
511 BAIL_ON_PTR_ERROR(ridlist, wbc_status);
513 len = 0;
514 for (i=0; i<num_rids; i++) {
515 len += snprintf(ridlist + len, ridbuf_size - len, "%u\n",
516 rids[i]);
518 ridlist[len] = '\0';
519 len += 1;
521 request.extra_data.data = ridlist;
522 request.extra_len = len;
524 wbc_status = wbcRequestResponse(WINBINDD_LOOKUPRIDS,
525 &request,
526 &response);
527 free(ridlist);
528 BAIL_ON_WBC_ERROR(wbc_status);
530 domain_name = wbcStrDup(response.data.domain_name);
531 BAIL_ON_PTR_ERROR(domain_name, wbc_status);
533 names = wbcAllocateStringArray(num_rids);
534 BAIL_ON_PTR_ERROR(names, wbc_status);
536 types = (enum wbcSidType *)wbcAllocateMemory(
537 num_rids, sizeof(enum wbcSidType), NULL);
538 BAIL_ON_PTR_ERROR(types, wbc_status);
540 p = (char *)response.extra_data.data;
542 for (i=0; i<num_rids; i++) {
543 char *q;
545 if (*p == '\0') {
546 wbc_status = WBC_ERR_INVALID_RESPONSE;
547 goto done;
550 types[i] = (enum wbcSidType)strtoul(p, &q, 10);
552 if (*q != ' ') {
553 wbc_status = WBC_ERR_INVALID_RESPONSE;
554 goto done;
557 p = q+1;
559 if ((q = strchr(p, '\n')) == NULL) {
560 wbc_status = WBC_ERR_INVALID_RESPONSE;
561 goto done;
564 *q = '\0';
566 names[i] = strdup(p);
567 BAIL_ON_PTR_ERROR(names[i], wbc_status);
569 p = q+1;
572 if (*p != '\0') {
573 wbc_status = WBC_ERR_INVALID_RESPONSE;
574 goto done;
577 wbc_status = WBC_ERR_SUCCESS;
579 done:
580 winbindd_free_response(&response);
582 if (WBC_ERROR_IS_OK(wbc_status)) {
583 *pp_domain_name = domain_name;
584 *pnames = names;
585 *ptypes = types;
587 else {
588 wbcFreeMemory(domain_name);
589 wbcFreeMemory(names);
590 wbcFreeMemory(types);
593 return wbc_status;
596 /* Get the groups a user belongs to */
597 wbcErr wbcLookupUserSids(const struct wbcDomainSid *user_sid,
598 bool domain_groups_only,
599 uint32_t *num_sids,
600 struct wbcDomainSid **_sids)
602 uint32_t i;
603 const char *s;
604 struct winbindd_request request;
605 struct winbindd_response response;
606 struct wbcDomainSid *sids = NULL;
607 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
608 int cmd;
610 /* Initialise request */
612 ZERO_STRUCT(request);
613 ZERO_STRUCT(response);
615 if (!user_sid) {
616 wbc_status = WBC_ERR_INVALID_PARAM;
617 BAIL_ON_WBC_ERROR(wbc_status);
620 wbcSidToStringBuf(user_sid, request.data.sid, sizeof(request.data.sid));
622 if (domain_groups_only) {
623 cmd = WINBINDD_GETUSERDOMGROUPS;
624 } else {
625 cmd = WINBINDD_GETUSERSIDS;
628 wbc_status = wbcRequestResponse(cmd,
629 &request,
630 &response);
631 BAIL_ON_WBC_ERROR(wbc_status);
633 if (response.data.num_entries &&
634 !response.extra_data.data) {
635 wbc_status = WBC_ERR_INVALID_RESPONSE;
636 BAIL_ON_WBC_ERROR(wbc_status);
639 sids = (struct wbcDomainSid *)wbcAllocateMemory(
640 response.data.num_entries, sizeof(struct wbcDomainSid),
641 NULL);
642 BAIL_ON_PTR_ERROR(sids, wbc_status);
644 s = (const char *)response.extra_data.data;
645 for (i = 0; i < response.data.num_entries; i++) {
646 char *n = strchr(s, '\n');
647 if (n) {
648 *n = '\0';
650 wbc_status = wbcStringToSid(s, &sids[i]);
651 BAIL_ON_WBC_ERROR(wbc_status);
652 s += strlen(s) + 1;
655 *num_sids = response.data.num_entries;
656 *_sids = sids;
657 sids = NULL;
658 wbc_status = WBC_ERR_SUCCESS;
660 done:
661 winbindd_free_response(&response);
662 if (sids) {
663 wbcFreeMemory(sids);
666 return wbc_status;
669 static inline
670 wbcErr _sid_to_rid(struct wbcDomainSid *sid, uint32_t *rid)
672 if (sid->num_auths < 1) {
673 return WBC_ERR_INVALID_RESPONSE;
675 *rid = sid->sub_auths[sid->num_auths - 1];
677 return WBC_ERR_SUCCESS;
680 /* Get alias membership for sids */
681 wbcErr wbcGetSidAliases(const struct wbcDomainSid *dom_sid,
682 struct wbcDomainSid *sids,
683 uint32_t num_sids,
684 uint32_t **alias_rids,
685 uint32_t *num_alias_rids)
687 uint32_t i;
688 const char *s;
689 struct winbindd_request request;
690 struct winbindd_response response;
691 ssize_t extra_data_len = 0;
692 char * extra_data = NULL;
693 ssize_t buflen = 0;
694 struct wbcDomainSid sid;
695 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
696 uint32_t * rids = NULL;
698 /* Initialise request */
700 ZERO_STRUCT(request);
701 ZERO_STRUCT(response);
703 if (!dom_sid) {
704 wbc_status = WBC_ERR_INVALID_PARAM;
705 goto done;
708 wbcSidToStringBuf(dom_sid, request.data.sid, sizeof(request.data.sid));
710 /* Lets assume each sid is around 57 characters
711 * S-1-5-21-AAAAAAAAAAA-BBBBBBBBBBB-CCCCCCCCCCC-DDDDDDDDDDD\n */
712 buflen = 57 * num_sids;
713 extra_data = (char *)malloc(buflen);
714 if (!extra_data) {
715 wbc_status = WBC_ERR_NO_MEMORY;
716 goto done;
719 /* Build the sid list */
720 for (i=0; i<num_sids; i++) {
721 char sid_str[WBC_SID_STRING_BUFLEN];
722 size_t sid_len;
724 sid_len = wbcSidToStringBuf(&sids[i], sid_str, sizeof(sid_str));
726 if (buflen < extra_data_len + sid_len + 2) {
727 buflen *= 2;
728 extra_data = (char *)realloc(extra_data, buflen);
729 if (!extra_data) {
730 wbc_status = WBC_ERR_NO_MEMORY;
731 BAIL_ON_WBC_ERROR(wbc_status);
735 strncpy(&extra_data[extra_data_len], sid_str,
736 buflen - extra_data_len);
737 extra_data_len += sid_len;
738 extra_data[extra_data_len++] = '\n';
739 extra_data[extra_data_len] = '\0';
741 extra_data_len += 1;
743 request.extra_data.data = extra_data;
744 request.extra_len = extra_data_len;
746 wbc_status = wbcRequestResponse(WINBINDD_GETSIDALIASES,
747 &request,
748 &response);
749 BAIL_ON_WBC_ERROR(wbc_status);
751 if (response.data.num_entries &&
752 !response.extra_data.data) {
753 wbc_status = WBC_ERR_INVALID_RESPONSE;
754 goto done;
757 rids = (uint32_t *)wbcAllocateMemory(response.data.num_entries,
758 sizeof(uint32_t), NULL);
759 BAIL_ON_PTR_ERROR(sids, wbc_status);
761 s = (const char *)response.extra_data.data;
762 for (i = 0; i < response.data.num_entries; i++) {
763 char *n = strchr(s, '\n');
764 if (n) {
765 *n = '\0';
767 wbc_status = wbcStringToSid(s, &sid);
768 BAIL_ON_WBC_ERROR(wbc_status);
769 wbc_status = _sid_to_rid(&sid, &rids[i]);
770 BAIL_ON_WBC_ERROR(wbc_status);
771 s += strlen(s) + 1;
774 *num_alias_rids = response.data.num_entries;
775 *alias_rids = rids;
776 rids = NULL;
777 wbc_status = WBC_ERR_SUCCESS;
779 done:
780 free(extra_data);
781 winbindd_free_response(&response);
782 wbcFreeMemory(rids);
783 return wbc_status;
787 /* Lists Users */
788 wbcErr wbcListUsers(const char *domain_name,
789 uint32_t *_num_users,
790 const char ***_users)
792 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
793 struct winbindd_request request;
794 struct winbindd_response response;
795 uint32_t num_users = 0;
796 const char **users = NULL;
797 const char *next;
799 /* Initialise request */
801 ZERO_STRUCT(request);
802 ZERO_STRUCT(response);
804 if (domain_name) {
805 strncpy(request.domain_name, domain_name,
806 sizeof(request.domain_name)-1);
809 wbc_status = wbcRequestResponse(WINBINDD_LIST_USERS,
810 &request,
811 &response);
812 BAIL_ON_WBC_ERROR(wbc_status);
814 users = wbcAllocateStringArray(response.data.num_entries);
815 if (users == NULL) {
816 return WBC_ERR_NO_MEMORY;
819 /* Look through extra data */
821 next = (const char *)response.extra_data.data;
822 while (next) {
823 const char *current;
824 char *k;
826 if (num_users >= response.data.num_entries) {
827 wbc_status = WBC_ERR_INVALID_RESPONSE;
828 goto done;
831 current = next;
832 k = strchr(next, ',');
834 if (k) {
835 k[0] = '\0';
836 next = k+1;
837 } else {
838 next = NULL;
841 users[num_users] = strdup(current);
842 BAIL_ON_PTR_ERROR(users[num_users], wbc_status);
843 num_users += 1;
845 if (num_users != response.data.num_entries) {
846 wbc_status = WBC_ERR_INVALID_RESPONSE;
847 goto done;
850 *_num_users = response.data.num_entries;
851 *_users = users;
852 users = NULL;
853 wbc_status = WBC_ERR_SUCCESS;
855 done:
856 winbindd_free_response(&response);
857 wbcFreeMemory(users);
858 return wbc_status;
861 /* Lists Groups */
862 wbcErr wbcListGroups(const char *domain_name,
863 uint32_t *_num_groups,
864 const char ***_groups)
866 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
867 struct winbindd_request request;
868 struct winbindd_response response;
869 uint32_t num_groups = 0;
870 const char **groups = NULL;
871 const char *next;
873 /* Initialise request */
875 ZERO_STRUCT(request);
876 ZERO_STRUCT(response);
878 if (domain_name) {
879 strncpy(request.domain_name, domain_name,
880 sizeof(request.domain_name)-1);
883 wbc_status = wbcRequestResponse(WINBINDD_LIST_GROUPS,
884 &request,
885 &response);
886 BAIL_ON_WBC_ERROR(wbc_status);
888 groups = wbcAllocateStringArray(response.data.num_entries);
889 if (groups == NULL) {
890 return WBC_ERR_NO_MEMORY;
893 /* Look through extra data */
895 next = (const char *)response.extra_data.data;
896 while (next) {
897 const char *current;
898 char *k;
900 if (num_groups >= response.data.num_entries) {
901 wbc_status = WBC_ERR_INVALID_RESPONSE;
902 goto done;
905 current = next;
906 k = strchr(next, ',');
908 if (k) {
909 k[0] = '\0';
910 next = k+1;
911 } else {
912 next = NULL;
915 groups[num_groups] = strdup(current);
916 BAIL_ON_PTR_ERROR(groups[num_groups], wbc_status);
917 num_groups += 1;
919 if (num_groups != response.data.num_entries) {
920 wbc_status = WBC_ERR_INVALID_RESPONSE;
921 goto done;
924 *_num_groups = response.data.num_entries;
925 *_groups = groups;
926 groups = NULL;
927 wbc_status = WBC_ERR_SUCCESS;
929 done:
930 winbindd_free_response(&response);
931 wbcFreeMemory(groups);
932 return wbc_status;
935 wbcErr wbcGetDisplayName(const struct wbcDomainSid *sid,
936 char **pdomain,
937 char **pfullname,
938 enum wbcSidType *pname_type)
940 wbcErr wbc_status;
941 char *domain = NULL;
942 char *name = NULL;
943 enum wbcSidType name_type;
945 wbc_status = wbcLookupSid(sid, &domain, &name, &name_type);
946 BAIL_ON_WBC_ERROR(wbc_status);
948 if (name_type == WBC_SID_NAME_USER) {
949 uid_t uid;
950 struct passwd *pwd;
952 wbc_status = wbcSidToUid(sid, &uid);
953 BAIL_ON_WBC_ERROR(wbc_status);
955 wbc_status = wbcGetpwuid(uid, &pwd);
956 BAIL_ON_WBC_ERROR(wbc_status);
958 wbcFreeMemory(name);
960 name = wbcStrDup(pwd->pw_gecos);
961 wbcFreeMemory(pwd);
962 BAIL_ON_PTR_ERROR(name, wbc_status);
965 wbc_status = WBC_ERR_SUCCESS;
967 done:
968 if (WBC_ERROR_IS_OK(wbc_status)) {
969 *pdomain = domain;
970 *pfullname = name;
971 *pname_type = name_type;
972 } else {
973 wbcFreeMemory(domain);
974 wbcFreeMemory(name);
977 return wbc_status;
980 const char* wbcSidTypeString(enum wbcSidType type)
982 switch (type) {
983 case WBC_SID_NAME_USE_NONE: return "SID_NONE";
984 case WBC_SID_NAME_USER: return "SID_USER";
985 case WBC_SID_NAME_DOM_GRP: return "SID_DOM_GROUP";
986 case WBC_SID_NAME_DOMAIN: return "SID_DOMAIN";
987 case WBC_SID_NAME_ALIAS: return "SID_ALIAS";
988 case WBC_SID_NAME_WKN_GRP: return "SID_WKN_GROUP";
989 case WBC_SID_NAME_DELETED: return "SID_DELETED";
990 case WBC_SID_NAME_INVALID: return "SID_INVALID";
991 case WBC_SID_NAME_UNKNOWN: return "SID_UNKNOWN";
992 case WBC_SID_NAME_COMPUTER: return "SID_COMPUTER";
993 default: return "Unknown type";