LCOV - code coverage report
Current view: top level - lib/util - util_pw.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 23 31 74.2 %
Date: 2024-04-21 15:09:00 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Safe versions of getpw* calls
       5             : 
       6             :    Copyright (C) Andrew Tridgell 1992-1998
       7             :    Copyright (C) Jeremy Allison  1998-2005
       8             :    Copyright (C) Andrew Bartlett 2002
       9             :    Copyright (C) Timur Bakeyev        2005
      10             :    Copyright (C) Bjoern Jacke    2006-2007
      11             : 
      12             :    
      13             :    This program is free software; you can redistribute it and/or modify
      14             :    it under the terms of the GNU General Public License as published by
      15             :    the Free Software Foundation; either version 3 of the License, or
      16             :    (at your option) any later version.
      17             :    
      18             :    This program is distributed in the hope that it will be useful,
      19             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21             :    GNU General Public License for more details.
      22             :    
      23             :    You should have received a copy of the GNU General Public License
      24             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      25             : */
      26             : 
      27             : #include "replace.h"
      28             : #include <talloc.h>
      29             : #include "system/passwd.h"
      30             : #include "lib/util/util_pw.h"
      31             : 
      32      240733 : struct passwd *tcopy_passwd(TALLOC_CTX *mem_ctx,
      33             :                             const struct passwd *from)
      34             : {
      35        1528 :         struct passwd *ret;
      36      240733 :         size_t len = 0;
      37             : 
      38      240733 :         len += strlen(from->pw_name)+1;
      39      240733 :         len += strlen(from->pw_passwd)+1;
      40      240733 :         len += strlen(from->pw_gecos)+1;
      41      240733 :         len += strlen(from->pw_dir)+1;
      42      240733 :         len += strlen(from->pw_shell)+1;
      43             : 
      44      240733 :         ret = talloc_pooled_object(mem_ctx, struct passwd, 5, len);
      45             : 
      46      240733 :         if (ret == NULL) {
      47           0 :                 return NULL;
      48             :         }
      49             : 
      50      240733 :         ret->pw_name = talloc_strdup(ret, from->pw_name);
      51      240733 :         ret->pw_passwd = talloc_strdup(ret, from->pw_passwd);
      52      240733 :         ret->pw_uid = from->pw_uid;
      53      240733 :         ret->pw_gid = from->pw_gid;
      54      240733 :         ret->pw_gecos = talloc_strdup(ret, from->pw_gecos);
      55      240733 :         ret->pw_dir = talloc_strdup(ret, from->pw_dir);
      56      240733 :         ret->pw_shell = talloc_strdup(ret, from->pw_shell);
      57             : 
      58      240733 :         return ret;
      59             : }
      60             : 
      61           0 : struct passwd *getpwnam_alloc(TALLOC_CTX *mem_ctx, const char *name)
      62             : {
      63           0 :         struct passwd *temp;
      64             : 
      65           0 :         temp = getpwnam(name);
      66             :         
      67           0 :         if (!temp) {
      68             : #if 0
      69             :                 if (errno == ENOMEM) {
      70             :                         /* what now? */
      71             :                 }
      72             : #endif
      73           0 :                 return NULL;
      74             :         }
      75             : 
      76           0 :         return tcopy_passwd(mem_ctx, temp);
      77             : }
      78             : 
      79             : /****************************************************************************
      80             :  talloc'ed version of getpwuid.
      81             : ****************************************************************************/
      82             : 
      83       24650 : struct passwd *getpwuid_alloc(TALLOC_CTX *mem_ctx, uid_t uid)
      84             : {
      85           0 :         struct passwd *temp;
      86             : 
      87       24650 :         temp = getpwuid(uid);
      88             :         
      89       24650 :         if (!temp) {
      90             : #if 0
      91             :                 if (errno == ENOMEM) {
      92             :                         /* what now? */
      93             :                 }
      94             : #endif
      95        1228 :                 return NULL;
      96             :         }
      97             : 
      98       23422 :         return tcopy_passwd(mem_ctx, temp);
      99             : }

Generated by: LCOV version 1.14