
/*
 *  PASSWD.C
 *
 *  $Header: Beta:src/uucp/src/getty/RCS/passwd.c,v 1.1 90/02/02 12:13:35 dillon Exp Locker: dillon $
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 *
 */

#include <exec/types.h>
#include <libraries/dosextens.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include "protos.h"

#include "log.h"

#define BTOCP(bp,type)  ((type)((long)bp << 2))

extern long NullFH;
extern char LoginBuf[];
extern char PasswdBuf[];
extern char *DeviceName;
extern long DeviceUnit;

static struct passwd *Pas;

CheckLoginAndPassword()
{
    Pas = getpwnam(LoginBuf);

    if (Pas == NULL)
	return(0);
    if (strcmp(Pas->pw_passwd, "*") == 0)
	return(1);
    if (strcmp(PasswdBuf, Pas->pw_passwd) == 0)
	return(1);
    return(0);
}

void
RunPasswdEntry()
{
    static char buf[256];
    static char redir[128];
    char *arg0 = Pas->pw_shell_arg0;
    struct Process *proc = (struct Process *)FindTask(NULL);
    APTR oldConsoleTask = proc->pr_ConsoleTask;
    BPTR oldDir;
    BPTR DirLock;

    strcpy(redir, " ");

    if (*arg0 == '*') {
	++arg0;
	sprintf(redir, " >UUSER:%s/%d/weT600 <UUSER:%s/%d/rcT600 ",
	    DeviceName,
	    DeviceUnit,
	    DeviceName,
	    DeviceUnit
	);
    } else {
	if (LogToStdout == 0)
	    strcpy(redir, " >null: <null: ");
    }
    if (LogToStdout == 0)
	proc->pr_ConsoleTask = (APTR)BTOCP(NullFH, struct FileHandle *)->fh_Port;

    sprintf(buf, "%s%s%s -Getty -DEVICE %s -UNIT %d",
	arg0,
	redir,
	Pas->pw_shell_argn,
	DeviceName,
	DeviceUnit
    );

    DirLock = (BPTR)Lock(Pas->pw_dir, SHARED_LOCK);
    if (DirLock)
	oldDir = (BPTR)CurrentDir(DirLock);

    ulog(1, "Execute %s\n", buf);

    Execute(buf, NullFH, NullFH);

    proc->pr_ConsoleTask = oldConsoleTask;

    if (DirLock)
	UnLock(CurrentDir(oldDir));

    ulog(1, "ran\n");
}

