/*
 *
 *
 *    uginfo.cc
 *
 *    C++ source code for class my::auth member functions for user/group info.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1999-2003 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 *
 */

#include <my++/auth.h>
#include <my++/stype.h>

#include <grp.h>
#include <pwd.h>
#include <sys/types.h>


void my::auth::userinfo::clear (void) {
	ui_name.erase();
	ui_gecos.erase();
	ui_home.erase();
	ui_shell.erase();
	ui_uid = 0;
	ui_gid = 0;
}


void my::auth::userinfo::assign (const my::auth::userinfo &ui) {
	ui_name = ui.ui_name;
	ui_gecos = ui.ui_gecos;
	ui_home = ui.ui_home;
	ui_shell = ui.ui_shell;
	ui_uid = ui.ui_uid;
	ui_gid = ui.ui_gid;
}


void my::auth::userinfo::assign (uid_t id) {
	// get the passwd entry
	::passwd *result = getpwuid(id);

	// if the user doesn't exist, clear the object data
	if (!result) {
		clear();
		return;
	}

	// setup the data
	ui_name = result->pw_name;
	ui_uid = result->pw_uid;
	ui_gid = result->pw_gid;
	ui_gecos = result->pw_gecos;
	ui_home = result->pw_dir;
	ui_shell = result->pw_shell;
}

void my::auth::userinfo::assign (const std::string &un) {
	// trim off careless whitespace
	std::string real_un = ltrim(rtrim(un));

	// if an empty string remains, clear the object data
	if (real_un.empty()) {
		clear();
		return;
	}

	// get the user information
	::passwd *result = getpwnam(real_un.c_str());

	// if the user doesn't exist, clear object data
	if (!result) {
		clear();
		return;
	}

	// setup the data
	ui_name = result->pw_name;
	ui_uid = result->pw_uid;
	ui_gid = result->pw_gid;
	ui_gecos = result->pw_gecos;
	ui_home = result->pw_dir;
	ui_shell = result->pw_shell;
}


void my::auth::groupinfo::clear (void) {
	gi_name.erase();     // you 'erase' strings...
	gi_members.clear();  // ...but 'clear' containers...
	gi_number = 0;       // ...ah, the consistency of STL...
}

void my::auth::groupinfo::assign (const std::string &gn) {
	// trim off careless whitespace
	std::string real_gn = ltrim(rtrim(gn));

	// if an empty string is passed, clear the object data
	if (real_gn.empty()) {
		clear();
		return;
	}

	// get the group information
	::group *result = getgrnam (real_gn.c_str());
	
	// if the group doesn't exist, clear object data
	if (!result) {
		clear();
		return;
	}

	// set the real group name
	gi_name = result->gr_name;
	gi_number = result->gr_gid;
	for (char **m = result->gr_mem; *m != 0; m++)
		gi_members.push_back((const char*)(*m));
}


void my::auth::groupinfo::assign (gid_t gi) {
	// get the group information
	::group *result = getgrgid (gi);
	
	// if the group doesn't exist, clear object data
	if (!result) {
		clear();
		return;
	}

	// set the real group name
	gi_name = result->gr_name;
	gi_number = result->gr_gid;
	for (char **m = result->gr_mem; *m != 0; m++)
		gi_members.push_back(*m);
}


void my::auth::groupinfo::assign (const my::auth::groupinfo &gi) {
	gi_name = gi.gi_name;
	gi_number = gi.gi_number;
	gi_members.assign(gi.gi_members.begin(), gi.gi_members.end());
}

