/*
 * T.C.F.S. Utils 2.0 $Revision: 2.1 $
 *
 * Authors:	Giuseppe Cattaneo, <cattaneo@udsab.dia.unisa.it>
 *		Giuseppe Persiano, <giuper@udsab.dia.unisa.it>
 *		Luigi Catuogno, <luicat@mikonos.dia.unisa.it>
 *		Angelo Celentano, <angcel@mikonos.dia.unisa.it>
 *		Andrea Cozzolino, <andcoz@mikonos.dia.unisa.it>
 *		Aniello Del Sorbo, <anidel@mikonos.dia.unisa.it>
 *		Ermelindo Mauriello, <ermmau@mikonos.dia.unisa.it>
 *		Raffaele Pisapia, <rafpis@mikonos.dia.unisa.it>
 *
 * Permission  to use,  copy,  and modify this  software  without fee
 * is hereby granted, provided that this entire notice is included in
 * all  copies  of  any  software  which  is  or includes  a copy  or
 * modification of this  software and in all copies of the supporting
 * documentation for such software.
 *
 * This  software maybe  be used for  any purpose provided  the above 
 * copyright  notice  is  retained.  It is  supplied as  is, with  no 
 * warranty expressed or implied.
 */

/*
 *       $Source: /home/anidel/Wip/TCFS-2.2.0a/tcfsutils-1.1/src/RCS/tcfsadduser.c,v $
 *        $State: Exp $
 *
 *     $Revision: 2.1 $
 *       $Author: anidel $
 *         $Date: 1998/11/24 11:28:20 $
 *       $Locker:  $
 */

static const char *RCSid="$Id: tcfsadduser.c,v 2.1 1998/11/24 11:28:20 anidel Exp $";
/* -+-_== */ 

#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <tcfslib.h>

#include "tcfserrors.h"

static struct option long_options[]=
{
	{ "login", 1, NULL, 'l' },
	{ "help", 0, NULL, 'h' },
	{ "verbose", 0, NULL, 'v' },
	{ 0, 0, 0, 0 }
};

char *usage="Usage: %s [OPTION]...
Add an user entry to the TCFS database.

  -l [--login] <user>    Username to add to the TCFS database
  -h [--help]            Shows this help
  -v [--verbose]         Makes the output a little more verbose\n";

int main (int argn, char *argv[])
{
	int index, val;
	int have_user=FALSE, be_verbose=FALSE;
	char user[9], passwd[9];
	tcfspwdb *user_info;

	/*
	 * Going to check the arguments
	 */
	while ((val=getopt_long (argn, argv, "g:l:hv", long_options, &index))!=EOF)
		switch (val)
		{
			case 'l':
				strncpy (user, optarg, 9);
				have_user=1;
				break;
			case 'h':
				show_usage (usage, argv[0]);
				exit (OK);
				break;
			case 'v':
				be_verbose=TRUE;
				break;
			default:
				fprintf (stderr, "Try %s --help for more information.\n", argv[0]);
				exit (ER_UNKOPT);
				break;
		}

	if (argn-optind)
		tcfs_error (ER_UNKOPT, NULL);

	/*
	 * Here we don't have to drop root privileges because only root
	 * should run us.
	 * However we can do better. Maybe in next versions.
	 */
	if (!have_user)
	{
		printf ("Username to add to TCFS database: ");
		gets (user);
	}

	if (be_verbose)
		printf ("Creating a new entry for user %s in the TCFS database...\n", user);

	/*
	 * Creating a new entry into the key database
	 */
	if (!tcfspwdbr_new (&user_info))
		tcfs_error (ER_MEM, NULL);

	if (!tcfspwdbr_edit (&user_info, F_USR, user))
		tcfs_error (ER_MEM, NULL);

	if (!tcfs_putpwnam (user, user_info, U_NEW))
		tcfs_error (ER_CUSTOM, "Error: cannot add user.");

	if (be_verbose)
		printf ("User entry created with success.\n");

	tcfs_error (OK, NULL);
}
