Debian Codegraph @

config/krb5conf.go

raw symbols SCIP
1 // Package config implements KRB5 client and service configuration as described at https://web.mit.edu/kerberos/krb5-latest/doc/admin/conf_files/krb5_conf.html
2 package config
3
4 import (
5 "bufio"
6 "encoding/hex"
7 "errors"
8 "fmt"
9 "io"
10 "net"
11 "os"
12 "os/user"
13 "regexp"
14 "strconv"
15 "strings"
16 "time"
17
18 "github.com/jcmturner/gofork/encoding/asn1"
19 "gopkg.in/jcmturner/gokrb5.v5/iana/etypeID"
20 )
21
22 // Config represents the KRB5 configuration.
23 type Config struct {
24 LibDefaults *LibDefaults
25 Realms []Realm
26 DomainRealm DomainRealm
27 //CaPaths
28 //AppDefaults
29 //Plugins
30 }
31
32 // WeakETypeList is a list of encryption types that have been deemed weak.
33 const WeakETypeList = "des-cbc-crc des-cbc-md4 des-cbc-md5 des-cbc-raw des3-cbc-raw des-hmac-sha1 arcfour-hmac-exp rc4-hmac-exp arcfour-hmac-md5-exp des"
34
35 // NewConfig creates a new config struct instance.
36 func NewConfig() *Config {
37 d := make(DomainRealm)
38 return &Config{
39 LibDefaults: newLibDefaults(),
40 DomainRealm: d,
41 }
42 }
43
44 // LibDefaults represents the [libdefaults] section of the configuration.
45 type LibDefaults struct {
46 AllowWeakCrypto bool //default false
47 // ap_req_checksum_type int //unlikely to support this
48 Canonicalize bool //default false
49 CCacheType int //default is 4. unlikely to implement older
50 Clockskew time.Duration //max allowed skew in seconds, default 300
51 //Default_ccache_name string // default /tmp/krb5cc_%{uid} //Not implementing as will hold in memory
52 DefaultClientKeytabName string //default /usr/local/var/krb5/user/%{euid}/client.keytab
53 DefaultKeytabName string //default /etc/krb5.keytab
54 DefaultRealm string
55 DefaultTGSEnctypes []string //default aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4
56 DefaultTktEnctypes []string //default aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4
57 DefaultTGSEnctypeIDs []int32 //default aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4
58 DefaultTktEnctypeIDs []int32 //default aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4
59 DNSCanonicalizeHostname bool //default true
60 DNSLookupKDC bool //default false
61 DNSLookupRealm bool
62 ExtraAddresses []net.IP //Not implementing yet
63 Forwardable bool //default false
64 IgnoreAcceptorHostname bool //default false
65 K5LoginAuthoritative bool //default false
66 K5LoginDirectory string //default user's home directory. Must be owned by the user or root
67 KDCDefaultOptions asn1.BitString //default 0x00000010 (KDC_OPT_RENEWABLE_OK)
68 KDCTimeSync int //default 1
69 //kdc_req_checksum_type int //unlikely to implement as for very old KDCs
70 NoAddresses bool //default true
71 PermittedEnctypes []string //default aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 des3-cbc-sha1 arcfour-hmac-md5 camellia256-cts-cmac camellia128-cts-cmac des-cbc-crc des-cbc-md5 des-cbc-md4
72 PermittedEnctypeIDs []int32
73 //plugin_base_dir string //not supporting plugins
74 PreferredPreauthTypes []int //default “17, 16, 15, 14”, which forces libkrb5 to attempt to use PKINIT if it is supported
75 Proxiable bool //default false
76 RDNS bool //default true
77 RealmTryDomains int //default -1
78 RenewLifetime time.Duration //default 0
79 SafeChecksumType int //default 8
80 TicketLifetime time.Duration //default 1 day
81 UDPPreferenceLimit int // 1 means to always use tcp. MIT krb5 has a default value of 1465, and it prevents user setting more than 32700.
82 VerifyAPReqNofail bool //default false
83 }
84
85 // Create a new LibDefaults struct.
86 func newLibDefaults() *LibDefaults {
87 uid := "0"
88 var hdir string
89 usr, _ := user.Current()
90 if usr != nil {
91 uid = usr.Uid
92 hdir = usr.HomeDir
93 }
94 opts := asn1.BitString{}
95 opts.Bytes, _ = hex.DecodeString("00000010")
96 opts.BitLength = len(opts.Bytes) * 8
97 return &LibDefaults{
98 CCacheType: 4,
99 Clockskew: time.Duration(300) * time.Second,
100 DefaultClientKeytabName: fmt.Sprintf("/usr/local/var/krb5/user/%s/client.keytab", uid),
101 DefaultKeytabName: "/etc/krb5.keytab",
102 DefaultTGSEnctypes: []string{"aes256-cts-hmac-sha1-96", "aes128-cts-hmac-sha1-96", "des3-cbc-sha1", "arcfour-hmac-md5", "camellia256-cts-cmac", "camellia128-cts-cmac", "des-cbc-crc", "des-cbc-md5", "des-cbc-md4"},
103 DefaultTktEnctypes: []string{"aes256-cts-hmac-sha1-96", "aes128-cts-hmac-sha1-96", "des3-cbc-sha1", "arcfour-hmac-md5", "camellia256-cts-cmac", "camellia128-cts-cmac", "des-cbc-crc", "des-cbc-md5", "des-cbc-md4"},
104 DNSCanonicalizeHostname: true,
105 K5LoginDirectory: hdir,
106 KDCDefaultOptions: opts,
107 KDCTimeSync: 1,
108 NoAddresses: true,
109 PermittedEnctypes: []string{"aes256-cts-hmac-sha1-96", "aes128-cts-hmac-sha1-96", "des3-cbc-sha1", "arcfour-hmac-md5", "camellia256-cts-cmac", "camellia128-cts-cmac", "des-cbc-crc", "des-cbc-md5", "des-cbc-md4"},
110 RDNS: true,
111 RealmTryDomains: -1,
112 SafeChecksumType: 8,
113 TicketLifetime: time.Duration(24) * time.Hour,
114 UDPPreferenceLimit: 1465,
115 PreferredPreauthTypes: []int{17, 16, 15, 14},
116 }
117 }
118
119 // Parse the lines of the [libdefaults] section of the configuration into the LibDefaults struct.
120 func (l *LibDefaults) parseLines(lines []string) error {
121 for _, line := range lines {
122 line = strings.TrimSpace(line)
123 if line == "" {
124 continue
125 }
126 if strings.Contains(line, "v4_") {
127 return errors.New("v4 configurations are not supported in Realms section")
128 }
129 if !strings.Contains(line, "=") {
130 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
131 }
132
133 p := strings.Split(line, "=")
134 key := strings.TrimSpace(strings.ToLower(p[0]))
135 switch key {
136 case "allow_weak_crypto":
137 v, err := parseBoolean(p[1])
138 if err != nil {
139 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
140 }
141 l.AllowWeakCrypto = v
142 case "canonicalize":
143 v, err := parseBoolean(p[1])
144 if err != nil {
145 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
146 }
147 l.Canonicalize = v
148 case "ccache_type":
149 p[1] = strings.TrimSpace(p[1])
150 v, err := strconv.ParseUint(p[1], 10, 32)
151 if err != nil || v < 0 || v > 4 {
152 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
153 }
154 l.CCacheType = int(v)
155 case "clockskew":
156 d, err := parseDuration(p[1])
157 if err != nil {
158 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
159 }
160 l.Clockskew = d
161 case "default_client_keytab_name":
162 l.DefaultClientKeytabName = strings.TrimSpace(p[1])
163 case "default_keytab_name":
164 l.DefaultKeytabName = strings.TrimSpace(p[1])
165 case "default_realm":
166 l.DefaultRealm = strings.TrimSpace(p[1])
167 case "default_tgs_enctypes":
168 l.DefaultTGSEnctypes = strings.Fields(p[1])
169 case "default_tkt_enctypes":
170 l.DefaultTktEnctypes = strings.Fields(p[1])
171 case "dns_canonicalize_hostname":
172 v, err := parseBoolean(p[1])
173 if err != nil {
174 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
175 }
176 l.DNSCanonicalizeHostname = v
177 case "dns_lookup_kdc":
178 v, err := parseBoolean(p[1])
179 if err != nil {
180 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
181 }
182 l.DNSLookupKDC = v
183 case "dns_lookup_realm":
184 v, err := parseBoolean(p[1])
185 if err != nil {
186 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
187 }
188 l.DNSLookupRealm = v
189 case "extra_addresses":
190 ipStr := strings.TrimSpace(p[1])
191 for _, ip := range strings.Split(ipStr, ",") {
192 if eip := net.ParseIP(ip); eip != nil {
193 l.ExtraAddresses = append(l.ExtraAddresses, eip)
194 }
195 }
196 case "forwardable":
197 v, err := parseBoolean(p[1])
198 if err != nil {
199 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
200 }
201 l.Forwardable = v
202 case "ignore_acceptor_hostname":
203 v, err := parseBoolean(p[1])
204 if err != nil {
205 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
206 }
207 l.IgnoreAcceptorHostname = v
208 case "k5login_authoritative":
209 v, err := parseBoolean(p[1])
210 if err != nil {
211 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
212 }
213 l.K5LoginAuthoritative = v
214 case "k5login_directory":
215 l.K5LoginDirectory = strings.TrimSpace(p[1])
216 case "kdc_default_options":
217 v := strings.TrimSpace(p[1])
218 v = strings.Replace(v, "0x", "", -1)
219 b, err := hex.DecodeString(v)
220 if err != nil {
221 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
222 }
223 l.KDCDefaultOptions.Bytes = b
224 l.KDCDefaultOptions.BitLength = len(b) * 8
225 case "kdc_timesync":
226 p[1] = strings.TrimSpace(p[1])
227 v, err := strconv.ParseInt(p[1], 10, 32)
228 if err != nil || v < 0 {
229 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
230 }
231 l.KDCTimeSync = int(v)
232 case "noaddresses":
233 v, err := parseBoolean(p[1])
234 if err != nil {
235 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
236 }
237 l.NoAddresses = v
238 case "permitted_enctypes":
239 l.PermittedEnctypes = strings.Fields(p[1])
240 case "preferred_preauth_types":
241 p[1] = strings.TrimSpace(p[1])
242 t := strings.Split(p[1], ",")
243 var v []int
244 for _, s := range t {
245 i, err := strconv.ParseInt(s, 10, 32)
246 if err != nil {
247 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
248 }
249 v = append(v, int(i))
250 }
251 l.PreferredPreauthTypes = v
252 case "proxiable":
253 v, err := parseBoolean(p[1])
254 if err != nil {
255 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
256 }
257 l.Proxiable = v
258 case "rdns":
259 v, err := parseBoolean(p[1])
260 if err != nil {
261 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
262 }
263 l.RDNS = v
264 case "realm_try_domains":
265 p[1] = strings.TrimSpace(p[1])
266 v, err := strconv.ParseInt(p[1], 10, 32)
267 if err != nil || v < -1 {
268 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
269 }
270 l.RealmTryDomains = int(v)
271 case "renew_lifetime":
272 d, err := parseDuration(p[1])
273 if err != nil {
274 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
275 }
276 l.RenewLifetime = d
277 case "safe_checksum_type":
278 p[1] = strings.TrimSpace(p[1])
279 v, err := strconv.ParseInt(p[1], 10, 32)
280 if err != nil || v < 0 {
281 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
282 }
283 l.SafeChecksumType = int(v)
284 case "ticket_lifetime":
285 d, err := parseDuration(p[1])
286 if err != nil {
287 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
288 }
289 l.TicketLifetime = d
290 case "udp_preference_limit":
291 p[1] = strings.TrimSpace(p[1])
292 v, err := strconv.ParseUint(p[1], 10, 32)
293 if err != nil || v > 32700 {
294 return fmt.Errorf("libdefaults configuration line invalid: %s", line)
295 }
296 l.UDPPreferenceLimit = int(v)
297 case "verify_ap_req_nofail":
298 v, err := parseBoolean(p[1])
299 if err != nil {
300 return fmt.Errorf("libdefaults configuration line invalid. %v: %s", err, line)
301 }
302 l.VerifyAPReqNofail = v
303 default:
304 //Ignore the line
305 continue
306 }
307 }
308 l.DefaultTGSEnctypeIDs = parseETypes(l.DefaultTGSEnctypes, l.AllowWeakCrypto)
309 l.DefaultTktEnctypeIDs = parseETypes(l.DefaultTktEnctypes, l.AllowWeakCrypto)
310 l.PermittedEnctypeIDs = parseETypes(l.PermittedEnctypes, l.AllowWeakCrypto)
311 return nil
312 }
313
314 // Realm represents an entry in the [realms] section of the configuration.
315 type Realm struct {
316 Realm string
317 AdminServer []string
318 //auth_to_local //Not implementing for now
319 //auth_to_local_names //Not implementing for now
320 DefaultDomain string
321 KDC []string
322 KPasswdServer []string //default admin_server:464
323 MasterKDC []string
324 }
325
326 // Parse the lines of a [realms] entry into the Realm struct.
327 func (r *Realm) parseLines(name string, lines []string) error {
328 r.Realm = name
329 var adminServerFinal bool
330 var KDCFinal bool
331 var kpasswdServerFinal bool
332 var masterKDCFinal bool
333 for _, line := range lines {
334 if strings.TrimSpace(line) == "" {
335 continue
336 }
337 if !strings.Contains(line, "=") {
338 return fmt.Errorf("realm configuration line invalid: %s", line)
339 }
340
341 p := strings.Split(line, "=")
342 key := strings.TrimSpace(strings.ToLower(p[0]))
343 v := strings.TrimSpace(p[1])
344 switch key {
345 case "admin_server":
346 appendUntilFinal(&r.AdminServer, v, &adminServerFinal)
347 case "default_domain":
348 r.DefaultDomain = v
349 case "kdc":
350 if !strings.Contains(v, ":") {
351 // No port number specified default to 88
352 if strings.HasSuffix(v, `*`) {
353 v = strings.TrimSpace(strings.TrimSuffix(v, `*`)) + ":88*"
354 } else {
355 v = strings.TrimSpace(v) + ":88"
356 }
357 }
358 appendUntilFinal(&r.KDC, v, &KDCFinal)
359 case "kpasswd_server":
360 appendUntilFinal(&r.KPasswdServer, v, &kpasswdServerFinal)
361 case "master_kdc":
362 appendUntilFinal(&r.MasterKDC, v, &masterKDCFinal)
363 default:
364 //Ignore the line
365 continue
366 }
367 }
368 //default for Kpasswd_server = admin_server:464
369 if len(r.KPasswdServer) < 1 {
370 for _, a := range r.AdminServer {
371 s := strings.Split(a, ":")
372 r.KPasswdServer = append(r.KPasswdServer, s[0]+":464")
373 }
374 }
375 return nil
376 }
377
378 // Parse the lines of the [realms] section of the configuration into an slice of Realm structs.
379 func parseRealms(lines []string) ([]Realm, error) {
380 var realms []Realm
381 start := -1
382 var name string
383 for i, l := range lines {
384 if strings.TrimSpace(l) == "" {
385 continue
386 }
387 if strings.Contains(l, "v4_") {
388 return nil, errors.New("v4 configurations are not supported in Realms section")
389 }
390 if strings.Contains(l, "{") {
391 if start >= 0 {
392 // already started a block!!!
393 return nil, errors.New("invalid Realms section in configuration")
394 }
395 start = i
396 if !strings.Contains(l, "=") {
397 return nil, fmt.Errorf("realm configuration line invalid: %s", l)
398 }
399 p := strings.Split(l, "=")
400 name = strings.TrimSpace(p[0])
401 }
402 if strings.Contains(l, "}") {
403 if start < 0 {
404 // but not started a block!!!
405 return nil, errors.New("invalid Realms section in configuration")
406 }
407 var r Realm
408 r.parseLines(name, lines[start+1:i])
409 realms = append(realms, r)
410 start = -1
411 }
412 }
413 return realms, nil
414 }
415
416 // DomainRealm maps the domains to realms representing the [domain_realm] section of the configuration.
417 type DomainRealm map[string]string
418
419 // Parse the lines of the [domain_realm] section of the configuration and add to the mapping.
420 func (d *DomainRealm) parseLines(lines []string) error {
421 for _, line := range lines {
422 if strings.TrimSpace(line) == "" {
423 continue
424 }
425 if !strings.Contains(line, "=") {
426 return fmt.Errorf("realm configuration line invalid: %s", line)
427 }
428 p := strings.Split(line, "=")
429 domain := strings.TrimSpace(strings.ToLower(p[0]))
430 realm := strings.TrimSpace(strings.ToUpper(p[1]))
431 d.addMapping(domain, realm)
432 }
433 return nil
434 }
435
436 // Add a domain to realm mapping.
437 func (d *DomainRealm) addMapping(domain, realm string) {
438 (*d)[domain] = realm
439 }
440
441 // Delete a domain to realm mapping.
442 func (d *DomainRealm) deleteMapping(domain, realm string) {
443 delete(*d, domain)
444 }
445
446 // ResolveRealm resolves the kerberos realm for the specified domain name from the domain to realm mapping.
447 // The most specific mapping is returned.
448 func (c *Config) ResolveRealm(domainName string) string {
449 domainName = strings.TrimSuffix(domainName, ".")
450
451 // Try to match the entire hostname first
452 if r, ok := c.DomainRealm[domainName]; ok {
453 return r
454 }
455
456 // Try to match all DNS domain parts
457 periods := strings.Count(domainName, ".") + 1
458 for i := 2; i <= periods; i++ {
459 z := strings.SplitN(domainName, ".", i)
460 if r, ok := c.DomainRealm["."+z[len(z)-1]]; ok {
461 return r
462 }
463 }
464 return c.LibDefaults.DefaultRealm
465 }
466
467 // Load the KRB5 configuration from the specified file path.
468 func Load(cfgPath string) (*Config, error) {
469 fh, err := os.Open(cfgPath)
470 if err != nil {
471 return nil, errors.New("configuration file could not be opened: " + cfgPath + " " + err.Error())
472 }
473 defer fh.Close()
474 scanner := bufio.NewScanner(fh)
475 return NewConfigFromScanner(scanner)
476 }
477
478 // NewConfigFromString creates a new Config struct from a string.
479 func NewConfigFromString(s string) (*Config, error) {
480 reader := strings.NewReader(s)
481 return NewConfigFromReader(reader)
482 }
483
484 // NewConfigFromReader creates a new Config struct from an io.Reader.
485 func NewConfigFromReader(r io.Reader) (*Config, error) {
486 scanner := bufio.NewScanner(r)
487 return NewConfigFromScanner(scanner)
488 }
489
490 // NewConfigFromScanner creates a new Config struct from a bufio.Scanner.
491 func NewConfigFromScanner(scanner *bufio.Scanner) (*Config, error) {
492 c := NewConfig()
493 sections := make(map[int]string)
494 var sectionLineNum []int
495 var lines []string
496 for scanner.Scan() {
497 // Skip comments and blank lines
498 if matched, _ := regexp.MatchString(`^\s*(#|;|\n)`, scanner.Text()); matched {
499 continue
500 }
501 if matched, _ := regexp.MatchString(`^\s*\[libdefaults\]\s*`, scanner.Text()); matched {
502 sections[len(lines)] = "libdefaults"
503 sectionLineNum = append(sectionLineNum, len(lines))
504 continue
505 }
506 if matched, _ := regexp.MatchString(`^\s*\[realms\]\s*`, scanner.Text()); matched {
507 sections[len(lines)] = "realms"
508 sectionLineNum = append(sectionLineNum, len(lines))
509 continue
510 }
511 if matched, _ := regexp.MatchString(`^\s*\[domain_realm\]\s*`, scanner.Text()); matched {
512 sections[len(lines)] = "domain_realm"
513 sectionLineNum = append(sectionLineNum, len(lines))
514 continue
515 }
516 if matched, _ := regexp.MatchString(`^\s*\[.*\]\s*`, scanner.Text()); matched {
517 sections[len(lines)] = "unknown_section"
518 sectionLineNum = append(sectionLineNum, len(lines))
519 continue
520 }
521 lines = append(lines, scanner.Text())
522 }
523 for i, start := range sectionLineNum {
524 var end int
525 if i+1 >= len(sectionLineNum) {
526 end = len(lines)
527 } else {
528 end = sectionLineNum[i+1]
529 }
530 switch section := sections[start]; section {
531 case "libdefaults":
532 err := c.LibDefaults.parseLines(lines[start:end])
533 if err != nil {
534 return nil, fmt.Errorf("error processing libdefaults section: %v", err)
535 }
536 case "realms":
537 realms, err := parseRealms(lines[start:end])
538 if err != nil {
539 return nil, fmt.Errorf("error processing realms section: %v", err)
540 }
541 c.Realms = realms
542 case "domain_realm":
543 err := c.DomainRealm.parseLines(lines[start:end])
544 if err != nil {
545 return nil, fmt.Errorf("error processing domaain_realm section: %v", err)
546 }
547 default:
548 continue
549 }
550 }
551 return c, nil
552 }
553
554 // Parse a space delimited list of ETypes into a list of EType numbers optionally filtering out weak ETypes.
555 func parseETypes(s []string, w bool) []int32 {
556 var eti []int32
557 for _, et := range s {
558 if !w {
559 var weak bool
560 for _, wet := range strings.Fields(WeakETypeList) {
561 if et == wet {
562 weak = true
563 break
564 }
565 }
566 if weak {
567 continue
568 }
569 }
570 i := etypeID.EtypeSupported(et)
571 if i != 0 {
572 eti = append(eti, i)
573 }
574 }
575 return eti
576 }
577
578 // Parse a time duration string in the configuration to a golang time.Duration.
579 func parseDuration(s string) (time.Duration, error) {
580 s = strings.Replace(strings.TrimSpace(s), " ", "", -1)
581
582 // handle Nd[NmNs]
583 if strings.Contains(s, "d") {
584 ds := strings.SplitN(s, "d", 2)
585 dn, err := strconv.ParseUint(ds[0], 10, 32)
586 if err != nil {
587 return time.Duration(0), errors.New("invalid time duration")
588 }
589 d := time.Duration(dn*24) * time.Hour
590 if ds[1] != "" {
591 dp, err := time.ParseDuration(ds[1])
592 if err != nil {
593 return time.Duration(0), errors.New("invalid time duration")
594 }
595 d = d + dp
596 }
597 return d, nil
598 }
599
600 // handle Nm[Ns]
601 d, err := time.ParseDuration(s)
602 if err == nil {
603 return d, nil
604 }
605
606 // handle N
607 v, err := strconv.ParseUint(s, 10, 32)
608 if err == nil && v > 0 {
609 return time.Duration(v) * time.Second, nil
610 }
611
612 // handle h:m[:s]
613 if strings.Contains(s, ":") {
614 t := strings.Split(s, ":")
615 if 2 > len(t) || len(t) > 3 {
616 return time.Duration(0), errors.New("invalid time duration value")
617 }
618 var i []int
619 for _, n := range t {
620 j, err := strconv.ParseInt(n, 10, 16)
621 if err != nil {
622 return time.Duration(0), errors.New("invalid time duration value")
623 }
624 i = append(i, int(j))
625 }
626 d := time.Duration(i[0])*time.Hour + time.Duration(i[1])*time.Minute
627 if len(i) == 3 {
628 d = d + time.Duration(i[2])*time.Second
629 }
630 return d, nil
631 }
632 return time.Duration(0), errors.New("invalid time duration value")
633 }
634
635 // Parse possible boolean values to golang bool.
636 func parseBoolean(s string) (bool, error) {
637 s = strings.TrimSpace(s)
638 v, err := strconv.ParseBool(s)
639 if err == nil {
640 return v, nil
641 }
642 switch strings.ToLower(s) {
643 case "yes":
644 return true, nil
645 case "y":
646 return true, nil
647 case "no":
648 return false, nil
649 case "n":
650 return false, nil
651 }
652 return false, errors.New("invalid boolean value")
653 }
654
655 // Parse array of strings but stop if an asterisk is placed at the end of a line.
656 func appendUntilFinal(s *[]string, value string, final *bool) {
657 if *final {
658 return
659 }
660 if last := len(value) - 1; last >= 0 && value[last] == '*' {
661 *final = true
662 value = value[:len(value)-1]
663 }
664 *s = append(*s, value)
665 }