github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/cmd/snapctl/main.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 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 main
    21  
    22  import (
    23  	"fmt"
    24  	"io"
    25  	"os"
    26  
    27  	"github.com/snapcore/snapd/client"
    28  	"github.com/snapcore/snapd/dirs"
    29  	"github.com/snapcore/snapd/usersession/xdgopenproxy"
    30  )
    31  
    32  var clientConfig = client.Config{
    33  	// snapctl should not try to read $HOME/.snap/auth.json, this will
    34  	// result in apparmor denials and configure task failures
    35  	// (LP: #1660941)
    36  	DisableAuth: true,
    37  
    38  	// we need the less privileged snap socket in snapctl
    39  	Socket: dirs.SnapSocket,
    40  }
    41  
    42  func main() {
    43  	// check for internal commands
    44  	if len(os.Args) > 2 && os.Args[1] == "internal" {
    45  		switch os.Args[2] {
    46  		case "configure-core":
    47  			fmt.Fprintf(os.Stderr, "no internal core configuration anymore")
    48  			os.Exit(1)
    49  		}
    50  	}
    51  	if len(os.Args) == 3 && os.Args[1] == "user-open" {
    52  		if err := xdgopenproxy.Run(os.Args[2]); err != nil {
    53  			fmt.Fprintf(os.Stderr, "user-open error: %v\n", err)
    54  			os.Exit(1)
    55  		}
    56  		os.Exit(0)
    57  	}
    58  
    59  	var stdin io.Reader
    60  	if len(os.Args) > 1 && client.InternalSnapctlCmdNeedsStdin(os.Args[1]) {
    61  		stdin = os.Stdin
    62  	}
    63  
    64  	// no internal command, route via snapd
    65  	stdout, stderr, err := run(stdin)
    66  	if err != nil {
    67  		if e, ok := err.(*client.Error); ok {
    68  			switch e.Kind {
    69  			case client.ErrorKindUnsuccessful:
    70  				if errRes, ok := e.Value.(map[string]interface{}); ok {
    71  					if stdout, ok := errRes["stdout"].(string); ok {
    72  						os.Stdout.Write([]byte(stdout))
    73  					}
    74  					if stderr, ok := errRes["stderr"].(string); ok {
    75  						os.Stderr.Write([]byte(stderr))
    76  					}
    77  					if errCode, ok := errRes["exit-code"].(float64); ok {
    78  						os.Exit(int(errCode))
    79  					}
    80  				}
    81  			}
    82  		}
    83  		fmt.Fprintf(os.Stderr, "error: %s\n", err)
    84  		os.Exit(1)
    85  	}
    86  
    87  	if stdout != nil {
    88  		os.Stdout.Write(stdout)
    89  	}
    90  
    91  	if stderr != nil {
    92  		os.Stderr.Write(stderr)
    93  	}
    94  }
    95  
    96  func run(stdin io.Reader) (stdout, stderr []byte, err error) {
    97  	cli := client.New(&clientConfig)
    98  
    99  	cookie := os.Getenv("SNAP_COOKIE")
   100  	// for compatibility, if re-exec is not enabled and facing older snapd.
   101  	if cookie == "" {
   102  		cookie = os.Getenv("SNAP_CONTEXT")
   103  	}
   104  	return cli.RunSnapctl(&client.SnapCtlOptions{
   105  		ContextID: cookie,
   106  		Args:      os.Args[1:],
   107  	}, stdin)
   108  }