This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
nss_prax.c
118 lines (102 loc) · 3.13 KB
/
nss_prax.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <nss.h>
#include <resolv.h>
#include <arpa/inet.h>
void
prax_fill_hostent(const char *name,
int af,
struct hostent *result)
{
result->h_name = malloc(sizeof(char) * strlen(name) + 1);
strcpy(result->h_name, name);
result->h_aliases = malloc(sizeof(char *));
*result->h_aliases = NULL;
result->h_addr_list = malloc(sizeof(char *) * 2);
switch (af) {
case AF_INET:
result->h_addrtype = AF_INET;
result->h_length = INADDRSZ;
in_addr_t addr = inet_addr("127.0.0.1");
*result->h_addr_list = malloc(sizeof(addr));
memcpy(*result->h_addr_list, &addr, sizeof(addr));
break;
case AF_INET6:
result->h_addrtype = AF_INET6;
result->h_length = IN6ADDRSZ;
struct in6_addr addr6 = {};
inet_pton(AF_INET6, "::1", &addr6);
*result->h_addr_list = malloc(sizeof(addr6));
memcpy(*result->h_addr_list, &addr6, sizeof(addr6));
break;
}
*(result->h_addr_list + 1) = NULL;
}
enum nss_status
_nss_prax_gethostbyname2_r(const char *name,
int af,
struct hostent *result,
char *buffer,
size_t buflen,
int *errnop,
int *h_errnop)
{
enum nss_status status = NSS_STATUS_NOTFOUND;
const char dot[1] = ".";
char *ext;
int len = 0;
//FILE *log = fopen("/tmp/nss_prax.log","a+");
//fprintf(log, "%s (%d)...", name, af);
char *env = getenv("PRAX_DOMAINS");
char *domains;
if (env == NULL) {
domains = strdup("dev");
} else {
domains = strdup(env);
}
char *domain = strtok(domains, ",");
while (domain != NULL) {
char *name_ext = strrchr(name, *dot);
if (name_ext != NULL) {
ext = malloc(sizeof(char) * (strlen(domain) + 2));
sprintf(ext, ".%s", domain);
if (strcasecmp(name_ext, ext) == 0) {
status = NSS_STATUS_SUCCESS;
prax_fill_hostent(name, af, result);
free(ext);
break;
}
free(ext);
}
domain = strtok(NULL, ",");
len++;
}
//fprintf(log, " done.\n");
//fclose(log);
free(domains);
return status;
}
enum nss_status
_nss_prax_gethostbyname_r(const char *name,
struct hostent *result,
char *buffer,
size_t buflen,
int *errnop,
int *h_errnop)
{
return _nss_prax_gethostbyname2_r(name, AF_INET, result, buffer, buflen, errnop, h_errnop);
}
enum nss_status
_nss_gns_gethostbyaddr_r(const void *addr,
int len,
int af,
struct hostent *result,
char *buffer,
size_t buflen,
int *errnop,
int *h_errnop)
{
return NSS_STATUS_UNAVAIL;
}