summaryrefslogtreecommitdiff
path: root/src/leap/bonafide/bonafide_cli2
blob: bf05a995e2a76a82b7e13a1ec36e7bda10510ef1 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# bonafide_cli2.py
# Copyright (C) 2015 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Bonafide command line interface: zmq client.
"""
import sys
import getpass
import argparse

from colorama import init as color_init
from colorama import Fore
from twisted.internet import reactor
from txzmq import ZmqEndpoint, ZmqFactory, ZmqREQConnection
import zmq

from leap.bonafide import config

description = (Fore.YELLOW + 'Manage and configure a LEAP Account '
               'using the bonafide protocol. This client connects to '
               'a running Bonafide service.' + Fore.RESET)

parser = argparse.ArgumentParser(description=description)
parser.add_argument("--stats", dest="do_stats", action="store_true",
                    help="print service stats")
parser.add_argument("--signup", action="store_true", dest="do_signup",
                    help="signup new user")
parser.add_argument("--auth", dest="do_auth", action="store_true",
                    help="authenticate the passed user")
parser.add_argument("--logout", dest="do_logout", action="store_true",
                    help="logout this user")
parser.add_argument("--username", dest="username",
                    help="user to operate with")
parser.add_argument("--shutdown", dest="do_shutdown", action="store_true",
                    help="shutdown the bonafide service.")

# XXX DEBUG --------------------------------------------------------
parser.add_argument("--debug", dest="do_debug", action="store_true",
                    help="debug command, can be anything")
# ------------------------------------------------------------------
ns = parser.parse_args()


def get_zmq_connection():
    zf = ZmqFactory()
    e = ZmqEndpoint('connect', config.ENDPOINT)
    return ZmqREQConnection(zf, e)


def error(msg):
    print Fore.RED + "[!] %s" % msg + Fore.RESET
    sys.exit(1)

if len(sys.argv) < 2:
    error("Too few arguments. Try %s --help" % sys.argv[0])


if (ns.do_signup or ns.do_auth or ns.do_logout) and not ns.username:
    error(Fore.RED + "Need to pass a username for signup/auth/logout" +
          Fore.RESET)

if ns.username and '@' not in ns.username:
    error(Fore.RED + "Username must be in the form user@provider" + Fore.RESET)


def do_print(stuff):
    print Fore.GREEN + stuff[0] + Fore.RESET


def send_command():

    cb = do_print
    if ns.do_shutdown:
        data = ("shutdown",)

    elif ns.do_stats:
        data = ("stats",)

    elif ns.do_signup:
        passwd = getpass.getpass()
        data = ("signup", ns.username, passwd)

    elif ns.do_auth:
        passwd = getpass.getpass()
        data = ("authenticate", ns.username, passwd)

    elif ns.do_logout:
        passwd = getpass.getpass()
        data = ("logout", ns.username, passwd)

    elif ns.do_debug:
        data = ("get_soledad",)

    s = get_zmq_connection()
    try:
        d = s.sendMsg(*data)
    except zmq.error.Again:
        print Fore.RED + "[ERROR] Server is down" + Fore.RESET
    d.addCallback(cb)
    d.addCallback(lambda x: reactor.stop())


if __name__ == "__main__":
    color_init()
    reactor.callWhenRunning(reactor.callLater, 0, send_command)
    reactor.run()