// Name: usrinfo(UNIX + web version)
// Version: .10
// Description: Displays information about a user
// Created 17 January 2004
// Last Modified 25 June 2004
// Copyright (c) 2004, Aaron Myles Landwehr
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//includes
#include "../include/usrinfo.h"
using namespace std;
//globals
char* linebreak ="\n"; //default break style: unix
char* usage ="usrinfo [-wtnpugdso]"; //the commandline usage information
char* usage2 ="usrinfo [-wtnpugdso] [user]"; //the commandline usage information
char* usage3 ="usrinfo [-iwtnpugdso] [uid]"; //the commandline usage information
char* usage4 ="usrinfo [-v]"; //the commandline usage information
char* usage5 ="usrinfo [-h]"; //the commandline usage information
char* version =".10(25 June 2004)"; //version number
bool pref =0; //used to determine if to display topics
bool pwnam =1; //used to determine if username or uid is used
struct passwd *pwd; //passwd structure
void argtest(int argc, char** argv)//test the arguments for correctness
{
if(argc<2||argc>3)//should be 2 || 3 arguments entered
{
argdisplay();//display error
}
if(argv[1][0]!='-')//argument 2 should begin with '-'
{
argdisplay();//display error
}
if(argc==2)
{
pwd=getpwuid(getuid());//no user was specified so current user is used.
}
for(int i=1;i<=strlen(argv[1]);i++)//cycle through arguments
{
if(argv[1][i]=='v')//if found display the version information
{
cout<<"usrinfo-"<<version<<", http://usrinfo.sourceforge.net, (c)Aaron Myles Landwehr aaron@snaphat.com assroot@snaphat.com whitehata_zz@yahoo.com"<<linebreak;//usrinfo version
exit(0);//die
}
if(argv[1][i]=='w')//web style(NOTE: unix style is default)
{
linebreak="<br>";//change to web style breaks
}
if(argv[1][i]=='t')//if found display topics
{
pref=1;//topics will be displayed
}
if(argv[1][i]=='h')//if found display help message
{
helpdisplay();//help message
}
if(argv[1][i]=='i')//if found uid will be used instead of username
{
pwnam=0;//uid is now used
}
}
}
void argdisplay()//display the correct arguments
{
cout<<" "<<usage<<linebreak<<" "<<usage2<<linebreak<<" "<<usage3<<linebreak<<" "<<usage4<<linebreak<<" "<<usage5<<linebreak;//arguments were entered incorrect
exit(0);//die
}
void usererrordisplay()
{
cout<<"User Not Found"<<linebreak;//user was not in the database
exit(0);//die
}
void helpdisplay()//displays the help message
{
cout<<"USAGE"<<linebreak;
cout<<" "<<usage<<linebreak<<" "<<usage2<<linebreak<<" "<<usage3<<linebreak<<" "<<usage4<<linebreak<<" "<<usage5<<linebreak<<linebreak;//arguments were entered incorrect
cout<<"FLAGS"<<linebreak;
cout<<" -i uid "<<"By default the username is used to find information about the user; The '-i' option uses the user's uid instead."<<linebreak;
cout<<" -w "<<"By default unix-style breaks are used; The '-w' option specifies webstyle breaks instead."<<linebreak;
cout<<" -t "<<"By default topics are turned off; The '-t' options turns topics on."<<linebreak;
cout<<" -n "<<"Displays the username specified during program initiation."<<linebreak;
cout<<" -p "<<"Displays the user's encrypted password if rooted; Otherwise a '*' is shown."<<linebreak;
cout<<" -u "<<"Displays the user's unix identification number."<<linebreak;
cout<<" -g "<<"Displays the user's group identification number."<<linebreak;
cout<<" -d "<<"Displays the user's home directory."<<linebreak;
cout<<" -s "<<"Displays the user's shell."<<linebreak;
cout<<" -o "<<"Displays other information about the user such as phone-number, etc."<<linebreak;
cout<<" -v "<<"Displays the program version, website, and creator."<<linebreak;
cout<<" -h "<<"Displays this help message."<<linebreak;
cout<<linebreak<<"*PLEASE see the usrinfo manpage for more detail.*"<<linebreak;
exit(0);//die
}
void usrtest(int argc,char** argv)//test of the usr exist
{
if(pwnam==1 && argc==3)
{
if((pwd=getpwnam(argv[2]))==NULL)//check if usr in database
{
usererrordisplay();//user not found
}
}
}
void uidtest(int argc,char** argv)//test of the usr exist
{
if(pwnam==0 && argc==3)
{
if((pwd=getpwuid(atoi(argv[2])))==NULL)//check if usr in database
{
usererrordisplay();// user not found
}
}
}
void infodisplay(char** argv)//display all of the information requested from the database
{
for(int i=1;i<=strlen(argv[1]);i++)//cycle through arguments
{
if(argv[1][i]=='n')
{
if(pref==1)
{
cout<<"Username: ";
}
cout<<pwd->pw_name<<linebreak;//usrname
}
if(argv[1][i]=='p')
{
if(pref==1)
{
cout<<"Encrypted Password: ";
}
cout<<pwd->pw_passwd<<linebreak;//encrypted passwd
}
if(argv[1][i]=='u')
{
if(pref==1)
{
cout<<"UNIX Identification: ";
}
cout<<pwd->pw_uid<<linebreak;//unix id
}
if(argv[1][i]=='g')
{
if(pref==1)
{
cout<<"Group Identification: ";
}
cout<<pwd->pw_gid<<linebreak;//group id
}
if(argv[1][i]=='d')
{
if(pref==1)
{
cout<<"Home Directory: ";
}
cout<<pwd->pw_dir<<linebreak;//home dir
}
if(argv[1][i]=='s')
{
if(pref==1)
{
cout<<"Shell: ";
}
cout<<pwd->pw_shell<<linebreak;//shell
}
if(argv[1][i]=='o')
{
if(pref==1)
{
cout<<"Other Information: ";
}
cout<<pwd->pw_gecos<<linebreak;//user information
}
}
}
int main(int argc, char** argv)
{
argtest(argc,argv);
uidtest(argc,argv);//use uid to get database information instead of username(NOTE: username is used by default)
usrtest(argc,argv);//use username to get database information
infodisplay(argv);
return 0;//die
}
syntax highlighted by Code2HTML, v. 0.9.1