Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: Peter.Mayne@cao.mts.dec.com (Peter Mayne)
Subject: Re: Change Password routine for C2
Date: 1996/01/22	[thread overview]
Message-ID: <4e0veb$sd7@funlwb2.stl.dec.com> (raw)
In-Reply-To: 4drtam$tsm@alpha.delta.edu

In article <4drtam$tsm@alpha.delta.edu>, keith@alpha.delta.edu! says...
>
>Is there a C2 security change password routine that can be called from a
>program?  We've implemented an automated signup procedure where the user
>provides some information, and is given an account.  I would really like
>this program to automatically set a password for the user too.

Here's a program loosely based on a POP password changer from Qualcomm, so it 
doesn't do exactly what you want, but you get the idea. The original changed 
passwords by calling /usr/bin/passwd through a pty, and was generic for 
several UNIX systems (who said UNIX was a standard?), and therefore was a bit 
complicated. One particular problem with the original was that depending on 
the setting in XIsso/XSysAdmin, the password prompts are different.

Since I only needed it for Digital UNIX, I ripped it to bits and used the API 
directly. For brevity's sake, I removed the original comments (and didn't put 
many in myself 8-), and I don't have the original source code handy to put 
them back in again. My apologies to anyone who recognises their code 
fragments.

Note that the APIs and data structures used here will apparently be retired 
some time after Digital UNIX v4, but I don't know what they'll be replaced by.

.... cut on dotted line ....

/* Steve Dorner's description of the simple protocol:
 *
 * The server's responses should be like an FTP server's responses; 
 * 1xx for in progress, 2xx for success, 3xx for more information
 * needed, 4xx for temporary failure, and 5xx for permanent failure.  
 * Putting it all together, here's a sample conversation:
 *
 *   S: 200 hello\r\n
 *   E: user yourloginname\r\n
 *   S: 300 please send your password now\r\n
 *   E: pass yourcurrentpassword\r\n
 *   S: 200 My, that was tasty\r\n
 *   E: newpass yournewpassword\r\n
 *   S: 200 Happy to oblige\r\n
 *   E: quit\r\n
 *   S: 200 Bye-bye\r\n
 *   S: <closes connection>
 *   E: <closes connection>
 */
 
#define VERSION "2.0"

#define BUFSIZE 512

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <stdarg.h>

#include <sys/types.h>
#include <sys/security.h>
#include <prot.h>

static void WriteToClient (char *fmt, ...);
static void ReadFromClient(char *line);
static int chkPass(struct pr_passwd *pr, char *p);
static void getSalt(char *salt);

main (int argc, char *argv[])
{
char line[BUFSIZE];
char user[BUFSIZE];
char oldpass[BUFSIZE];
char newpass[BUFSIZE];
struct pr_passwd *pr_pwd, pr_newpwd;
/*
int c;
*/
char salt[3];
     
	set_auth_parameters (argc, argv);

	*user = *oldpass = *newpass = 0;
     
	if (openlog ("poppassd", LOG_PID, LOG_LOCAL2) < 0)
	{
		WriteToClient ("500 Can't open syslog.");
		exit (1);
	}
     
	WriteToClient ("200 poppassd v%s Hello, who are you?", VERSION);
	ReadFromClient (line);
	sscanf (line, "user %s", user) ;
	if (strlen (user) == 0)
	{
		WriteToClient ("500 Username required.");
		exit(1);
	}

	WriteToClient ("200 your password please.");
	ReadFromClient (line);
	sscanf (line, "pass %s", oldpass) ;
	if (strlen (oldpass) == 0)
	{
		WriteToClient ("500 Password required.");
		exit(1);
	}
     
	if ((pr_pwd = getprpwnam (user)) == NULL)
	{
		WriteToClient ("500 Unknown user, %s.", user);
		exit(1);
	}

	if (!chkPass (pr_pwd, oldpass))
	{
		WriteToClient ("500 Old password is incorrect.");
		exit(1);
	}

	WriteToClient ("200 your new password please.");
	ReadFromClient (line);
	sscanf (line, "newpass %s", newpass);
     
	if (strlen (newpass) == 0)
	{
		WriteToClient ("500 New password required.");
		exit(1);
	}

	if (strcmp (oldpass, newpass) == 0)
	{
		WriteToClient ("500 New password must be different.");
		exit (1);
	}

	pr_newpwd = *pr_pwd;
	pr_newpwd.ufld.fd_schange = time (NULL);
	pr_newpwd.uflg.fg_schange = 1;
	pr_newpwd.ufld.fd_pwchanger = pr_newpwd.ufld.fd_uid;
	pr_newpwd.uflg.fg_pwchanger = 1;
	getSalt (salt);
	strcpy (pr_newpwd.ufld.fd_encrypt, bigcrypt (newpass, salt));
	if (putprpwnam (user, &pr_newpwd))
	{
		syslog (LOG_ERR, "password changed for %s", user);
		WriteToClient ("200 Password changed, thank you.");

		ReadFromClient (line);
		if (strncmp(line, "quit", 4) != 0)
		{
			WriteToClient("500 Quit required.");
			exit (1);
		}
	  
		WriteToClient("200 Bye.");
		exit (0);
	}
	else
	{
		syslog (LOG_ERR, "failed attempt by %s", user);
		WriteToClient ("500 Unable to change password");
		exit(1);
	}
}

static void WriteToClient (char *fmt, ...)
{
va_list args;
char *str;
	
	va_start (args, fmt);
	vfprintf (stdout, fmt, args);
	va_end (args);
	fputs ("\r\n", stdout);
	fflush (stdout);
}

static void ReadFromClient(char *line)
{
char *sp;
int i;

	strcpy (line, "");
	fgets (line, BUFSIZE, stdin);
	if ((sp = strchr(line, '\n')) != NULL) *sp = '\0'; 
	if ((sp = strchr(line, '\r')) != NULL) *sp = '\0'; 
	
	/* convert initial keyword on line to lower case. */
	
	for (sp = line; isupper(*sp); sp++)
		*sp = tolower(*sp);
}

static int chkPass(struct pr_passwd *pr, char *p)
{
    /*
	We don't accept connections from users with null passwords.
	Compare the supplied password with the password file entry.
	*/
    if ((pr->ufld.fd_encrypt == NULL) || (pr->ufld.fd_encrypt[0] == '\0') ||
		strcmp (bigcrypt (p, pr->ufld.fd_encrypt), 
pr->ufld.fd_encrypt))
    {
        return (0);
    }

    return(1);
}

static void getSalt(char *salt)
{
static char *saltset=
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890./";
struct timespec tp;

	getclock (TIMEOFDAY, &tp);
	srandom ((unsigned)(tp.tv_sec-tp.tv_nsec));

	salt[0] = saltset[random () % 64];
	salt[1] = saltset[random () % 64];
	salt[2] = '\0';
}

.... cut on dotted line....
-- 
PJDM
Peter Mayne
Digital Equipment Corporation (Australia)
Canberra, ACT
----
These are my opinions, and have nothing to do with Digital.
The truth is out there, but not necessarily in here.




  reply	other threads:[~1996-01-22  5:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-01-20  5:00 Change Password routine for C2 Keith DeLong
1996-01-22  5:00 ` Peter Mayne [this message]
1996-01-31  5:00 ` Martin Ibert
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox