Print this page
2989 Eliminate use of LOGNAME_MAX in ON
1166 useradd have warning with name more 8 chars

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libc/port/gen/getlogin.c
          +++ new/usr/src/lib/libc/port/gen/getlogin.c
↓ open down ↓ 12 lines elided ↑ open up ↑
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
       23 + * Copyright (c) 2013 Gary Mills
       24 + *
  23   25   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24   26   * Use is subject to license terms.
  25   27   */
  26   28  
  27   29  /*      Copyright (c) 1988 AT&T */
  28   30  /*        All Rights Reserved   */
  29   31  
  30      -#pragma ident   "%Z%%M% %I%     %E% SMI"
       32 +#pragma weak _getlogin = getloginx
       33 +#pragma weak _getlogin_r = getloginx_r
  31   34  
  32      -#pragma weak _getlogin = getlogin
  33      -#pragma weak _getlogin_r = getlogin_r
  34      -
  35   35  #include "lint.h"
  36   36  #include <sys/types.h>
  37   37  #include <sys/stat.h>
  38   38  #include <fcntl.h>
  39   39  #include <string.h>
  40   40  #include <stdlib.h>
  41   41  #include <limits.h>
  42   42  #include "utmpx.h"
  43   43  #include <unistd.h>
  44   44  #include <errno.h>
  45   45  #include <thread.h>
  46   46  #include <synch.h>
  47   47  #include <mtlib.h>
  48   48  #include "tsd.h"
  49   49  
       50 +/* Revert the renames done in unistd.h */
       51 +#ifdef  __PRAGMA_REDEFINE_EXTNAME
       52 +#pragma redefine_extname        getlogint       getlogin
       53 +#pragma redefine_extname        getlogint_r     getlogin_r
       54 +#pragma redefine_extname        __posix_getlogint_r     __posix_getlogin_r
       55 +#else   /* __PRAGMA_REDEFINE_EXTNAME */
       56 +#ifdef  getlogin
       57 +#undef  getlogin
       58 +#endif  /* getlogin */
       59 +#ifdef  getlogin_r
       60 +#undef  getlogin_r
       61 +#endif  /* getlogin_r */
       62 +#ifdef  __posix_getlogin_r
       63 +#undef  __posix_getlogin_r
       64 +#endif  /* __posix_getlogin_r */
       65 +#define getlogint       getlogin
       66 +#define getlogint_r     getlogin_r
       67 +#define __posix_getlogint_r     __posix_getlogin_r
       68 +#endif  /* __PRAGMA_REDEFINE_EXTNAME */
       69 +
  50   70  /*
  51      - * XXX - _POSIX_LOGIN_NAME_MAX limits the length of a login name.  The utmpx
  52      - * interface provides for a 32 character login name, but for the sake of
  53      - * compatibility, we are still using the old utmp-imposed limit.
       71 + * Use the full length of a login name.
       72 + * The utmpx interface provides for a 32 character login name.
  54   73   */
       74 +#define NMAX    (sizeof (((struct utmpx *)0)->ut_user))
  55   75  
  56   76  /*
  57      - * POSIX.1c Draft-6 version of the function getlogin_r.
  58      - * It was implemented by Solaris 2.3.
       77 + * Common function
  59   78   */
  60      -char *
  61      -getlogin_r(char *answer, int namelen)
       79 +static char *
       80 +getl_r_common(char *answer, size_t namelen, size_t maxlen)
  62   81  {
  63   82          int             uf;
  64   83          off64_t         me;
  65   84          struct futmpx   ubuf;
  66   85  
  67      -        if (namelen < _POSIX_LOGIN_NAME_MAX) {
  68      -                errno = ERANGE;
  69      -                return (NULL);
  70      -        }
  71      -
  72   86          if ((me = (off64_t)ttyslot()) < 0)
  73   87                  return (NULL);
  74   88          if ((uf = open64(UTMPX_FILE, 0)) < 0)
  75   89                  return (NULL);
  76   90          (void) lseek64(uf, me * sizeof (ubuf), SEEK_SET);
  77   91          if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) {
  78   92                  (void) close(uf);
  79   93                  return (NULL);
  80   94          }
  81   95          (void) close(uf);
  82   96          if (ubuf.ut_user[0] == '\0')
  83   97                  return (NULL);
  84      -        (void) strncpy(&answer[0], &ubuf.ut_user[0],
  85      -            _POSIX_LOGIN_NAME_MAX - 1);
  86      -        answer[_POSIX_LOGIN_NAME_MAX - 1] = '\0';
       98 +
       99 +        /* Insufficient buffer size */
      100 +        if (namelen < strnlen(&ubuf.ut_user[0], maxlen)) {
      101 +                errno = ERANGE;
      102 +                return (NULL);
      103 +        }
      104 +        (void) strncpy(&answer[0], &ubuf.ut_user[0], maxlen);
      105 +        answer[maxlen] = '\0';
  87  106          return (&answer[0]);
  88  107  }
  89  108  
  90  109  /*
      110 + * POSIX.1c Draft-6 version of the function getlogin_r.
      111 + * It was implemented by Solaris 2.3.
      112 + */
      113 +char *
      114 +getlogint_r(char *answer, int namelen)
      115 +{
      116 +        return (getl_r_common(answer, (size_t)namelen, LOGNAME_MAX_TRAD));
      117 +}
      118 +
      119 +/*
  91  120   * POSIX.1c standard version of the function getlogin_r.
  92  121   * User gets it via static getlogin_r from the header file.
  93  122   */
  94  123  int
  95      -__posix_getlogin_r(char *name, int namelen)
      124 +__posix_getlogint_r(char *name, int namelen)
  96  125  {
  97  126          int nerrno = 0;
  98  127          int oerrno = errno;
  99  128  
 100  129          errno = 0;
 101      -        if (getlogin_r(name, namelen) == NULL) {
      130 +        if (getl_r_common(name, (size_t)namelen, LOGNAME_MAX_TRAD) == NULL) {
 102  131                  if (errno == 0)
 103  132                          nerrno = EINVAL;
 104  133                  else
 105  134                          nerrno = errno;
 106  135          }
 107  136          errno = oerrno;
 108  137          return (nerrno);
 109  138  }
 110  139  
 111  140  char *
 112      -getlogin(void)
      141 +getlogint(void)
 113  142  {
 114      -        char *answer = tsdalloc(_T_LOGIN, _POSIX_LOGIN_NAME_MAX, NULL);
      143 +        char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX_TRAD, NULL);
 115  144  
 116  145          if (answer == NULL)
 117  146                  return (NULL);
 118      -        return (getlogin_r(answer, _POSIX_LOGIN_NAME_MAX));
      147 +        return (getl_r_common(answer, LOGIN_NAME_MAX_TRAD, LOGNAME_MAX_TRAD));
      148 +}
      149 +
      150 +/*
      151 + * POSIX.1c Draft-6 version of the function getlogin_r.
      152 + * It was implemented by Solaris 2.3.
      153 + * For extended login names, selected by redefine_extname in unistd.h.
      154 + */
      155 +char *
      156 +getloginx_r(char *answer, int namelen)
      157 +{
      158 +        return (getl_r_common(answer, (size_t)namelen, NMAX));
      159 +}
      160 +
      161 +/*
      162 + * POSIX.1c standard version of the function getlogin_r.
      163 + * User gets it via static getlogin_r from the header file.
      164 + * For extended login names, selected by redefine_extname in unistd.h.
      165 + */
      166 +int
      167 +__posix_getloginx_r(char *name, int namelen)
      168 +{
      169 +        int nerrno = 0;
      170 +        int oerrno = errno;
      171 +
      172 +        errno = 0;
      173 +        if (getl_r_common(name, (size_t)namelen, NMAX) == NULL) {
      174 +                if (errno == 0)
      175 +                        nerrno = EINVAL;
      176 +                else
      177 +                        nerrno = errno;
      178 +        }
      179 +        errno = oerrno;
      180 +        return (nerrno);
      181 +}
      182 +
      183 +/*
      184 + * For extended login names, selected by redefine_extname in unistd.h.
      185 + */
      186 +char *
      187 +getloginx(void)
      188 +{
      189 +        char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX, NULL);
      190 +
      191 +        if (answer == NULL)
      192 +                return (NULL);
      193 +        return (getl_r_common(answer, LOGIN_NAME_MAX, NMAX));
 119  194  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX