1#include <config.h>
2#include <stdlib.h>
3#include <sasl.h>
4#include <sfio.h>
5
6/*
7 * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
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
19 *    distribution.
20 *
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
27 *      5000 Forbes Avenue
28 *      Pittsburgh, PA  15213-3890
29 *      (412) 268-4387, fax: (412) 268-7395
30 *      tech-transfer@andrew.cmu.edu
31 *
32 * 4. Redistributions of any form whatsoever must retain the following
33 *    acknowledgment:
34 *    "This product includes software developed by Computing Services
35 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 *
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.
44 */
45
46
47/* sf discipline to add sasl
48 */
49
50typedef struct _sasldisc
51{
52    Sfdisc_t	disc;
53    sasl_conn_t *conn;
54} Sasldisc_t;
55
56ssize_t sasl_read(Sfio_t *f, Void_t *buf, size_t size, Sfdisc_t *disc)
57{
58    int len, result;
59    const char *outbuf;
60    int outlen;
61    Sasldisc_t *sd = (Sasldisc_t *) disc;
62
63    len = sfrd(f, buf, size, disc);
64
65    if (len <= 0)
66	return len;
67
68    result = sasl_decode(sd->conn, buf, len, &outbuf, &outlen);
69
70    if (result != SASL_OK) {
71	/* eventually, we'll want an exception here */
72	return -1;
73    }
74
75    if (outbuf != NULL) {
76	memcpy(buf, outbuf, outlen);
77    }
78
79    return outlen;
80}
81
82ssize_t sasl_write(Sfio_t *f, const Void_t *buf, size_t size, Sfdisc_t *disc)
83{
84    int result;
85    const char *outbuf;
86    int outlen;
87    Sasldisc_t *sd = (Sasldisc_t *) disc;
88
89    result = sasl_encode(sd->conn, buf, size, &outbuf, &outlen);
90
91    if (result != SASL_OK) {
92	return -1;
93    }
94
95    if (outbuf != NULL) {
96	sfwr(f, outbuf, outlen, disc);
97    }
98
99    return size;
100}
101
102int sfdcsasl(Sfio_t *f, sasl_conn_t *conn)
103{
104    Sasldisc_t *sasl;
105
106    if (conn == NULL) {
107	/* no need to do anything */
108	return 0;
109    }
110
111    if(!(sasl = (Sasldisc_t*)malloc(sizeof(Sasldisc_t))) )
112	return -1;
113
114    sasl->disc.readf = sasl_read;
115    sasl->disc.writef = sasl_write;
116    sasl->disc.seekf = NULL;
117    sasl->disc.exceptf = NULL;
118
119    sasl->conn = conn;
120
121    if (sfdisc(f, (Sfdisc_t *) sasl) != (Sfdisc_t *) sasl) {
122	free(sasl);
123	return -1;
124    }
125
126    return 0;
127}
128
129
130