diff options
Diffstat (limited to 'src/gsasl.c')
-rw-r--r-- | src/gsasl.c | 75 |
1 files changed, 57 insertions, 18 deletions
diff --git a/src/gsasl.c b/src/gsasl.c index b52bf3f..a967861 100644 --- a/src/gsasl.c +++ b/src/gsasl.c | |||
@@ -2,7 +2,7 @@ | |||
2 | gsasl.c | 2 | gsasl.c |
3 | 3 | ||
4 | This file is part of GNU Anubis. | 4 | This file is part of GNU Anubis. |
5 | Copyright (C) 2003-2014 The Anubis Team. | 5 | Copyright (C) 2003-2024 The Anubis Team. |
6 | 6 | ||
7 | GNU Anubis is free software; you can redistribute it and/or modify it | 7 | GNU Anubis is free software; you can redistribute it and/or modify it |
8 | under the terms of the GNU General Public License as published by the | 8 | under the terms of the GNU General Public License as published by the |
@@ -20,10 +20,6 @@ | |||
20 | 20 | ||
21 | #include "headers.h" | 21 | #include "headers.h" |
22 | #include "extern.h" | 22 | #include "extern.h" |
23 | |||
24 | #if defined(WITH_GSASL) | ||
25 | |||
26 | #include "lbuf.h" | ||
27 | 23 | ||
28 | 24 | ||
29 | /* Basic I/O Functions */ | 25 | /* Basic I/O Functions */ |
@@ -31,7 +27,7 @@ | |||
31 | struct anubis_gsasl_stream | 27 | struct anubis_gsasl_stream |
32 | { | 28 | { |
33 | Gsasl_session *sess_ctx; /* Context */ | 29 | Gsasl_session *sess_ctx; /* Context */ |
34 | struct _line_buffer *lb; | 30 | struct stringbuf sb; |
35 | NET_STREAM stream; | 31 | NET_STREAM stream; |
36 | }; | 32 | }; |
37 | 33 | ||
@@ -75,16 +71,55 @@ write_chunk (void *data, char *start, char *end) | |||
75 | return 0; | 71 | return 0; |
76 | } | 72 | } |
77 | 73 | ||
74 | int | ||
75 | stringbuf_writelines (struct stringbuf *s, const char *iptr, size_t isize, | ||
76 | int (*wr) (void *data, char *start, char *end), | ||
77 | void *data, | ||
78 | size_t *nbytes) | ||
79 | { | ||
80 | if (s->len > 2) | ||
81 | { | ||
82 | char *start, *end; | ||
83 | |||
84 | for (start = s->base, | ||
85 | end = memchr (start, '\n', s->base + s->len - start); | ||
86 | end && end < s->base + s->len; | ||
87 | start = end + 1, | ||
88 | end = memchr (start, '\n', s->base + s->len - start)) | ||
89 | if (end[-1] == '\r') | ||
90 | { | ||
91 | int rc = wr (data, start, end); | ||
92 | if (rc) | ||
93 | return rc; | ||
94 | } | ||
95 | |||
96 | if (start > s->base) | ||
97 | { | ||
98 | if (start < s->base + s->len) | ||
99 | { | ||
100 | int rest = s->base + s->len - start; | ||
101 | memmove (s->base, start, rest); | ||
102 | s->len = rest; | ||
103 | } | ||
104 | else | ||
105 | s->len = 0; | ||
106 | } | ||
107 | } | ||
108 | |||
109 | if (nbytes) | ||
110 | *nbytes = isize; | ||
111 | return 0; | ||
112 | } | ||
78 | 113 | ||
79 | static int | 114 | static int |
80 | _gsasl_write (void *sd, const char *data, size_t size, size_t * nbytes) | 115 | _gsasl_write (void *sd, const char *data, size_t size, size_t * nbytes) |
81 | { | 116 | { |
82 | struct anubis_gsasl_stream *s = sd; | 117 | struct anubis_gsasl_stream *s = sd; |
83 | int rc = _auth_lb_grow (s->lb, data, size); | 118 | int rc = stringbuf_add (&s->sb, data, size); |
84 | if (rc) | 119 | if (rc) |
85 | return rc; | 120 | return rc; |
86 | 121 | ||
87 | return _auth_lb_writelines (s->lb, data, size, write_chunk, s, nbytes); | 122 | return stringbuf_writelines (&s->sb, data, size, write_chunk, s, nbytes); |
88 | } | 123 | } |
89 | 124 | ||
90 | static int | 125 | static int |
@@ -108,13 +143,13 @@ _gsasl_read (void *sd, char *data, size_t size, size_t * nbytes) | |||
108 | return rc; | 143 | return rc; |
109 | } | 144 | } |
110 | 145 | ||
111 | rc = _auth_lb_grow (s->lb, buf, sz); | 146 | rc = stringbuf_add (&s->sb, buf, sz); |
112 | if (rc) | 147 | if (rc) |
113 | return rc; | 148 | return rc; |
114 | 149 | ||
115 | rc = gsasl_decode (s->sess_ctx, | 150 | rc = gsasl_decode (s->sess_ctx, |
116 | _auth_lb_data (s->lb), | 151 | stringbuf_value (&s->sb), |
117 | _auth_lb_level (s->lb), &bufp, &len); | 152 | stringbuf_len (&s->sb), &bufp, &len); |
118 | } | 153 | } |
119 | while (rc == GSASL_NEEDS_MORE); | 154 | while (rc == GSASL_NEEDS_MORE); |
120 | 155 | ||
@@ -124,13 +159,13 @@ _gsasl_read (void *sd, char *data, size_t size, size_t * nbytes) | |||
124 | if (len > size) | 159 | if (len > size) |
125 | { | 160 | { |
126 | memcpy (data, bufp, size); | 161 | memcpy (data, bufp, size); |
127 | _auth_lb_drop (s->lb); | 162 | stringbuf_reset (&s->sb); |
128 | _auth_lb_grow (s->lb, bufp + size, len - size); | 163 | stringbuf_add (&s->sb, bufp + size, len - size); |
129 | len = size; | 164 | len = size; |
130 | } | 165 | } |
131 | else | 166 | else |
132 | { | 167 | { |
133 | _auth_lb_drop (s->lb); | 168 | stringbuf_reset (&s->sb); |
134 | memcpy (data, bufp, len); | 169 | memcpy (data, bufp, len); |
135 | } | 170 | } |
136 | if (nbytes) | 171 | if (nbytes) |
@@ -155,18 +190,24 @@ _gsasl_destroy (void *sd) | |||
155 | struct anubis_gsasl_stream *s = sd; | 190 | struct anubis_gsasl_stream *s = sd; |
156 | if (s->sess_ctx) | 191 | if (s->sess_ctx) |
157 | gsasl_finish (s->sess_ctx); | 192 | gsasl_finish (s->sess_ctx); |
158 | _auth_lb_destroy (&s->lb); | 193 | stringbuf_free (&s->sb); |
159 | free (sd); | 194 | free (sd); |
160 | return 0; | 195 | return 0; |
161 | } | 196 | } |
162 | 197 | ||
198 | static void | ||
199 | gsasl_nomem (void) | ||
200 | { | ||
201 | anubis_error (EXIT_FAILURE, 0, "%s", _("Not enough memory")); | ||
202 | } | ||
203 | |||
163 | void | 204 | void |
164 | install_gsasl_stream (Gsasl_session *sess_ctx, NET_STREAM *stream) | 205 | install_gsasl_stream (Gsasl_session *sess_ctx, NET_STREAM *stream) |
165 | { | 206 | { |
166 | struct anubis_gsasl_stream *s = xmalloc (sizeof *s); | 207 | struct anubis_gsasl_stream *s = xmalloc (sizeof *s); |
167 | 208 | ||
168 | s->sess_ctx = sess_ctx; | 209 | s->sess_ctx = sess_ctx; |
169 | _auth_lb_create (&s->lb); | 210 | stringbuf_init (&s->sb, gsasl_nomem); |
170 | s->stream = *stream; | 211 | s->stream = *stream; |
171 | 212 | ||
172 | stream_create (stream); | 213 | stream_create (stream); |
@@ -174,5 +215,3 @@ install_gsasl_stream (Gsasl_session *sess_ctx, NET_STREAM *stream) | |||
174 | _gsasl_read, _gsasl_write, | 215 | _gsasl_read, _gsasl_write, |
175 | _gsasl_close, _gsasl_destroy, _gsasl_strerror); | 216 | _gsasl_close, _gsasl_destroy, _gsasl_strerror); |
176 | } | 217 | } |
177 | |||
178 | #endif | ||