2 Unix SMB/CIFS implementation.
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Andrew Tridgell 2000
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this library; if not, write to the
21 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
26 #include "nsswitch/winbind_nss.h"
29 #define DBGC_CLASS DBGC_WINBIND
31 extern DOM_SID global_sid_NULL; /* NULL sid */
33 NSS_STATUS winbindd_request(int req_type,
34 struct winbindd_request *request,
35 struct winbindd_response *response);
37 /* Call winbindd to convert a name to a sid */
39 BOOL winbind_lookup_name(const char *dom_name, const char *name, DOM_SID *sid,
40 enum SID_NAME_USE *name_type)
42 struct winbindd_request request;
43 struct winbindd_response response;
46 if (!sid || !name_type)
49 /* Send off request */
52 ZERO_STRUCT(response);
54 fstrcpy(request.data.name.dom_name, dom_name);
55 fstrcpy(request.data.name.name, name);
57 if ((result = winbindd_request(WINBINDD_LOOKUPNAME, &request,
58 &response)) == NSS_STATUS_SUCCESS) {
59 if (!string_to_sid(sid, response.data.sid.sid))
61 *name_type = (enum SID_NAME_USE)response.data.sid.type;
64 return result == NSS_STATUS_SUCCESS;
67 /* Call winbindd to convert sid to name */
69 BOOL winbind_lookup_sid(const DOM_SID *sid,
70 fstring dom_name, fstring name,
71 enum SID_NAME_USE *name_type)
73 struct winbindd_request request;
74 struct winbindd_response response;
78 /* Initialise request */
81 ZERO_STRUCT(response);
83 sid_to_string(sid_str, sid);
84 fstrcpy(request.data.sid, sid_str);
88 result = winbindd_request(WINBINDD_LOOKUPSID, &request, &response);
92 if (result == NSS_STATUS_SUCCESS) {
93 fstrcpy(dom_name, response.data.name.dom_name);
94 fstrcpy(name, response.data.name.name);
95 *name_type = (enum SID_NAME_USE)response.data.name.type;
97 DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
98 sid_str, dom_name, name));
101 return (result == NSS_STATUS_SUCCESS);
104 /* Call winbindd to convert SID to uid */
106 BOOL winbind_sid_to_uid(uid_t *puid, const DOM_SID *sid)
108 struct winbindd_request request;
109 struct winbindd_response response;
116 /* Initialise request */
118 ZERO_STRUCT(request);
119 ZERO_STRUCT(response);
121 sid_to_string(sid_str, sid);
122 fstrcpy(request.data.sid, sid_str);
126 result = winbindd_request(WINBINDD_SID_TO_UID, &request, &response);
128 /* Copy out result */
130 if (result == NSS_STATUS_SUCCESS) {
131 *puid = response.data.uid;
134 return (result == NSS_STATUS_SUCCESS);
137 /* Call winbindd to convert uid to sid */
139 BOOL winbind_uid_to_sid(DOM_SID *sid, uid_t uid)
141 struct winbindd_request request;
142 struct winbindd_response response;
148 /* Initialise request */
150 ZERO_STRUCT(request);
151 ZERO_STRUCT(response);
153 request.data.uid = uid;
157 result = winbindd_request(WINBINDD_UID_TO_SID, &request, &response);
159 /* Copy out result */
161 if (result == NSS_STATUS_SUCCESS) {
162 if (!string_to_sid(sid, response.data.sid.sid))
165 sid_copy(sid, &global_sid_NULL);
168 return (result == NSS_STATUS_SUCCESS);
171 /* Call winbindd to convert SID to gid */
173 BOOL winbind_sid_to_gid(gid_t *pgid, const DOM_SID *sid)
175 struct winbindd_request request;
176 struct winbindd_response response;
183 /* Initialise request */
185 ZERO_STRUCT(request);
186 ZERO_STRUCT(response);
188 sid_to_string(sid_str, sid);
189 fstrcpy(request.data.sid, sid_str);
193 result = winbindd_request(WINBINDD_SID_TO_GID, &request, &response);
195 /* Copy out result */
197 if (result == NSS_STATUS_SUCCESS) {
198 *pgid = response.data.gid;
201 return (result == NSS_STATUS_SUCCESS);
204 /* Call winbindd to convert gid to sid */
206 BOOL winbind_gid_to_sid(DOM_SID *sid, gid_t gid)
208 struct winbindd_request request;
209 struct winbindd_response response;
215 /* Initialise request */
217 ZERO_STRUCT(request);
218 ZERO_STRUCT(response);
220 request.data.gid = gid;
224 result = winbindd_request(WINBINDD_GID_TO_SID, &request, &response);
226 /* Copy out result */
228 if (result == NSS_STATUS_SUCCESS) {
229 if (!string_to_sid(sid, response.data.sid.sid))
232 sid_copy(sid, &global_sid_NULL);
235 return (result == NSS_STATUS_SUCCESS);
238 BOOL winbind_allocate_rid(uint32 *rid)
240 struct winbindd_request request;
241 struct winbindd_response response;
244 /* Initialise request */
246 ZERO_STRUCT(request);
247 ZERO_STRUCT(response);
251 result = winbindd_request(WINBINDD_ALLOCATE_RID, &request, &response);
253 if (result != NSS_STATUS_SUCCESS)
256 /* Copy out result */
257 *rid = response.data.rid;
262 /* Fetch the list of groups a user is a member of from winbindd. This is
263 used by winbind_getgroups. */
265 static int wb_getgroups(const char *user, gid_t **groups)
267 struct winbindd_request request;
268 struct winbindd_response response;
273 fstrcpy(request.data.username, user);
275 ZERO_STRUCT(response);
277 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
279 if (result == NSS_STATUS_SUCCESS) {
281 /* Return group list. Don't forget to free the group list
284 *groups = (gid_t *)response.extra_data;
285 return response.data.num_entries;
291 /* Call winbindd to initialise group membership. This is necessary for
292 some systems (i.e RH5.2) that do not have an initgroups function as part
293 of the nss extension. In RH5.2 this is implemented using getgrent()
294 which can be amazingly inefficient as well as having problems with
297 int winbind_initgroups(char *user, gid_t gid)
299 gid_t *tgr, *groups = NULL;
302 /* Call normal initgroups if we are a local user */
304 if (!strchr(user, *lp_winbind_separator())) {
305 return initgroups(user, gid);
308 result = wb_getgroups(user, &groups);
310 DEBUG(10,("winbind_getgroups: %s: result = %s\n", user,
311 result == -1 ? "FAIL" : "SUCCESS"));
314 int ngroups = result, i;
315 BOOL is_member = False;
317 /* Check to see if the passed gid is already in the list */
319 for (i = 0; i < ngroups; i++) {
320 if (groups[i] == gid) {
325 /* Add group to list if necessary */
328 tgr = (gid_t *)Realloc(groups, sizeof(gid_t) * ngroups + 1);
337 groups[ngroups] = gid;
343 if (sys_setgroups(ngroups, groups) == -1) {
351 /* The call failed. Set errno to something so we don't get
352 a bogus value from the last failed system call. */
357 /* Free response data if necessary */
365 /* Return a list of groups the user is a member of. This function is
366 useful for large systems where inverting the group database would be too
367 time consuming. If size is zero, list is not modified and the total
368 number of groups for the user is returned. */
370 int winbind_getgroups(const char *user, gid_t **list)
373 * Don't do the lookup if the name has no separator _and_ we are not in
374 * 'winbind use default domain' mode.
377 if (!(strchr(user, *lp_winbind_separator()) || lp_winbind_use_default_domain()))
380 /* Fetch list of groups */
382 return wb_getgroups(user, list);
385 /**********************************************************************
386 simple wrapper function to see if winbindd is alive
387 **********************************************************************/
389 BOOL winbind_ping( void )
393 result = winbindd_request(WINBINDD_PING, NULL, NULL);
395 return result == NSS_STATUS_SUCCESS;
398 /**********************************************************************
399 Ask winbindd to create a local user
400 **********************************************************************/
402 BOOL winbind_create_user( const char *name, uint32 *rid )
404 struct winbindd_request request;
405 struct winbindd_response response;
408 if ( !lp_winbind_enable_local_accounts() )
414 DEBUG(10,("winbind_create_user: %s\n", name));
416 ZERO_STRUCT(request);
417 ZERO_STRUCT(response);
419 /* see if the caller wants a new RID returned */
422 request.flags = WBFLAG_ALLOCATE_RID;
424 fstrcpy( request.data.acct_mgt.username, name );
425 fstrcpy( request.data.acct_mgt.groupname, "" );
427 result = winbindd_request( WINBINDD_CREATE_USER, &request, &response);
430 *rid = response.data.rid;
432 return result == NSS_STATUS_SUCCESS;
435 /**********************************************************************
436 Ask winbindd to create a local group
437 **********************************************************************/
439 BOOL winbind_create_group( const char *name, uint32 *rid )
441 struct winbindd_request request;
442 struct winbindd_response response;
445 if ( !lp_winbind_enable_local_accounts() )
451 DEBUG(10,("winbind_create_group: %s\n", name));
453 ZERO_STRUCT(request);
454 ZERO_STRUCT(response);
456 /* see if the caller wants a new RID returned */
459 request.flags = WBFLAG_ALLOCATE_RID;
461 fstrcpy( request.data.acct_mgt.groupname, name );
464 result = winbindd_request( WINBINDD_CREATE_GROUP, &request, &response);
467 *rid = response.data.rid;
469 return result == NSS_STATUS_SUCCESS;
472 /**********************************************************************
473 Ask winbindd to add a user to a local group
474 **********************************************************************/
476 BOOL winbind_add_user_to_group( const char *user, const char *group )
478 struct winbindd_request request;
479 struct winbindd_response response;
482 if ( !lp_winbind_enable_local_accounts() )
485 if ( !user || !group )
488 ZERO_STRUCT(request);
489 ZERO_STRUCT(response);
491 DEBUG(10,("winbind_add_user_to_group: user(%s), group(%s) \n",
494 fstrcpy( request.data.acct_mgt.username, user );
495 fstrcpy( request.data.acct_mgt.groupname, group );
497 result = winbindd_request( WINBINDD_ADD_USER_TO_GROUP, &request, &response);
499 return result == NSS_STATUS_SUCCESS;
502 /**********************************************************************
503 Ask winbindd to remove a user to a local group
504 **********************************************************************/
506 BOOL winbind_remove_user_from_group( const char *user, const char *group )
508 struct winbindd_request request;
509 struct winbindd_response response;
512 if ( !lp_winbind_enable_local_accounts() )
515 if ( !user || !group )
518 ZERO_STRUCT(request);
519 ZERO_STRUCT(response);
521 DEBUG(10,("winbind_remove_user_from_group: user(%s), group(%s) \n",
524 ZERO_STRUCT(response);
526 fstrcpy( request.data.acct_mgt.username, user );
527 fstrcpy( request.data.acct_mgt.groupname, group );
529 result = winbindd_request( WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
531 return result == NSS_STATUS_SUCCESS;
534 /**********************************************************************
535 Ask winbindd to set the primary group for a user local user
536 **********************************************************************/
538 BOOL winbind_set_user_primary_group( const char *user, const char *group )
540 struct winbindd_request request;
541 struct winbindd_response response;
544 if ( !lp_winbind_enable_local_accounts() )
547 if ( !user || !group )
550 ZERO_STRUCT(request);
551 ZERO_STRUCT(response);
553 DEBUG(10,("winbind_set_user_primary_group: user(%s), group(%s) \n",
556 fstrcpy( request.data.acct_mgt.username, user );
557 fstrcpy( request.data.acct_mgt.groupname, group );
559 result = winbindd_request( WINBINDD_SET_USER_PRIMARY_GROUP, &request, &response);
561 return result == NSS_STATUS_SUCCESS;
565 /**********************************************************************
566 Ask winbindd to remove a user from its lists of accounts
567 **********************************************************************/
569 BOOL winbind_delete_user( const char *user )
571 struct winbindd_request request;
572 struct winbindd_response response;
575 if ( !lp_winbind_enable_local_accounts() )
581 ZERO_STRUCT(request);
582 ZERO_STRUCT(response);
584 DEBUG(10,("winbind_delete_user: user (%s)\n", user));
586 fstrcpy( request.data.acct_mgt.username, user );
588 result = winbindd_request( WINBINDD_DELETE_USER, &request, &response);
590 return result == NSS_STATUS_SUCCESS;
593 /**********************************************************************
594 Ask winbindd to remove a group from its lists of accounts
595 **********************************************************************/
597 BOOL winbind_delete_group( const char *group )
599 struct winbindd_request request;
600 struct winbindd_response response;
603 if ( !lp_winbind_enable_local_accounts() )
609 ZERO_STRUCT(request);
610 ZERO_STRUCT(response);
612 DEBUG(10,("winbind_delete_group: group (%s)\n", group));
614 fstrcpy( request.data.acct_mgt.groupname, group );
616 result = winbindd_request( WINBINDD_DELETE_GROUP, &request, &response);
618 return result == NSS_STATUS_SUCCESS;
621 /***********************************************************************/
622 #if 0 /* not needed currently since winbindd_acct was added -- jerry */
624 /* Call winbindd to convert SID to uid. Do not allocate */
626 BOOL winbind_sid_to_uid_query(uid_t *puid, const DOM_SID *sid)
628 struct winbindd_request request;
629 struct winbindd_response response;
636 /* Initialise request */
638 ZERO_STRUCT(request);
639 ZERO_STRUCT(response);
641 sid_to_string(sid_str, sid);
642 fstrcpy(request.data.sid, sid_str);
644 request.flags = WBFLAG_QUERY_ONLY;
648 result = winbindd_request(WINBINDD_SID_TO_UID, &request, &response);
650 /* Copy out result */
652 if (result == NSS_STATUS_SUCCESS) {
653 *puid = response.data.uid;
656 return (result == NSS_STATUS_SUCCESS);
659 /* Call winbindd to convert SID to gid. Do not allocate */
661 BOOL winbind_sid_to_gid_query(gid_t *pgid, const DOM_SID *sid)
663 struct winbindd_request request;
664 struct winbindd_response response;
671 /* Initialise request */
673 ZERO_STRUCT(request);
674 ZERO_STRUCT(response);
676 sid_to_string(sid_str, sid);
677 fstrcpy(request.data.sid, sid_str);
679 request.flags = WBFLAG_QUERY_ONLY;
683 result = winbindd_request(WINBINDD_SID_TO_GID, &request, &response);
685 /* Copy out result */
687 if (result == NSS_STATUS_SUCCESS) {
688 *pgid = response.data.gid;
691 return (result == NSS_STATUS_SUCCESS);
696 /***********************************************************************/