github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap-confine/user-support.c (about)

     1  /*
     2   * Copyright (C) 2015 Canonical Ltd
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License version 3 as
     6   * published by the Free Software Foundation.
     7   *
     8   * This program is distributed in the hope that it will be useful,
     9   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11   * GNU General Public License for more details.
    12   *
    13   * You should have received a copy of the GNU General Public License
    14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15   *
    16   */
    17  #include "config.h"
    18  #include "user-support.h"
    19  
    20  #include <errno.h>
    21  #include <stdlib.h>
    22  #include <sys/stat.h>
    23  
    24  #include "../libsnap-confine-private/string-utils.h"
    25  #include "../libsnap-confine-private/utils.h"
    26  
    27  void setup_user_data(void)
    28  {
    29  	const char *user_data = getenv("SNAP_USER_DATA");
    30  
    31  	if (user_data == NULL)
    32  		return;
    33  
    34  	// Only support absolute paths.
    35  	if (user_data[0] != '/') {
    36  		die("user data directory must be an absolute path");
    37  	}
    38  
    39  	debug("creating user data directory: %s", user_data);
    40  	if (sc_nonfatal_mkpath(user_data, 0755) < 0) {
    41  		if (errno == EROFS && !sc_startswith(user_data, "/home/")) {
    42  			// clear errno or it will be displayed in die()
    43  			errno = 0;
    44  			die("Sorry, home directories outside of /home are not currently supported. \nSee https://forum.snapcraft.io/t/11209 for details.");
    45  		}
    46  		die("cannot create user data directory: %s", user_data);
    47  	};
    48  }
    49  
    50  void setup_user_xdg_runtime_dir(void)
    51  {
    52  	const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
    53  
    54  	if (xdg_runtime_dir == NULL)
    55  		return;
    56  	// Only support absolute paths.
    57  	if (xdg_runtime_dir[0] != '/') {
    58  		die("XDG_RUNTIME_DIR must be an absolute path");
    59  	}
    60  
    61  	errno = 0;
    62  	debug("creating user XDG_RUNTIME_DIR directory: %s", xdg_runtime_dir);
    63  	if (sc_nonfatal_mkpath(xdg_runtime_dir, 0755) < 0) {
    64  		die("cannot create user XDG_RUNTIME_DIR directory: %s",
    65  		    xdg_runtime_dir);
    66  	}
    67  	// if successfully created the directory (ie, not EEXIST), then chmod it.
    68  	if (errno == 0 && chmod(xdg_runtime_dir, 0700) != 0) {
    69  		die("cannot change permissions of user XDG_RUNTIME_DIR directory to 0700");
    70  	}
    71  }