-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnss-tls.c
192 lines (165 loc) · 5.39 KB
/
nss-tls.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
/*
* This file is part of nss-tls.
*
* Copyright (C) 2018, 2019, 2020 Dima Krasner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <nss.h>
#include <pthread.h>
#include <stdint.h>
#include "nss-tls.h"
static void cleanup(void *arg)
{
close((int)(intptr_t)arg);
}
enum nss_status _nss_tls_gethostbyname2_r(const char *name,
int af,
struct hostent *ret,
char *buf,
size_t buflen,
int *errnop,
int *h_errnop)
{
struct sockaddr_un sun = {.sun_family = AF_UNIX};
struct timeval tv = {.tv_sec = NSS_TLS_TIMEOUT / 2, .tv_usec = 0};
struct nss_tls_data *data = (struct nss_tls_data *)buf;
const char *dir;
ssize_t out, total;
size_t len;
int s, i, state;
enum nss_status status = NSS_STATUS_TRYAGAIN;
uint8_t count;
if (buflen < sizeof(*data)) {
*errnop = ERANGE;
*h_errnop = NETDB_INTERNAL;
return NSS_STATUS_TRYAGAIN;
}
*errnop = ENOENT;
*h_errnop = NETDB_SUCCESS;
if (geteuid() == 0)
strcpy(sun.sun_path, NSS_TLS_SOCKET_PATH);
else {
dir = getenv("XDG_RUNTIME_DIR");
if (dir) {
len = strlen(dir);
if (len > sizeof(sun.sun_path) - sizeof("/"NSS_TLS_SOCKET_NAME))
return NSS_STATUS_TRYAGAIN;
memcpy(sun.sun_path, dir, len);
sun.sun_path[len] = '/';
++len;
strncpy(sun.sun_path + len,
NSS_TLS_SOCKET_NAME,
sizeof(sun.sun_path) - len);
sun.sun_path[sizeof(sun.sun_path) - 1] = '\0';
} else
strcpy(sun.sun_path, NSS_TLS_SOCKET_PATH);
}
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &state);
s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (s < 0) {
if (state != PTHREAD_CANCEL_DISABLE)
pthread_setcancelstate(state, NULL);
*errnop = EAGAIN;
return NSS_STATUS_TRYAGAIN;
}
pthread_cleanup_push(cleanup, (void *)(intptr_t)s);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if ((setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) ||
(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0))
goto pop;
if (connect(s, (const struct sockaddr *)&sun, sizeof(sun)) < 0) {
if (errno != ENOENT)
goto pop;
strcpy(sun.sun_path, NSS_TLS_SOCKET_PATH);
if (connect(s, (const struct sockaddr *)&sun, sizeof(sun)) < 0)
goto pop;
}
data->req.af = af;
strncpy(data->req.name, name, sizeof(data->req.name));
data->req.name[sizeof(data->req.name) - 1] = '\0';
for (total = 0; total < sizeof(data->req); total += out) {
out = send(s,
(unsigned char *)&data->req + total,
sizeof(data->req) - total,
MSG_NOSIGNAL);
if (out <= 0)
goto pop;
}
for (total = 0; total < sizeof(data->res); total += out) {
out = recv(s,
(unsigned char *)&data->res + total,
sizeof(data->res) - total,
0);
if (out < 0)
goto pop;
if (out == 0)
break;
}
if (total == 0) {
status = NSS_STATUS_NOTFOUND;
goto pop;
}
if (total != sizeof(data->res))
goto pop;
if (data->res.cname[0]) {
ret->h_name = data->res.cname;
data->aliases[0] = data->req.name;
data->aliases[1] = NULL;
} else {
ret->h_name = data->req.name;
data->aliases[0] = NULL;
}
ret->h_aliases = data->aliases;
ret->h_addrtype = af;
data->addrs[0] = NULL;
ret->h_addr_list = data->addrs;
count = data->res.count;
if (count == 0) {
*h_errnop = HOST_NOT_FOUND;
status = NSS_STATUS_NOTFOUND;
goto pop;
}
if (count > NSS_TLS_ADDRS_MAX)
count = NSS_TLS_ADDRS_MAX;
switch (af) {
case AF_INET:
ret->h_length = sizeof(struct in_addr);
break;
case AF_INET6:
ret->h_length = sizeof(struct in6_addr);
break;
default:
status = NSS_STATUS_NOTFOUND;
goto pop;
}
for (i = 0; i < count; ++i)
data->addrs[i] = (char *)&data->res.addrs[i];
data->addrs[i] = NULL;
*errnop = 0;
status = NSS_STATUS_SUCCESS;
pop:
pthread_cleanup_pop(1);
return status;
}