Debian Codegraph @

internal/sockets.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package internalscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal`/
16
17 import (
18 "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/"
19 "io/fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/"
20 "strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/"
21 "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/"
22 )
23
24 // SplitUnixSocketPermissionsBits takes a unix socket address in the
25 // unusual "path|bits" format (e.g. /run/caddy.sock|0222) and tries
26 // to split it into socket path (host) and permissions bits (port).
27 // Colons (":") can't be used as separator, as socket paths on Windows
28 // may include a drive letter (e.g. `unix/c:\absolute\path.sock`).
29 // Permission bits will default to 0200 if none are specified.
30 // Throws an error, if the first carrying bit does not
31 // include write perms (e.g. `0422` or `022`).
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 SplitUnixSocketPermissionsBitsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal`/SplitUnixSocketPermissionsBits().(addr string) (path string, fileMode fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.FileModescip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/FileMode#, err error) {
35 addrSplit := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.SplitNscip-go gomod github.com/golang/go/src go1.25.1 strings/SplitN().(addr, "|", 2)
36
37 if len(addrSplit) == 2 {
38 // parse octal permission bit string as uint32
39 fileModeUInt64, err := strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.1 strconv/ParseUint().(addrSplit[1], 8, 32)
40 if err != nil {
41 return "", 0, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("could not parse octal permission bits in %s: %v", addr, err)
42 }
43 fileMode = fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.FileModescip-go gomod github.com/golang/go/src go1.25.1 `io/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.Stringscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/FileMode#String().()[2]) != "w" {
47 return "", 0, fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("owner of the socket requires '-w-' (write, octal: '2') permissions at least; got '%s' in %s", fileMode.Stringscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/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 }