1 /* SASL server API implementation
4 * $Id: checkpw.c,v 1.49 2002/03/07 19:14:04 ken3 Exp $
7 * Copyright (c) 2001 Carnegie Mellon University. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any other legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer@andrew.cmu.edu
32 * 4. Redistributions of any form whatsoever must retain the following
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 * Taken from Cyrus-SASL library and adapted by Alexander S. Sabourenkov
48 * Oct 2001 - Apr 2002: Slightly modified by Philip Hazel.
49 * Aug 2003: new code for saslauthd from Alexander S. Sabourenkov incorporated
50 * by Philip Hazel (minor mods to avoid compiler warnings)
51 * Oct 2006: (PH) removed redundant tests on "reply" being NULL - some were
52 * missing, and confused someone who was using this code for some
53 * other purpose. Here in Exim, "reply" is never NULL.
55 * screwdriver@lxnt.info
59 /* Originally this module supported only the pwcheck daemon, which is where its
60 name comes from. Nowadays it supports saslauthd as well; pwcheck is in fact
61 deprecated. The definitions of CYRUS_PWCHECK_SOCKET and CYRUS_SASLAUTHD_SOCKET
62 determine whether the facilities are actually supported or not. */
69 #if defined(CYRUS_PWCHECK_SOCKET) || defined(CYRUS_SASLAUTHD_SOCKET)
73 static int retry_read(int, void *, unsigned );
74 static int retry_writev(int, struct iovec *, int );
75 static int read_string(int, uschar **);
76 static int write_string(int, const uschar *, int);
81 /* A dummy function that always fails if pwcheck support is not
84 #ifndef CYRUS_PWCHECK_SOCKET
85 int pwcheck_verify_password(const char *userid,
89 *reply = "pwcheck support is not included in this Exim binary";
94 /* This is the real function */
98 /* taken from cyrus-sasl file checkpw.c */
99 /* pwcheck daemon-authenticated login */
100 int pwcheck_verify_password(const char *userid,
105 struct sockaddr_un srvaddr;
107 static char response[1024];
111 s = socket(AF_UNIX, SOCK_STREAM, 0);
112 if (s == -1) { return PWCHECK_FAIL; }
114 memset(CS &srvaddr, 0, sizeof(srvaddr));
115 srvaddr.sun_family = AF_UNIX;
116 strncpy(srvaddr.sun_path, CYRUS_PWCHECK_SOCKET, sizeof(srvaddr.sun_path));
117 r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
120 debug_printf("Cannot connect to pwcheck daemon (at '%s')\n",CYRUS_PWCHECK_SOCKET);
121 *reply = "cannot connect to pwcheck daemon";
125 iov[0].iov_base = CS userid;
126 iov[0].iov_len = strlen(userid)+1;
127 iov[1].iov_base = CS passwd;
128 iov[1].iov_len = strlen(passwd)+1;
130 retry_writev(s, iov, 2);
133 while (start < sizeof(response) - 1) {
134 n = read(s, response+start, sizeof(response) - 1 - start);
141 if (start > 1 && !strncmp(response, "OK", 2)) {
145 response[start] = '\0';
154 /* A dummy function that always fails if saslauthd support is not
157 #ifndef CYRUS_SASLAUTHD_SOCKET
158 int saslauthd_verify_password(const uschar *userid,
159 const uschar *passwd,
160 const uschar *service,
162 const uschar **reply)
164 *reply = US"saslauthd support is not included in this Exim binary";
169 /* This is the real function */
172 /* written from scratch */
173 /* saslauthd daemon-authenticated login */
175 int saslauthd_verify_password(const uschar *userid,
176 const uschar *password,
177 const uschar *service,
179 const uschar **reply)
181 uschar *daemon_reply = NULL;
183 struct sockaddr_un srvaddr;
186 debug_printf("saslauthd userid='%s' servicename='%s'"
187 " realm='%s'\n", userid, service, realm );
191 s = socket(AF_UNIX, SOCK_STREAM, 0);
193 *reply = CUstrerror(errno);
197 memset(CS &srvaddr, 0, sizeof(srvaddr));
198 srvaddr.sun_family = AF_UNIX;
199 strncpy(srvaddr.sun_path, CYRUS_SASLAUTHD_SOCKET,
200 sizeof(srvaddr.sun_path));
201 r = connect(s, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
204 debug_printf("Cannot connect to saslauthd daemon (at '%s'): %s\n",
205 CYRUS_SASLAUTHD_SOCKET, strerror(errno));
206 *reply = string_sprintf("cannot connect to saslauthd daemon at "
207 "%s: %s", CYRUS_SASLAUTHD_SOCKET,
212 if ( write_string(s, userid, Ustrlen(userid)) < 0) {
214 debug_printf("Failed to send userid to saslauthd daemon \n");
219 if ( write_string(s, password, Ustrlen(password)) < 0) {
221 debug_printf("Failed to send password to saslauthd daemon \n");
226 memset((void *)password, 0, Ustrlen(password));
228 if ( write_string(s, service, Ustrlen(service)) < 0) {
230 debug_printf("Failed to send service name to saslauthd daemon \n");
235 if ( write_string(s, realm, Ustrlen(realm)) < 0) {
237 debug_printf("Failed to send realm to saslauthd daemon \n");
242 if ( read_string(s, &daemon_reply ) < 2) {
244 debug_printf("Corrupted answer '%s' received. \n", daemon_reply);
252 debug_printf("Answer '%s' received. \n", daemon_reply);
254 *reply = daemon_reply;
256 if ( (daemon_reply[0] == 'O') && (daemon_reply[1] == 'K') )
259 if ( (daemon_reply[0] == 'N') && (daemon_reply[1] == 'O') )
268 /* helper functions */
269 #if defined(CYRUS_PWCHECK_SOCKET) || defined(CYRUS_SASLAUTHD_SOCKET)
271 #define MAX_REQ_LEN 1024
273 /* written from scratch */
275 /* FUNCTION: read_string */
278 * read a sasld-style counted string into
279 * store-allocated buffer, set pointer to the buffer,
280 * return number of bytes read or -1 on failure.
283 static int read_string(int fd, uschar **retval) {
284 unsigned short count;
287 rc = (retry_read(fd, &count, sizeof(count)) < (int) sizeof(count));
289 count = ntohs(count);
290 if (count > MAX_REQ_LEN) {
293 /* Assume the file is trusted, so no tainting */
294 *retval = store_get(count + 1, FALSE);
295 rc = (retry_read(fd, *retval, count) < (int) count);
296 (*retval)[count] = '\0';
304 /* FUNCTION: write_string */
307 * write a sasld-style counted string into given fd
308 * written bytes on success, -1 on failure.
311 static int write_string(int fd, const uschar *string, int len) {
312 unsigned short count;
318 iov[0].iov_base = (void *) &count;
319 iov[0].iov_len = sizeof(count);
320 iov[1].iov_base = (void *) string;
321 iov[1].iov_len = len;
323 rc = retry_writev(fd, iov, 2);
329 /* taken from cyrus-sasl file saslauthd/saslauthd-unix.c */
331 /* FUNCTION: retry_read */
334 * Keep calling the read() system call with 'fd', 'buf', and 'nbyte'
335 * until all the data is read in or an error occurs.
337 static int retry_read(int fd, void *inbuf, unsigned nbyte)
341 char *buf = CS inbuf;
343 if (nbyte == 0) return 0;
346 n = read(fd, buf, nbyte);
352 if (errno == EINTR) continue;
358 if (n >= (int) nbyte) return nread;
365 /* END FUNCTION: retry_read */
367 /* FUNCTION: retry_writev */
370 * Keep calling the writev() system call with 'fd', 'iov', and 'iovcnt'
371 * until all the data is written out or an error occurs.
374 static int /* R: bytes written, or -1 on error */
377 int fd, /* I: fd to write on */
378 struct iovec *iov, /* U: iovec array base
379 * modified as data written */
380 int iovcnt /* I: number of iovec entries */
385 int n; /* return value from writev() */
386 int i; /* loop counter */
387 int written; /* bytes written so far */
388 static int iov_max; /* max number of iovec entries */
397 # else /* ! IOV_MAX */
399 # endif /* ! IOV_MAX */
400 #endif /* ! MAXIOV */
405 while (iovcnt && iov[0].iov_len == 0) {
414 n = writev(fd, iov, iovcnt > iov_max ? iov_max : iovcnt);
416 if (errno == EINVAL && iov_max > 10) {
420 if (errno == EINTR) {
428 for (i = 0; i < iovcnt; i++) {
429 if (iov[i].iov_len > (unsigned) n) {
430 iov[i].iov_base = CS iov[i].iov_base + n;
445 /* END FUNCTION: retry_writev */
448 /* End of auths/pwcheck.c */