/usr/bin/fp-reg-decryptd is in flashproxy-facilitator 1.7-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #! /usr/bin/python
"""
Accepts encrypted client registrations and forwards them to the facilitator.
"""
import SocketServer
import getopt
import os
import socket
import sys
import threading
import time
from flashproxy import fac
from flashproxy import proc
from flashproxy.util import format_addr
from M2Crypto import RSA
# Generating an RSA keypair for use by this program:
# openssl genrsa -out /etc/flashproxy/reg-daemon.key 2048
# chmod 600 /etc/flashproxy/reg-daemon.key
LISTEN_ADDRESS = "127.0.0.1"
DEFAULT_LISTEN_PORT = 9003
FACILITATOR_ADDR = ("127.0.0.1", 9002)
DEFAULT_LOG_FILENAME = "fp-reg-decryptd.log"
# Don't indulge clients for more than this many seconds.
CLIENT_TIMEOUT = 1.0
# Buffer no more than this many bytes per connection.
MAX_LENGTH = 40 * 1024
LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
class options(object):
key_filename = None
listen_port = DEFAULT_LISTEN_PORT
log_filename = DEFAULT_LOG_FILENAME
log_file = sys.stdout
daemonize = True
pid_filename = None
privdrop_username = None
safe_logging = True
def usage(f = sys.stdout):
print >> f, """\
Usage: %(progname)s --key=KEYFILE
Facilitator-side daemon that reads base64-encoded encrypted client
registrations and registers them with a local facilitator. This program
exists on its own in order to isolate the reading of key material in a
single process.
-d, --debug don't daemonize, log to stdout.
-h, --help show this help.
-k, --key=KEYFILE read the private key from KEYFILE (required).
-l, --log FILENAME write log to FILENAME (default \"%(log)s\").
-p, --port PORT listen on PORT (default %(port)d).
--pidfile FILENAME write PID to FILENAME after daemonizing.
--privdrop-user USER switch UID and GID to those of USER.
--unsafe-logging don't scrub IP addresses from logs.\
""" % {
"progname": sys.argv[0],
"log": DEFAULT_LOG_FILENAME,
"port": DEFAULT_LISTEN_PORT,
}
def safe_str(s):
"""Return "[scrubbed]" if options.safe_logging is true, and s otherwise."""
if options.safe_logging:
return "[scrubbed]"
else:
return s
log_lock = threading.Lock()
def log(msg):
log_lock.acquire()
try:
print >> options.log_file, (u"%s %s" % (time.strftime(LOG_DATE_FORMAT), msg)).encode("UTF-8")
options.log_file.flush()
finally:
log_lock.release()
class Handler(SocketServer.StreamRequestHandler):
def __init__(self, *args, **kwargs):
self.deadline = time.time() + CLIENT_TIMEOUT
self.buffer = ""
SocketServer.StreamRequestHandler.__init__(self, *args, **kwargs)
def recv(self):
timeout = self.deadline - time.time()
self.connection.settimeout(timeout)
return self.connection.recv(1024)
def read_input(self):
while True:
data = self.recv()
if not data:
break
self.buffer += data
buflen = len(self.buffer)
if buflen > MAX_LENGTH:
raise socket.error("refusing to buffer %d bytes (last read was %d bytes)" % (buflen, len(data)))
return self.buffer
@proc.catch_epipe
def handle(self):
try:
b64_ciphertext = self.read_input()
except socket.error, e:
log("socket error reading input: %s" % str(e))
return
try:
ciphertext = b64_ciphertext.decode("base64")
plaintext = rsa.private_decrypt(ciphertext, RSA.pkcs1_oaep_padding)
for client_reg in fac.read_client_registrations(plaintext):
log(u"registering %s" % safe_str(format_addr(client_reg.addr)))
if not fac.put_reg(FACILITATOR_ADDR, client_reg.addr, client_reg.transport):
print >> self.wfile, "FAIL"
break
else:
print >> self.wfile, "OK"
except Exception, e:
log("error registering: %s" % str(e))
print >> self.wfile, "FAIL"
raise
finish = proc.catch_epipe(SocketServer.StreamRequestHandler.finish)
class Server(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
allow_reuse_address = True
def main():
global rsa
opts, args = getopt.gnu_getopt(sys.argv[1:], "dhk:l:p:",
["debug", "help", "key=", "log=", "port=", "pidfile=", "privdrop-user=", "unsafe-logging"])
for o, a in opts:
if o == "-d" or o == "--debug":
options.daemonize = False
options.log_filename = None
elif o == "-h" or o == "--help":
usage()
sys.exit()
elif o == "-k" or o == "--key":
options.key_filename = a
elif o == "-l" or o == "--log":
options.log_filename = a
elif o == "-p" or o == "--pass":
options.listen_port = int(a)
elif o == "--pidfile":
options.pid_filename = a
elif o == "--privdrop-user":
options.privdrop_username = a
elif o == "--unsafe-logging":
options.safe_logging = False
if len(args) != 0:
usage(sys.stderr)
sys.exit(1)
# Load the private key.
if options.key_filename is None:
print >> sys.stderr, "The --key option is required."
sys.exit(1)
try:
key_file = open(options.key_filename)
except Exception, e:
print >> sys.stderr, "Failed to open private key file \"%s\": %s." % (options.key_filename, str(e))
sys.exit(1)
try:
if not proc.check_perms(key_file.fileno()):
print >> sys.stderr, "Refusing to run with group- or world-readable private key file. Try"
print >> sys.stderr, "\tchmod 600 %s" % options.key_filename
sys.exit(1)
rsa = RSA.load_key_string(key_file.read())
finally:
key_file.close()
if options.log_filename:
options.log_file = open(options.log_filename, "a")
# Send error tracebacks to the log.
sys.stderr = options.log_file
else:
options.log_file = sys.stdout
addrinfo = socket.getaddrinfo(LISTEN_ADDRESS, options.listen_port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP)[0]
server = Server(addrinfo[4], Handler)
log(u"start on %s" % format_addr(addrinfo[4]))
if options.daemonize:
log(u"daemonizing")
pid = os.fork()
if pid != 0:
if options.pid_filename:
f = open(options.pid_filename, "w")
print >> f, pid
f.close()
sys.exit(0)
if options.privdrop_username is not None:
log(u"dropping privileges to those of user %s" % options.privdrop_username)
try:
proc.drop_privs(options.privdrop_username)
except BaseException, e:
print >> sys.stderr, "Can't drop privileges:", str(e)
sys.exit(1)
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)
if __name__ == "__main__":
main()
|