/* vi: set sw=4 ts=4: */
/*
 * password utility routines.
 *
 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
 *
 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
 */

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "libbb.h"

  /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  * flexible :
  *
  * if bufsize is > 0 char *group cannot be set to NULL.
  *                   On success groupname is written on static allocated buffer
  *                   group (and a pointer to it is returned).
  *                   On failure gid as string is written to static allocated
  *                   buffer group and NULL is returned.
  * if bufsize is = 0 char *group can be set to NULL.
  *                   On success groupname is returned.
  *                   On failure NULL is returned.
  * if bufsize is < 0 char *group can be set to NULL.
  *                   On success groupname is returned.
  *                   On failure an error message is printed and
  *                   the program exits.
  */

/* gets a groupname given a gid */
char * bb_getgrgid(char *group, long gid, int bufsize)
{
	struct group *mygroup = getgrgid(gid);

	return bb_getug(group, (mygroup) ?
			mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g');
}

/* returns a gid given a group name */
long bb_xgetgrnam(const char *name)
{
	struct group *mygroup;

	mygroup  = getgrnam(name);
	if (mygroup==NULL)
		bb_error_msg_and_die("unknown group name: %s", name);

	return (mygroup->gr_gid);
}

/* returns a uid given a username */
long bb_xgetpwnam(const char *name)
{
	struct passwd *myuser;

	myuser  = getpwnam(name);
	if (myuser==NULL)
		bb_error_msg_and_die("unknown user name: %s", name);

	return myuser->pw_uid;
}

 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
  * flexible :
  *
  * if bufsize is > 0 char *name cannot be set to NULL.
  *                   On success username is written on the static allocated
  *                   buffer name (and a pointer to it is returned).
  *                   On failure uid as string is written to the static
  *                   allocated buffer name and NULL is returned.
  * if bufsize is = 0 char *name can be set to NULL.
  *                   On success username is returned.
  *                   On failure NULL is returned.
  * if bufsize is < 0 char *name can be set to NULL
  *                   On success username is returned.
  *                   On failure an error message is printed and
  *                   the program exits.
  */

/* gets a username given a uid */
char * bb_getpwuid(char *name, long uid, int bufsize)
{
	struct passwd *myuser = getpwuid(uid);

	return  bb_getug(name, (myuser) ?
			myuser->pw_name : (char *)myuser , uid, bufsize, 'u');
}

 /*
  * if bufsize is > 0 char *buffer cannot be set to NULL.
  *                   If idname is not NULL it is written on the static
  *                   allocated buffer (and a pointer to it is returned).
  *                   if idname is NULL, id as string is written to the static
  *                   allocated buffer and NULL is returned.
  * if bufsize is = 0 char *buffer can be set to NULL.
  *                   If idname exists a pointer to it is returned,
  *                   else NULL is returned.
  * if bufsize is < 0 char *buffer can be set to NULL.
  *                   If idname exists a pointer to it is returned,
  *                   else an error message is printed and the program exits.
  */

/* internal function for bb_getpwuid and bb_getgrgid */
char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
{
	if(bufsize > 0 ) {
		assert(buffer!=NULL);
		if(idname) {
			return safe_strncpy(buffer, idname, bufsize);
		}
		snprintf(buffer, bufsize, "%ld", id);
	} else if(bufsize < 0 && !idname) {
		bb_error_msg_and_die("unknown %cid %ld", prefix, id);
	}
	return idname;
}

unsigned long get_ug_id(const char *s,
		long (*__bb_getxxnam)(const char *))
{
	unsigned long r;
	char *p;

	r = strtoul(s, &p, 10);
	if (*p || (s == p)) {
		r = __bb_getxxnam(s);
	}

	return r;
}
