github.com/iDigitalFlame/xmt@v0.5.4/device/local/bsd_crypt.go (about)

     1  //go:build !windows && !plan9 && !js && !darwin && !linux && !android && crypt
     2  // +build !windows,!plan9,!js,!darwin,!linux,!android,crypt
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package local
    21  
    22  import (
    23  	"bytes"
    24  	"runtime"
    25  	"strings"
    26  
    27  	"github.com/iDigitalFlame/xmt/data"
    28  	"github.com/iDigitalFlame/xmt/device/unix"
    29  	"github.com/iDigitalFlame/xmt/util/crypt"
    30  )
    31  
    32  func sysID() []byte {
    33  	switch {
    34  	case runtime.GOOS[0] == 'a':
    35  		// AIX specific support: https://github.com/denisbrodbeck/machineid/pull/16
    36  		if b, err := output(crypt.Get(71)); err == nil { // lsattr -l sys0 -a os_uuid -E
    37  			if i := bytes.IndexByte(b, ' '); i > 0 {
    38  				return b[i+1:]
    39  			}
    40  			return b
    41  		}
    42  	case runtime.GOOS[0] == 'o':
    43  		// Support get hardware UUID for OpenBSD: https://github.com/denisbrodbeck/machineid/pull/14
    44  		if b, err := output(crypt.Get(72)); err == nil { // sysctl -n hw.uuid
    45  			return b
    46  		}
    47  	}
    48  	if b, err := data.ReadFile(crypt.Get(73)); err == nil { // /etc/hostid
    49  		return b
    50  	}
    51  	o, _ := output(crypt.Get(74)) // kenv -q smbios.system.uuid
    52  	return o
    53  }
    54  func version() string {
    55  	var (
    56  		ok      bool
    57  		b, n, v string
    58  	)
    59  	if m := release(); len(m) > 0 {
    60  		b = m[crypt.Get(1)]                // ID
    61  		if n, ok = m[crypt.Get(75)]; !ok { // PRETTY_NAME
    62  			n = m[crypt.Get(75)[7:]] // PRETTY_NAME
    63  		}
    64  		if v, ok = m[crypt.Get(76)]; !ok { // VERSION_ID
    65  			v = m[crypt.Get(76)[0:7]] // VERSION_ID
    66  		}
    67  	}
    68  	// NOTE(dij): Used a little string hack here since "bsd" is only used here
    69  	//            same with "freebsd-version" so it fits nicely.
    70  	if len(b) == 0 && strings.Contains(runtime.GOOS, crypt.Get(77)[4:7]) { // freebsd-version -k
    71  		if o, err := output(crypt.Get(77)); err == nil { // freebsd-version -k
    72  			b = strings.Replace(string(o), "\n", "", -1)
    73  		}
    74  	}
    75  	if len(v) == 0 {
    76  		v = unix.Release()
    77  	}
    78  	switch {
    79  	case len(n) == 0 && len(b) == 0 && len(v) == 0:
    80  		return crypt.Get(78) // BSD
    81  	case len(n) == 0 && len(b) > 0 && len(v) > 0:
    82  		return crypt.Get(78) + " (" + v + ", " + b + ")" // BSD
    83  	case len(n) == 0 && len(b) == 0 && len(v) > 0:
    84  		return crypt.Get(78) + " (" + v + ")" // BSD
    85  	case len(n) == 0 && len(b) > 0 && len(v) == 0:
    86  		return crypt.Get(78) + " (" + b + ")" // BSD
    87  	case len(n) > 0 && len(b) > 0 && len(v) > 0:
    88  		return n + " (" + v + ", " + b + ")"
    89  	case len(n) > 0 && len(b) == 0 && len(v) > 0:
    90  		return n + " (" + v + ")"
    91  	case len(n) > 0 && len(b) > 0 && len(v) == 0:
    92  		return n + " (" + b + ")"
    93  	}
    94  	return crypt.Get(78) // BSD
    95  }