/* SASL server API implementation
 * Rob Siemborski
 * Tim Martin
 * $Id: checkpw.c,v 1.49 2002/03/07 19:14:04 ken3 Exp $
 */
/*
 * Copyright (c) 2001 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any other legal
 *    details, please contact
 *      Office of Technology Transfer
 *      Carnegie Mellon University
 *      5000 Forbes Avenue
 *      Pittsburgh, PA  15213-3890
 *      (412) 268-4387, fax: (412) 268-7395
 *      tech-transfer@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Taken from Cyrus-SASL library and adapted by Alexander S. Sabourenkov
 * Oct 2001 - Apr 2002. Slightly modified by Philip Hazel.
 *
 * screwdriver@lxnt.info
 *
 */


#include "../exim.h"
#include "pwcheck.h"

/* A dummy function that always fails if pwcheck support is not
wanted. */

#ifndef CYRUS_PWCHECK_SOCKET
int pwcheck_verify_password(const char *userid,
			    const char *passwd,
			    const char **reply)
{
userid = userid;  /* Keep picky compilers happy */
passwd = passwd;
*reply = "pwcheck support is not included in this Exim binary";
return PWCHECK_FAIL;
}


/* This is the real function */

#else
#include <sys/uio.h>


 /* taken from cyrus-sasl file checkpw.c */
 /*
  * Keep calling the writev() system call with 'fd', 'iov', and 'iovcnt'
  * until all the data is written out or an error occurs.
  */
 static int retry_writev(int fd, struct iovec *iov, int iovcnt)
 {
     int n;
     int i;
     int written = 0;
     static int iov_max =
 #ifdef MAXIOV
       MAXIOV
 #else
 #ifdef IOV_MAX
       IOV_MAX
 #else
       8192
 #endif
 #endif
       ;

     for (;;) {
       while (iovcnt && iov[0].iov_len == 0) {
	   iov++;
	   iovcnt--;
       }

       if (!iovcnt) return written;

       n = writev(fd, iov, iovcnt > iov_max ? iov_max : iovcnt);
       if (n == -1) {
	   if (errno == EINVAL && iov_max > 10) {
	       iov_max /= 2;
	       continue;
	   }
	   if (errno == EINTR) continue;
	   return -1;
       }

       written += n;

       for (i = 0; i < iovcnt; i++) {
	   if (iov[i].iov_len > n) {
	       iov[i].iov_base = (char *)iov[i].iov_base + n;
	       iov[i].iov_len -= n;
	       break;
	   }
	   n -= iov[i].iov_len;
	   iov[i].iov_len = 0;
       }

       if (i == iovcnt) return written;
     }
 }


 /* taken from cyrus-sasl file checkpw.c */
 /* pwcheck daemon-authenticated login */
 int pwcheck_verify_password(const char *userid,
				  const char *passwd,
				  const char **reply)
 {
     int s;
     struct sockaddr_un srvaddr;
     int r;
     struct iovec iov[10];
     static char response[1024];
     int start, n;
     /* char pwpath[1024];   is not used - PH */

     if (reply) { *reply = NULL; }

     s = socket(AF_UNIX, SOCK_STREAM, 0);
     if (s == -1) return errno;

     memset((char *)&srvaddr, 0, sizeof(srvaddr));
     srvaddr.sun_family = AF_UNIX;
     strncpy(srvaddr.sun_path, CYRUS_PWCHECK_SOCKET, sizeof(srvaddr.sun_path));
     r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
     if (r == -1) {
        DEBUG(D_auth)
            debug_printf("Cannot connect to pwcheck daemon (at '%s')\n",CYRUS_PWCHECK_SOCKET);
       if (reply) { *reply = "cannot connect to pwcheck daemon"; }
       return PWCHECK_FAIL;
     }

     iov[0].iov_base = (char *)userid;
     iov[0].iov_len = strlen(userid)+1;
     iov[1].iov_base = (char *)passwd;
     iov[1].iov_len = strlen(passwd)+1;

     retry_writev(s, iov, 2);

     start = 0;
     while (start < sizeof(response) - 1) {
       n = read(s, response+start, sizeof(response) - 1 - start);
       if (n < 1) break;
       start += n;
     }

     close(s);

     if (start > 1 && !strncmp(response, "OK", 2)) {
       return PWCHECK_OK;
     }

     response[start] = '\0';
     if (reply) { *reply = response; }
     return PWCHECK_BAD;
 }

#endif

/* End of auths/pwcheck.c */
