2013-06-28 19:58:13 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import gettext
|
|
|
|
import ldap
|
|
|
|
import yaml
|
|
|
|
import json
|
2013-06-30 13:25:17 +02:00
|
|
|
from twisted.python.log import ILogObserver, FileLogObserver, startLogging
|
|
|
|
from twisted.python.logfile import DailyLogFile
|
2013-06-28 19:58:13 +02:00
|
|
|
from twisted.web.server import Site
|
2013-06-30 13:25:17 +02:00
|
|
|
from twisted.internet import reactor
|
|
|
|
from twisted.application import internet,service
|
2013-06-28 19:58:13 +02:00
|
|
|
from txrestapi.resource import APIResource
|
|
|
|
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate, win, parse_dict
|
|
|
|
|
|
|
|
if not __debug__:
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
gettext.install('YunoHost')
|
|
|
|
|
|
|
|
action_dict = {}
|
2013-06-30 13:25:17 +02:00
|
|
|
api = APIResource()
|
2013-06-28 19:58:13 +02:00
|
|
|
|
|
|
|
def http_exec(request):
|
|
|
|
global win
|
2013-06-30 13:25:17 +02:00
|
|
|
|
|
|
|
request.setHeader('Access-Control-Allow-Origin', '*') # Allow cross-domain requests
|
|
|
|
request.setHeader('Content-Type', 'application/json') # Return JSON anyway
|
|
|
|
|
|
|
|
# Return OK to 'OPTIONS' xhr requests
|
|
|
|
if request.method == 'OPTIONS':
|
|
|
|
request.setResponseCode(200, 'OK')
|
|
|
|
request.setHeader('Access-Control-Allow-Headers', 'Authorization')
|
|
|
|
return ''
|
|
|
|
|
|
|
|
# Simple HTTP auth
|
|
|
|
else:
|
|
|
|
authorized = request.getUser() == 'admin'
|
2013-06-30 13:57:23 +02:00
|
|
|
if authorized:
|
|
|
|
try: YunoHostLDAP(password=request.getPassword())
|
|
|
|
except YunoHostError: authorized = False
|
2013-06-30 13:25:17 +02:00
|
|
|
|
|
|
|
if not authorized:
|
|
|
|
request.setResponseCode(401, 'Unauthorized')
|
|
|
|
request.setHeader('Access-Control-Allow-Origin', '*')
|
2013-06-30 13:57:23 +02:00
|
|
|
request.setHeader('www-authenticate', 'Basic realm="Restricted Area"')
|
2013-06-30 13:25:17 +02:00
|
|
|
return 'Unauthorized'
|
|
|
|
|
|
|
|
# Sanitize arguments
|
2013-06-28 19:58:13 +02:00
|
|
|
dict = action_dict[request.method+' '+request.path]
|
2013-06-29 01:05:28 +02:00
|
|
|
if 'arguments' in dict: args = dict['arguments']
|
|
|
|
else: args = {}
|
2013-06-28 19:58:13 +02:00
|
|
|
for arg, params in args.items():
|
|
|
|
sanitized_key = arg.replace('-', '_')
|
|
|
|
if sanitized_key is not arg:
|
|
|
|
args[sanitized_key] = args[arg]
|
|
|
|
del args[arg]
|
|
|
|
arg = sanitized_key
|
|
|
|
if arg[0] == '_':
|
|
|
|
if 'nargs' not in params:
|
|
|
|
args[arg]['nargs'] = '*'
|
|
|
|
if 'full' in params:
|
|
|
|
new_key = params['full'][2:]
|
|
|
|
else:
|
|
|
|
new_key = arg[2:]
|
|
|
|
args[new_key] = args[arg]
|
|
|
|
del args[arg]
|
|
|
|
|
|
|
|
try:
|
2013-06-30 13:25:17 +02:00
|
|
|
|
|
|
|
# Validate arguments
|
2013-06-28 19:58:13 +02:00
|
|
|
validated_args = {}
|
|
|
|
for key, value in request.args.items():
|
|
|
|
if key in args:
|
|
|
|
# Validate args
|
2013-06-30 14:11:30 +02:00
|
|
|
if 'pattern' in args[key]:
|
|
|
|
validate(args[key]['pattern'], value)
|
|
|
|
if 'nargs' not in args[key] or ('nargs' != '*' and 'nargs' != '+'):
|
|
|
|
value = value[0]
|
|
|
|
if 'choices' in args[key] and value not in args[key]['choices']:
|
|
|
|
raise YunoHostError(22, _('Invalid argument') + ' ' + value)
|
2013-06-28 19:58:13 +02:00
|
|
|
if 'action' in args[key] and args[key]['action'] == 'store_true':
|
|
|
|
yes = ['true', 'True', 'yes', 'Yes']
|
|
|
|
value = value in yes
|
|
|
|
validated_args[key] = value
|
|
|
|
|
|
|
|
func = str_to_func(dict['function'])
|
2013-06-30 13:25:17 +02:00
|
|
|
|
|
|
|
# Execute requested function
|
2013-06-28 19:58:13 +02:00
|
|
|
with YunoHostLDAP(password=request.getPassword()):
|
|
|
|
result = func(**validated_args)
|
|
|
|
if result is None:
|
|
|
|
result = {}
|
|
|
|
if win:
|
|
|
|
result['win'] = win
|
|
|
|
win = []
|
2013-06-30 13:25:17 +02:00
|
|
|
|
|
|
|
# Build response
|
2013-06-29 01:05:28 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
request.setResponseCode(201, 'Created')
|
|
|
|
elif request.method == 'DELETE':
|
|
|
|
request.setResponseCode(204, 'No Content')
|
|
|
|
else:
|
|
|
|
request.setResponseCode(200, 'OK')
|
|
|
|
|
2013-06-28 19:58:13 +02:00
|
|
|
except YunoHostError, error:
|
2013-06-30 13:25:17 +02:00
|
|
|
|
|
|
|
# Set response code with function's raised code
|
2013-06-29 01:05:28 +02:00
|
|
|
server_errors = [1, 111, 169]
|
|
|
|
client_errors = [13, 17, 22, 87, 122, 125, 167, 168]
|
|
|
|
if error.code in client_errors:
|
|
|
|
request.setResponseCode(400, 'Bad Request')
|
|
|
|
else:
|
|
|
|
request.setResponseCode(500, 'Internal Server Error')
|
|
|
|
result = { 'error' : error.message }
|
|
|
|
|
2013-06-28 19:58:13 +02:00
|
|
|
return json.dumps(result)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global action_dict
|
2013-06-30 13:25:17 +02:00
|
|
|
global api
|
|
|
|
|
|
|
|
# Load & parse yaml file
|
2013-06-28 19:58:13 +02:00
|
|
|
with open('action_map.yml') as f:
|
|
|
|
action_map = yaml.load(f)
|
|
|
|
|
|
|
|
del action_map['general_arguments']
|
|
|
|
for category, category_params in action_map.items():
|
|
|
|
for action, action_params in category_params['actions'].items():
|
|
|
|
if 'help' not in action_params:
|
|
|
|
action_params['help'] = ''
|
|
|
|
if 'api' not in action_params:
|
|
|
|
action_params['api'] = 'GET /'+ category +'/'+ action
|
|
|
|
method, path = action_params['api'].split(' ')
|
2013-06-30 13:25:17 +02:00
|
|
|
# Register route
|
2013-06-28 19:58:13 +02:00
|
|
|
api.register(method, path, http_exec)
|
2013-06-30 13:25:17 +02:00
|
|
|
api.register('OPTIONS', path, http_exec)
|
2013-06-28 19:58:13 +02:00
|
|
|
action_dict[action_params['api']] = {
|
|
|
|
'function': 'yunohost_'+ category +'.'+ category +'_'+ action,
|
|
|
|
'help' : action_params['help']
|
|
|
|
}
|
|
|
|
if 'arguments' in action_params:
|
|
|
|
action_dict[action_params['api']]['arguments'] = action_params['arguments']
|
2013-06-30 13:25:17 +02:00
|
|
|
|
2013-06-28 19:58:13 +02:00
|
|
|
|
2013-06-30 13:25:17 +02:00
|
|
|
# Register only postinstall action if YunoHost isn't completely set up
|
2013-06-28 19:58:13 +02:00
|
|
|
try:
|
|
|
|
with open('/etc/yunohost/installed') as f: pass
|
|
|
|
except IOError:
|
2013-06-30 13:25:17 +02:00
|
|
|
api = APIResource()
|
|
|
|
api.register('POST', '/postinstall', http_exec)
|
|
|
|
api.register('OPTIONS', '/postinstall', http_exec)
|
|
|
|
action_dict['POST /postinstall'] = {
|
|
|
|
'function' : 'yunohost_tools.tools_postinstall',
|
|
|
|
'help' : 'Execute post-install',
|
|
|
|
'arguments' : action_map['tools']['postinstall']['arguments']
|
|
|
|
}
|
2013-06-28 19:58:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-06-30 13:30:23 +02:00
|
|
|
startLogging(open('/var/log/yunohost.log', 'a+')) # Log actions to API
|
2013-06-28 19:58:13 +02:00
|
|
|
main()
|
2013-06-30 13:30:23 +02:00
|
|
|
reactor.listenTCP(6767, Site(api, timeout=None))
|
2013-06-30 13:25:17 +02:00
|
|
|
reactor.run()
|
|
|
|
else:
|
|
|
|
application = service.Application("YunoHost API")
|
|
|
|
logfile = DailyLogFile("yunohost.log", "/var/log")
|
|
|
|
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
|
2013-06-30 13:30:23 +02:00
|
|
|
main()
|
2013-06-30 13:25:17 +02:00
|
|
|
internet.TCPServer(6767, Site(api, timeout=None)).setServiceParent(application)
|