/*

A user-respectful FINGERD for BSD. 

Rather than displaying the compromising utmp or wtmp info, it
displays only .plan files in the following manner:

(1) For each user specified on the command line, FINGERD looks for
the user in the master password file.  If the user exists, and a
file named .plan exists in their home directory, the contents of
this file is sent in response. If either the user or file does not
exist, no response is given; the users existence or non-existence
is not revealed.

(2) If the command line is empty, or no users' .plan files were
found, FINGERD provides the contents of /etc/.plan as response, if
it exists.  This allows a general "for more information contact..."
mechanism.

Tom Jennings
tomj@wps.com
8 July 93

(x) All rites revered. You may freely copy, use or distribute this file.

*/

#include <stdio.h>
#include <pwd.h>

#define PLANFILE "/.plan"
#define PLANLEN (strlen(PLANFILE))

main()
{
	int plan_found;				/* T == found at least one... */
	char *lp, *cp;
	char line[1024];			/* line read from stdin (args) */
	char *strtok();
	struct passwd *pwd;

	if (!fgets(line, sizeof(line), stdin))	/* read args (usernames) from stdin */
		exit(1);

	plan_found= 0;				/* decide later on /etc/.plan */

	for (lp= cp= line; *cp; ) {		/* for each arg/username... */
		if ((cp= strtok(lp, " \t\r\n")) == NULL)/* lookup username */
			break;			/* no more names */
		lp = NULL;			/* for (n > 0)th strtok() calls */

		pwd= getpwnam(cp);
		if (pwd == NULL) continue;	/* no such user */

		if (planfile(pwd-> pw_dir))	/* display plan file */ 
			++plan_found;		/* yup, we found one */
	}
	if (! plan_found) 			/* if no such user or users plan file */
		planfile("/etc");		/* try /etc/.plan */

	exit(0);
}


/* Display the .plan file in the specified directory; return 0 if failure. */

planfile(dir)
	char *dir;
{
        register int ch, lastc;
        register FILE *fp;
	char buff[1024];

	if (strlen(dir) + PLANLEN > sizeof(buff))	/* check string too long! */
		return(0);

	strcpy(buff,dir);				/* build pathname */
	strcat(buff,PLANFILE);
        if ((fp = fopen(buff, "r")) == NULL)		/* open text file */
                return(0);

        while ((ch = getc(fp)) != EOF)			/* output the file */
                putchar(lastc = ch);
        if (lastc != '\n')
                (void)putchar('\n');

        (void)fclose(fp);
	return(1);					/* success */
}
