aboutsummaryrefslogtreecommitdiff
path: root/src/gsasl.c
blob: 90325d2c6f8ba0044d2f65f579202f438f19ebab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
   gsasl.c

   This file is part of GNU Anubis.
   Copyright (C) 2003,2004 The Anubis Team.

   GNU Anubis is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   GNU Anubis is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Anubis; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   GNU Anubis is released under the GPL with the additional exemption that
   compiling, linking, and/or using OpenSSL is allowed.
*/

#include "headers.h"
#include "extern.h"

#if defined(WITH_GSASL)

#include "lbuf.h"


/* Basic I/O Functions */

struct anubis_gsasl_stream
{
  Gsasl_session_ctx *sess_ctx;	/* Context */
  struct _line_buffer *lb;
  NET_STREAM stream;
};

static const char *
_gsasl_strerror (void *ignored_data, int rc)
{
  return gsasl_strerror (rc);
}

int
write_chunk (void *data, char *start, char *end)
{
  struct anubis_gsasl_stream *s = data;
  size_t chunk_size = end - start + 1;
  size_t len;
  size_t wrsize;
  char *buf = NULL;

  len = 0;
  gsasl_encode (s->sess_ctx, start, chunk_size, &buf, &len);

  wrsize = 0;
  do
    {
      size_t sz;
      int rc = stream_write (s->stream, buf + wrsize, len - wrsize,
			     &sz);
      if (rc)
	{
	  if (rc == EINTR)
	    continue;
	  free (buf);
	  return rc;
	}
      wrsize += sz;
    }
  while (wrsize < len);

  free (buf);

  return 0;
}


static int
_gsasl_write (void *sd, char *data, size_t size, size_t * nbytes)
{
  struct anubis_gsasl_stream *s = sd;
  int rc = _auth_lb_grow (s->lb, data, size);
  if (rc)
    return rc;

  return _auth_lb_writelines (s->lb, data, size, write_chunk, s, nbytes);
}

static int
_gsasl_read (void *sd, char *data, size_t size, size_t * nbytes)
{
  struct anubis_gsasl_stream *s = sd;
  int rc;
  char *bufp = NULL;
  size_t len = 0;

  do
    {
      char buf[80];
      size_t sz;

      rc = stream_read (s->stream, buf, sizeof (buf), &sz);
      if (rc)
	{
	  if (rc == EINTR)
	    continue;
	  return rc;
	}

      rc = _auth_lb_grow (s->lb, buf, sz);
      if (rc)
	return rc;

      rc = gsasl_decode (s->sess_ctx,
			 _auth_lb_data (s->lb),
			 _auth_lb_level (s->lb), &bufp, &len);
    }
  while (rc == GSASL_NEEDS_MORE);

  if (rc != GSASL_OK)
    return rc;

  if (len > size)
    {
      memcpy (data, bufp, size);
      _auth_lb_drop (s->lb);
      _auth_lb_grow (s->lb, bufp + size, len - size);
      len = size;
    }
  else
    {
      _auth_lb_drop (s->lb);
      memcpy (data, bufp, len);
    }
  if (nbytes)
    *nbytes = len;

  free (bufp);
  return 0;
}

static int
_gsasl_close (void *sd)
{
  struct anubis_gsasl_stream *s = sd;

  stream_close (s->stream);
  return 0;
}

static int
_gsasl_destroy (void *sd)
{
  struct anubis_gsasl_stream *s = sd;
  if (s->sess_ctx)
    gsasl_server_finish (s->sess_ctx);
  _auth_lb_destroy (&s->lb);
  free (sd);
  return 0;
}

void
install_gsasl_stream (Gsasl_session_ctx * sess_ctx, NET_STREAM * stream)
{
  struct anubis_gsasl_stream *s = xmalloc (sizeof *s);

  s->sess_ctx = sess_ctx;
  _auth_lb_create (&s->lb);
  s->stream = *stream;

  stream_create (stream);
  stream_set_io (*stream, s,
		 _gsasl_read, _gsasl_write,
		 _gsasl_close, _gsasl_destroy, _gsasl_strerror);
}

#endif

Return to:

Send suggestions and report system problems to the System administrator.