1 /* $Cambridge: exim/src/src/auths/pwcheck.c,v 1.2 2005/06/27 14:29:44 ph10 Exp $ */
3 /* SASL server API implementation
6 * $Id: checkpw.c,v 1.49 2002/03/07 19:14:04 ken3 Exp $
9 * Copyright (c) 2001 Carnegie Mellon University. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
23 * 3. The name "Carnegie Mellon University" must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission. For permission or any other legal
26 * details, please contact
27 * Office of Technology Transfer
28 * Carnegie Mellon University
30 * Pittsburgh, PA 15213-3890
31 * (412) 268-4387, fax: (412) 268-7395
32 * tech-transfer@andrew.cmu.edu
34 * 4. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by Computing Services
37 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
39 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
40 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
41 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
42 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
44 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
45 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 * Taken from Cyrus-SASL library and adapted by Alexander S. Sabourenkov
50 * Oct 2001 - Apr 2002: Slightly modified by Philip Hazel.
51 * Aug 2003: new code for saslauthd from Alexander S. Sabourenkov incorporated
52 * by Philip Hazel (minor mods to avoid compiler warnings)
54 * screwdriver@lxnt.info
58 /* Originally this module supported only the pwcheck daemon, which is where its
59 name comes from. Nowadays it supports saslauthd as well; pwcheck is in fact
60 deprecated. The definitions of CYRUS_PWCHECK_SOCKET and CYRUS_SASLAUTHD_SOCKET
61 determine whether the facilities are actually supported or not. */
68 #if defined(CYRUS_PWCHECK_SOCKET) || defined(CYRUS_SASLAUTHD_SOCKET)
72 static int retry_read(int, void *, unsigned );
73 static int retry_writev(int, struct iovec *, int );
74 static int read_string(int, uschar **);
75 static int write_string(int, const uschar *, int);
80 /* A dummy function that always fails if pwcheck support is not
83 #ifndef CYRUS_PWCHECK_SOCKET
84 int pwcheck_verify_password(const char *userid,
88 userid = userid; /* Keep picky compilers happy */
90 *reply = "pwcheck support is not included in this Exim binary";
95 /* This is the real function */
99 /* taken from cyrus-sasl file checkpw.c */
100 /* pwcheck daemon-authenticated login */
101 int pwcheck_verify_password(const char *userid,
106 struct sockaddr_un srvaddr;
108 static char response[1024];
110 if (reply) { *reply = NULL; }
112 s = socket(AF_UNIX, SOCK_STREAM, 0);
113 if (s == -1) { return PWCHECK_FAIL; }
115 memset((char *)&srvaddr, 0, sizeof(srvaddr));
116 srvaddr.sun_family = AF_UNIX;
117 strncpy(srvaddr.sun_path, CYRUS_PWCHECK_SOCKET, sizeof(srvaddr.sun_path));
118 r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
121 debug_printf("Cannot connect to pwcheck daemon (at '%s')\n",CYRUS_PWCHECK_SOCKET);
122 if (reply) { *reply = "cannot connect to pwcheck daemon"; }
126 iov[0].iov_base = (char *)userid;
127 iov[0].iov_len = strlen(userid)+1;
128 iov[1].iov_base = (char *)passwd;
129 iov[1].iov_len = strlen(passwd)+1;
131 retry_writev(s, iov, 2);
134 while (start < sizeof(response) - 1) {
135 n = read(s, response+start, sizeof(response) - 1 - start);
142 if (start > 1 && !strncmp(response, "OK", 2)) {
146 response[start] = '\0';
147 if (reply) { *reply = response; }
155 /* A dummy function that always fails if saslauthd support is not
158 #ifndef CYRUS_SASLAUTHD_SOCKET
159 int saslauthd_verify_password(const uschar *userid,
160 const uschar *passwd,
161 const uschar *service,
163 const uschar **reply)
165 userid = userid; /* Keep picky compilers happy */
169 *reply = US"saslauthd support is not included in this Exim binary";
174 /* This is the real function */
177 /* written from scratch */
178 /* saslauthd daemon-authenticated login */
180 int saslauthd_verify_password(const uschar *userid,
181 const uschar *password,
182 const uschar *service,
184 const uschar **reply)
186 uschar *daemon_reply;
188 struct sockaddr_un srvaddr;
191 debug_printf("saslauthd userid='%s' servicename='%s'"
192 " realm='%s'\n", userid, service, realm );
197 s = socket(AF_UNIX, SOCK_STREAM, 0);
200 *reply = CUstrerror(errno);
204 memset((char *)&srvaddr, 0, sizeof(srvaddr));
205 srvaddr.sun_family = AF_UNIX;
206 strncpy(srvaddr.sun_path, CYRUS_SASLAUTHD_SOCKET,
207 sizeof(srvaddr.sun_path));
208 r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
211 debug_printf("Cannot connect to saslauthd daemon (at '%s'): %s\n",
212 CYRUS_SASLAUTHD_SOCKET, strerror(errno));
214 *reply = string_sprintf("cannot connect to saslauthd daemon at "
215 "%s: %s", CYRUS_SASLAUTHD_SOCKET,
220 if ( write_string(s, userid, Ustrlen(userid)) < 0) {
222 debug_printf("Failed to send userid to saslauthd daemon \n");
227 if ( write_string(s, password, Ustrlen(password)) < 0) {
229 debug_printf("Failed to send password to saslauthd daemon \n");
234 memset((void *)password, 0, Ustrlen(password));
236 if ( write_string(s, service, Ustrlen(service)) < 0) {
238 debug_printf("Failed to send service name to saslauthd daemon \n");
243 if ( write_string(s, realm, Ustrlen(realm)) < 0) {
245 debug_printf("Failed to send realm to saslauthd daemon \n");
250 if ( read_string(s, &daemon_reply ) < 2) {
252 debug_printf("Corrupted answer '%s' received. \n", daemon_reply);
260 debug_printf("Answer '%s' received. \n", daemon_reply);
262 *reply = daemon_reply;
264 if ( (daemon_reply[0] == 'O') && (daemon_reply[1] == 'K') )
267 if ( (daemon_reply[0] == 'N') && (daemon_reply[1] == 'O') )
276 /* helper functions */
277 #if defined(CYRUS_PWCHECK_SOCKET) || defined(CYRUS_SASLAUTHD_SOCKET)
279 #define MAX_REQ_LEN 1024
281 /* written from scratch */
283 /* FUNCTION: read_string */
286 * read a sasld-style counted string into
287 * store-allocated buffer, set pointer to the buffer,
288 * return number of bytes read or -1 on failure.
291 static int read_string(int fd, uschar **retval) {
292 unsigned short count;
295 rc = (retry_read(fd, &count, sizeof(count)) < (int) sizeof(count));
297 count = ntohs(count);
298 if (count > MAX_REQ_LEN) {
301 *retval = store_get(count + 1);
302 rc = (retry_read(fd, *retval, count) < (int) count);
303 (*retval)[count] = '\0';
311 /* FUNCTION: write_string */
314 * write a sasld-style counted string into given fd
315 * written bytes on success, -1 on failure.
318 static int write_string(int fd, const uschar *string, int len) {
319 unsigned short count;
325 iov[0].iov_base = (void *) &count;
326 iov[0].iov_len = sizeof(count);
327 iov[1].iov_base = (void *) string;
328 iov[1].iov_len = len;
330 rc = retry_writev(fd, iov, 2);
336 /* taken from cyrus-sasl file saslauthd/saslauthd-unix.c */
338 /* FUNCTION: retry_read */
341 * Keep calling the read() system call with 'fd', 'buf', and 'nbyte'
342 * until all the data is read in or an error occurs.
344 static int retry_read(int fd, void *inbuf, unsigned nbyte)
348 char *buf = (char *)inbuf;
350 if (nbyte == 0) return 0;
353 n = read(fd, buf, nbyte);
359 if (errno == EINTR) continue;
365 if (n >= (int) nbyte) return nread;
372 /* END FUNCTION: retry_read */
374 /* FUNCTION: retry_writev */
377 * Keep calling the writev() system call with 'fd', 'iov', and 'iovcnt'
378 * until all the data is written out or an error occurs.
381 static int /* R: bytes written, or -1 on error */
384 int fd, /* I: fd to write on */
385 struct iovec *iov, /* U: iovec array base
386 * modified as data written */
387 int iovcnt /* I: number of iovec entries */
392 int n; /* return value from writev() */
393 int i; /* loop counter */
394 int written; /* bytes written so far */
395 static int iov_max; /* max number of iovec entries */
404 # else /* ! IOV_MAX */
406 # endif /* ! IOV_MAX */
407 #endif /* ! MAXIOV */
412 while (iovcnt && iov[0].iov_len == 0) {
421 n = writev(fd, iov, iovcnt > iov_max ? iov_max : iovcnt);
423 if (errno == EINVAL && iov_max > 10) {
427 if (errno == EINTR) {
435 for (i = 0; i < iovcnt; i++) {
436 if (iov[i].iov_len > (unsigned) n) {
437 iov[i].iov_base = (char *)iov[i].iov_base + n;
452 /* END FUNCTION: retry_writev */
455 /* End of auths/pwcheck.c */