Skip to content

Commit

Permalink
fix(run): extract use-ghost-user into a util
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Jul 15, 2017
1 parent 1e03d8a commit 13f442c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
43 changes: 4 additions & 39 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const execa = require('execa');
const spawn = require('child_process').spawn;
const errors = require('../errors');
const Command = require('../command');
const shouldUseGhostUser = require('../utils/use-ghost-user');

class RunCommand extends Command {
run() {
Expand All @@ -22,44 +20,11 @@ class RunCommand extends Command {
instance.config.set('paths.contentPath', path.join(instance.dir, 'content')).save();
}

if (os.platform() !== 'linux') {
// platform isn't linux, we don't need to to anything
return this.useDirect(instance);
if (shouldUseGhostUser(path.join(instance.dir, 'content'))) {
return this.useGhostUser(instance);
}

let ghostuid, ghostgid;

try {
ghostuid = execa.shellSync('id -u ghost').stdout;
ghostgid = execa.shellSync('id -g ghost').stdout;
} catch (e) {
if (!e.message.match(/no such user/)) {
return Promise.reject(new errors.ProcessError(e));
}

// Ghost user doesn't exist, use direct
return this.useDirect(instance);
}

ghostuid = parseInt(ghostuid);
ghostgid = parseInt(ghostgid);

let stats = fs.lstatSync(path.join(instance.dir, 'content'));

if (stats.uid !== ghostuid && stats.gid !== ghostgid) {
// folder isn't owned by ghost user, use direct
return this.useDirect(instance);
}

let currentuid = process.getuid();

// This is important for systemd, as it will run this as the ghost user
if (currentuid === ghostuid) {
// current user is ghost, use direct
return this.useDirect(instance);
}

return this.useGhostUser(instance);
return this.useDirect(instance);
}

useGhostUser(instance) {
Expand Down
36 changes: 36 additions & 0 deletions lib/utils/use-ghost-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const fs = require('fs');
const os = require('os');
const execa = require('execa');

const errors = require('../errors');

module.exports = function useGhostUser(contentDir) {
if (os.platform() !== 'linux') {
return false;
}

let ghostuid, ghostgid;

try {
ghostuid = execa.shellSync('id -u ghost').stdout;
ghostgid = execa.shellSync('id -g ghost').stdout;
} catch (e) {
if (!e.message.match(/no such user/)) {
throw new errors.ProcessError(e);
}

return false;
}

ghostuid = parseInt(ghostuid);
ghostgid = parseInt(ghostgid);

let stats = fs.lstatSync(contentDir);

if (stats.uid !== ghostuid && stats.gid !== ghostgid) {
return false;
}

return process.getuid() !== ghostuid;
}

0 comments on commit 13f442c

Please sign in to comment.