1#!/usr/bin/env python
2
3"""
4Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
5See the file 'LICENSE' for copying permission
6"""
7
8import sys
9
10sys.dont_write_bytecode = True
11
12__import__("lib.utils.versioncheck")  # this has to be the first non-standard import
13
14import logging
15import optparse
16import os
17import warnings
18
19warnings.filterwarnings(action="ignore", message=".*was already imported", category=UserWarning)
20warnings.filterwarnings(action="ignore", category=DeprecationWarning)
21
22from lib.core.common import getUnicode
23from lib.core.common import setPaths
24from lib.core.data import logger
25from lib.core.patch import dirtyPatches
26from lib.core.patch import resolveCrossReferences
27from lib.core.settings import RESTAPI_DEFAULT_ADAPTER
28from lib.core.settings import RESTAPI_DEFAULT_ADDRESS
29from lib.core.settings import RESTAPI_DEFAULT_PORT
30from lib.core.settings import UNICODE_ENCODING
31from lib.utils.api import client
32from lib.utils.api import server
33
34try:
35    from sqlmap import modulePath
36except ImportError:
37    def modulePath():
38        return getUnicode(os.path.dirname(os.path.realpath(__file__)), encoding=sys.getfilesystemencoding() or UNICODE_ENCODING)
39
40def main():
41    """
42    REST-JSON API main function
43    """
44
45    dirtyPatches()
46    resolveCrossReferences()
47
48    # Set default logging level to debug
49    logger.setLevel(logging.DEBUG)
50
51    # Initialize paths
52    setPaths(modulePath())
53
54    # Parse command line options
55    apiparser = optparse.OptionParser()
56    apiparser.add_option("-s", "--server", help="Run as a REST-JSON API server", default=RESTAPI_DEFAULT_PORT, action="store_true")
57    apiparser.add_option("-c", "--client", help="Run as a REST-JSON API client", default=RESTAPI_DEFAULT_PORT, action="store_true")
58    apiparser.add_option("-H", "--host", help="Host of the REST-JSON API server (default \"%s\")" % RESTAPI_DEFAULT_ADDRESS, default=RESTAPI_DEFAULT_ADDRESS, action="store")
59    apiparser.add_option("-p", "--port", help="Port of the the REST-JSON API server (default %d)" % RESTAPI_DEFAULT_PORT, default=RESTAPI_DEFAULT_PORT, type="int", action="store")
60    apiparser.add_option("--adapter", help="Server (bottle) adapter to use (default \"%s\")" % RESTAPI_DEFAULT_ADAPTER, default=RESTAPI_DEFAULT_ADAPTER, action="store")
61    apiparser.add_option("--username", help="Basic authentication username (optional)", action="store")
62    apiparser.add_option("--password", help="Basic authentication password (optional)", action="store")
63    (args, _) = apiparser.parse_args()
64
65    # Start the client or the server
66    if args.server is True:
67        server(args.host, args.port, adapter=args.adapter, username=args.username, password=args.password)
68    elif args.client is True:
69        client(args.host, args.port, username=args.username, password=args.password)
70    else:
71        apiparser.print_help()
72
73if __name__ == "__main__":
74    main()
75