From: beaty@ftcollins.ncr.com (Steve Beaty)
Newsgroups: comp.sys.sun.admin,comp.sys.sun.apps,comp.sys.sun.wanted
Subject: Re: Sun Dialback Software
Date: 9 Oct 92 04:54:16 GMT
Reply-To: Steve.Beaty@ftcollins.ncr.com
Organization: NCR Microelectronics, Ft. Collins, CO. (303) 223-5100
X-Newsreader: Tin 1.1 PL3
X-Newsreader: Tin 1.1 PL3
X-Newsreader: Tin 1.1 PL3

randy@megatek.com (Randy Davis) writes:
: 
:   Three weeks ago, I posted a note asking pointers and recommendations for
: dialback software that runs on Suns and uses off-the-shelf modems.
: [...]
: 3) Steve Beaty actually provided source code to one he wrote himself (thanks,
: Steve).  Steve's email is Steve.Beaty@ftcollins.ncr.com should he want to
: distribute it.  I'll let him post it if he wants to.

	well, i've had a number of requests, so i'll go ahead and post
	it.  i hope it helps someone.  if you need help, i'll do what i
	can.

--
Steve Beaty					Steve.Beaty@ftcollins.ncr.com
Advanced Development		              beaty@longs.lance.colostate.edu
NCR Microelectronics                              (303) 226-9622 (palinphone)
"What You See Is What You Get, but it sure ain't what we need." Talking Heads
-----------------------------------------------------------------------------
/*
**  	Concise directions for creating a dialback system.
**  
**  dialback.c is the source for a dialback program that has been tested
**  under SunOS 4.1.1 using Hayes dialing commands.  it prompts the user
**  for a password, giving up to three tries.  if correct, it drops the
**  connection and dials a predetermined number back.  if a modem answers,
**  it spawns a real getty to allow a usual login process.  several steps
**  must be taken to install this.
**  
**  1) do a "make dialback" in the directory containing dialback.c.  move
**  the resulting executable to /etc.
**  
**  2) put these lines in /etc/gettytab (for a 9600 baud modem):
**  
**  secure.9600:\
**  	:lm=\r\nDialback Login\72 :\
**  	:sp#9600:lo=/etc/dialback:
**  
**  3) use this entry in /etc/ttytab for the port the modem is attached to:
**  
**  ttya	"/usr/etc/getty secure.9600"	unknown		on local secure
**  
**  4) create the /etc/dialback.passwd file that has a format similar to
**  /etc/passwd.  to wit, the user name and password do the same things.
**  it is suggested that the password is different from the user's usual
**  password.  the gcos field is the number the user will be called at.
**  the rest of the fields are ignored.  here is an example entry:
**  
**  	beaty:lkkada7/adkdf:0:0:555-1212::
**  
**  that's it.  all calls will be logged to the file /etc/dialback.log.
**  
**  i guess it goes without saying that this is unsupported software.
**  i'll try to respond to any questions you might have.  it probably
**  also goes without saying that this does not necessarily increase the
**  security of your system, there are ways to fool almost any system. one
**  of these days i need to get around to using other modems to dial back.
**  there should be more information in these directions, i'll work on it.
**  examine the source or write for more details.  i'd like to know if this
**  works for you, so email me at:
**  
**  	steve.beaty@ftcollins.ncr.com 
**  		or
**  	beaty@longs.lance.colostate.edu
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pwd.h>
#include <sys/termios.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

#define	LOGFILE		"/etc/dialback.log"
#define	DATABASE	"/etc/dialback.passwd"

FILE *logfile;

struct passwd *authorize (username)
char *username;
{
	struct passwd *passwd;

#ifdef CHECKPASSWD
	if (getpwnam (username) == NULL)
	{
		fprintf (logfile, "user %s not in system passwd file!!!\n", 
			username);
		fclose (logfile);
		exit (1);
	}
	endpwent ();
#endif

	fprintf (logfile, "looking up: %s\n", username);

	if (!setpwfile (DATABASE))
	{
		fprintf (logfile, "unable to open %s for reading\n", DATABASE);
		fclose (logfile);
		exit (1);
	}

	if ((passwd = getpwnam (username)) == NULL)
	{
		fprintf (logfile, "user %s not in dialback passwd file!!!\n", 
			username);
		fclose (logfile);
		exit (1);
	}

	if (!strcmp (passwd->pw_passwd, "*"))
	{
		fprintf (logfile, "warning, user %s has no password\n", 
			username);
	}
	else
	{
		extern char *getpass();
		int found = 0, i;

		/*
		** give 'em three tries
		*/
		for (i = 1; i <= 3; i++)
		{
			char *password = getpass ("Secure password: ");
			if (!strcmp (crypt (password, passwd->pw_passwd),
				passwd->pw_passwd))
			{
				found = 1;
				break;
			}
			else
			{
				fprintf (logfile, 
					"user %s incorrect password: %s\n",
					username, password);
			}
		}
		if (!found)
		{
			fclose (logfile);
			exit (1);
		}
	}
}

main (argc, argv)
int argc;
char **argv;
{
	struct termios termdata;
	struct passwd *passwd;
	char line[100];
	time_t now = time ((time_t *) NULL);
	int dtr = TIOCM_DTR;

	if ((logfile = fopen (LOGFILE, "a")) == NULL)
	{
		exit (1);
	}

	fprintf (logfile, "%s", ctime (&now));

	/*
	** parse the arguments: eat the "-p", get the user name, 
	** others must be junk
	*/
	if (argc != 3)
	{
		fprintf (logfile, "wrong number of arguments: %d!!!\n", argc);
		fclose (logfile);
		exit (1);
	}
	if (strcmp (argv[1], "-p") != 0)
	{
		fprintf (logfile, "argument %s unknown!!!\n", argv[1]);
		fclose (logfile);
		exit (1);
	}

	passwd = authorize (argv[2]);

#ifndef TESTING
 	assert (ioctl (0, TCGETS , &termdata) != -1); 
 	termdata.c_cflag |= CLOCAL;	/* don't die on hangup */
 	termdata.c_iflag &= ~ICRNL;	/* don't map CR -> NL on input */
	termdata.c_lflag &= ~ECHO;	/* don't echo back */
 	assert (ioctl (0, TCSETS , &termdata) != -1); 

	assert (ioctl (0, TIOCMBIC, &dtr) != -1);	/* drop dtr */

        sleep (30);

	assert (ioctl (0, TIOCMBIS, &dtr) != -1);  	/* raise dtr */
#endif

	printf ("ATV1\r\n");
	fgets (line, 99, stdin);
	fprintf (logfile, "--->%s", line);
	fgets (line, 99, stdin);
	fprintf (logfile, "--->%s", line);

	if (strncmp (line, "OK", 2) != 0)
	{
		fprintf (logfile, "can't get the modem's attention\n");
		fclose (logfile);
		exit (1);
	}

	fprintf (logfile, "Dialing %s\n", passwd->pw_gecos);
	printf ("ATDT%s\r\n", passwd->pw_gecos);

	fgets (line, 99, stdin);
	fprintf (logfile, "--->%s", line);
	fgets (line, 99, stdin);
	fprintf (logfile, "--->%s", line);

	if (strncmp (line, "CONNECT", 7) == 0)
	{
		fprintf (logfile, "spawning getty for %s\n", argv[2]);
		fclose (logfile);

		execl ("/usr/etc/getty", "/usr/etc/getty", 
			"dialup", "-", (char*) 0);
		exit (1);	/* just in case */
	}
	else if (strncmp (line, "NO DIALTONE", 11) == 0)
	{
		fprintf (logfile, "ERROR: phone line hosed\n");
	}
	else if (strncmp (line, "NO CARRIER", 10) == 0)
	{
		fprintf (logfile, "ERROR: no one answered\n");
	}
	else if (strncmp (line, "BUSY", 4) == 0)
	{
		fprintf (logfile, "ERROR: line busy\n");
	}
	else
	{
		fprintf (logfile, "ERROR: modem hosed\n");
	}
	fclose (logfile);
	return (1);
}
Newsgroups: comp.sys.sun.admin