SplitUnixSocketPermissionsBits takes a unix socket address in the unusual "path|bits" format (e.g. /run/caddy.sock|0222) and tries to split it into socket path (host) and permissions bits (port). Colons (":") can't be used as separator, as socket paths on Windows may include a drive letter (e.g. `un
(addr string)
| 32 | // Symbolic permission representation (e.g. `u=w,g=w,o=w`) |
| 33 | // is not supported and will throw an error for now! |
| 34 | func SplitUnixSocketPermissionsBits(addr string) (path string, fileMode fs.FileMode, err error) { |
| 35 | addrSplit := strings.SplitN(addr, "|", 2) |
| 36 | |
| 37 | if len(addrSplit) == 2 { |
| 38 | // parse octal permission bit string as uint32 |
| 39 | fileModeUInt64, err := strconv.ParseUint(addrSplit[1], 8, 32) |
| 40 | if err != nil { |
| 41 | return "", 0, fmt.Errorf("could not parse octal permission bits in %s: %v", addr, err) |
| 42 | } |
| 43 | fileMode = fs.FileMode(fileModeUInt64) |
| 44 | |
| 45 | // FileMode.String() returns a string like `-rwxr-xr--` for `u=rwx,g=rx,o=r` (`0754`) |
| 46 | if string(fileMode.String()[2]) != "w" { |
| 47 | return "", 0, fmt.Errorf("owner of the socket requires '-w-' (write, octal: '2') permissions at least; got '%s' in %s", fileMode.String()[1:4], addr) |
| 48 | } |
| 49 | |
| 50 | return addrSplit[0], fileMode, nil |
| 51 | } |
| 52 | |
| 53 | // default to 0200 (symbolic: `u=w,g=,o=`) |
| 54 | // if no permission bits are specified |
| 55 | return addr, 0o200, nil |
| 56 | } |