/*
 * Copyright (c) 1989 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef lint
static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
static char SccsId[] = "@(#)@(#)pop_pass.c	2.3  2.3 4/2/91";
#endif not lint

#include <stdio.h>
#include <sys/types.h>
#if defined(SYSV) || defined(AIX)
# include <string.h>
#else
# include <strings.h>
#endif

#include <pwd.h>
#include "popper.h"



#ifdef AUTHFILE
checkauthfile(user) 
     char *user;
{
    char buf[MAXUSERNAMELEN+1];
    FILE *fp;
    char cool = 0;

    if ((fp = fopen(AUTHFILE, "r")) != NULL) {
	while (fgets(buf, MAXUSERNAMELEN+1, fp)) {
	    buf[strlen(buf) -1] = '\0';
	    if (!strcmp(buf, user)) {
		fclose(fp);
		return(0);
	    }
	}

	fclose(fp);
    }
    return(-1);
}
#endif

#ifdef KERBEROS
int auth_user (p, pw)
POP     *   p;
struct passwd *pw;
{
    char lrealm[REALM_SZ];
    int status;
    struct passwd *pwp;
 
    if ((status = krb_get_lrealm(lrealm,1)) == KFAILURE) {
        pop_log(p, LOG_WARNING, "%s: (%s.%s@%s) %s", p->client, kdata.pname, 
		kdata.pinst, kdata.prealm, krb_err_txt[status]);
        return(pop_msg(p,POP_FAILURE,
            "Kerberos error:  \"%s\".", krb_err_txt[status]));
    }

    if (strcmp(kdata.prealm,lrealm))  {
         pop_log(p, LOG_WARNING, "%s: (%s.%s@%s) realm not accepted.", 
		 p->client, kdata.pname, kdata.pinst, kdata.prealm);
	 return(pop_msg(p,POP_FAILURE,
		     "Kerberos realm \"%s\" not accepted.", kdata.prealm));
    }

    if (strcmp(kdata.pinst,"")) {
        pop_log(p, LOG_WARNING, "%s: (%s.%s@%s) instance not accepted.", 
		 p->client, kdata.pname, kdata.pinst, kdata.prealm);
        return(pop_msg(p,POP_FAILURE,
	      "Must use null Kerberos(tm) instance -  \"%s.%s\" not accepted.",
	      kdata.pname, kdata.pinst));
    }

    /*  Look for the user in the password file */
    if ((pwp = getpwnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    *pw = *pwp;

    return(POP_SUCCESS);
}

#else /* Not Kerberos */
#ifdef AUTH
    char *crypt();

#ifdef SUNOS4

#include <sys/label.h>
#include <sys/audit.h>
#include <pwdadj.h>

static int
auth_user(p, pw)
POP     *   p;
struct passwd *pw;
{
    struct passwd *pwp;
    struct passwd_adjunct *pwadj;

    /*  Look for the user in the password file */
    if ((pwp = getpwnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    *pw = *pwp;

    /*  Look for the user in the shadow password file */
    if ((pwadj = getpwanam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "(shadow) Password supplied for \"%s\" is empty.",p->user));
    else
        pw->pw_passwd = pwadj->pwa_passwd;

    /*  We don't accept connections from users with null passwords */
    if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0')
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.", p->user));

    /*  Compare the supplied password with the password file entry */
    if (strcmp (crypt (p->pop_parm[1], pw->pw_passwd), pw->pw_passwd) != 0)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    return(POP_SUCCESS);
}

#endif	/* SUNOS4 */

#ifdef SOLARIS2

#include <shadow.h>

static int
auth_user(p, pw)
POP     *   p;
struct passwd *pw;
{
    struct passwd *pwp;
    register struct spwd * pwd;

    /*  Look for the user in the password file */
    if ((pwp = getpwnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    *pw = *pwp;

    /*  Look for the user in the shadow password file */
    if ((pwd = getspnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "(shadow) Password supplied for \"%s\" is empty.",p->user));

    /*  We don't accept connections from users with null passwords */
    if (pwd->sp_pwdp == NULL || *pwd->sp_pwdp == '\0')
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.", p->user));

    /*  Compare the supplied password with the password file entry */
    if (strcmp (crypt (p->pop_parm[1], pwd->sp_pwdp), pwd->sp_pwdp) != 0)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    return(POP_SUCCESS);
}

#endif	/* SOLARIS2 */

#ifdef POPSCO

# include <sys/security.h>
# include <sys/audit.h>
# include <prot.h>
#define   PASSWD(p)	(p->ufld.fd_encrypt)

static int
auth_user(p, pw)
POP     *   p;
struct passwd *pw;
{
    struct passwd *pwp;
    register struct pr_passwd *pr;

    /*  Look for the user in the password file */
    if ((pwp = getpwnam(p->user)) == NULL || (pr = getprpwnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    *pw = *pwp;

    /*  We don't accept connections from users with null passwords */
    if (PASSWD(pr) == NULL || PASSWD(pr) == '\0')
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    /*  Compare the supplied password with the password file entry */
    if (strcmp (crypt (p->pop_parm[1], PASSWD(pr)), PASSWD(pr)) != 0)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    return(POP_SUCCESS);
}

#endif	/* POPSCO */

#ifdef ULTRIX

#include <auth.h>

static int
auth_user(p, pw)
struct passwd  *   pw;
POP     *   p;
{
    struct passwd *pwp;
    AUTHORIZATION *auth, *getauthuid();

    /*  Look for the user in the password file */
    if ((pwp = getpwnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    *pw = *pwp;

    auth = getauthuid(pw->pw_uid);

    /*  We don't accept connections from users with null passwords */
    if (auth == NULL || auth->a_password == NULL || *auth->a_password == '\0')
        return (pop_msg(p,POP_FAILURE,
            "(shadow) Password for \"%s\" is blank.",p->user));

    /*  Compare the supplied password with the password file entry */
    if (strcmp(crypt16(p->pop_parm[1],auth->a_password),auth->a_password) != 0)
        return (pop_msg(p,POP_FAILURE,
            "Password entered for \"%s\" is incorrect.",p->user));

    return(POP_SUCCESS);
}

#endif	/* ULTRIX */

#else	/* NOT AUTH */

char *crypt();

static int
auth_user(p, pw)
POP     *   p;
struct passwd  *   pw;
{
    struct passwd *pwp;

    /*  Look for the user in the password file */
    if ((pwp = getpwnam(p->user)) == NULL)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    *pw = *pwp;

#ifdef AUTHFILE
    /* Is the user authorized to use POP? */
    if (checkauthfile(p->user) != 0)
	return(pop_msg(p, POP_FAILURE,
		    "Password supplied for \"%s\" is incorrect.", p->user));
#endif

    /*  We don't accept connections from users with null passwords */
    if (pw->pw_passwd == NULL || *pw->pw_passwd == '\0')
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    /*  Compare the supplied password with the password file entry */
    if (strcmp (crypt (p->pop_parm[1], pw->pw_passwd), pw->pw_passwd) != 0)
        return (pop_msg(p,POP_FAILURE,
            "Password supplied for \"%s\" is incorrect.",p->user));

    return(POP_SUCCESS);
}

#endif	/* AUTH */

#endif  /* Kerberos */

/* 
 *  pass:   Obtain the user password from a POP client
 */

int pop_pass (p)
POP     *   p;
{
    struct passwd pw;

#ifdef AUTHFILE
    /* Is the user authorized to use POP? */
    if (checkauthfile(p->user) != 0)
	return(pop_msg(p, POP_FAILURE,
		    "Password supplied for \"%s\" is incorrect.", p->user));
#endif

    if (auth_user(p, &pw) != POP_SUCCESS)
	return(POP_FAILURE);

    /*  Build the name of the user's maildrop */
    (void)sprintf(p->drop_name,"%s/%s",POP_MAILDIR,p->user);

    /*  Make a temporary copy of the user's maildrop */
    /*    and set the group and user id */
    /*    and get information about the maildrop */
    if (pop_dropcopy(p, &pw) != POP_SUCCESS) return (POP_FAILURE);

    /*  Initialize the last-message-accessed number */
    p->last_msg = 0;

    /*  Authorization completed successfully */
    return (pop_msg (p,POP_SUCCESS,
        "%s has %d message(s) (%d octets).",
            p->user,p->msg_count,p->drop_size));
}

