1 /*
2 * Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The name "Carnegie Mellon University" must not be used to
17 * endorse or promote products derived from this software without
18 * prior written permission. For permission or any other legal
19 * details, please contact
20 * Carnegie Mellon University
21 * Center for Technology Transfer and Enterprise Creation
22 * 4615 Forbes Avenue
23 * Suite 302
24 * Pittsburgh, PA 15213
25 * (412) 268-7393, fax: (412) 268-7395
26 * innovation@andrew.cmu.edu
27 *
28 * 4. Redistributions of any form whatsoever must retain the following
29 * acknowledgment:
30 * "This product includes software developed by Computing Services
31 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
32 *
33 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
34 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
35 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
36 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
37 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
38 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
39 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 */
41
42 #include <config.h>
43
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <stdarg.h>
47
48 #include <sasl.h>
49
50 /* send/recv library for IMAP4 style literals.
51
52 really not important; just one way of doing length coded strings */
53
send_string(FILE * f,const char * s,int l)54 int send_string(FILE *f, const char *s, int l)
55 {
56 int al;
57
58 al = fprintf(f, "{%d}\r\n", l);
59 fwrite(s, 1, l, f);
60 fflush(f);
61
62 printf("send: {%d}\n", l);
63 while (l--) {
64 if (isprint((unsigned char) *s)) {
65 printf("%c", *s);
66 } else {
67 printf("[%X]", (unsigned char) *s);
68 }
69 s++;
70 }
71 printf("\n");
72
73 return al;
74 }
75
recv_string(FILE * f,char * buf,int buflen)76 int recv_string(FILE *f, char *buf, int buflen)
77 {
78 int c;
79 int len, l;
80 char *s;
81
82 c = fgetc(f);
83 if (c != '{') return -1;
84
85 /* read length */
86 len = 0;
87 c = fgetc(f);
88 while (isdigit(c)) {
89 len = len * 10 + (c - '0');
90 c = fgetc(f);
91 }
92 if (c != '}') return -1;
93 c = fgetc(f);
94 if (c != '\r') return -1;
95 c = fgetc(f);
96 if (c != '\n') return -1;
97
98 /* read string */
99 if (buflen <= len) {
100 fread(buf, buflen - 1, 1, f);
101 buf[buflen - 1] = '\0';
102 /* discard oversized string */
103 len -= buflen - 1;
104 while (len--) (void)fgetc(f);
105
106 len = buflen - 1;
107 } else {
108 fread(buf, len, 1, f);
109 buf[len] = '\0';
110 }
111
112 l = len;
113 s = buf;
114 printf("recv: {%d}\n", len);
115 while (l--) {
116 if (isprint((unsigned char) *s)) {
117 printf("%c", *s);
118 } else {
119 printf("[%X]", (unsigned char) *s);
120 }
121 s++;
122 }
123 printf("\n");
124
125 return len;
126 }
127
128 int debuglevel = 0;
129
dprintf(int lvl,const char * fmt,...)130 int dprintf(int lvl, const char *fmt, ...)
131 {
132 va_list ap;
133 int ret = 0;
134
135 if (debuglevel >= lvl) {
136 va_start(ap, fmt);
137 ret = vfprintf(stdout, fmt, ap);
138 va_end(ap);
139 }
140
141 return ret;
142 }
143
saslerr(int why,const char * what)144 void saslerr(int why, const char *what)
145 {
146 fprintf(stderr, "%s: %s", what, sasl_errstring(why, NULL, NULL));
147 }
148
saslfail(int why,const char * what)149 void saslfail(int why, const char *what)
150 {
151 saslerr(why, what);
152 exit(EX_TEMPFAIL);
153 }
154
155