-
Notifications
You must be signed in to change notification settings - Fork 0
/
Netgroup.xs
136 lines (114 loc) · 2.84 KB
/
Netgroup.xs
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
/*
* $Id: Netgroup.xs,v 1.3 2010/08/12 14:10:24 bastian Exp $
*
* Copyright (C) 2009, 2010 Collax GmbH
* (Bastian Friedrich <[email protected]>)
*/
#include <unistd.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = Net::NIS::Netgroup PACKAGE = Net::NIS::Netgroup
SV *
getdomainname()
PREINIT:
int len = 512;
char buf[512];
PPCODE:
int ret = getdomainname(buf, len);
if (ret == -1) {
XPUSHs(&PL_sv_undef);
} else {
XPUSHs(sv_2mortal(newSVpv(buf, 0)));
}
IV *
setdomainname(name)
const char *name
PREINIT:
int len = strlen(name) + 1;
PPCODE:
int ret = setdomainname(name, len);
if (ret == -1) {
XPUSHs(&PL_sv_undef);
} else {
XPUSHs(sv_2mortal(newSViv(1)));
}
IV *
innetgr(sv_netgroup, sv_host, sv_user, sv_domain)
SV *sv_netgroup
SV *sv_host
SV *sv_user
SV *sv_domain
PREINIT:
char *netgroup;
char *host;
char *user;
char *domain;
int ret;
PPCODE:
ret = 0;
if (!SvOK(sv_netgroup)) { netgroup = NULL; } else { netgroup = SvPV_nolen(sv_netgroup); }
if (!SvOK(sv_host)) { host = NULL; } else { host = SvPV_nolen(sv_host); }
if (!SvOK(sv_user)) { user = NULL; } else { user = SvPV_nolen(sv_user); }
if (!SvOK(sv_domain)) { domain = NULL; } else { domain = SvPV_nolen(sv_domain); }
/*
printf("xs code: netgroup == %s\n", netgroup);
printf("xs code: host == '%s'\n", host);
printf("xs code: user == '%s'\n", user);
printf("xs code: domain == '%s'\n", domain);
*/
if (netgroup) {
ret = innetgr(netgroup, host, user, domain);
}
if (ret == 1) {
XPUSHs(sv_2mortal(newSViv(1)));
} else {
XPUSHs(&PL_sv_undef);
}
AV *
listnetgr(netgroup, stringify = 0)
char *netgroup
bool stringify
PREINIT:
char *host;
char *user;
char *domain;
HV *el;
char *buf;
int bufsize = 256;
int len;
PPCODE:
if (stringify) {
buf = malloc(bufsize);
if (!buf) {
Perl_croak(aTHX_ "Could not allocate memory");
}
}
setnetgrent(netgroup);
while (getnetgrent (&host, &user, &domain)) {
if (stringify) {
if (!host) host = "";
if (!user) user = "";
if (!domain) domain = "";
len = strlen("(,,)") + strlen(host) + strlen(user) + strlen(domain);
if ((len+1) > bufsize) {
bufsize = len + 1;
buf = realloc(buf, bufsize);
if (!buf) {
Perl_croak(aTHX_ "Could not allocate memory");
}
}
sprintf(buf, "(%s,%s,%s)", host, user, domain);
XPUSHs(sv_2mortal(newSVpv(buf, len)));
} else {
el = newHV();
hv_store(el, "host", strlen("host"), host ? newSVpv(strdup(host), 0) : newSV(0), 0);
hv_store(el, "user", strlen("user"), user ? newSVpv(strdup(user), 0) : newSV(0), 0);
hv_store(el, "domain", strlen("domain"), domain ? newSVpv(strdup(domain), 0) : newSV(0), 0);
XPUSHs(sv_2mortal(newRV_noinc((SV*)el)));
}
}
if (stringify) {
free(buf);
}
endnetgrent();