LCOV - code coverage report
Current view: top level - source3/libads - ldap_user.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 33 75 44.0 %
Date: 2024-04-21 15:09:00 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    ads (active directory) utility library
       4             :    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
       5             :    
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             :    
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             :    
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "ads.h"
      22             : #include "../libds/common/flags.h"
      23             : 
      24             : #ifdef HAVE_ADS
      25             : 
      26             : /*
      27             :   find a user account
      28             : */
      29           8 :  ADS_STATUS ads_find_user_acct(ADS_STRUCT *ads, LDAPMessage **res,
      30             :                                const char *user)
      31             : {
      32           0 :         ADS_STATUS status;
      33           0 :         char *ldap_exp;
      34           8 :         const char *attrs[] = {"*", NULL};
      35           8 :         char *escaped_user = escape_ldap_string(talloc_tos(), user);
      36           8 :         if (!escaped_user) {
      37           0 :                 return ADS_ERROR(LDAP_NO_MEMORY);
      38             :         }
      39             : 
      40           8 :         if (asprintf(&ldap_exp, "(samAccountName=%s)", escaped_user) == -1) {
      41           0 :                 TALLOC_FREE(escaped_user);
      42           0 :                 return ADS_ERROR(LDAP_NO_MEMORY);
      43             :         }
      44           8 :         status = ads_search(ads, res, ldap_exp, attrs);
      45           8 :         SAFE_FREE(ldap_exp);
      46           8 :         TALLOC_FREE(escaped_user);
      47           8 :         return status;
      48             : }
      49             : 
      50           4 : ADS_STATUS ads_add_user_acct(ADS_STRUCT *ads, const char *user, 
      51             :                              const char *container, const char *fullname)
      52             : {
      53           0 :         TALLOC_CTX *ctx;
      54           0 :         ADS_MODLIST mods;
      55           0 :         ADS_STATUS status;
      56           0 :         const char *upn, *new_dn, *name, *controlstr;
      57           4 :         char *name_escaped = NULL;
      58           4 :         const char *objectClass[] = {"top", "person", "organizationalPerson",
      59             :                                      "user", NULL};
      60             : 
      61           4 :         if (fullname && *fullname) name = fullname;
      62           4 :         else name = user;
      63             : 
      64           4 :         if (!(ctx = talloc_init("ads_add_user_acct")))
      65           0 :                 return ADS_ERROR(LDAP_NO_MEMORY);
      66             : 
      67           4 :         status = ADS_ERROR(LDAP_NO_MEMORY);
      68             : 
      69           4 :         if (!(upn = talloc_asprintf(ctx, "%s@%s", user, ads->config.realm)))
      70           0 :                 goto done;
      71           4 :         if (!(name_escaped = escape_rdn_val_string_alloc(name)))
      72           0 :                 goto done;
      73           4 :         if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name_escaped, container,
      74             :                                        ads->config.bind_path)))
      75           0 :                 goto done;
      76           4 :         if (!(controlstr = talloc_asprintf(ctx, "%u", (UF_NORMAL_ACCOUNT | UF_ACCOUNTDISABLE))))
      77           0 :                 goto done;
      78           4 :         if (!(mods = ads_init_mods(ctx)))
      79           0 :                 goto done;
      80             : 
      81           4 :         ads_mod_str(ctx, &mods, "cn", name);
      82           4 :         ads_mod_strlist(ctx, &mods, "objectClass", objectClass);
      83           4 :         ads_mod_str(ctx, &mods, "userPrincipalName", upn);
      84           4 :         ads_mod_str(ctx, &mods, "name", name);
      85           4 :         ads_mod_str(ctx, &mods, "displayName", name);
      86           4 :         ads_mod_str(ctx, &mods, "sAMAccountName", user);
      87           4 :         ads_mod_str(ctx, &mods, "userAccountControl", controlstr);
      88           4 :         status = ads_gen_add(ads, new_dn, mods);
      89             : 
      90           4 :  done:
      91           4 :         SAFE_FREE(name_escaped);
      92           4 :         talloc_destroy(ctx);
      93           4 :         return status;
      94             : }
      95             : 
      96           0 : ADS_STATUS ads_add_group_acct(ADS_STRUCT *ads, const char *group, 
      97             :                               const char *container, const char *comment)
      98             : {
      99           0 :         TALLOC_CTX *ctx;
     100           0 :         ADS_MODLIST mods;
     101           0 :         ADS_STATUS status;
     102           0 :         char *new_dn;
     103           0 :         char *name_escaped = NULL;
     104           0 :         const char *objectClass[] = {"top", "group", NULL};
     105             : 
     106           0 :         if (!(ctx = talloc_init("ads_add_group_acct")))
     107           0 :                 return ADS_ERROR(LDAP_NO_MEMORY);
     108             : 
     109           0 :         status = ADS_ERROR(LDAP_NO_MEMORY);
     110             : 
     111           0 :         if (!(name_escaped = escape_rdn_val_string_alloc(group)))
     112           0 :                 goto done;
     113           0 :         if (!(new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", name_escaped, container,
     114             :                                        ads->config.bind_path)))
     115           0 :                 goto done;
     116           0 :         if (!(mods = ads_init_mods(ctx)))
     117           0 :                 goto done;
     118             : 
     119           0 :         ads_mod_str(ctx, &mods, "cn", group);
     120           0 :         ads_mod_strlist(ctx, &mods, "objectClass",objectClass);
     121           0 :         ads_mod_str(ctx, &mods, "name", group);
     122           0 :         if (comment && *comment) 
     123           0 :                 ads_mod_str(ctx, &mods, "description", comment);
     124           0 :         ads_mod_str(ctx, &mods, "sAMAccountName", group);
     125           0 :         status = ads_gen_add(ads, new_dn, mods);
     126             : 
     127           0 :  done:
     128           0 :         SAFE_FREE(name_escaped);
     129           0 :         talloc_destroy(ctx);
     130           0 :         return status;
     131             : }
     132             : #endif

Generated by: LCOV version 1.14