LCOV - code coverage report
Current view: top level - source3/lib - winbind_util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 63 143 44.1 %
Date: 2024-02-28 12:06:22 Functions: 8 13 61.5 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Winbind Utility functions
       4             : 
       5             :    Copyright (C) Gerald (Jerry) Carter   2007
       6             : 
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : 
      21             : #include "includes.h"
      22             : #include "../libcli/security/security.h"
      23             : #include "../lib/util/util_pw.h"
      24             : #include "nsswitch/libwbclient/wbclient.h"
      25             : 
      26             : #include "lib/winbind_util.h"
      27             : 
      28             : #if defined(WITH_WINBIND)
      29             : 
      30           0 : struct passwd * winbind_getpwnam(const char * name)
      31             : {
      32           0 :         wbcErr result;
      33           0 :         struct passwd * tmp_pwd = NULL;
      34           0 :         struct passwd * pwd = NULL;
      35             : 
      36           0 :         result = wbcGetpwnam(name, &tmp_pwd);
      37           0 :         if (result != WBC_ERR_SUCCESS)
      38           0 :                 return pwd;
      39             : 
      40           0 :         pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
      41             : 
      42           0 :         wbcFreeMemory(tmp_pwd);
      43             : 
      44           0 :         return pwd;
      45             : }
      46             : 
      47           0 : struct passwd * winbind_getpwsid(const struct dom_sid *sid)
      48             : {
      49           0 :         wbcErr result;
      50           0 :         struct passwd * tmp_pwd = NULL;
      51           0 :         struct passwd * pwd = NULL;
      52           0 :         struct wbcDomainSid dom_sid;
      53             : 
      54           0 :         memcpy(&dom_sid, sid, sizeof(dom_sid));
      55             : 
      56           0 :         result = wbcGetpwsid(&dom_sid, &tmp_pwd);
      57           0 :         if (result != WBC_ERR_SUCCESS)
      58           0 :                 return pwd;
      59             : 
      60           0 :         pwd = tcopy_passwd(talloc_tos(), tmp_pwd);
      61             : 
      62           0 :         wbcFreeMemory(tmp_pwd);
      63             : 
      64           0 :         return pwd;
      65             : }
      66             : 
      67             : /* Call winbindd to convert a name to a sid */
      68             : 
      69         665 : bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
      70             :                          enum lsa_SidType *name_type)
      71             : {
      72           0 :         struct wbcDomainSid dom_sid;
      73           0 :         wbcErr result;
      74           0 :         enum wbcSidType type;
      75             : 
      76         665 :         result = wbcLookupName(dom_name, name, &dom_sid, &type);
      77         665 :         if (result != WBC_ERR_SUCCESS)
      78         203 :                 return false;
      79             : 
      80         462 :         memcpy(sid, &dom_sid, sizeof(struct dom_sid));
      81         462 :         *name_type = (enum lsa_SidType)type;
      82             : 
      83         462 :         return true;
      84             : }
      85             : 
      86             : /* Call winbindd to convert sid to name */
      87             : 
      88           0 : bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
      89             :                         const char **domain, const char **name,
      90             :                         enum lsa_SidType *name_type)
      91             : {
      92           0 :         struct wbcDomainSid dom_sid;
      93           0 :         wbcErr result;
      94           0 :         enum wbcSidType type;
      95           0 :         char *domain_name = NULL;
      96           0 :         char *account_name = NULL;
      97           0 :         struct dom_sid_buf buf;
      98             : 
      99           0 :         memcpy(&dom_sid, sid, sizeof(dom_sid));
     100             : 
     101           0 :         result = wbcLookupSid(&dom_sid, &domain_name, &account_name, &type);
     102           0 :         if (result != WBC_ERR_SUCCESS)
     103           0 :                 return false;
     104             : 
     105             :         /* Copy out result */
     106             : 
     107           0 :         if (domain) {
     108           0 :                 *domain = talloc_strdup(mem_ctx, domain_name);
     109             :         }
     110           0 :         if (name) {
     111           0 :                 *name = talloc_strdup(mem_ctx, account_name);
     112             :         }
     113           0 :         *name_type = (enum lsa_SidType)type;
     114             : 
     115           0 :         DEBUG(10, ("winbind_lookup_sid: SUCCESS: SID %s -> %s %s\n",
     116             :                    dom_sid_str_buf(sid, &buf), domain_name, account_name));
     117             : 
     118           0 :         wbcFreeMemory(domain_name);
     119           0 :         wbcFreeMemory(account_name);
     120             : 
     121           0 :         if ((domain && !*domain) || (name && !*name)) {
     122           0 :                 DEBUG(0,("winbind_lookup_sid: talloc() failed!\n"));
     123           0 :                 return false;
     124             :         }
     125             : 
     126             : 
     127           0 :         return true;
     128             : }
     129             : 
     130             : /* Ping winbindd to see it is alive */
     131             : 
     132       46800 : bool winbind_ping(void)
     133             : {
     134       46800 :         wbcErr result = wbcPing();
     135             : 
     136       46800 :         return (result == WBC_ERR_SUCCESS);
     137             : }
     138             : 
     139             : /* Call winbindd to convert SID to uid */
     140             : 
     141        2432 : bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
     142             : {
     143           8 :         struct wbcDomainSid dom_sid;
     144           8 :         wbcErr result;
     145             : 
     146        2432 :         memcpy(&dom_sid, sid, sizeof(dom_sid));
     147             : 
     148        2432 :         result = wbcSidToUid(&dom_sid, puid);
     149             : 
     150        2432 :         return (result == WBC_ERR_SUCCESS);
     151             : }
     152             : 
     153             : /* Call winbindd to convert SID to gid */
     154             : 
     155        1319 : bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
     156             : {
     157          20 :         struct wbcDomainSid dom_sid;
     158          20 :         wbcErr result;
     159             : 
     160        1319 :         memcpy(&dom_sid, sid, sizeof(dom_sid));
     161             : 
     162        1319 :         result = wbcSidToGid(&dom_sid, pgid);
     163             : 
     164        1319 :         return (result == WBC_ERR_SUCCESS);
     165             : }
     166             : 
     167      225155 : bool winbind_xid_to_sid(struct dom_sid *sid, const struct unixid *xid)
     168             : {
     169          12 :         struct wbcUnixId wbc_xid;
     170          12 :         struct wbcDomainSid dom_sid;
     171          12 :         wbcErr result;
     172             : 
     173      225155 :         switch (xid->type) {
     174        5948 :         case ID_TYPE_UID:
     175        5948 :                 wbc_xid = (struct wbcUnixId) {
     176        5948 :                         .type = WBC_ID_TYPE_UID, .id.uid = xid->id
     177             :                 };
     178        5948 :                 break;
     179      219207 :         case ID_TYPE_GID:
     180      219207 :                 wbc_xid = (struct wbcUnixId) {
     181      219207 :                         .type = WBC_ID_TYPE_GID, .id.gid = xid->id
     182             :                 };
     183      219207 :                 break;
     184           0 :         default:
     185           0 :                 return false;
     186             :         }
     187             : 
     188      225155 :         result = wbcUnixIdsToSids(&wbc_xid, 1, &dom_sid);
     189      225155 :         if (result != WBC_ERR_SUCCESS) {
     190      213453 :                 return false;
     191             :         }
     192             : 
     193       11690 :         memcpy(sid, &dom_sid, sizeof(struct dom_sid));
     194       11690 :         return true;
     195             : }
     196             : 
     197             : /* Check for a trusted domain */
     198             : 
     199           0 : wbcErr wb_is_trusted_domain(const char *domain)
     200             : {
     201           0 :         wbcErr result;
     202           0 :         struct wbcDomainInfo *info = NULL;
     203             : 
     204           0 :         result = wbcDomainInfo(domain, &info);
     205             : 
     206           0 :         if (WBC_ERROR_IS_OK(result)) {
     207           0 :                 wbcFreeMemory(info);
     208             :         }
     209             : 
     210           0 :         return result;
     211             : }
     212             : 
     213             : /* Lookup a set of rids in a given domain */
     214             : 
     215         150 : bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
     216             :                          const struct dom_sid *domain_sid,
     217             :                          int num_rids, uint32_t *rids,
     218             :                          const char **domain_name,
     219             :                          const char ***names, enum lsa_SidType **types)
     220             : {
     221         150 :         const char *dom_name = NULL;
     222         150 :         const char **namelist = NULL;
     223         150 :         enum wbcSidType *name_types = NULL;
     224           0 :         struct wbcDomainSid dom_sid;
     225           0 :         wbcErr ret;
     226           0 :         int i;
     227             : 
     228         150 :         memcpy(&dom_sid, domain_sid, sizeof(struct wbcDomainSid));
     229             : 
     230         150 :         ret = wbcLookupRids(&dom_sid, num_rids, rids,
     231             :                             &dom_name, &namelist, &name_types);
     232         150 :         if (ret != WBC_ERR_SUCCESS) {
     233         150 :                 return false;
     234             :         }
     235             : 
     236           0 :         *domain_name = talloc_strdup(mem_ctx, dom_name);
     237           0 :         *names       = talloc_array(mem_ctx, const char*, num_rids);
     238           0 :         *types       = talloc_array(mem_ctx, enum lsa_SidType, num_rids);
     239             : 
     240           0 :         for(i=0; i<num_rids; i++) {
     241           0 :                 (*names)[i] = talloc_strdup(*names, namelist[i]);
     242           0 :                 (*types)[i] = (enum lsa_SidType)name_types[i];
     243             :         }
     244             : 
     245           0 :         wbcFreeMemory(discard_const_p(char, dom_name));
     246           0 :         wbcFreeMemory(namelist);
     247           0 :         wbcFreeMemory(name_types);
     248             : 
     249           0 :         return true;
     250             : }
     251             : 
     252             : /* Ask Winbind to allocate a new uid for us */
     253             : 
     254           0 : bool winbind_allocate_uid(uid_t *uid)
     255             : {
     256           0 :         wbcErr ret;
     257             : 
     258           0 :         ret = wbcAllocateUid(uid);
     259             : 
     260           0 :         return (ret == WBC_ERR_SUCCESS);
     261             : }
     262             : 
     263             : /* Ask Winbind to allocate a new gid for us */
     264             : 
     265         486 : bool winbind_allocate_gid(gid_t *gid)
     266             : {
     267           0 :         wbcErr ret;
     268             : 
     269         486 :         ret = wbcAllocateGid(gid);
     270             : 
     271         486 :         return (ret == WBC_ERR_SUCCESS);
     272             : }
     273             : 
     274         257 : bool winbind_lookup_usersids(TALLOC_CTX *mem_ctx,
     275             :                              const struct dom_sid *user_sid,
     276             :                              uint32_t *p_num_sids,
     277             :                              struct dom_sid **p_sids)
     278             : {
     279           0 :         wbcErr ret;
     280           0 :         struct wbcDomainSid dom_sid;
     281         257 :         struct wbcDomainSid *sid_list = NULL;
     282           0 :         uint32_t num_sids;
     283             : 
     284         257 :         memcpy(&dom_sid, user_sid, sizeof(dom_sid));
     285             : 
     286         257 :         ret = wbcLookupUserSids(&dom_sid,
     287             :                                 false,
     288             :                                 &num_sids,
     289             :                                 &sid_list);
     290         257 :         if (ret != WBC_ERR_SUCCESS) {
     291         119 :                 return false;
     292             :         }
     293             : 
     294         138 :         *p_sids = talloc_array(mem_ctx, struct dom_sid, num_sids);
     295         138 :         if (*p_sids == NULL) {
     296           0 :                 wbcFreeMemory(sid_list);
     297           0 :                 return false;
     298             :         }
     299             : 
     300         138 :         memcpy(*p_sids, sid_list, sizeof(dom_sid) * num_sids);
     301             : 
     302         138 :         *p_num_sids = num_sids;
     303         138 :         wbcFreeMemory(sid_list);
     304             : 
     305         138 :         return true;
     306             : }
     307             : 
     308             : #else      /* WITH_WINBIND */
     309             : 
     310             : struct passwd * winbind_getpwnam(const char * name)
     311             : {
     312             :         return NULL;
     313             : }
     314             : 
     315             : struct passwd * winbind_getpwsid(const struct dom_sid *sid)
     316             : {
     317             :         return NULL;
     318             : }
     319             : 
     320             : bool winbind_lookup_name(const char *dom_name, const char *name, struct dom_sid *sid,
     321             :                          enum lsa_SidType *name_type)
     322             : {
     323             :         return false;
     324             : }
     325             : 
     326             : /* Call winbindd to convert sid to name */
     327             : 
     328             : bool winbind_lookup_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
     329             :                         const char **domain, const char **name,
     330             :                         enum lsa_SidType *name_type)
     331             : {
     332             :         return false;
     333             : }
     334             : 
     335             : /* Ping winbindd to see it is alive */
     336             : 
     337             : bool winbind_ping(void)
     338             : {
     339             :         return false;
     340             : }
     341             : 
     342             : /* Call winbindd to convert SID to uid */
     343             : 
     344             : bool winbind_sid_to_uid(uid_t *puid, const struct dom_sid *sid)
     345             : {
     346             :         return false;
     347             : }
     348             : 
     349             : /* Call winbindd to convert SID to gid */
     350             : 
     351             : bool winbind_sid_to_gid(gid_t *pgid, const struct dom_sid *sid)
     352             : {
     353             :         return false;
     354             : }
     355             : 
     356             : /* Call winbindd to convert uid or gid to SID */
     357             : 
     358             : bool winbind_xid_to_sid(struct dom_sid *sid, const struct unixid *xid)
     359             : {
     360             :         return false;
     361             : }
     362             : 
     363             : /* Check for a trusted domain */
     364             : 
     365             : wbcErr wb_is_trusted_domain(const char *domain)
     366             : {
     367             :         return WBC_ERR_UNKNOWN_FAILURE;
     368             : }
     369             : 
     370             : /* Lookup a set of rids in a given domain */
     371             : 
     372             : bool winbind_lookup_rids(TALLOC_CTX *mem_ctx,
     373             :                          const struct dom_sid *domain_sid,
     374             :                          int num_rids, uint32_t *rids,
     375             :                          const char **domain_name,
     376             :                          const char ***names, enum lsa_SidType **types)
     377             : {
     378             :         return false;
     379             : }
     380             : 
     381             : /* Ask Winbind to allocate a new uid for us */
     382             : 
     383             : bool winbind_allocate_uid(uid_t *uid)
     384             : {
     385             :         return false;
     386             : }
     387             : 
     388             : /* Ask Winbind to allocate a new gid for us */
     389             : 
     390             : bool winbind_allocate_gid(gid_t *gid)
     391             : {
     392             :         return false;
     393             : }
     394             : 
     395             : bool winbind_lookup_usersids(TALLOC_CTX *mem_ctx,
     396             :                              const struct dom_sid *user_sid,
     397             :                              uint32_t *p_num_sids,
     398             :                              struct dom_sid **p_sids)
     399             : {
     400             :         return false;
     401             : }
     402             : 
     403             : #endif     /* WITH_WINBIND */

Generated by: LCOV version 1.14