Show More
@@ -1,136 +1,136 b'' | |||
|
1 | 1 | // Copyright 2011, 2018 Yuya Nishihara <yuya@tcha.org> |
|
2 | 2 | // |
|
3 | 3 | // This software may be used and distributed according to the terms of the |
|
4 | 4 | // GNU General Public License version 2 or any later version. |
|
5 | 5 | |
|
6 | 6 | //! Utility for locating command-server process. |
|
7 | 7 | |
|
8 | 8 | use std::env; |
|
9 | 9 | use std::ffi::{OsStr, OsString}; |
|
10 | 10 | use std::fs::{self, DirBuilder}; |
|
11 | 11 | use std::io; |
|
12 | 12 | use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
|
13 | 13 | use std::os::unix::fs::{DirBuilderExt, MetadataExt}; |
|
14 | 14 | use std::path::{Path, PathBuf}; |
|
15 | 15 | use std::process; |
|
16 | 16 | use std::time::Duration; |
|
17 | 17 | |
|
18 | 18 | use super::procutil; |
|
19 | 19 | |
|
20 | 20 | /// Helper to connect to and spawn a server process. |
|
21 | 21 | #[derive(Clone, Debug)] |
|
22 | 22 | pub struct Locator { |
|
23 | 23 | hg_command: OsString, |
|
24 | 24 | current_dir: PathBuf, |
|
25 | 25 | env_vars: Vec<(OsString, OsString)>, |
|
26 | 26 | process_id: u32, |
|
27 | 27 | base_sock_path: PathBuf, |
|
28 | 28 | timeout: Duration, |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | impl Locator { |
|
32 | 32 | /// Creates locator capturing the current process environment. |
|
33 | 33 | /// |
|
34 | 34 | /// If no `$CHGSOCKNAME` is specified, the socket directory will be |
|
35 | 35 | /// created as necessary. |
|
36 | 36 | pub fn prepare_from_env() -> io::Result<Locator> { |
|
37 | 37 | Ok(Locator { |
|
38 | 38 | hg_command: default_hg_command(), |
|
39 | 39 | current_dir: env::current_dir()?, |
|
40 | 40 | env_vars: env::vars_os().collect(), |
|
41 | 41 | process_id: process::id(), |
|
42 | 42 | base_sock_path: prepare_server_socket_path()?, |
|
43 | 43 | timeout: default_timeout(), |
|
44 | 44 | }) |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | /// Temporary socket path for this client process. |
|
48 | 48 | fn temp_sock_path(&self) -> PathBuf { |
|
49 | 49 | let src = self.base_sock_path.as_os_str().as_bytes(); |
|
50 | let mut buf = Vec::with_capacity(src.len() + 6); | |
|
50 | let mut buf = Vec::with_capacity(src.len() + 6); // "{src}.{pid}".len() | |
|
51 | 51 | buf.extend_from_slice(src); |
|
52 | 52 | buf.extend_from_slice(format!(".{}", self.process_id).as_bytes()); |
|
53 | 53 | OsString::from_vec(buf).into() |
|
54 | 54 | } |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | /// Determines the server socket to connect to. |
|
58 | 58 | /// |
|
59 | 59 | /// If no `$CHGSOCKNAME` is specified, the socket directory will be created |
|
60 | 60 | /// as necessary. |
|
61 | 61 | pub fn prepare_server_socket_path() -> io::Result<PathBuf> { |
|
62 | 62 | if let Some(s) = env::var_os("CHGSOCKNAME") { |
|
63 | 63 | Ok(PathBuf::from(s)) |
|
64 | 64 | } else { |
|
65 | 65 | let mut path = default_server_socket_dir(); |
|
66 | 66 | create_secure_dir(&path)?; |
|
67 | 67 | path.push("server"); |
|
68 | 68 | Ok(path) |
|
69 | 69 | } |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | /// Determines the default server socket path as follows. |
|
73 | 73 | /// |
|
74 | 74 | /// 1. `$XDG_RUNTIME_DIR/chg` |
|
75 | 75 | /// 2. `$TMPDIR/chg$UID` |
|
76 | 76 | /// 3. `/tmp/chg$UID` |
|
77 | 77 | pub fn default_server_socket_dir() -> PathBuf { |
|
78 | 78 | // XDG_RUNTIME_DIR should be ignored if it has an insufficient permission. |
|
79 | 79 | // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
|
80 | 80 | if let Some(Ok(s)) = env::var_os("XDG_RUNTIME_DIR").map(check_secure_dir) { |
|
81 | 81 | let mut path = PathBuf::from(s); |
|
82 | 82 | path.push("chg"); |
|
83 | 83 | path |
|
84 | 84 | } else { |
|
85 | 85 | let mut path = env::temp_dir(); |
|
86 | 86 | path.push(format!("chg{}", procutil::get_effective_uid())); |
|
87 | 87 | path |
|
88 | 88 | } |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | /// Determines the default hg command. |
|
92 | 92 | pub fn default_hg_command() -> OsString { |
|
93 | 93 | // TODO: maybe allow embedding the path at compile time (or load from hgrc) |
|
94 | 94 | env::var_os("CHGHG") |
|
95 | 95 | .or(env::var_os("HG")) |
|
96 | 96 | .unwrap_or(OsStr::new("hg").to_owned()) |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | fn default_timeout() -> Duration { |
|
100 | 100 | let secs = env::var("CHGTIMEOUT") |
|
101 | 101 | .ok() |
|
102 | 102 | .and_then(|s| s.parse().ok()) |
|
103 | 103 | .unwrap_or(60); |
|
104 | 104 | Duration::from_secs(secs) |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | /// Creates a directory which the other users cannot access to. |
|
108 | 108 | /// |
|
109 | 109 | /// If the directory already exists, tests its permission. |
|
110 | 110 | fn create_secure_dir<P>(path: P) -> io::Result<()> |
|
111 | 111 | where |
|
112 | 112 | P: AsRef<Path>, |
|
113 | 113 | { |
|
114 | 114 | DirBuilder::new() |
|
115 | 115 | .mode(0o700) |
|
116 | 116 | .create(path.as_ref()) |
|
117 | 117 | .or_else(|err| { |
|
118 | 118 | if err.kind() == io::ErrorKind::AlreadyExists { |
|
119 | 119 | check_secure_dir(path).map(|_| ()) |
|
120 | 120 | } else { |
|
121 | 121 | Err(err) |
|
122 | 122 | } |
|
123 | 123 | }) |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | fn check_secure_dir<P>(path: P) -> io::Result<P> |
|
127 | 127 | where |
|
128 | 128 | P: AsRef<Path>, |
|
129 | 129 | { |
|
130 | 130 | let a = fs::symlink_metadata(path.as_ref())?; |
|
131 | 131 | if a.is_dir() && a.uid() == procutil::get_effective_uid() && (a.mode() & 0o777) == 0o700 { |
|
132 | 132 | Ok(path) |
|
133 | 133 | } else { |
|
134 | 134 | Err(io::Error::new(io::ErrorKind::Other, "insecure directory")) |
|
135 | 135 | } |
|
136 | 136 | } |
General Comments 0
You need to be logged in to leave comments.
Login now