Blame pwd/putpwent.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <pwd.h>
Packit 6c4009
#include <nss.h>
Packit 6c4009
Packit 6c4009
#define _S(x)	x ?: ""
Packit 6c4009
Packit 6c4009
/* Write an entry to the given stream.  This must know the format of
Packit 6c4009
   the password file.  If the input contains invalid characters,
Packit 6c4009
   return EINVAL, or replace them with spaces (if they are contained
Packit 6c4009
   in the GECOS field).  */
Packit 6c4009
int
Packit 6c4009
putpwent (const struct passwd *p, FILE *stream)
Packit 6c4009
{
Packit 6c4009
  if (p == NULL || stream == NULL
Packit 6c4009
      || p->pw_name == NULL || !__nss_valid_field (p->pw_name)
Packit 6c4009
      || !__nss_valid_field (p->pw_passwd)
Packit 6c4009
      || !__nss_valid_field (p->pw_dir)
Packit 6c4009
      || !__nss_valid_field (p->pw_shell))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int ret;
Packit 6c4009
  char *gecos_alloc;
Packit 6c4009
  const char *gecos = __nss_rewrite_field (p->pw_gecos, &gecos_alloc);
Packit 6c4009
Packit 6c4009
  if (gecos == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  if (p->pw_name[0] == '+' || p->pw_name[0] == '-')
Packit 6c4009
      ret = fprintf (stream, "%s:%s:::%s:%s:%s\n",
Packit 6c4009
		     p->pw_name, _S (p->pw_passwd),
Packit 6c4009
		     gecos, _S (p->pw_dir), _S (p->pw_shell));
Packit 6c4009
  else
Packit 6c4009
      ret = fprintf (stream, "%s:%s:%lu:%lu:%s:%s:%s\n",
Packit 6c4009
		     p->pw_name, _S (p->pw_passwd),
Packit 6c4009
		     (unsigned long int) p->pw_uid,
Packit 6c4009
		     (unsigned long int) p->pw_gid,
Packit 6c4009
		     gecos, _S (p->pw_dir), _S (p->pw_shell));
Packit 6c4009
Packit 6c4009
  free (gecos_alloc);
Packit 6c4009
  if (ret >= 0)
Packit 6c4009
    ret = 0;
Packit 6c4009
  return ret;
Packit 6c4009
}