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"
  31      -
  32   32  #pragma weak _getlogin = getlogin
  33   33  #pragma weak _getlogin_r = getlogin_r
  34   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   50  /*
  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.
       51 + * Use the full length of a login name, from LOGIN_NAME_MAX .
       52 + * The utmpx interface provides for a 32 character login name.
  54   53   */
  55   54  
  56   55  /*
  57   56   * POSIX.1c Draft-6 version of the function getlogin_r.
  58   57   * It was implemented by Solaris 2.3.
  59   58   */
  60   59  char *
  61   60  getlogin_r(char *answer, int namelen)
  62   61  {
  63   62          int             uf;
  64   63          off64_t         me;
  65   64          struct futmpx   ubuf;
  66   65  
       66 +        /* Required minimum */
  67   67          if (namelen < _POSIX_LOGIN_NAME_MAX) {
  68   68                  errno = ERANGE;
  69   69                  return (NULL);
  70   70          }
  71   71  
  72   72          if ((me = (off64_t)ttyslot()) < 0)
  73   73                  return (NULL);
  74   74          if ((uf = open64(UTMPX_FILE, 0)) < 0)
  75   75                  return (NULL);
  76   76          (void) lseek64(uf, me * sizeof (ubuf), SEEK_SET);
  77   77          if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) {
  78   78                  (void) close(uf);
  79   79                  return (NULL);
  80   80          }
  81   81          (void) close(uf);
  82   82          if (ubuf.ut_user[0] == '\0')
  83   83                  return (NULL);
       84 +
       85 +        /* Insufficient buffer size */
       86 +        if (namelen < strnlen(&ubuf.ut_user[0], LOGIN_NAME_MAX - 1)) {
       87 +                errno = ERANGE;
       88 +                return (NULL);
       89 +        }
  84   90          (void) strncpy(&answer[0], &ubuf.ut_user[0],
  85      -            _POSIX_LOGIN_NAME_MAX - 1);
  86      -        answer[_POSIX_LOGIN_NAME_MAX - 1] = '\0';
       91 +            LOGIN_NAME_MAX - 1);
       92 +        answer[LOGIN_NAME_MAX - 1] = '\0';
  87   93          return (&answer[0]);
  88   94  }
  89   95  
  90   96  /*
  91   97   * POSIX.1c standard version of the function getlogin_r.
  92   98   * User gets it via static getlogin_r from the header file.
  93   99   */
  94  100  int
  95  101  __posix_getlogin_r(char *name, int namelen)
  96  102  {
↓ open down ↓ 7 lines elided ↑ open up ↑
 104  110                  else
 105  111                          nerrno = errno;
 106  112          }
 107  113          errno = oerrno;
 108  114          return (nerrno);
 109  115  }
 110  116  
 111  117  char *
 112  118  getlogin(void)
 113  119  {
 114      -        char *answer = tsdalloc(_T_LOGIN, _POSIX_LOGIN_NAME_MAX, NULL);
      120 +        char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX, NULL);
 115  121  
 116  122          if (answer == NULL)
 117  123                  return (NULL);
 118      -        return (getlogin_r(answer, _POSIX_LOGIN_NAME_MAX));
      124 +        return (getlogin_r(answer, LOGIN_NAME_MAX));
 119  125  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX