-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.c
2627 lines (2218 loc) · 71.7 KB
/
common.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* common.c - Functions that are common to server and clinet
* Rob Siemborski
* Tim Martin
* $Id: common.c,v 1.134 2011/09/22 14:40:30 mel Exp $
*/
/*
* Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any other legal
* details, please contact
* Office of Technology Transfer
* Carnegie Mellon University
* 5000 Forbes Avenue
* Pittsburgh, PA 15213-3890
* (412) 268-4387, fax: (412) 268-7395
* tech-transfer@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#ifdef HAVE_SYSLOG
#include <syslog.h>
#endif
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <sasl.h>
#include <saslutil.h>
#include <saslplug.h>
#include "saslint.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
static const char *implementation_string = "Cyrus SASL";
#define VSTR0(maj, min, step) #maj "." #min "." #step
#define VSTR(maj, min, step) VSTR0(maj, min, step)
#define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
SASL_VERSION_STEP)
static int _sasl_getpath(void *context __attribute__((unused)), const char **path);
static int _sasl_getpath_simple(void *context __attribute__((unused)), const char **path);
static int _sasl_getconfpath(void *context __attribute__((unused)), char ** path);
static int _sasl_getconfpath_simple(void *context __attribute__((unused)), const char **path);
#if !defined(WIN32)
static char * _sasl_get_default_unix_path(void *context __attribute__((unused)),
char * env_var_name, char * default_value);
#else
/* NB: Always returned allocated value */
static char * _sasl_get_default_win_path(void *context __attribute__((unused)),
char * reg_attr_name, char * default_value);
#endif
static const char build_ident[] = "$Build: libsasl " PACKAGE "-" VERSION " $";
/* It turns out to be convenient to have a shared sasl_utils_t */
const sasl_utils_t *sasl_global_utils = NULL;
/* Should be a null-terminated array that lists the available mechanisms */
static char **global_mech_list = NULL;
void *free_mutex = NULL;
int (*_sasl_client_cleanup_hook)(void) = NULL;
int (*_sasl_server_cleanup_hook)(void) = NULL;
int (*_sasl_client_idle_hook)(sasl_conn_t *conn) = NULL;
int (*_sasl_server_idle_hook)(sasl_conn_t *conn) = NULL;
sasl_allocation_utils_t _sasl_allocation_utils={
(sasl_malloc_t *) &malloc,
(sasl_calloc_t *) &calloc,
(sasl_realloc_t *) &realloc,
(sasl_free_t *) &free
};
int _sasl_allocation_locked = 0;
#define SASL_ENCODEV_EXTRA 4096
/* Default getpath/getconfpath callbacks. These can be edited by sasl_set_path(). */
static sasl_callback_t default_getpath_cb = {
SASL_CB_GETPATH, (sasl_callback_ft)&_sasl_getpath, NULL
};
static sasl_callback_t default_getconfpath_cb = {
SASL_CB_GETCONFPATH, (sasl_callback_ft)&_sasl_getconfpath, NULL
};
static char * default_plugin_path = NULL;
static char * default_conf_path = NULL;
static int _sasl_global_getopt(void *context,
const char *plugin_name,
const char *option,
const char ** result,
unsigned *len);
/* Intenal mutex functions do as little as possible (no thread protection) */
static void *sasl_mutex_alloc(void)
{
return (void *)0x1;
}
static int sasl_mutex_lock(void *mutex __attribute__((unused)))
{
return SASL_OK;
}
static int sasl_mutex_unlock(void *mutex __attribute__((unused)))
{
return SASL_OK;
}
static void sasl_mutex_free(void *mutex __attribute__((unused)))
{
return;
}
sasl_mutex_utils_t _sasl_mutex_utils={
&sasl_mutex_alloc,
&sasl_mutex_lock,
&sasl_mutex_unlock,
&sasl_mutex_free
};
void sasl_set_mutex(sasl_mutex_alloc_t *n,
sasl_mutex_lock_t *l,
sasl_mutex_unlock_t *u,
sasl_mutex_free_t *d)
{
/* Disallow mutex function changes once sasl_client_init
and/or sasl_server_init is called */
if (_sasl_server_cleanup_hook || _sasl_client_cleanup_hook) {
return;
}
_sasl_mutex_utils.alloc=n;
_sasl_mutex_utils.lock=l;
_sasl_mutex_utils.unlock=u;
_sasl_mutex_utils.free=d;
}
/* copy a string to malloced memory */
int _sasl_strdup(const char *in, char **out, size_t *outlen)
{
size_t len = strlen(in);
if (outlen) *outlen = len;
*out=sasl_ALLOC((unsigned) len + 1);
if (! *out) return SASL_NOMEM;
strcpy((char *) *out, in);
return SASL_OK;
}
/* adds a string to the buffer; reallocing if need be */
int _sasl_add_string(char **out, size_t *alloclen,
size_t *outlen, const char *add)
{
size_t addlen;
if (add==NULL) add = "(null)";
addlen=strlen(add); /* only compute once */
if (_buf_alloc(out, alloclen, (*outlen)+addlen)!=SASL_OK)
return SASL_NOMEM;
strncpy(*out + *outlen, add, addlen);
*outlen += addlen;
return SASL_OK;
}
/* a simpler way to set plugin path or configuration file path
* without the need to set sasl_getpath_t callback.
*
* This function can be called before sasl_server_init/sasl_client_init.
*
* Don't call this function without locking in a multithreaded application.
*/
int sasl_set_path (int path_type, char * path)
{
int result;
if (path == NULL) {
return (SASL_FAIL);
}
switch (path_type) {
case SASL_PATH_TYPE_PLUGIN:
if (default_plugin_path != NULL) {
sasl_FREE (default_plugin_path);
default_plugin_path = NULL;
}
result = _sasl_strdup (path, &default_plugin_path, NULL);
if (result != SASL_OK) {
return (result);
}
/* Update the default getpath_t callback */
default_getpath_cb.proc = (sasl_callback_ft)&_sasl_getpath_simple;
break;
case SASL_PATH_TYPE_CONFIG:
if (default_conf_path != NULL) {
sasl_FREE (default_conf_path);
default_conf_path = NULL;
}
result = _sasl_strdup (path, &default_conf_path, NULL);
if (result != SASL_OK) {
return (result);
}
/* Update the default getpath_t callback */
default_getconfpath_cb.proc = (sasl_callback_ft)&_sasl_getconfpath_simple;
break;
default:
return (SASL_FAIL);
}
return (SASL_OK);
}
/* return the version of the cyrus sasl library as compiled,
* using 32 bits: high byte is major version, second byte is minor version,
* low 16 bits are step #.
* Patch version is not available using this function,
* use sasl_version_info() instead.
*/
void sasl_version(const char **implementation, int *version)
{
if(implementation) *implementation = implementation_string;
/* NB: the format is not the same as in SASL_VERSION_FULL */
if(version) *version = (SASL_VERSION_MAJOR << 24) |
(SASL_VERSION_MINOR << 16) |
(SASL_VERSION_STEP);
}
/* Extended version of sasl_version above */
void sasl_version_info (const char **implementation, const char **version_string,
int *version_major, int *version_minor, int *version_step,
int *version_patch)
{
if (implementation) *implementation = implementation_string;
if (version_string) *version_string = SASL_VERSION_STRING;
if (version_major) *version_major = SASL_VERSION_MAJOR;
if (version_minor) *version_minor = SASL_VERSION_MINOR;
if (version_step) *version_step = SASL_VERSION_STEP;
/* Version patch is always 0 for CMU SASL */
if (version_patch) *version_patch = 0;
}
/* security-encode a regular string. Mostly a wrapper for sasl_encodev */
/* output is only valid until next call to sasl_encode or sasl_encodev */
int sasl_encode(sasl_conn_t *conn, const char *input,
unsigned inputlen,
const char **output, unsigned *outputlen)
{
int result;
struct iovec tmp;
if(!conn) return SASL_BADPARAM;
if(!input || !inputlen || !output || !outputlen)
PARAMERROR(conn);
/* maxoutbuf checking is done in sasl_encodev */
/* Note: We are casting a const pointer here, but it's okay
* because we believe people downstream of us are well-behaved, and the
* alternative is an absolute mess, performance-wise. */
tmp.iov_base = (void *)input;
tmp.iov_len = inputlen;
result = sasl_encodev(conn, &tmp, 1, output, outputlen);
RETURN(conn, result);
}
/* Internal function that doesn't do any verification */
static int
_sasl_encodev (sasl_conn_t *conn,
const struct iovec *invec,
unsigned numiov,
int * p_num_packets, /* number of packets generated so far */
const char **output, /* previous output, if *p_num_packets > 0 */
unsigned *outputlen)
{
int result;
char * new_buf;
assert (conn->oparams.encode != NULL);
if (*p_num_packets == 1) {
/* This is the second call to this function,
so we need to allocate a new output buffer
and copy existing data there. */
conn->multipacket_encoded_data.curlen = *outputlen;
if (conn->multipacket_encoded_data.data == NULL) {
conn->multipacket_encoded_data.reallen =
conn->multipacket_encoded_data.curlen + SASL_ENCODEV_EXTRA;
conn->multipacket_encoded_data.data =
sasl_ALLOC(conn->multipacket_encoded_data.reallen + 1);
if (conn->multipacket_encoded_data.data == NULL) {
MEMERROR(conn);
}
} else {
/* A buffer left from a previous sasl_encodev call.
Make sure it is big enough. */
if (conn->multipacket_encoded_data.curlen >
conn->multipacket_encoded_data.reallen) {
conn->multipacket_encoded_data.reallen =
conn->multipacket_encoded_data.curlen + SASL_ENCODEV_EXTRA;
new_buf = sasl_REALLOC(conn->multipacket_encoded_data.data,
conn->multipacket_encoded_data.reallen + 1);
if (new_buf == NULL) {
MEMERROR(conn);
}
conn->multipacket_encoded_data.data = new_buf;
}
}
memcpy (conn->multipacket_encoded_data.data,
*output,
*outputlen);
}
result = conn->oparams.encode(conn->context,
invec,
numiov,
output,
outputlen);
if (*p_num_packets > 0 && result == SASL_OK) {
/* Is the allocated buffer big enough? If not, grow it. */
if ((conn->multipacket_encoded_data.curlen + *outputlen) >
conn->multipacket_encoded_data.reallen) {
conn->multipacket_encoded_data.reallen =
conn->multipacket_encoded_data.curlen + *outputlen;
new_buf = sasl_REALLOC(conn->multipacket_encoded_data.data,
conn->multipacket_encoded_data.reallen + 1);
if (new_buf == NULL) {
MEMERROR(conn);
}
conn->multipacket_encoded_data.data = new_buf;
}
/* Append new data to the end of the buffer */
memcpy (conn->multipacket_encoded_data.data +
conn->multipacket_encoded_data.curlen,
*output,
*outputlen);
conn->multipacket_encoded_data.curlen += *outputlen;
*output = conn->multipacket_encoded_data.data;
*outputlen = (unsigned)conn->multipacket_encoded_data.curlen;
}
(*p_num_packets)++;
RETURN(conn, result);
}
/* security-encode an iovec */
/* output is only valid until the next call to sasl_encode or sasl_encodev */
int sasl_encodev(sasl_conn_t *conn,
const struct iovec *invec,
unsigned numiov,
const char **output,
unsigned *outputlen)
{
int result = SASL_OK;
unsigned i;
unsigned j;
size_t total_size = 0;
struct iovec *cur_invec = NULL;
struct iovec last_invec;
unsigned cur_numiov;
char * next_buf = NULL;
size_t remainder_len;
unsigned index_offset;
unsigned allocated = 0;
/* Number of generated SASL packets */
int num_packets = 0;
if (!conn) return SASL_BADPARAM;
if (! invec || ! output || ! outputlen || numiov < 1) {
PARAMERROR(conn);
}
if (!conn->props.maxbufsize) {
sasl_seterror(conn, 0,
"called sasl_encode[v] with application that does not support security layers");
return SASL_TOOWEAK;
}
/* If oparams.encode is NULL, this means there is no SASL security
layer in effect, so no SASL framing is needed. */
if (conn->oparams.encode == NULL) {
result = _iovec_to_buf(invec, numiov, &conn->encode_buf);
if (result != SASL_OK) INTERROR(conn, result);
*output = conn->encode_buf->data;
*outputlen = (unsigned) conn->encode_buf->curlen;
RETURN(conn, result);
}
/* This might be better to check on a per-plugin basis, but I think
* it's cleaner and more effective here. It also encourages plugins
* to be honest about what they accept */
last_invec.iov_base = NULL;
remainder_len = 0;
next_buf = NULL;
i = 0;
while (i < numiov) {
if ((total_size + invec[i].iov_len) > conn->oparams.maxoutbuf) {
/* CLAIM: total_size < conn->oparams.maxoutbuf */
/* Fit as many bytes in last_invec, so that we have conn->oparams.maxoutbuf
bytes in total. */
last_invec.iov_len = conn->oparams.maxoutbuf - total_size;
/* Point to the first byte of the current record. */
last_invec.iov_base = invec[i].iov_base;
/* Note that total_size < conn->oparams.maxoutbuf */
/* The total size of the iov is bigger then the other end can accept.
So we allocate a new iov that contains just enough. */
/* +1 --- for the tail record */
cur_numiov = i + 1;
/* +1 --- just in case we need the head record */
if ((cur_numiov + 1) > allocated) {
struct iovec *new_invec;
allocated = cur_numiov + 1;
new_invec = sasl_REALLOC (cur_invec, sizeof(struct iovec) * allocated);
if (new_invec == NULL) {
if (cur_invec != NULL) {
sasl_FREE(cur_invec);
}
MEMERROR(conn);
}
cur_invec = new_invec;
}
if (next_buf != NULL) {
cur_invec[0].iov_base = next_buf;
cur_invec[0].iov_len = (long)remainder_len;
cur_numiov++;
index_offset = 1;
} else {
index_offset = 0;
}
if (i > 0) {
/* Copy all previous chunks */
/* NOTE - The starting index in invec is always 0 */
for (j = 0; j < i; j++) {
cur_invec[j + index_offset] = invec[j];
}
}
/* Initialize the last record */
cur_invec[i + index_offset] = last_invec;
result = _sasl_encodev (conn,
cur_invec,
cur_numiov,
&num_packets,
output,
outputlen);
if (result != SASL_OK) {
goto cleanup;
}
/* Point to the first byte that wouldn't fit into
the conn->oparams.maxoutbuf buffer. */
/* Note, if next_buf points to the very end of the IOV record,
it will be reset to NULL below */
/* Note, that some platforms define iov_base as "void *",
thus the typecase below */
next_buf = (char *) last_invec.iov_base + last_invec.iov_len;
/* Note - remainder_len is how many bytes left to be encoded in
the current IOV slot. */
remainder_len = (total_size + invec[i].iov_len) - conn->oparams.maxoutbuf;
/* Skip all consumed IOV records */
invec += i + 1;
numiov = numiov - (i + 1);
i = 0;
while (remainder_len > conn->oparams.maxoutbuf) {
last_invec.iov_base = next_buf;
last_invec.iov_len = conn->oparams.maxoutbuf;
/* Note, if next_buf points to the very end of the IOV record,
it will be reset to NULL below */
/* Note, that some platforms define iov_base as "void *",
thus the typecase below */
next_buf = (char *) last_invec.iov_base + last_invec.iov_len;
remainder_len = remainder_len - conn->oparams.maxoutbuf;
result = _sasl_encodev (conn,
&last_invec,
1,
&num_packets,
output,
outputlen);
if (result != SASL_OK) {
goto cleanup;
}
}
total_size = remainder_len;
if (remainder_len == 0) {
/* Just clear next_buf */
next_buf = NULL;
}
} else {
total_size += invec[i].iov_len;
i++;
}
}
/* CLAIM - The remaining data is shorter then conn->oparams.maxoutbuf. */
/* Force encoding of any partial buffer. Might not be optimal on the wire. */
if (next_buf != NULL) {
last_invec.iov_base = next_buf;
last_invec.iov_len = (long)remainder_len;
result = _sasl_encodev (conn,
&last_invec,
1,
&num_packets,
output,
outputlen);
if (result != SASL_OK) {
goto cleanup;
}
}
if (numiov > 0) {
result = _sasl_encodev (conn,
invec,
numiov,
&num_packets,
output,
outputlen);
}
cleanup:
if (cur_invec != NULL) {
sasl_FREE(cur_invec);
}
RETURN(conn, result);
}
/* output is only valid until next call to sasl_decode */
int sasl_decode(sasl_conn_t *conn,
const char *input, unsigned inputlen,
const char **output, unsigned *outputlen)
{
int result;
if(!conn) return SASL_BADPARAM;
if(!input || !output || !outputlen)
PARAMERROR(conn);
if(!conn->props.maxbufsize) {
sasl_seterror(conn, 0,
"called sasl_decode with application that does not support security layers");
RETURN(conn, SASL_TOOWEAK);
}
if(conn->oparams.decode == NULL)
{
/* Since we know how long the output is maximally, we can
* just allocate it to begin with, and never need another
* allocation! */
/* However, if they pass us more than they actually can take,
* we cannot help them... */
if(inputlen > conn->props.maxbufsize) {
sasl_seterror(conn, 0,
"input too large for default sasl_decode");
RETURN(conn,SASL_BUFOVER);
}
if(!conn->decode_buf)
conn->decode_buf = sasl_ALLOC(conn->props.maxbufsize + 1);
if(!conn->decode_buf)
MEMERROR(conn);
memcpy(conn->decode_buf, input, inputlen);
conn->decode_buf[inputlen] = '\0';
*output = conn->decode_buf;
*outputlen = inputlen;
return SASL_OK;
} else {
result = conn->oparams.decode(conn->context, input, inputlen,
output, outputlen);
/* NULL an empty buffer (for misbehaved applications) */
if (*outputlen == 0) *output = NULL;
RETURN(conn, result);
}
INTERROR(conn, SASL_FAIL);
}
void
sasl_set_alloc(sasl_malloc_t *m,
sasl_calloc_t *c,
sasl_realloc_t *r,
sasl_free_t *f)
{
if (_sasl_allocation_locked++) return;
_sasl_allocation_utils.malloc=m;
_sasl_allocation_utils.calloc=c;
_sasl_allocation_utils.realloc=r;
_sasl_allocation_utils.free=f;
}
void sasl_common_done(void)
{
/* NOTE - the caller will need to reinitialize the values,
if it is going to call sasl_client_init/sasl_server_init again. */
if (default_plugin_path != NULL) {
sasl_FREE (default_plugin_path);
default_plugin_path = NULL;
}
if (default_conf_path != NULL) {
sasl_FREE (default_conf_path);
default_conf_path = NULL;
}
_sasl_canonuser_free();
_sasl_done_with_plugins();
sasl_MUTEX_FREE(free_mutex);
free_mutex = NULL;
_sasl_free_utils(&sasl_global_utils);
if (global_mech_list) {
sasl_FREE(global_mech_list);
global_mech_list = NULL;
}
}
/* This function is for backward compatibility */
void sasl_done(void)
{
if (_sasl_server_cleanup_hook && _sasl_server_cleanup_hook() == SASL_OK) {
_sasl_server_idle_hook = NULL;
_sasl_server_cleanup_hook = NULL;
}
if (_sasl_client_cleanup_hook && _sasl_client_cleanup_hook() == SASL_OK) {
_sasl_client_idle_hook = NULL;
_sasl_client_cleanup_hook = NULL;
}
if (_sasl_server_cleanup_hook || _sasl_client_cleanup_hook) {
return;
}
sasl_common_done();
}
/* fills in the base sasl_conn_t info */
int _sasl_conn_init(sasl_conn_t *conn,
const char *service,
unsigned int flags,
enum Sasl_conn_type type,
int (*idle_hook)(sasl_conn_t *conn),
const char *serverFQDN,
const char *iplocalport,
const char *ipremoteport,
const sasl_callback_t *callbacks,
const sasl_global_callbacks_t *global_callbacks) {
int result = SASL_OK;
conn->type = type;
result = _sasl_strdup(service, &conn->service, NULL);
if (result != SASL_OK)
MEMERROR(conn);
memset(&conn->oparams, 0, sizeof(sasl_out_params_t));
memset(&conn->external, 0, sizeof(_sasl_external_properties_t));
conn->flags = flags;
result = sasl_setprop(conn, SASL_IPLOCALPORT, iplocalport);
if(result != SASL_OK)
RETURN(conn, result);
result = sasl_setprop(conn, SASL_IPREMOTEPORT, ipremoteport);
if(result != SASL_OK)
RETURN(conn, result);
conn->encode_buf = NULL;
conn->context = NULL;
conn->secret = NULL;
conn->idle_hook = idle_hook;
conn->callbacks = callbacks;
conn->global_callbacks = global_callbacks;
memset(&conn->props, 0, sizeof(conn->props));
/* Start this buffer out as an empty string */
conn->error_code = SASL_OK;
conn->errdetail_buf = conn->error_buf = NULL;
conn->errdetail_buf_len = conn->error_buf_len = 150;
result = _buf_alloc(&conn->error_buf, &conn->error_buf_len, 150);
if(result != SASL_OK) MEMERROR(conn);
result = _buf_alloc(&conn->errdetail_buf, &conn->errdetail_buf_len, 150);
if(result != SASL_OK) MEMERROR(conn);
conn->error_buf[0] = '\0';
conn->errdetail_buf[0] = '\0';
conn->decode_buf = NULL;
if(serverFQDN) {
result = _sasl_strdup(serverFQDN, &conn->serverFQDN, NULL);
sasl_strlower (conn->serverFQDN);
} else if (conn->type == SASL_CONN_SERVER) {
/* We can fake it because we *are* the server */
char name[MAXHOSTNAMELEN];
memset(name, 0, sizeof(name));
if (get_fqhostname (name, MAXHOSTNAMELEN, 0) != 0) {
return (SASL_FAIL);
}
result = _sasl_strdup(name, &conn->serverFQDN, NULL);
} else {
conn->serverFQDN = NULL;
}
if(result != SASL_OK) MEMERROR( conn );
RETURN(conn, SASL_OK);
}
int _sasl_common_init(sasl_global_callbacks_t *global_callbacks)
{
int result;
/* The last specified global callback always wins */
if (sasl_global_utils != NULL) {
sasl_utils_t * global_utils = (sasl_utils_t *)sasl_global_utils;
global_utils->getopt = &_sasl_global_getopt;
global_utils->getopt_context = global_callbacks;
}
/* Do nothing if we are already initialized */
if (free_mutex) {
return SASL_OK;
}
/* Setup the global utilities */
if(!sasl_global_utils) {
sasl_global_utils = _sasl_alloc_utils(NULL, global_callbacks);
if(sasl_global_utils == NULL) return SASL_NOMEM;
}
/* Init the canon_user plugin */
result = sasl_canonuser_add_plugin("INTERNAL", internal_canonuser_init);
if(result != SASL_OK) return result;
if (!free_mutex) {
free_mutex = sasl_MUTEX_ALLOC();
}
if (!free_mutex) return SASL_FAIL;
return SASL_OK;
}
/* dispose connection state, sets it to NULL
* checks for pointer to NULL
*/
void sasl_dispose(sasl_conn_t **pconn)
{
int result;
if (! pconn) return;
if (! *pconn) return;
/* serialize disposes. this is necessary because we can't
dispose of conn->mutex if someone else is locked on it */
result = sasl_MUTEX_LOCK(free_mutex);
if (result!=SASL_OK) return;
/* *pconn might have become NULL by now */
if (! (*pconn))
{
sasl_MUTEX_UNLOCK(free_mutex);
return;
}
(*pconn)->destroy_conn(*pconn);
sasl_FREE(*pconn);
*pconn=NULL;
sasl_MUTEX_UNLOCK(free_mutex);
}
void _sasl_conn_dispose(sasl_conn_t *conn) {
if (conn->serverFQDN)
sasl_FREE(conn->serverFQDN);
if (conn->external.auth_id)
sasl_FREE(conn->external.auth_id);
if(conn->encode_buf) {
if(conn->encode_buf->data) sasl_FREE(conn->encode_buf->data);
sasl_FREE(conn->encode_buf);
}
if(conn->error_buf)
sasl_FREE(conn->error_buf);
if(conn->errdetail_buf)
sasl_FREE(conn->errdetail_buf);
if(conn->decode_buf)
sasl_FREE(conn->decode_buf);
if(conn->mechlist_buf)
sasl_FREE(conn->mechlist_buf);
if(conn->service)
sasl_FREE(conn->service);
if (conn->multipacket_encoded_data.data) {
sasl_FREE(conn->multipacket_encoded_data.data);
}
/* oparams sub-members should be freed by the plugin, in so much
* as they were allocated by the plugin */
}
/* get property from SASL connection state
* propnum -- property number
* pvalue -- pointer to value
* returns:
* SASL_OK -- no error
* SASL_NOTDONE -- property not available yet
* SASL_BADPARAM -- bad property number or SASL context is NULL
*/
int sasl_getprop(sasl_conn_t *conn, int propnum, const void **pvalue)
{
int result = SASL_OK;
sasl_getopt_t *getopt;
void *context;
if (! conn) return SASL_BADPARAM;
if (! pvalue) PARAMERROR(conn);
switch(propnum)
{
case SASL_SSF:
*(sasl_ssf_t **)pvalue= &conn->oparams.mech_ssf;
break;
case SASL_MAXOUTBUF:
*(unsigned **)pvalue = &conn->oparams.maxoutbuf;
break;
case SASL_GETOPTCTX:
result = _sasl_getcallback(conn, SASL_CB_GETOPT, (sasl_callback_ft *)&getopt, &context);
if(result != SASL_OK) break;
*(void **)pvalue = context;
break;
case SASL_CALLBACK:
*(const sasl_callback_t **)pvalue = conn->callbacks;
break;
case SASL_IPLOCALPORT:
if(conn->got_ip_local)
*(const char **)pvalue = conn->iplocalport;
else {
*(const char **)pvalue = NULL;
result = SASL_NOTDONE;
}
break;
case SASL_IPREMOTEPORT:
if(conn->got_ip_remote)
*(const char **)pvalue = conn->ipremoteport;
else {
*(const char **)pvalue = NULL;
result = SASL_NOTDONE;
}
break;
case SASL_USERNAME:
if(! conn->oparams.user)
result = SASL_NOTDONE;
else
*((const char **)pvalue) = conn->oparams.user;
break;
case SASL_AUTHUSER:
if(! conn->oparams.authid)
result = SASL_NOTDONE;
else
*((const char **)pvalue) = conn->oparams.authid;
break;
case SASL_APPNAME:
/* Currently we only support server side contexts, but we should
be able to extend this to support client side contexts as well */
if(conn->type != SASL_CONN_SERVER) result = SASL_BADPROT;
else
*((const char **)pvalue) = ((sasl_server_conn_t *)conn)->sparams->appname;
break;
case SASL_SERVERFQDN:
*((const char **)pvalue) = conn->serverFQDN;
break;
case SASL_DEFUSERREALM:
if(conn->type != SASL_CONN_SERVER) result = SASL_BADPROT;
else
*((const char **)pvalue) = ((sasl_server_conn_t *)conn)->user_realm;
break;
case SASL_SERVICE:
*((const char **)pvalue) = conn->service;
break;
case SASL_AUTHSOURCE: /* name of plugin (not name of mech) */
if(conn->type == SASL_CONN_CLIENT) {
if(!((sasl_client_conn_t *)conn)->mech) {
result = SASL_NOTDONE;
break;
}
*((const char **)pvalue) =
((sasl_client_conn_t *)conn)->mech->m.plugname;
} else if (conn->type == SASL_CONN_SERVER) {
if(!((sasl_server_conn_t *)conn)->mech) {
result = SASL_NOTDONE;
break;
}
*((const char **)pvalue) =
((sasl_server_conn_t *)conn)->mech->m.plugname;
} else {
result = SASL_BADPARAM;
}
break;
case SASL_MECHNAME: /* name of mech */
if(conn->type == SASL_CONN_CLIENT) {
if(!((sasl_client_conn_t *)conn)->mech) {
result = SASL_NOTDONE;
break;
}
*((const char **)pvalue) =
((sasl_client_conn_t *)conn)->mech->m.plug->mech_name;