github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/interfaces/dbus/dbus.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     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 version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  // Package dbus implements interaction between snappy and dbus.
    21  //
    22  // Snappy creates dbus configuration files that describe how various
    23  // services on the system bus can communicate with other peers.
    24  //
    25  // Each configuration is an XML file containing <busconfig>...</busconfig>.
    26  // Particular security snippets define whole <policy>...</policy> entires.
    27  //
    28  // NOTE: This interacts with systemd.
    29  // TODO: Explain how this works (security).
    30  package dbus
    31  
    32  import (
    33  	"bytes"
    34  	"fmt"
    35  	"strings"
    36  )
    37  
    38  // SafePath returns a string suitable for use in a DBus object
    39  func SafePath(s string) string {
    40  	const allowed = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`
    41  	buf := bytes.NewBuffer(make([]byte, 0, len(s)))
    42  
    43  	for _, c := range []byte(s) {
    44  		if strings.IndexByte(allowed, c) >= 0 {
    45  			fmt.Fprintf(buf, "%c", c)
    46  		} else {
    47  			fmt.Fprintf(buf, "_%02x", c)
    48  		}
    49  	}
    50  
    51  	return buf.String()
    52  }