github.com/iDigitalFlame/xmt@v0.5.4/com/pipe/xy_nix_crypt.go (about)

     1  //go:build !windows && crypt
     2  // +build !windows,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 pipe
    21  
    22  import (
    23  	"os"
    24  
    25  	"github.com/iDigitalFlame/xmt/util/crypt"
    26  )
    27  
    28  // PermEveryone is the Linux permission string used in sockets to allow anyone to write and read
    29  // to the listening socket. This can be used for socket communication between privilege boundaries.
    30  // This can be applied to the ListenPerm function.
    31  var PermEveryone = crypt.Get(19) // 0766
    32  
    33  // Format will ensure the path for this Pipe socket fits the proper OS based pathname. Valid path names will be
    34  // returned without any changes.
    35  func Format(s string) string {
    36  	if len(s) > 0 && s[0] != '/' {
    37  		var (
    38  			p      = crypt.Get(20) + s // /var/run/
    39  			f, err = os.OpenFile(p, 0x242, 0600)
    40  			// 0x242 - CREATE | TRUNCATE | RDWR
    41  		)
    42  		if err != nil {
    43  			return crypt.Get(21) + s // /tmp/
    44  		}
    45  		f.Close()
    46  		os.Remove(p)
    47  		return p
    48  	}
    49  	return s
    50  }