1 /*
2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Ucred.xs contains XS wrappers for the process privilege maniplulation
6 * functions.
7 */
8
9
10 /* Solaris includes. */
11 #include <ucred.h>
12 #include <priv.h>
13
14 /* Perl includes. */
15 #include "EXTERN.h"
16 #include "perl.h"
17 #include "XSUB.h"
18
19 typedef int sysret;
20 typedef priv_set_t Sun__Solaris__Privilege__Privset;
21 typedef ucred_t Sun__Solaris__Ucred__Ucred;
22
23 static priv_set_t *
24 dupset(const priv_set_t *s)
25 {
26 priv_set_t *new = priv_allocset();
27 if (new == NULL)
28 return (NULL);
29
30 priv_copyset(s, new);
31 return (new);
32 }
33
34 /*
35 * The XS code exported to perl is below here. Note that the XS preprocessor
36 * has its own commenting syntax, so all comments from this point on are in
37 * that form.
38 */
39
40 MODULE = Sun::Solaris::Ucred PACKAGE = Sun::Solaris::Ucred
41 PROTOTYPES: ENABLE
42
43 Sun::Solaris::Ucred::Ucred *
44 ucred_get(pid);
45 pid_t pid;
46
47 uid_t
48 ucred_geteuid(uc)
49 Sun::Solaris::Ucred::Ucred *uc;
50
51 uid_t
52 ucred_getruid(uc)
53 Sun::Solaris::Ucred::Ucred *uc;
54
55 uid_t
56 ucred_getsuid(uc)
57 Sun::Solaris::Ucred::Ucred *uc;
58
59 gid_t
60 ucred_getegid(uc)
61 Sun::Solaris::Ucred::Ucred *uc;
62
63 gid_t
64 ucred_getrgid(uc)
65 Sun::Solaris::Ucred::Ucred *uc;
66
67 gid_t
68 ucred_getsgid(uc)
69 Sun::Solaris::Ucred::Ucred *uc;
70
71 pid_t
72 ucred_getpid(uc)
73 Sun::Solaris::Ucred::Ucred *uc;
74
75 zoneid_t
76 ucred_getzoneid(uc)
77 Sun::Solaris::Ucred::Ucred *uc;
78
79 projid_t
80 ucred_getprojid(uc)
81 Sun::Solaris::Ucred::Ucred *uc;
82
83 uint_t
84 ucred_getpflags(uc, flags)
85 Sun::Solaris::Ucred::Ucred *uc;
86 uint_t flags;
87
88 Sun::Solaris::Privilege::Privset *
89 ucred_getprivset(uc, which)
90 Sun::Solaris::Ucred::Ucred *uc;
91 const char *which;
92 PREINIT:
93 const priv_set_t *val;
94 CODE:
95 /*
96 * Since this function returns a pointer into the ucred_t, we need
97 * to copy it or perl may free one before the other; and the
98 * priv_set_t * returned by it doesn't react kindly to free().
99 */
100 val = ucred_getprivset(uc, which);
101 if (val == NULL || (RETVAL = dupset(val)) == NULL)
102 XSRETURN_UNDEF;
103 OUTPUT:
104 RETVAL
105 CLEANUP:
106 SvREADONLY_on(SvRV(ST(0)));
107
108 Sun::Solaris::Ucred::Ucred *
109 getpeerucred(fd)
110 int fd;
111 CODE:
112 RETVAL = NULL;
113 if (getpeerucred(fd, &RETVAL) != 0)
114 XSRETURN_UNDEF;
115 OUTPUT:
116 RETVAL
117 CLEANUP:
118 SvREADONLY_on(SvRV(ST(0)));
119
120 void
121 ucred_getgroups(uc)
122 Sun::Solaris::Ucred::Ucred *uc;
123 PREINIT:
124 const gid_t *gids;
125 int n;
126 PPCODE:
127 n = ucred_getgroups(uc, &gids);
128 if (n < 0)
129 XSRETURN_UNDEF;
130
131 PUTBACK;
132 if (GIMME_V == G_SCALAR) {
133 EXTEND(SP, 1);
134 PUSHs(sv_2mortal(newSViv(n)));
135 PUTBACK;
136 XSRETURN(1);
137 } else if (GIMME_V == G_ARRAY) {
138 int i;
139 EXTEND(SP, n);
140
141 for (i = 0; i < n; i++)
142 PUSHs(sv_2mortal(newSViv(gids[i])));
143 PUTBACK;
144 XSRETURN(n);
145 } else {
146 PUTBACK;
147 XSRETURN(0);
148 }
149
150
151
152
153 MODULE = Sun::Solaris::Ucred PACKAGE = Sun::Solaris::Ucred::UcredPtr PREFIX = Ucred_
154
155 void
156 Ucred_DESTROY(uc)
157 Sun::Solaris::Ucred::Ucred *uc;
158 CODE:
159 ucred_free(uc);
160