Exemple #1
0
/* Write an entry to the given stream.
   This must know the format of the group file.  */
int
putsgent (const struct sgrp *g, FILE *stream)
{
  int errors = 0;

  if (g->sg_namp == NULL || !__nss_valid_field (g->sg_namp)
      || !__nss_valid_field (g->sg_passwd)
      || !__nss_valid_list_field (g->sg_adm)
      || !__nss_valid_list_field (g->sg_mem))
    {
      __set_errno (EINVAL);
      return -1;
    }

  _IO_flockfile (stream);

  if (fprintf (stream, "%s:%s:", g->sg_namp, _S (g->sg_passwd)) < 0)
    ++errors;

  bool first = true;
  char **sp = g->sg_adm;
  if (sp != NULL)
    while (*sp != NULL)
      {
	if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
	  {
	    ++errors;
	    break;
	  }
	first = false;
      }
  if (putc_unlocked (':', stream) == EOF)
    ++errors;

  first = true;
  sp = g->sg_mem;
  if (sp != NULL)
    while (*sp != NULL)
      {
	if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
	  {
	    ++errors;
	    break;
	  }
	first = false;
      }
  if (putc_unlocked ('\n', stream) == EOF)
    ++errors;

  _IO_funlockfile (stream);

  return errors ? -1 : 0;
}
Exemple #2
0
/* Write an entry to the given stream.
   This must know the format of the group file.  */
int
putgrent (const struct group *gr, FILE *stream)
{
  int retval;

  if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL)
      || gr->gr_name == NULL || !__nss_valid_field (gr->gr_name)
      || !__nss_valid_field (gr->gr_passwd)
      || !__nss_valid_list_field (gr->gr_mem))
    {
      __set_errno (EINVAL);
      return -1;
    }

  flockfile (stream);

  if (gr->gr_name[0] == '+' || gr->gr_name[0] == '-')
    retval = fprintf (stream, "%s:%s::",
		      gr->gr_name, _S (gr->gr_passwd));
  else
    retval = fprintf (stream, "%s:%s:%lu:",
		      gr->gr_name, _S (gr->gr_passwd),
		      (unsigned long int) gr->gr_gid);
  if (__builtin_expect (retval, 0) < 0)
    {
      funlockfile (stream);
      return -1;
    }

  if (gr->gr_mem != NULL)
    {
      for (size_t i = 0; gr->gr_mem[i] != NULL; i++)
	if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0)
	  {
	    /* What else can we do?  */
	    funlockfile (stream);
	    return -1;
	  }
    }

  retval = fputc_unlocked ('\n', stream);

  funlockfile (stream);

  return retval < 0 ? -1 : 0;
}
Exemple #3
0
/* Write an entry to the given stream.  This must know the format of
   the password file.  If the input contains invalid characters,
   return EINVAL, or replace them with spaces (if they are contained
   in the GECOS field).  */
int
putpwent (const struct passwd *p, FILE *stream)
{
  if (p == NULL || stream == NULL
      || p->pw_name == NULL || !__nss_valid_field (p->pw_name)
      || !__nss_valid_field (p->pw_passwd)
      || !__nss_valid_field (p->pw_dir)
      || !__nss_valid_field (p->pw_shell))
    {
      __set_errno (EINVAL);
      return -1;
    }

  int ret;
  char *gecos_alloc;
  const char *gecos = __nss_rewrite_field (p->pw_gecos, &gecos_alloc);

  if (gecos == NULL)
    return -1;

  if (p->pw_name[0] == '+' || p->pw_name[0] == '-')
      ret = fprintf (stream, "%s:%s:::%s:%s:%s\n",
		     p->pw_name, _S (p->pw_passwd),
		     gecos, _S (p->pw_dir), _S (p->pw_shell));
  else
      ret = fprintf (stream, "%s:%s:%lu:%lu:%s:%s:%s\n",
		     p->pw_name, _S (p->pw_passwd),
		     (unsigned long int) p->pw_uid,
		     (unsigned long int) p->pw_gid,
		     gecos, _S (p->pw_dir), _S (p->pw_shell));

  free (gecos_alloc);
  if (ret >= 0)
    ret = 0;
  return ret;
}