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 ZERO_STRUCT(request);
274 fstrcpy(request.data.username, user);
276 ZERO_STRUCT(response);
278 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
280 if (result == NSS_STATUS_SUCCESS) {
282 /* Return group list. Don't forget to free the group list
285 *groups = (gid_t *)response.extra_data;
286 return response.data.num_entries;
292 /* Call winbindd to initialise group membership. This is necessary for
293 some systems (i.e RH5.2) that do not have an initgroups function as part
294 of the nss extension. In RH5.2 this is implemented using getgrent()
295 which can be amazingly inefficient as well as having problems with
298 int winbind_initgroups(char *user, gid_t gid)
300 gid_t *tgr, *groups = NULL;
303 /* Call normal initgroups if we are a local user */
305 if (!strchr(user, *lp_winbind_separator())) {
306 return initgroups(user, gid);
309 result = wb_getgroups(user, &groups);
311 DEBUG(10,("winbind_getgroups: %s: result = %s\n", user,
312 result == -1 ? "FAIL" : "SUCCESS"));
315 int ngroups = result, i;
316 BOOL is_member = False;
318 /* Check to see if the passed gid is already in the list */
320 for (i = 0; i < ngroups; i++) {
321 if (groups[i] == gid) {
326 /* Add group to list if necessary */
329 tgr = SMB_REALLOC_ARRAY(groups, gid_t, ngroups + 1);
338 groups[ngroups] = gid;
344 if (sys_setgroups(ngroups, groups) == -1) {
352 /* The call failed. Set errno to something so we don't get
353 a bogus value from the last failed system call. */
358 /* Free response data if necessary */
366 /* Return a list of groups the user is a member of. This function is
367 useful for large systems where inverting the group database would be too
368 time consuming. If size is zero, list is not modified and the total
369 number of groups for the user is returned. */
371 int winbind_getgroups(const char *user, gid_t **list)
374 * Don't do the lookup if the name has no separator _and_ we are not in
375 * 'winbind use default domain' mode.
378 if (!(strchr(user, *lp_winbind_separator()) || lp_winbind_use_default_domain()))
381 /* Fetch list of groups */
383 return wb_getgroups(user, list);
386 /**********************************************************************
387 simple wrapper function to see if winbindd is alive
388 **********************************************************************/
390 BOOL winbind_ping( void )
394 result = winbindd_request(WINBINDD_PING, NULL, NULL);
396 return result == NSS_STATUS_SUCCESS;
399 /**********************************************************************
400 Ask winbindd to create a local user
401 **********************************************************************/
403 BOOL winbind_create_user( const char *name, uint32 *rid )
405 struct winbindd_request request;
406 struct winbindd_response response;
409 if ( !lp_winbind_enable_local_accounts() )
415 DEBUG(10,("winbind_create_user: %s\n", name));
417 ZERO_STRUCT(request);
418 ZERO_STRUCT(response);
420 /* see if the caller wants a new RID returned */
423 request.flags = WBFLAG_ALLOCATE_RID;
425 fstrcpy( request.data.acct_mgt.username, name );
426 fstrcpy( request.data.acct_mgt.groupname, "" );
428 result = winbindd_request( WINBINDD_CREATE_USER, &request, &response);
431 *rid = response.data.rid;
433 return result == NSS_STATUS_SUCCESS;
436 /**********************************************************************
437 Ask winbindd to create a local group
438 **********************************************************************/
440 BOOL winbind_create_group( const char *name, uint32 *rid )
442 struct winbindd_request request;
443 struct winbindd_response response;
446 if ( !lp_winbind_enable_local_accounts() )
452 DEBUG(10,("winbind_create_group: %s\n", name));
454 ZERO_STRUCT(request);
455 ZERO_STRUCT(response);
457 /* see if the caller wants a new RID returned */
460 request.flags = WBFLAG_ALLOCATE_RID;
462 fstrcpy( request.data.acct_mgt.groupname, name );
465 result = winbindd_request( WINBINDD_CREATE_GROUP, &request, &response);
468 *rid = response.data.rid;
470 return result == NSS_STATUS_SUCCESS;
473 /**********************************************************************
474 Ask winbindd to add a user to a local group
475 **********************************************************************/
477 BOOL winbind_add_user_to_group( const char *user, const char *group )
479 struct winbindd_request request;
480 struct winbindd_response response;
483 if ( !lp_winbind_enable_local_accounts() )
486 if ( !user || !group )
489 ZERO_STRUCT(request);
490 ZERO_STRUCT(response);
492 DEBUG(10,("winbind_add_user_to_group: user(%s), group(%s) \n",
495 fstrcpy( request.data.acct_mgt.username, user );
496 fstrcpy( request.data.acct_mgt.groupname, group );
498 result = winbindd_request( WINBINDD_ADD_USER_TO_GROUP, &request, &response);
500 return result == NSS_STATUS_SUCCESS;
503 /**********************************************************************
504 Ask winbindd to remove a user to a local group
505 **********************************************************************/
507 BOOL winbind_remove_user_from_group( const char *user, const char *group )
509 struct winbindd_request request;
510 struct winbindd_response response;
513 if ( !lp_winbind_enable_local_accounts() )
516 if ( !user || !group )
519 ZERO_STRUCT(request);
520 ZERO_STRUCT(response);
522 DEBUG(10,("winbind_remove_user_from_group: user(%s), group(%s) \n",
525 ZERO_STRUCT(response);
527 fstrcpy( request.data.acct_mgt.username, user );
528 fstrcpy( request.data.acct_mgt.groupname, group );
530 result = winbindd_request( WINBINDD_REMOVE_USER_FROM_GROUP, &request, &response);
532 return result == NSS_STATUS_SUCCESS;
535 /**********************************************************************
536 Ask winbindd to set the primary group for a user local user
537 **********************************************************************/
539 BOOL winbind_set_user_primary_group( const char *user, const char *group )
541 struct winbindd_request request;
542 struct winbindd_response response;
545 if ( !lp_winbind_enable_local_accounts() )
548 if ( !user || !group )
551 ZERO_STRUCT(request);
552 ZERO_STRUCT(response);
554 DEBUG(10,("winbind_set_user_primary_group: user(%s), group(%s) \n",
557 fstrcpy( request.data.acct_mgt.username, user );
558 fstrcpy( request.data.acct_mgt.groupname, group );
560 result = winbindd_request( WINBINDD_SET_USER_PRIMARY_GROUP, &request, &response);
562 return result == NSS_STATUS_SUCCESS;
566 /**********************************************************************
567 Ask winbindd to remove a user from its lists of accounts
568 **********************************************************************/
570 BOOL winbind_delete_user( const char *user )
572 struct winbindd_request request;
573 struct winbindd_response response;
576 if ( !lp_winbind_enable_local_accounts() )
582 ZERO_STRUCT(request);
583 ZERO_STRUCT(response);
585 DEBUG(10,("winbind_delete_user: user (%s)\n", user));
587 fstrcpy( request.data.acct_mgt.username, user );
589 result = winbindd_request( WINBINDD_DELETE_USER, &request, &response);
591 return result == NSS_STATUS_SUCCESS;
594 /**********************************************************************
595 Ask winbindd to remove a group from its lists of accounts
596 **********************************************************************/
598 BOOL winbind_delete_group( const char *group )
600 struct winbindd_request request;
601 struct winbindd_response response;
604 if ( !lp_winbind_enable_local_accounts() )
610 ZERO_STRUCT(request);
611 ZERO_STRUCT(response);
613 DEBUG(10,("winbind_delete_group: group (%s)\n", group));
615 fstrcpy( request.data.acct_mgt.groupname, group );
617 result = winbindd_request( WINBINDD_DELETE_GROUP, &request, &response);
619 return result == NSS_STATUS_SUCCESS;
622 /***********************************************************************/
623 #if 0 /* not needed currently since winbindd_acct was added -- jerry */
625 /* Call winbindd to convert SID to uid. Do not allocate */
627 BOOL winbind_sid_to_uid_query(uid_t *puid, const DOM_SID *sid)
629 struct winbindd_request request;
630 struct winbindd_response response;
637 /* Initialise request */
639 ZERO_STRUCT(request);
640 ZERO_STRUCT(response);
642 sid_to_string(sid_str, sid);
643 fstrcpy(request.data.sid, sid_str);
645 request.flags = WBFLAG_QUERY_ONLY;
649 result = winbindd_request(WINBINDD_SID_TO_UID, &request, &response);
651 /* Copy out result */
653 if (result == NSS_STATUS_SUCCESS) {
654 *puid = response.data.uid;
657 return (result == NSS_STATUS_SUCCESS);
660 /* Call winbindd to convert SID to gid. Do not allocate */
662 BOOL winbind_sid_to_gid_query(gid_t *pgid, const DOM_SID *sid)
664 struct winbindd_request request;
665 struct winbindd_response response;
672 /* Initialise request */
674 ZERO_STRUCT(request);
675 ZERO_STRUCT(response);
677 sid_to_string(sid_str, sid);
678 fstrcpy(request.data.sid, sid_str);
680 request.flags = WBFLAG_QUERY_ONLY;
684 result = winbindd_request(WINBINDD_SID_TO_GID, &request, &response);
686 /* Copy out result */
688 if (result == NSS_STATUS_SUCCESS) {
689 *pgid = response.data.gid;
692 return (result == NSS_STATUS_SUCCESS);
697 /***********************************************************************/