File: utils.c

package info (click to toggle)
cyrus-sasl2 2.1.27+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 19,700 kB
  • sloc: ansic: 58,924; sh: 4,843; java: 1,614; xml: 1,498; makefile: 1,265; python: 350; perl: 199
file content (321 lines) | stat: -rw-r--r-- 8,049 bytes parent folder | download | duplicates (4)
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
/******************************************************************************
 *
 * utils.c
 *
 * Description:  Central utility functions that may be frequently needed
 *               accross the application.
 *
 * Copyright (c) 1997-2000 Messaging Direct Ltd.
 * All rights reserved.
 *
 * Portions Copyright (c) 2003 Jeremy Rumpf
 * jrumpf@heavyload.net
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY MESSAGING DIRECT LTD. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL MESSAGING DIRECT LTD. OR
 * ITS EMPLOYEES OR AGENTS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * HISTORY
 *
 *
 * This source file created using 8 space tabs.
 *
 *****************************************************************************/

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#include "utils.h"
#include "globals.h"


/****************************************
 * flags     global from saslauthd-main.c
 *****************************************/

/*************************************************************
 * Variadic logging function to simplify printing of log
 * messages
 **************************************************************/
void logger(int priority, const char *function, const char *format, ...) {
        va_list 	arg_list;
        char    	buffer[1024];
	static int	have_syslog = 0;

        va_start(arg_list, format);
        vsnprintf(buffer, 1023, format, arg_list);
	va_end(arg_list);

        buffer[1023] = '\0';

        if (flags & LOG_USE_STDERR)
                fprintf(stderr, L_STDERR_FORMAT, getpid(), function, buffer);

        if (flags & LOG_USE_SYSLOG) {
		if (!have_syslog) {
			openlog("saslauthd", LOG_PID|LOG_NDELAY, LOG_AUTH);
			have_syslog = 1;
		}

                syslog(priority, "%-16s: %s", function, buffer);
	}
}


/**************************************************************
 * I/O wrapper to attempt to ensure a full record gets
 * transmitted. If the function returns anything less than
 * bytesrequested, it should be considered a failure.
 **************************************************************/
ssize_t tx_rec(int filefd, void *prebuff, size_t bytesrequested) {
	int		rc;
        ssize_t		bytesio = 0;
        size_t		bytesleft = 0;
        void		*buff;

        bytesleft = bytesrequested;
        buff = prebuff;

        while (bytesleft > 0) {
                bytesio = write(filefd, buff, bytesleft);
		rc = errno;

                if (bytesio < 0) {
			logger(L_ERR, L_FUNC, "write failure");
			logger(L_ERR, L_FUNC, "write: %s", strerror(rc));
                        return(bytesio);
		}

                if (bytesio == 0 && errno != EINTR)
                        return(bytesrequested - bytesleft);

                bytesleft -= bytesio;
                buff = (void *)((char *)buff + bytesio);

        }

        return(bytesrequested);
}


/**************************************************************
 * I/O wrapper to attempt to read in the specified amount of
 * data, without any guarantees. If the function returns 
 * anything less than bytesrequested, it should be considered
 * a failure.
 **************************************************************/
ssize_t rx_rec(int filefd, void *prebuff, size_t bytesrequested) {
	int		rc;
        ssize_t		bytesio = 0;
        size_t		bytesleft = 0;
        void		*buff;

        bytesleft = bytesrequested;
        buff = prebuff;

        while (bytesleft > 0) {
                bytesio = read(filefd, buff, bytesleft);
		rc = errno;

                if (bytesio < 0) {
			logger(L_ERR, L_FUNC, "read failure");
			logger(L_ERR, L_FUNC, "read: %s", strerror(rc));
                        return(bytesio);
		}

                if (bytesio == 0 && errno != EINTR)
                        return(bytesrequested - bytesleft);

                bytesleft -= bytesio;
                buff = (void *)((char *)buff + bytesio);

        }

        return(bytesrequested);
}


/**************************************************************
 * I/O wrapper to attempt to write out the specified vector.
 * data, without any guarantees. If the function returns 
 * -1, the vector wasn't completely written.
 **************************************************************/
int retry_writev(int fd, struct iovec *iov, int iovcnt) {
	int n;               /* return value from writev() */
	int i;               /* loop counter */
	int written;         /* bytes written so far */
	static int iov_max;  /* max number of iovec entries */

#ifdef MAXIOV
	iov_max = MAXIOV;
#else 
# ifdef IOV_MAX
	iov_max = IOV_MAX;
# else 
	iov_max = 8192;
# endif 
#endif

	written = 0;

	for (;;) {

		while (iovcnt && iov[0].iov_len == 0) {
			iov++;
			iovcnt--;
		}

		if (!iovcnt) {
			return written;
		}

		n = writev(fd, iov, iovcnt > iov_max ? iov_max : iovcnt);

		if (n == -1) {
			if (errno == EINVAL && iov_max > 10) {
				iov_max /= 2;
				continue;
			}

			if (errno == EINTR) {
				continue;
			}

			return -1;

		} else {
			written += n;
		}

		for (i = 0; i < iovcnt; i++) {
			if ((int) iov[i].iov_len > n) {
				iov[i].iov_base = (char *)iov[i].iov_base + n;
				iov[i].iov_len -= n;
				break;
			}

			n -= iov[i].iov_len;
			iov[i].iov_len = 0;
		}

		if (i == iovcnt) {
			return written;
		}
	}
}

#ifndef HAVE_ASPRINTF

# include <stdarg.h>

/*
 * asprintf -- work around lame systems that haven't added their own yet
 *
 * XXX relies on a valid working (SuSv3) vsnprintf(), OK on SunOS-5.10 BUT NOT BEFORE!
 */
int
asprintf(char **str,
	 const char *fmt,
	 ...)
{
	va_list ap;
	char *newstr;
	size_t len;
	int ret;

	*str = NULL;

	va_start(ap, fmt);
	ret = vsnprintf((char *) NULL, (size_t) 0, fmt, ap);
	va_end(ap);
	if (ret < 0) {
		return ret;
	}
	len = (size_t) ret + 1;		/* allow for nul */
	if ((newstr = malloc(len)) == NULL) {
		return (-1);
	}
	va_start(ap, fmt);
	ret = vsnprintf(newstr, len, fmt, ap);
	va_end(ap);
	if (ret >= 0 && (size_t) ret < len) { /* XXX (ret == len-1) */
		*str = newstr;
	} else {
		free(newstr);
		return ret;
	}

	return ret;
}
#endif /* HAVE_ASPRINTF */

#ifndef HAVE_STRLCPY
/* strlcpy -- copy string smartly.
 *
 * i believe/hope this is compatible with the BSD strlcpy(). 
 */
size_t saslauthd_strlcpy(char *dst, const char *src, size_t len)
{
	size_t n;

	if (len <= 0) {
		/* we can't do anything ! */
		return strlen(src);
	}

	/* assert(len >= 1); */
	for (n = 0; n < len-1; n++) {
		if ((dst[n] = src[n]) == '\0') break;
	}
	if (n >= len-1) {
		/* ran out of space */
		dst[n] = '\0';
		while(src[n]) n++;
	}
	return n;
}
#endif

#ifndef HAVE_STRLCAT
size_t saslauthd_strlcat(char *dst, const char *src, size_t len)
{
	size_t i, j, o;

	o = strlen(dst);
	if (len < o + 1)
		return o + strlen(src);
	len -= o + 1;
	for (i = 0, j = o; i < len; i++, j++) {
		if ((dst[j] = src[i]) == '\0') break;
	}
	dst[j] = '\0';
	if (src[i] == '\0') {
		return j;
	} else {
		return j + strlen(src + i);
	}
}
#endif