<commit_before>/**
 * Copyright 2014 Netsend.
 *
 * This file is part of Mastersync.
 *
 * Mastersync is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * Mastersync is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with Mastersync. If not, see <https://www.gnu.org/licenses/>.
 */

'use strict';

if (process.getuid() !== 0) {
  console.error('execute as root');
  process.exit(1);
}

var net = require('net');
var fs = require('fs');

var async = require('async');
var chroot = require('chroot');
var LDJSONStream = require('ld-jsonstream');

var authRequest = require('./auth_request');

var programName = require('path').basename(__filename, '.js');

/**
 * Instantiate a server that listens to incoming auth requests.
 *
 * This module should be forked. Once initialized an "init" message is sent. After
 * this and exactly one message should be sent with the
 * following structure:
 *
 * {
 *   [serverConfig]: {}
 *   [chrootConfig]: {}
 * }
 *
 * serverConfig:
 * {
 *   [host]:         {String}     // defaults to 127.0.0.1
 *   [port]:         {Number}     // defaults to 2344
 *   [path]:         {String}     // defaults to /var/run/ms-2344.sock
 * }
 *
 * chrootConfig:
 * {
 *   [user]:         {String}     // defaults to "nobody"
 *   [newRoot]:      {String}     // defaults to /var/empty
 * }
 *
 * No other messages should be sent to this process.
 *
 * If the server is listening a "listen" message is sent. After this only new
 * incoming auth requests are sent from this process to it's parent.
 */

function connErrorHandler(conn, e) {
  console.error('%s: connection error: "%s"', programName, e);
  try {
    conn.write('invalid auth request\n');
    conn.destroy();
  } catch(err) {
    console.error('%s: connection write or disconnect error: "%s"', programName, err);
  }
}

// filter password out request
function debugReq(req) {
  var result = {};
  Object.keys(req).forEach(function(key) {
    if (key === 'password') {
      result.password = '*****';
    } else {
      result[key] = req[key];
    }
  });
  return JSON.stringify(result);
}

var conns = [];
function connHandler(conn) {
  console.log('%s: server connected', programName);

  conn.pipe(new LDJSONStream({ maxBytes: 1024 })).once('data', function(req) {
    console.log('%s: req received', programName, debugReq(req));

    if (!authRequest.valid(req)) {
      connErrorHandler(conn, 'invalid auth request');
      return;
    }

    // send auth request to parent for validation, with the connection socket
    process.send(req, conn);
  }).on('error', function(err) {
    connErrorHandler(conn, err);
  });

  conns.push(conn);

  conn.on('end', function() {
    console.log('%s: user disconnected', programName);<commit_msg>preauth: debug<commit_after>/**
 * Copyright 2014 Netsend.
 *
 * This file is part of Mastersync.
 *
 * Mastersync is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * Mastersync is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with Mastersync. If not, see <https://www.gnu.org/licenses/>.
 */

'use strict';

if (process.getuid() !== 0) {
  console.error('execute as root');
  process.exit(1);
}

var net = require('net');
var fs = require('fs');

var async = require('async');
var chroot = require('chroot');
var LDJSONStream = require('ld-jsonstream');

var authRequest = require('./auth_request');

var programName = require('path').basename(__filename, '.js');

/**
 * Instantiate a server that listens to incoming auth requests.
 *
 * This module should be forked. Once initialized an "init" message is sent. After
 * this and exactly one message should be sent with the
 * following structure:
 *
 * {
 *   [serverConfig]: {}
 *   [chrootConfig]: {}
 * }
 *
 * serverConfig:
 * {
 *   [host]:         {String}     // defaults to 127.0.0.1
 *   [port]:         {Number}     // defaults to 2344
 *   [path]:         {String}     // defaults to /var/run/ms-2344.sock
 * }
 *
 * chrootConfig:
 * {
 *   [user]:         {String}     // defaults to "nobody"
 *   [newRoot]:      {String}     // defaults to /var/empty
 * }
 *
 * No other messages should be sent to this process.
 *
 * If the server is listening a "listen" message is sent. After this only new
 * incoming auth requests are sent from this process to it's parent.
 */

function connErrorHandler(conn, e) {
  console.error('%s: connection error: "%s"', programName, e);
  try {
    conn.write('invalid auth request\n');
    conn.destroy();
  } catch(err) {
    console.error('%s: connection write or disconnect error: "%s"', programName, err);
  }
}

// filter password out request
function debugReq(req) {
  var result = {};
  Object.keys(req).forEach(function(key) {
    if (key === 'password') {
      result.password = '*****';
    } else {
      result[key] = req[key];
    }
  });
  return JSON.stringify(result);
}

var conns = [];
function connHandler(conn) {
  console.log('%s: client connected: %s', programName, JSON.stringify(conn.address()));

  conn.pipe(new LDJSONStream({ maxBytes: 1024 })).once('data', function(req) {
    console.log('%s: req received', programName, debugReq(req));

    if (!authRequest.valid(req)) {
      connErrorHandler(conn, 'invalid auth request');
      return;
    }

    // send auth request to parent for validation, with the connection socket
    process.send(req, conn);
  }).on('error', function(err) {
    connErrorHandler(conn, err);
  });

  conns.push(conn);

  conn.on('end', function() {
    console.log('%s: user disconnected', programName);
{'fraction_non_alphanumeric': 0.1385645337069905, 'fraction_numerical': 0.0077845243655612646, 'mean_word_length': 3.3114093959731545, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4076868', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1882, 'n_words': 660}