shared.vim 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60b5eda1a082207e5112700dac63a520fb9ea4fbc26a57d02fedf8088215db17
4
- data.tar.gz: 3d27db402b9c7157b67e8cdcf73fcde9710b5475a35ae6d8fc22d2f893617d42
3
+ metadata.gz: 8e8d3fc66493fe75703ff74762e6eab72d6f44b545500d6cd51a0fa037ed3425
4
+ data.tar.gz: f5788ba62e02a0e198aca97891d08e9e5faa891e9d8625316e3637ead6b5d166
5
5
  SHA512:
6
- metadata.gz: 194cb1926a4e810fb2f25f4eeaf71b621341dbef1b4da3840cb5b67cc6d06a2d335e1b74cf423d2a8c41a2b8823ce96485e40f9d3b8629651a4683659b15a9c3
7
- data.tar.gz: f22d2585d7df348d932165b5f6677f5c88143b222b78da9539dc3034ba03ca1ecc2c957412e8b3cafca01b0b113627929df6b778d7291b4bb3c0d97d1ddec8e1
6
+ metadata.gz: 682372e72800850377c6e440c1165f0e7ecaffcbaf1a86b92bbb6c0691096e58f559bfe1041e36d9e9f0b2935a082ccadf9c7d0c08148059e504b1783e6fc03f
7
+ data.tar.gz: 119fcb45b570c6c4d295f69754bd81bee9e4e08ec8ac865430215c68c0f0e58711072c30005d6fe36270bac6e60b4556a76bf23e54f423d9fe2fb9f7b26cf6ec
data/README.md CHANGED
@@ -12,7 +12,8 @@ or start.
12
12
 
13
13
  * A single Ruby server on the Unix socket `/tmp/shared-vim/sock`, shared by all
14
14
  local users. The first Vim that needs it starts it detached; it exits 60 s
15
- after the last Vim has disconnected.
15
+ after the last Vim has disconnected. Or systemd runs it permanently, see
16
+ [systemd](#systemd).
16
17
  * Every real file buffer joins automatically. Names are `$USERNAME` (else
17
18
  `$USER`) + a number per user per file: `alice1`, `bob1`, `alice2` (same
18
19
  user twice is fine). Set `USERNAME` when several people share an account.
@@ -89,6 +90,40 @@ let g:shared_vim_socket = '/other/path/sock' " max ~100 chars
89
90
  let g:shared_vim_max_size = 10000000 " bytes; bigger files are not shared
90
91
  ```
91
92
 
93
+ With a socket of your own you get your own private, trusted circle:
94
+
95
+ ```vim
96
+ let g:shared_vim_socket = '/home/user/.shared.vim.sock'
97
+ ```
98
+
99
+ It is shared by whatever users can read/write there (e.g. group members), and
100
+ by nobody else: connecting needs search permission on the directory, starting
101
+ the server (when none runs) also write permission, since it creates the
102
+ socket there. Everybody in the circle sets the same path. A Vim uses one
103
+ socket, so all its files go to that server.
104
+
105
+ ### systemd
106
+
107
+ `contrib/shared-vim.service` is an example unit that runs one server for all
108
+ users permanently (`shared-vim-server --persistent`: no idle exit, no lock
109
+ file, log to stderr, i.e. `journalctl -u shared-vim`). It runs as an
110
+ unprivileged user of its own (`DynamicUser=yes`) on `/run/shared-vim/sock`;
111
+ Vims use that socket when it exists, else `/tmp/shared-vim/sock`. Started at
112
+ boot, it owns the socket before any user could put something else there. It
113
+ needs a system-wide gem install, since the service user cannot read gems in
114
+ home directories:
115
+
116
+ ```sh
117
+ sudo gem install shared.vim
118
+ sudo cp contrib/shared-vim.service /etc/systemd/system/ # adapt ExecStart if needed
119
+ sudo systemctl daemon-reload
120
+ sudo systemctl enable --now shared-vim
121
+ ```
122
+
123
+ `:SharedRestart` still works: the server exits and systemd starts it again,
124
+ with the newest installed version. Permission checks work as far as that
125
+ user can see the files (not inside private home directories, see Limits).
126
+
92
127
  Commands:
93
128
 
94
129
  | command | |
@@ -103,6 +138,7 @@ Commands:
103
138
  |---|---|
104
139
  | `tools/shared-vim-server` | the server (`IO.select` loop, one JSON object per line) |
105
140
  | `tools/shared-vim-install` | installs the stub plugin that loads the gem |
141
+ | `contrib/shared-vim.service` | example systemd unit for a permanent server |
106
142
  | `lib/shared_vim_core.rb` | line model, ops, transform, client state, undo stacks |
107
143
  | `lib/shared_vim.rb` | Vim side in Ruby: listener, buffer patching, colors, status |
108
144
  | `plugin/shared.vim` | autocmds, channel, mappings, swap/timestamp handling |
@@ -191,7 +227,17 @@ If you need editing over a network, or Neovim, one of these may fit better:
191
227
  * After undoing back to the saved text, the buffer still shows `[+]`.
192
228
  * Unsaved shared state lives only while somebody has the file open, and colors
193
229
  of unsaved text are lost when the server restarts.
194
- * No authentication: any local user can connect to the socket.
230
+ * Permissions: the server only lets a user join a file they may read (or, if
231
+ it does not exist yet, create), judged by the uid of the connection and the
232
+ mode bits (ACLs are ignored). Where the server cannot tell (e.g. a private
233
+ home directory of another account than the server's), only the same user
234
+ as everybody already in it may join. Whoever joined may edit, also without
235
+ write permission: the edits reach the Vims of those who can save them.
236
+ * The server itself has to be trusted. It runs as whoever started it first,
237
+ and a local user can put a program of their own on the socket instead,
238
+ which then sees every file any Vim opens. If you are afraid, or there are
239
+ users on your server that you don't trust, go the systemctl route (see
240
+ [systemd](#systemd)).
195
241
  * The size limit (`g:shared_vim_max_size`) is checked only by each Vim; the
196
242
  server accepts files of any size.
197
243
  * Edits that LSP plugins or other scripts make are shared like your own; text
@@ -0,0 +1,29 @@
1
+ # Example systemd unit: one persistent shared.vim server for all local users,
2
+ # started at boot, so the socket belongs to it before any user could take it.
3
+ #
4
+ # Needs a system-wide install (sudo gem install shared.vim): the service user
5
+ # cannot read gems in home directories. Adapt ExecStart if `gem` puts the
6
+ # executable elsewhere (see `gem environment`, EXECUTABLE DIRECTORY). Then:
7
+ #
8
+ # sudo cp shared-vim.service /etc/systemd/system/
9
+ # sudo systemctl daemon-reload
10
+ # sudo systemctl enable --now shared-vim
11
+ #
12
+ # Vims use /run/shared-vim/sock when it exists (else /tmp/shared-vim/sock).
13
+ # Log: journalctl -u shared-vim. :SharedRestart stops the server, systemd
14
+ # starts it again, with the newest installed gem version.
15
+
16
+ [Unit]
17
+ Description=shared.vim server (live co-editing for Vim)
18
+
19
+ [Service]
20
+ ExecStart=/usr/local/bin/shared-vim-server --persistent /run/shared-vim/sock
21
+ # An unprivileged user of its own. It implies a private /tmp, hence /run.
22
+ DynamicUser=yes
23
+ RuntimeDirectory=shared-vim
24
+ RuntimeDirectoryMode=0755
25
+ Restart=always
26
+ RestartSec=0.2
27
+
28
+ [Install]
29
+ WantedBy=multi-user.target
data/lib/shared_vim.rb CHANGED
@@ -213,6 +213,11 @@ module SharedVim
213
213
  m = JSON.parse(u(line))
214
214
  b = @paths[m['path']] or return
215
215
  s = @bufs[b]
216
+ if m['t'] == 'denied'
217
+ @bufs.delete(b)
218
+ @paths.delete(s.path)
219
+ return VIM.message("shared.vim: #{File.basename(s.path)} not shared, no permission")
220
+ end
216
221
  case m['t']
217
222
  when 'joined' then joined(s, m)
218
223
  when 'ack'
@@ -23,6 +23,8 @@
23
23
  # String insert it
24
24
  # Positions are characters, not bytes; "\n" counts as one.
25
25
 
26
+ require 'etc'
27
+
26
28
  module SharedVimCore
27
29
  module OT
28
30
  module_function
@@ -254,6 +256,47 @@ module SharedVimCore
254
256
  end
255
257
  end
256
258
 
259
+ # File permissions of some user, as the server sees them: the mode bits of
260
+ # owner, group (incl. supplementary groups) and others. ACLs are ignored.
261
+ module Access
262
+ module_function
263
+
264
+ # May uid read path (an existing file) or create it (a new one)? Every
265
+ # directory on the way must be searchable. nil if this process cannot
266
+ # tell, e.g. a file in a private home directory of another account.
267
+ def allowed?(uid, path)
268
+ return true if uid == 0
269
+ dir = File.dirname(path)
270
+ dirs = [dir]
271
+ dirs << (dir = File.dirname(dir)) until dir == '/'
272
+ return false unless dirs.all? { |d| can?(uid, File.stat(d), 1) }
273
+ if File.exist?(path) then can?(uid, File.stat(path), 4)
274
+ else can?(uid, File.stat(File.dirname(path)), 3)
275
+ end
276
+ rescue SystemCallError
277
+ nil
278
+ end
279
+
280
+ # bits: 4 read, 2 write, 1 search. As in Unix, only the first class that
281
+ # applies counts: an owner without read permission may not read.
282
+ def can?(uid, st, bits)
283
+ shift = if st.uid == uid then 6
284
+ elsif groups(uid).include?(st.gid) then 3
285
+ else 0
286
+ end
287
+ (st.mode >> shift) & bits == bits
288
+ end
289
+
290
+ def groups(uid)
291
+ (@groups ||= {})[uid] ||= begin
292
+ pw = Etc.getpwuid(uid)
293
+ [pw.gid] + Etc.enum_for(:group).select { |g| g.mem.include?(pw.name) }.map(&:gid)
294
+ rescue ArgumentError
295
+ []
296
+ end
297
+ end
298
+ end
299
+
257
300
  # Client side of one shared document: one op in flight, the rest queued.
258
301
  # undo/redo: [key, ops] groups, newest first. All their ops form one
259
302
  # sequence starting at the current text, so every op that is not recorded
data/plugin/shared.vim CHANGED
@@ -20,7 +20,9 @@ endif
20
20
  let g:loaded_shared_vim = 1
21
21
 
22
22
  let g:shared_vim_root = expand('<sfile>:p:h:h')
23
- let s:sock = get(g:, 'shared_vim_socket', '/tmp/shared-vim/sock')
23
+ " /run/shared-vim/sock: a server run by systemd (contrib/shared-vim.service)
24
+ let s:sock = get(g:, 'shared_vim_socket',
25
+ \ getftype('/run/shared-vim/sock') ==# 'socket' ? '/run/shared-vim/sock' : '/tmp/shared-vim/sock')
24
26
  let s:server = g:shared_vim_root . '/tools/shared-vim-server'
25
27
  let g:shared_vim_ch = v:none
26
28
  execute 'rubyfile ' . fnameescape(g:shared_vim_root . '/lib/shared_vim.rb')
data/shared.vim.gemspec CHANGED
@@ -1,13 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "shared.vim"
3
- s.version = "1.1.0"
3
+ s.version = "1.1.2"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.license = "GPL-3.0-or-later"
6
6
  s.summary = "shared.vim - Live co-editing in Vim, for everybody on the same computer."
7
7
 
8
8
  s.description = "see https://github.com/etm/shared.vim"
9
9
 
10
- s.files = Dir['{plugin/**/*,lib/**/*,tools/**/*,test/**/*}'] + %w(LICENSE Rakefile shared.vim.gemspec README.md AUTHORS)
10
+ s.files = Dir['{plugin/**/*,lib/**/*,tools/**/*,test/**/*,contrib/**/*}'] + %w(LICENSE Rakefile shared.vim.gemspec README.md AUTHORS)
11
11
  s.require_path = 'lib'
12
12
  s.extra_rdoc_files = ['README.md']
13
13
  s.bindir = 'tools'
data/test/test.rb CHANGED
@@ -228,4 +228,30 @@ raise "neighbour kept: #{doc(a.text).inspect}" unless doc(a.text) == "foo bar\n"
228
228
  raise 'xform' unless OT.xform_index([2, 'XY', 3], 3) == 5
229
229
  raise 'xform' unless OT.xform_index([1, -2, 2], 2) == 1
230
230
 
231
+ # 7. file permissions of another user (nobody), from the mode bits
232
+ require 'tmpdir'
233
+ nobody = (Etc.getpwnam('nobody').uid rescue nil)
234
+ if nobody && nobody != Process.uid
235
+ Dir.mktmpdir('shared-vim-test', '/tmp') do |dir|
236
+ File.chmod(0o755, dir)
237
+ f = File.join(dir, 'f')
238
+ File.write(f, 'x')
239
+ File.chmod(0o644, f)
240
+ raise 'access: readable for others' unless Access.allowed?(nobody, f)
241
+ File.chmod(0o600, f)
242
+ raise 'access: private file' if Access.allowed?(nobody, f)
243
+ raise 'access: own private file' unless Access.allowed?(Process.uid, f)
244
+ raise 'access: new file in foreign dir' if Access.allowed?(nobody, File.join(dir, 'new'))
245
+ File.chmod(0o777, dir)
246
+ raise 'access: new file in open dir' unless Access.allowed?(nobody, File.join(dir, 'new'))
247
+ File.chmod(0o644, f)
248
+ File.chmod(0o700, dir)
249
+ raise 'access: dir not searchable' if Access.allowed?(nobody, f)
250
+ File.chmod(0o200, f)
251
+ raise 'access: owner without read' if Access.allowed?(Process.uid, f)
252
+ end
253
+ else
254
+ puts 'skipped permission test (no user nobody)'
255
+ end
256
+
231
257
  puts 'test ok'
@@ -15,10 +15,12 @@
15
15
  # <http://www.gnu.org/licenses/>.
16
16
 
17
17
  # shared.vim server: one process for all users, on a Unix domain socket.
18
- # Usage: shared-vim-server [socket] (default /tmp/shared-vim/sock)
18
+ # Usage: shared-vim-server [--persistent] [socket] (default /tmp/shared-vim/sock)
19
19
  # Protocol: one JSON object per line, see README.md.
20
20
  # Ops not based on the current revision are rejected (nack); the client
21
- # transforms and resends them. Exits after IDLE seconds without connections.
21
+ # transforms and resends them. Exits after IDLE seconds without connections,
22
+ # unless --persistent: for a service manager (see contrib/shared-vim.service),
23
+ # which also keeps it single and takes its log from stderr.
22
24
 
23
25
  require 'socket'
24
26
  require 'json'
@@ -26,6 +28,8 @@ require_relative '../lib/shared_vim_core'
26
28
 
27
29
  Text = SharedVimCore::Text
28
30
  OT = SharedVimCore::OT
31
+ Access = SharedVimCore::Access
32
+ PERSISTENT = !ARGV.delete('--persistent').nil?
29
33
  SOCK = ARGV[0] || '/tmp/shared-vim/sock'
30
34
  DIR = File.dirname(SOCK)
31
35
 
@@ -34,14 +38,18 @@ begin
34
38
  File.chmod(01777, DIR)
35
39
  rescue SystemCallError
36
40
  end
37
- lock = File.open(File.join(DIR, 'lock'), File::RDWR | File::CREAT, 0666)
38
- File.chmod(0666, lock.path) rescue nil
39
- exit 0 unless lock.flock(File::LOCK_EX | File::LOCK_NB) # another server runs
40
- begin
41
- $stderr.reopen(File.join(DIR, "server-#{Process.uid}.log"), 'a')
42
- $stderr.sync = true
43
- rescue SystemCallError
41
+ # The lock is open to everybody, so anybody could hold it: not used when a
42
+ # service manager keeps the server single.
43
+ unless PERSISTENT
44
+ lock = File.open(File.join(DIR, 'lock'), File::RDWR | File::CREAT, 0666)
45
+ File.chmod(0666, lock.path) rescue nil
46
+ exit 0 unless lock.flock(File::LOCK_EX | File::LOCK_NB) # another server runs
47
+ begin
48
+ $stderr.reopen(File.join(DIR, "server-#{Process.uid}.log"), 'a')
49
+ rescue SystemCallError
50
+ end
44
51
  end
52
+ $stderr.sync = true
45
53
 
46
54
  File.unlink(SOCK) if File.socket?(SOCK)
47
55
  SERVER = UNIXServer.new(SOCK)
@@ -55,6 +63,7 @@ Doc = Struct.new(:text, :rev, :members)
55
63
  IDLE = 60 # seconds without any connection before the server exits
56
64
  DOCS = {} # path => Doc
57
65
  INBUF = {} # io => String (binary)
66
+ UIDS = {} # io => uid of the connected user
58
67
 
59
68
  def log(msg) = $stderr.puts("#{Time.now.strftime('%F %T')} #{msg}")
60
69
 
@@ -84,6 +93,15 @@ def handle(io, m)
84
93
  case m['t']
85
94
  when 'join'
86
95
  return if me
96
+ # Only who may read the file (or create it) gets its text. If the server
97
+ # cannot tell, only the same user as everybody already in it.
98
+ uid = UIDS[io]
99
+ ok = Access.allowed?(uid, path)
100
+ ok = !d || d.members.all? { |x| UIDS[x.io] == uid } if ok.nil?
101
+ unless ok
102
+ log "denied #{path} for uid #{uid}"
103
+ return out(io, t: 'denied', path: path)
104
+ end
87
105
  created = d.nil?
88
106
  d = DOCS[path] ||= Doc.new(Text.new(m['lines']), 0, [])
89
107
  slots = d.members.map(&:slot)
@@ -126,12 +144,13 @@ end
126
144
  def drop(io)
127
145
  DOCS.keys.each { |p| leave(io, p) }
128
146
  INBUF.delete(io)
147
+ UIDS.delete(io)
129
148
  io.close rescue nil
130
149
  end
131
150
 
132
- log "listening on #{SOCK}"
151
+ log "listening on #{SOCK}#{' (persistent)' if PERSISTENT}"
133
152
  loop do
134
- ready, = IO.select([SERVER, *INBUF.keys], nil, nil, INBUF.empty? ? IDLE : nil)
153
+ ready, = IO.select([SERVER, *INBUF.keys], nil, nil, INBUF.empty? && !PERSISTENT ? IDLE : nil)
135
154
  unless ready
136
155
  # a Vim connecting right now sees its channel close and starts a new server
137
156
  File.unlink(SOCK) rescue nil
@@ -141,7 +160,10 @@ loop do
141
160
  ready.each do |io|
142
161
  if io == SERVER
143
162
  c = SERVER.accept_nonblock(exception: false)
144
- INBUF[c] = ''.b if c.is_a?(UNIXSocket)
163
+ if c.is_a?(UNIXSocket)
164
+ INBUF[c] = ''.b
165
+ UIDS[c] = c.getpeereid[0]
166
+ end
145
167
  next
146
168
  end
147
169
  begin
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shared.vim
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juergen eTM Mangler
@@ -22,6 +22,7 @@ files:
22
22
  - LICENSE
23
23
  - README.md
24
24
  - Rakefile
25
+ - contrib/shared-vim.service
25
26
  - lib/shared_vim.rb
26
27
  - lib/shared_vim_core.rb
27
28
  - plugin/shared.vim