mailing list of musl libc
 help / color / mirror / code / Atom feed
539cc2aa01e2f98780355b1c8ebf29b285c2bdb4 blob 2401 bytes (raw)

  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
 
#include <sys/socket.h>
#include <byteswap.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "nscd.h"

static const struct {
	short sun_family;
	char sun_path[21];
} addr = {
	AF_UNIX,
	"/var/run/nscd/socket"
};

static ssize_t full_sendmsg(int fd, struct msghdr *m, int flags)
{
	ssize_t n, count = 0;

	while (m->msg_iovlen) {
		n = sendmsg(fd, m, flags);
		if(n < 0) return n;
		count += n;

		while (n) {
			if (n >= m->msg_iov[0].iov_len) {
				n -= m->msg_iov[0].iov_len;
				m->msg_iov++;
				m->msg_iovlen--;
			} else {
				m->msg_iov[0].iov_len -= n;
				n = 0;
			}
		}
	}
	return count;
}

FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap)
{
	size_t i;
	int fd;
	int res = 0;
	FILE *f = 0;
	int32_t req_buf[REQ_LEN] = {
		NSCDVERSION,
		req,
		strlen(key)+1
	};
	struct msghdr msg = {
		.msg_iov = (struct iovec[]){
			{&req_buf, sizeof(req_buf)},
			{(char*)key, strlen(key)+1}
		},
		.msg_iovlen = 2
	};

	if (strlen(key) > INT32_MAX - 1) {
		return (FILE*)-1;
	}

	*swap = 0;
retry:

	fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
	if (fd < 0) return NULL;

	if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
		/* If there isn't a running nscd we return -1 to indicate that
		 * that is precisely what happened
		 */
		if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
			res = -1;
		}
		goto error;
	}

	if (full_sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
		goto error;

	f = fdopen(fd, "r");

	if (fread(buf, 1, len, f) < len) {
		/* If the VERSION entry mismatches nscd will disconnect. The
		 * most likely cause is that the endianness mismatched. So, we
		 * byteswap and try once more. (if we already swapped, just
		 * fail out)
		 */
		if (!feof(f)) goto error;
		if (!*swap) {
			fclose(f);
			for (i = 0; i < sizeof(req_buf)/sizeof(req_buf[0]); i++) {
				req_buf[i] = bswap_32(req_buf[i]);
			}
			*swap = 1;
			goto retry;
		} else {
			fclose(f);
			res = -1;
			goto error;
		}
	}

	if (*swap) {
		for (i = 0; i < len/sizeof(buf[0]); i++) {
			buf[i] = bswap_32(buf[i]);
		}
	}

	/* The first entry in every nscd response is the version number. This
	 * really shouldn't happen, and is evidence of some form of malformed
	 * response.
	 */
	if(buf[0] != NSCDVERSION) {
		res = -1;
		errno = EIO;
		goto error;
	}

	return f;
error:
	fclose(f);
	return (FILE*)res;
}
debug log:

solving 539cc2a ...
found 539cc2a in https://inbox.vuxu.org/musl/1424660290-25921-1-git-send-email-josiahw@gmail.com/ ||
	https://inbox.vuxu.org/musl/1424658940-16635-1-git-send-email-josiahw@gmail.com/

applying [1/2] https://inbox.vuxu.org/musl/1424660290-25921-1-git-send-email-josiahw@gmail.com/
diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c
new file mode 100644
index 0000000..539cc2a

Checking patch src/passwd/nscd_query.c...
Applied patch src/passwd/nscd_query.c cleanly.

skipping https://inbox.vuxu.org/musl/1424658940-16635-1-git-send-email-josiahw@gmail.com/ for 539cc2a
index at:
100644 539cc2aa01e2f98780355b1c8ebf29b285c2bdb4	src/passwd/nscd_query.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).