-
Notifications
You must be signed in to change notification settings - Fork 3
/
Context.pm6
402 lines (303 loc) · 9.48 KB
/
Context.pm6
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use NativeCall;
constant LIBMUNGE = ('munge', v2);
enum Munge::Opt <
MUNGE_OPT_CIPHER_TYPE
MUNGE_OPT_MAC_TYPE
MUNGE_OPT_ZIP_TYPE
MUNGE_OPT_REALM
MUNGE_OPT_TTL
MUNGE_OPT_ADDR4
MUNGE_OPT_ENCODE_TIME
MUNGE_OPT_DECODE_TIME
MUNGE_OPT_SOCKET
MUNGE_OPT_UID_RESTRICTION
MUNGE_OPT_GID_RESTRICTION
>;
enum Munge::Cipher <
MUNGE_CIPHER_NONE
MUNGE_CIPHER_DEFAULT
MUNGE_CIPHER_BLOWFISH
MUNGE_CIPHER_CAST5
MUNGE_CIPHER_AES128
MUNGE_CIPHER_AES256
>;
enum Munge::MAC <
MUNGE_MAC_NONE
MUNGE_MAC_DEFAULT
MUNGE_MAC_MD5
MUNGE_MAC_SHA1
MUNGE_MAC_RIPEMD160
MUNGE_MAC_SHA256
MUNGE_MAC_SHA512
>;
enum Munge::Zip <
MUNGE_ZIP_NONE
MUNGE_ZIP_DEFAULT
MUNGE_ZIP_BZLIB
MUNGE_ZIP_ZLIB
>;
constant \MUNGE_TTL_MAXIMUM := -1;
constant \MUNGE_TTL_DEFAULT := 0;
constant \MUNGE_UID_ANY := -1;
constant \MUNGE_GID_ANY := -1;
enum Munge::Error <
EMUNGE_SUCCESS
EMUNGE_SNAFU
EMUNGE_BAD_ARG
EMUNGE_BAD_LENGTH
EMUNGE_OVERFLOW
EMUNGE_NO_MEMORY
EMUNGE_SOCKET
EMUNGE_TIMEOUT
EMUNGE_BAD_CRED
EMUNGE_BAD_VERSION
EMUNGE_BAD_CIPHER
EMUNGE_BAD_MAC
EMUNGE_BAD_ZIP
EMUNGE_BAD_REALM
EMUNGE_CRED_INVALID
EMUNGE_CRED_EXPIRED
EMUNGE_CRED_REWOUND
EMUNGE_CRED_REPLAYED
EMUNGE_CRED_UNAUTHORIZED
>;
class X::Munge::Error is Exception
{
has Munge::Error $.code;
sub munge_strerror(int32 --> Str) is native(LIBMUNGE) {}
method message() { munge_strerror($!code) }
}
class X::Munge::Unknown is X::Munge::Error
{
has $.thing;
has $.value;
method message { "Unknown $!thing: $!value" }
}
class X::Munge::UnknownCipher is X::Munge::Unknown
{
method new($cipher) { nextwith(thing => 'cipher', value => $cipher) }
}
class X::Munge::UnknownMAC is X::Munge::Unknown
{
method new($mac) { nextwith(thing => 'MAC', value => $mac) }
}
class X::Munge::UnknownZip is X::Munge::Unknown
{
method new($zip) { nextwith(thing => 'Zip', value => $zip) }
}
sub munge-check($code) is export
{
die X::Munge::Error.new(code => Munge::Error($code))
unless $code == EMUNGE_SUCCESS;
}
class Munge::Context is repr('CPointer')
{
sub munge_ctx_create(--> Munge::Context) is native(LIBMUNGE) {}
sub munge_ctx_destroy(Munge::Context) is native(LIBMUNGE) {}
sub munge_ctx_get_int32(Munge::Context, int32, int32 is rw --> int32)
is native(LIBMUNGE) is symbol('munge_ctx_get') {}
sub munge_ctx_set_int32(Munge::Context, int32, int32 --> int32)
is native(LIBMUNGE) is symbol('munge_ctx_set') {}
sub munge_ctx_get_int64(Munge::Context, int32, int64 is rw --> int32)
is native(LIBMUNGE) is symbol('munge_ctx_get') {}
sub munge_ctx_set_int64(Munge::Context, int32, int64 --> int32)
is native(LIBMUNGE) is symbol('munge_ctx_set') {}
sub munge_ctx_get_str(Munge::Context, int32, Pointer is rw --> int32)
is native(LIBMUNGE) is symbol('munge_ctx_get') {}
sub munge_ctx_set_str(Munge::Context, int32, Str --> int32)
is native(LIBMUNGE) is symbol('munge_ctx_set') {}
sub inet_ntoa(int64 --> Str) is native is symbol('inet_ntoa') {}
method new(--> Munge::Context) { munge_ctx_create }
method clone(--> Munge::Context)
is native(LIBMUNGE) is symbol('munge_ctx_copy') {}
method error(--> Str) is native(LIBMUNGE) is symbol('munge_ctx_strerror') {}
multi method cipher(Munge::Cipher $cipher?)
{
my int32 $ciphertype;
with $cipher
{
munge-check(munge_ctx_set_int32(self, MUNGE_OPT_CIPHER_TYPE, $_))
}
munge-check(munge_ctx_get_int32(self, MUNGE_OPT_CIPHER_TYPE,
$ciphertype));
Munge::Cipher($ciphertype)
}
multi method cipher(Str $cipher)
{
samewith Munge::Cipher::{"MUNGE_CIPHER_$cipher.uc()"}
// die X::Munge::UnknownCipher.new($cipher)
}
multi method MAC(Munge::MAC $mac?)
{
my int32 $mactype;
with $mac
{
munge-check(munge_ctx_set_int32(self, MUNGE_OPT_MAC_TYPE, $_))
}
munge-check(munge_ctx_get_int32(self, MUNGE_OPT_MAC_TYPE, $mactype));
Munge::MAC($mactype)
}
multi method MAC(Str $mac)
{
samewith Munge::MAC::{"MUNGE_MAC_$mac.uc()"}
// die X::Munge::UnknownMAC.new($mac)
}
multi method zip(Munge::Zip $zip?)
{
my int32 $ziptype;
with $zip
{
munge-check(munge_ctx_set_int32(self, MUNGE_OPT_ZIP_TYPE, $_))
}
munge-check(munge_ctx_get_int32(self, MUNGE_OPT_ZIP_TYPE, $ziptype));
Munge::Zip($ziptype)
}
multi method zip(Str $zip)
{
samewith Munge::Zip::{"MUNGE_ZIP_$zip.uc()"}
// die X::Munge::UnknownZip.new($zip)
}
method ttl(Int $seconds?)
{
my int32 $ttl;
with $seconds
{
munge-check(munge_ctx_set_int32(self, MUNGE_OPT_TTL, $_))
}
munge-check(munge_ctx_get_int32(self, MUNGE_OPT_TTL, $ttl));
$ttl
}
method addr4
{
my int64 $addr4;
munge-check(munge_ctx_get_int64(self, MUNGE_OPT_ADDR4, $addr4));
inet_ntoa($addr4)
}
method encode-time
{
my int64 $time;
munge-check(munge_ctx_get_int64(self, MUNGE_OPT_ENCODE_TIME, $time));
DateTime.new($time)
}
method decode-time
{
my int64 $time;
munge-check(munge_ctx_get_int64(self, MUNGE_OPT_DECODE_TIME, $time));
DateTime.new($time)
}
method socket(Str $local-domain-socket?)
{
my Pointer $p .= new;
with $local-domain-socket
{
munge-check: munge_ctx_set_str(self, MUNGE_OPT_SOCKET,
$local-domain-socket)
}
munge-check: munge_ctx_get_str(self, MUNGE_OPT_SOCKET, $p);
nativecast(Str, $p)
}
method uid-restriction(Int $uid?)
{
my int32 $uid_t;
with $uid
{
munge-check(munge_ctx_set_int32(self, MUNGE_OPT_UID_RESTRICTION,
$uid))
}
munge-check(munge_ctx_get_int32(self, MUNGE_OPT_UID_RESTRICTION,
$uid_t));
$uid_t
}
method gid-restriction(Int $gid?)
{
my int32 $gid_t;
with $gid
{
munge-check(munge_ctx_set_int32(self, MUNGE_OPT_GID_RESTRICTION,
$gid))
}
munge-check(munge_ctx_get_int32(self, MUNGE_OPT_GID_RESTRICTION,
$gid_t));
$gid_t
}
submethod DESTROY { munge_ctx_destroy(self) }
}
=begin pod
=head1 NAME
Munge::Context -- Context for MUNGE
=head1 SYNOPSIS
use Munge::Context;
my $ctx = Munge::Context.new;
say $ctx.cipher;
$ctx.cipher(MUNGE_CIPHER_BLOWFISH);
# or
$ctx.cipher('BLOWFISH');
say $ctx.MAC;
$ctx.MAC(MUNGE_MAC_SHA1);
# or
$ctx.MAC('SHA1');
say $ctx.zip;
$ctx.zip(MUNGE_ZIP_BZLIB);
# or
$ctx.zip('BZLIB');
$ctx.ttl(300); # seconds
$ctx.ttl(MUNGE_TTL_MAXIMUM);
say $ctx.addr4; # dotted quad IP address
say $ctx.encode-time; # DateTime
say $ctx.decode-time;
say $ctx.socket; # /path/to/socket
$ctx.socket('/path/to/socket');
$ctx.uid-restriction(MUNGE_UID_ANY); # or specific uid
$ctx.gid-restriction(MUNGE_GID_ANY); # or specific gid
=head1 DESCRIPTION
A Context for C<Munge>;
=head1 METHODS
=head2 B<new>(--> B<Munge::Context>)
Create a new context.
=head2 B<clone>(--> B<Munge::Context>)
Copy a context.
=head2 B<error>(--> Str)
Returns a descriptive text string describing the MUNGE error number
according to the context.
=head2 B<cipher>(Munge::Cipher $cipher?)
=head2 B<cipher>(Str $cipher)
Optionally set the context cipher to: MUNGE_CIPHER_NONE,
MUNGE_CIPHER_DEFAULT, MUNGE_CIPHER_BLOWFISH, MUNGE_CIPHER_CAST5,
MUNGE_CIPHER_AES128, MUNGE_CIPHER_AES256
Ciphers can also be specified by string without MUNGE_CIPHER.
Returns a Munge::Cipher enumeration one of above.
=head2 B<MAC>(Munge::MAC $mac?)
=head2 B<MAC>(Str $mac)
Optionally set the context message authentication code (MAC) to:
MUNGE_MAC_DEFAULT MUNGE_MAC_MD5 MUNGE_MAC_SHA1 MUNGE_MAC_RIPEMD160
MUNGE_MAC_SHA256 MUNGE_MAC_SHA512
MAC can also be specified by string without MUNGE_MAC.
Returns a Munge::MAC enumeration one of above.
=head2 B<zip>(Munge::Zip $zip?)
=head2 B<zip>(Str $zip)
Optionally set the context compression type to: MUNGE_ZIP_NONE
MUNGE_ZIP_DEFAULT MUNGE_ZIP_BZLIB MUNGE_ZIP_ZLIB
Zip can also be specified by string without MUNGE_ZIP.
Returns a Munge::Zip enumeration one of above.
=head2 B<ttl>(Int $seconds?)
Optionally set Time-To-Live number of seconds after the encode-time
that the credential is considered valid. Can also specify
MUNGE_TTL_DEFAULT (0) or MUNGE_TTL_MAXIMUM (-1).
Returns the current value.
=head2 B<addr4>(--> Str)
Returns the dotted quad for the IPv4 address of the host where the
credential was encoded.
=head2 B<encode-time>(--> DateTime)
Returns the time at which the credential was encoded.
=head2 B<decode-time>(--> DateTime)
Returns the time at which the credential was decoded.
=head2 B<socket>(Str $local-domain-socket?)
Get and optionally set the local domain socket for connecting with
munged.
=head2 B<uid-restriction>(Int $uid?)
Get and optionally set the UID allowed to decode the credential.
Defaults to MUNGE_UID_ANY (-1).
=head2 B<gid-restriction>(Int $gid?)
Get and optionally set the GID allowed to decode the credential.
Defaults to MUNGE_GID_ANY (-1).
=end pod