|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build windows |
| 19 | +// +build windows |
| 20 | + |
| 21 | +package npipe |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "net" |
| 27 | + "os/user" |
| 28 | + "strings" |
| 29 | + |
| 30 | + winio "github.com/Microsoft/go-winio" |
| 31 | +) |
| 32 | + |
| 33 | +// NewListener creates a new Listener receiving events over a named pipe. |
| 34 | +func NewListener(name, sd string) (net.Listener, error) { |
| 35 | + c := &winio.PipeConfig{ |
| 36 | + SecurityDescriptor: sd, |
| 37 | + } |
| 38 | + |
| 39 | + l, err := winio.ListenPipe(name, c) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("failed to listen on the named pipe %s: %w", name, err) |
| 42 | + } |
| 43 | + |
| 44 | + return l, nil |
| 45 | +} |
| 46 | + |
| 47 | +// TransformString takes an input type name defined as a URI like `npipe:///hello` and transform it into |
| 48 | +// `\\.\pipe\hello` |
| 49 | +func TransformString(name string) string { |
| 50 | + if strings.HasPrefix(name, "npipe:///") { |
| 51 | + path := strings.TrimPrefix(name, "npipe:///") |
| 52 | + return `\\.\pipe\` + path |
| 53 | + } |
| 54 | + |
| 55 | + if strings.HasPrefix(name, `\\.\pipe\`) { |
| 56 | + return name |
| 57 | + } |
| 58 | + |
| 59 | + return name |
| 60 | +} |
| 61 | + |
| 62 | +// DialContext create a Dial to be use with an http.Client to connect to a pipe. |
| 63 | +func DialContext(npipe string) func(context.Context, string, string) (net.Conn, error) { |
| 64 | + return func(ctx context.Context, _, _ string) (net.Conn, error) { |
| 65 | + return winio.DialPipeContext(ctx, npipe) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// Dial create a Dial to be use with an http.Client to connect to a pipe. |
| 70 | +func Dial(npipe string) func(string, string) (net.Conn, error) { |
| 71 | + return func(_, _ string) (net.Conn, error) { |
| 72 | + return winio.DialPipe(npipe, nil) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// DefaultSD returns a default SecurityDescriptor which is the minimal required permissions to be |
| 77 | +// able to write to the named pipe. The security descriptor is returned in SDDL format. |
| 78 | +// |
| 79 | +// Docs: https://docs.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-string-format |
| 80 | +func DefaultSD(forUser string) (string, error) { |
| 81 | + var u *user.User |
| 82 | + var err error |
| 83 | + // No user configured we fallback to the current running user. |
| 84 | + if len(forUser) == 0 { |
| 85 | + u, err = user.Current() |
| 86 | + if err != nil { |
| 87 | + return "", fmt.Errorf("failed to retrieve the current user: %w", err) |
| 88 | + } |
| 89 | + } else { |
| 90 | + u, err = user.Lookup(forUser) |
| 91 | + if err != nil { |
| 92 | + return "", fmt.Errorf("failed to retrieve the user %s: %w", forUser, err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Named pipe security and access rights. |
| 97 | + // We create the pipe and the specific users should only be able to write to it. |
| 98 | + // See docs: https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipe-security-and-access-rights |
| 99 | + // String definition: https://docs.microsoft.com/en-us/windows/win32/secauthz/ace-strings |
| 100 | + // Give generic read/write access to the specified user. |
| 101 | + descriptor := "D:P(A;;GA;;;" + u.Uid + ")" |
| 102 | + if u.Username == "NT AUTHORITY\\SYSTEM" { |
| 103 | + // running as SYSTEM, include Administrators group so Administrators can talk over |
| 104 | + // the named pipe to the running Elastic Agent system process |
| 105 | + // https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems |
| 106 | + descriptor += "(A;;GA;;;S-1-5-32-544)" // Administrators group |
| 107 | + } |
| 108 | + return descriptor, nil |
| 109 | +} |
0 commit comments