-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathse.go
557 lines (468 loc) · 14 KB
/
se.go
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package radir
/*
se.go implements a generic subentry type with basic methods.
*/
/*
Subentry contains contents derived from, or for use in creating, LDAP
subentries within the context of the I-D series. It is meant to be used
as a convenient abstraction of a "[subentry]", per RFC 3672.
In cases where Collective Attributes, or other virtualization services,
are not available, and where manual population of values meant for broad
assignment to many entries is simply not practical, this type provides
for a crude alternative.
This type incorporates certain struct types, such as *[Spatial],
*[FirstAuthority], *[CurrentAuthority] and *[Sponsor], which are used
in cases where many entries share common values.
The drawback is that the RA DUA must perform a separate LDAP search
request for subentries, and must understand how to utilize the values
found as "fallbacks" for missing (explicit) values normally assigned
to entries directly.
In cases where Collective Attributes are supported, this type can provide
for convenient creation of subentries for submission through an LDAP Add
request or other means.
Instances of this type are created using the *[DITProfile.NewSubentry]
method, and can be marshaled using the [go-ldap/ldap Entry.Unmarshal]
method submitted as a closure to the [Subentry.Marshal] method.
See also the [NewSubtreeSpecification] function, which is used to produce
an instance of [SubtreeSpecification] using a string input value.
[subentry]: https://datatracker.ietf.org/doc/html/rfc3672#section-2.4
*/
type Subentry struct {
R_DN string `ldap:"dn"` // full DN
R_CN string `ldap:"cn"` // rDN value
R_TTL string `ldap:"rATTL"`
RC_TTL string `ldap:"c-rATTL;collective"`
R_GSR string `ldap:"governingStructureRule"`
R_OC []string `ldap:"objectClass"`
R_STS []string `ldap:"subtreeSpecification"`
R_Spatial *Spatial
R_X660 *X660
R_Extra *Supplement
r_DITProfile *DITProfile
r_root *registeredRoot
}
/*
Subentries implements the slice type of *[Subentry].
*/
type Subentries []*Subentry
/*
Push appends the input *[Subentry] instance to the receiver instance.
The common name of the input *[Subentry] instance MUST be unique to the
receiver instance, meaning no other preexisting slice member may bear
the common name in question.
*/
func (r *Subentries) Push(se *Subentry) {
if !r.IsZero() && !structEmpty(se) {
if se.validName(se.DN()) && se.CN() != "" && !r.Contains(se.CN()) {
*r = append(*r, se)
}
}
}
/*
Contains wraps [Subentries.Get] to return a Boolean value indicative of
a positive match between the input value and the candidate common name or
alternatively the distinguished name.
Case is not significant in the matching process.
*/
func (r *Subentries) Contains(try string) (has bool) {
if !r.IsZero() {
has = !r.Get(try).IsZero()
}
return
}
/*
Get returns the *[Subentry] instance which bears a matching common name
or distinguished name to the input value.
Case is not significant in the matching process.
*/
func (r *Subentries) Get(try string) (got *Subentry) {
if !r.IsZero() {
for i := 0; i < r.Len(); i++ {
if slice := r.Index(i); !slice.IsZero() {
if eq(slice.CN(), try) || eq(slice.DN(), try) {
got = slice
break
}
}
}
}
return
}
/*
Len returns the integer length of the receiver instance.
*/
func (r *Subentries) Len() (l int) {
if !r.IsZero() {
l = len(*r)
}
return
}
/*
Index returns the Nth slice member found within the receiver instance.
*/
func (r *Subentries) Index(idx int) (got *Subentry) {
if L := r.Len(); L > 0 {
if 0 <= idx && idx < L {
got = (*r)[idx]
} else if idx == -1 {
got = (*r)[L-1]
}
}
return
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r *Subentries) IsZero() bool {
return r == nil
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r *Subentry) IsZero() bool {
return r == nil
}
func (r *Subentry) isEmpty() bool {
return structEmpty(r)
}
/*
StructuralObjectClass returns the STRUCTURAL objectClass "[subentry]"
under any circumstance.
[subentry]: https://datatracker.ietf.org/doc/html/rfc3672#section-2.4
*/
func (r *Subentry) StructuralObjectClass() (s string) {
return `subentry`
}
/*
SubtreeSpecification returns the underlying Subtree Specification slice
values, if set, else zero slices are returned.
*/
func (r *Subentry) SubtreeSpecification() (sts []string) {
if !r.IsZero() {
sts = r.R_STS
}
return
}
/*
SetSubtreeSpecification appends the provided string value to the receiver
instance as a Subtree Specification. If an instance of []string is provided,
the receiver value is clobbered (overwritten).
*/
func (r *Subentry) SetSubtreeSpecification(args ...any) error {
if len(args) > 0 {
if ss, assert := args[0].(SubtreeSpecification); assert {
args[0] = ss.String()
}
}
return writeFieldByTag(`subtreeSpecification`, r.SetSubtreeSpecification, r, args...)
}
/*
SubtreeSpecificationGetFunc executes the [GetOrSetFunc] instance and
returns its own return values. The 'any' value will require type assertion
in order to be accessed reliably. An error is returned if issues arise.
*/
func (r *Subentry) SubtreeSpecificationGetFunc(getfunc GetOrSetFunc) (any, error) {
return getFieldValueByNameTagAndGoSF(r, getfunc, `subtreeSpecification`)
}
/*
CN returns the common name value assigned to the receiver instance.
*/
func (r *Subentry) CN() (val string) {
if !r.IsZero() {
val = r.R_CN
}
return
}
/*
SetCN assigns the provided string value to the receiver instance.
*/
func (r *Subentry) SetCN(args ...any) error {
return writeFieldByTag(`cn`, r.SetCN, r, args...)
}
/*
CNGetFunc processes the underlying field value(s) through the provided
[GetOrSetFunc] instance, returning an interface value alongside an error.
*/
func (r *Subentry) CNGetFunc(getfunc GetOrSetFunc) (any, error) {
return getFieldValueByNameTagAndGoSF(r, getfunc, `cn`)
}
/*
validName returns a Boolean value indicative of a valid naming convention for
the receiver instance in terms of RFC 3672 and ITU-T Rec. X.501 conformity.
*/
func (r *Subentry) validName(dn string) (ok bool) {
if tkz := tokenizeDN(dn); len(tkz) > 0 {
if len(tkz[0]) == 1 {
if len(tkz[0][0]) == 2 {
ok = strInSlice(tkz[0][0][0], []string{`cn`, `2.5.4.3`})
}
}
}
return
}
/*
LDIF returns the string LDIF form of the receiver instance. Note that
this is a crude approximation of LDIF and should ideally be parsed
through a reliable LDIF parser such as [go-ldap/ldif] to verify integrity.
Also note that if the receiver instance produces an LDIF entry which is
named in a manner that violates [clause 14.2.2 of ITU-T Rec. X.501], the
output will be zero.
[go-ldap/ldif]: https://pkg.go.dev/github.com/go-ldap/ldif
[clause 14.2.2 of ITU-T Rec. X.501]: https://www.itu.int/rec/T-REC-X.501
*/
func (r *Subentry) LDIF() (l string) {
if !r.IsZero() {
if dn := readFieldByTag(`dn`, r); len(dn) > 0 {
if !r.validName(dn[0]) {
return
}
r.refreshObjectClasses()
oc := readFieldByTag(`objectClass`, r)
bld := newBuilder()
bld.WriteString(`dn: ` + dn[0])
bld.WriteRune(10)
for i := 0; i < len(oc); i++ {
bld.WriteString(`objectClass: ` + oc[i])
bld.WriteRune(10)
}
// Just to avoid needless errors, if the subtree
// specification is empty, set it to []string{`{}`}
// to defer to administrative area defaults.
if len(r.R_STS) == 0 {
r.R_STS = []string{`{}`}
}
bld.WriteString(toLDIF(r))
bld.WriteString(r.X660().ldif())
bld.WriteString(r.Spatial().ldif())
bld.WriteRune(10)
l = bld.String()
}
}
return
}
func (r *Subentry) refreshObjectClasses() {
bools := []bool{
r.X660().isEmpty(),
r.Spatial().isEmpty(),
r.Supplement().isEmpty(),
}
for tx, tag := range []string{
`x660Context`,
`spatialContext`,
`registrationSupplement`,
} {
if bools[tx] {
r.R_OC = removeStrInSlice(tag, r.R_OC)
} else {
if !strInSlice(tag, r.R_OC) {
r.R_OC = append(r.R_OC, tag)
}
}
}
// go-ldap/v3.Entry.Unmarshal is sloppy about adding
// duplicate objectClasses, so let's clean up any
// that may have appeared.
var tmp []string
for _, oc := range r.R_OC {
if !strInSlice(oc, tmp) {
tmp = append(tmp, oc)
}
}
if len(tmp) != len(r.R_OC) {
r.R_OC = tmp
}
}
/*
DN returns the string-based LDAP Distinguished Name value, or a zero
string if unset.
*/
func (r *Subentry) DN() (dn string) {
if !r.IsZero() {
dn = r.R_DN
}
return
}
/*
DNGetFunc executes the [GetOrSetFunc] instance and returns its own
return values. The 'any' value will require type assertion in order
to be accessed reliably. An error is returned if issues arise.
*/
func (r *Subentry) DNGetFunc(getfunc GetOrSetFunc) (any, error) {
return getFieldValueByNameTagAndGoSF(r, getfunc, `dn`)
}
/*
SetDN assigns the first provided value to the receiver instance as a string DN.
If additional arguments are present, the second value will be asserted as an
instance of [GetOrSetFunc], returning an error if this fails.
*/
func (r *Subentry) SetDN(args ...any) error {
return writeFieldByTag(`dn`, r.SetDN, r, args...)
}
/*
TTL returns the raw time-to-live within the receiver instance.
*/
func (r *Subentry) TTL() (ttl string) {
if !r.IsZero() {
ttl = r.R_TTL
}
return
}
/*
TTL returns the raw (collective) time-to-live within the receiver instance.
*/
func (r *Subentry) CTTL() (cttl string) {
if !r.IsZero() {
cttl = r.RC_TTL
}
return
}
/*
SetCTTL assigns the provided string TTL (collective) value to the receiver instance.
*/
func (r *Subentry) SetCTTL(args ...any) error {
return writeFieldByTag(`c-rATTL;collective`, r.SetCTTL, r, args...)
}
/*
SetTTL assigns the provided string TTL value to the receiver instance.
*/
func (r *Subentry) SetTTL(args ...any) error {
return writeFieldByTag(`rATTL`, r.SetTTL, r, args...)
}
/*
TTLGetFunc processes the underlying TTL field value through the provided
[GetOrSetFunc] instance, returning an interface value alongside an error.
*/
func (r *Subentry) TTLGetFunc(getfunc GetOrSetFunc) (any, error) {
return getFieldValueByNameTagAndGoSF(r, getfunc, `rATTL`)
}
/*
CTTLGetFunc processes the underlying (collective) TTL field value through
the provided [GetOrSetFunc] instance, returning an interface value alongside
an error.
*/
func (r *Subentry) CTTLGetFunc(getfunc GetOrSetFunc) (any, error) {
return getFieldValueByNameTagAndGoSF(r, getfunc, `c-rATTL`)
}
/*
Marshal returns an error following an attempt to execute the input meth
"func(any) error" method signature.
The meth value should be the (unexecuted) [go-ldap/v3 Entry.Unmarshal]
method instance for the [Entry] being transferred (marshaled) into the
receiver instance.
Alternatively, if the user has fashioned an alternative method of the
same signature, this may be supplied instead.
[go-ldap/v3 Entry.Unmarshal]: https://pkg.go.dev/github.com/go-ldap/ldap/v3#Entry.Unmarshal
[Entry]: https://pkg.go.dev/github.com/go-ldap/ldap/v3#Entry
*/
func (r *Subentry) Marshal(meth func(any) error) (err error) {
if meth == nil {
err = NilMethodErr
return
} else if r == nil {
err = NilRegistrationErr
return
}
for _, err = range []error{
meth(r),
r.X660().marshal(meth),
r.Spatial().marshal(meth),
} {
if err != nil {
return
}
}
// Just to avoid needless errors, if the subtree
// specification is empty, set it to []string{`{}`}
// to defer to administrative area defaults.
if len(r.R_STS) == 0 {
r.R_STS = []string{`{}`}
}
// clean-up any duplicate objectClass
// slices that may have appeared, and
// remove any classes that aren't used.
r.refreshObjectClasses()
return
}
/*
Unmarshal transports values from the receiver into an instance of
map[string][]string, which can subsequently be fed to the [go-ldap/v3
NewEntry] function.
[go-ldap/v3 NewEntry]: https://pkg.go.dev/github.com/go-ldap/ldap/v3#NewEntry
*/
func (r *Subentry) Unmarshal() (outer map[string][]string) {
if r.IsZero() {
return
}
outer = make(map[string][]string)
for _, inner := range []map[string][]string{
unmarshalStruct(r, outer),
r.X660().unmarshal(),
r.Spatial().unmarshal(),
} {
if inner != nil {
for k, v := range inner {
outer[k] = v
}
}
}
return
}
/*
ObjectClasses returns string slices of "[objectClass]" in descriptor or
numeric OID forms currently held by the receiver instance.
[objectClass]: https://www.rfc-editor.org/rfc/rfc4512.html#section-3.3
*/
func (r *Subentry) ObjectClasses() []string {
return r.R_OC
}
/*
SetObjectClasses appends one or more string descriptor or numeric OID
"[objectClass]" values to the receiver instance. Note that if a slice is
passed as X, the destination value will be clobbered.
[objectClass]: https://www.rfc-editor.org/rfc/rfc4512.html#section-3.3
*/
func (r *Subentry) SetObjectClasses(args ...any) error {
return writeFieldByTag(`objectClass`, r.SetObjectClasses, r, args...)
}
/*
ObjectClassesGetFunc processes the underlying field value(s) through the
provided [GetOrSetFunc] instance, returning an interface value alongside
an error.
*/
func (r *Subentry) ObjectClassesGetFunc(getfunc GetOrSetFunc) (any, error) {
return getFieldValueByNameTagAndGoSF(r, getfunc, `objectClass`)
}
/*
Kind returns the static string value `STRUCTURAL` merely as a convenient
means to determine what [kind of "objectClass"] the receiver describes.
[kind of "objectClass"]: https://www.rfc-editor.org/rfc/rfc4512.html#section-4.1.1
*/
func (r *Subentry) Kind() string {
return `STRUCTURAL`
}
/*
DITProfile returns the *[DITProfile] instance assigned to the receiver,
if set, else nil is returned.
*/
func (r *Subentry) Profile() (prof *DITProfile) {
if !r.IsZero() {
if prof = r.r_DITProfile; !prof.Valid() {
prof = &DITProfile{}
}
}
return
}
/*
SetDITProfile assigns *[DITProfile] d to the receiver instance if, and
only if, both of the following evaluate as true:
- [DITProfile.Valid] returns true for d, AND ...
- the DN of the receiver instance matches a *[Registration] search base within d
Case is not significant in the matching process.
*/
func (r *Subentry) SetDITProfile(d *DITProfile) {
if d.Valid() {
if idx := d.RegistrationSuffixEqual(r.DN()); idx != -1 {
r.r_DITProfile = d
}
}
}