|
1 |
| -exports.basicEnvironment = require('./basicEnvironment') |
2 |
| -exports.mountUsersFS = require('./mountUsersFS') |
3 |
| -exports.prepareSessions = require('./prepareSessions') |
| 1 | +const fs = require('fs') |
| 2 | + |
| 3 | +const each = require('async/each') |
| 4 | +const jocker = require('jocker') |
| 5 | +const rimraf = require('rimraf').sync |
| 6 | +const startRepl = require('nodeos-mount-utils').startRepl |
| 7 | + |
| 8 | +const jocker_root = require('./jocker_root') |
| 9 | + |
| 10 | + |
| 11 | +/** |
| 12 | + * Filter folders that are valid user `$HOME` |
| 13 | + * @access private |
| 14 | + * @param {String} user The name of the user |
| 15 | + * @return {Boolean} Returns true If the first char is not a dot |
| 16 | + * and not `root` and not ´lost+found´ |
| 17 | + */ |
| 18 | +function filterUser(user) |
| 19 | +{ |
| 20 | + return user[0] !== '.' && user !== 'root' && user !== 'lost+found' |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Overlays the users filesystem |
| 25 | + * |
| 26 | + * @param {String} usersFolder The path to folder of the users |
| 27 | + * @param {Function} callback |
| 28 | + */ |
| 29 | +function usersSessions(usersFolder, callback) |
| 30 | +{ |
| 31 | + function done(error) |
| 32 | + { |
| 33 | + // Remove the modules from initramfs to free memory |
| 34 | + // rimraf('/lib/node_modules') |
| 35 | + rimraf('/lib/node_modules/jocker') |
| 36 | + |
| 37 | + // Make '/usr' a opaque folder (OverlayFS feature) |
| 38 | + rimraf('/usr') |
| 39 | + |
| 40 | + callback(error) |
| 41 | + } |
| 42 | + |
| 43 | + // Mount users directories and exec their init files |
| 44 | + fs.readdir(usersFolder, function(error, users) |
| 45 | + { |
| 46 | + if(error) return done(error) |
| 47 | + |
| 48 | + each(users.filter(filterUser), function(username, callback) |
| 49 | + { |
| 50 | + jocker.run(usersFolder+'/'+username, '/init', {PATH: '/bin'}, callback) |
| 51 | + }, |
| 52 | + done) |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +// |
| 58 | +// Public API |
| 59 | +// |
| 60 | + |
| 61 | +/** |
| 62 | + * Prepares the session and checks if the users filesystem has a root account, |
| 63 | + * if not check if `/proc/cmdline` has the single key |
| 64 | + * It deletes the `root`, `rootfstype` and `vga` environment variables |
| 65 | + * and adds `NODE_PATH` to it. |
| 66 | + * @access private |
| 67 | + * @return {Repl} Returns either a repl or a error if the error contains |
| 68 | + * a `ENOENT` code |
| 69 | + */ |
| 70 | +function prepareSessions(home, single, callback) |
| 71 | +{ |
| 72 | + const upperdir = home+'/root' |
| 73 | + |
| 74 | + // Check if users filesystem has an administrator account |
| 75 | + fs.readdir(upperdir, function(error) |
| 76 | + { |
| 77 | + if(error) |
| 78 | + { |
| 79 | + if(error.code !== 'ENOENT') return callback(error) |
| 80 | + |
| 81 | + return usersSessions(home, callback) |
| 82 | + } |
| 83 | + |
| 84 | + // There's an administrator account, prepare it first |
| 85 | + jocker_root.create(upperdir, function(error, newHome) |
| 86 | + { |
| 87 | + if(error) return callback(error) |
| 88 | + |
| 89 | + // Enter administrator mode |
| 90 | + if(single) return startRepl('Administrator mode') |
| 91 | + |
| 92 | + // Execute `root` user init in un-priviledged environment |
| 93 | + jocker.exec(home, '/init', {PATH: '/bin'}, function(error) |
| 94 | + { |
| 95 | + if(error) console.warn(error) |
| 96 | + |
| 97 | + usersSessions(newHome, callback) |
| 98 | + }) |
| 99 | + }) |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +module.exports = prepareSessions |
0 commit comments