diff --git a/action_map.yml b/action_map.yml index b9f23deb..897399ed 100644 --- a/action_map.yml +++ b/action_map.yml @@ -638,3 +638,29 @@ tools: help: Subscribe domain to a DynDNS service action: store_true + ### tools_lemonrule() + lemonrule: + action_help: Add/remove LemonLDAP location rule + api: PUT /lemonrule + arguments: + -i: + full: --id + help: ID to attribute to the rule + -u: + full: --url + help: URL to apply to the rule + -v: + full: --value + help: Value of the rule to write to the configuration file + -p: + full: --priority + help: Priority of the rule + -d: + full: --delete + help: Delete the rule + action: store_true + -a: + full: --apply + help: Apply the LemonLDAP configuration by reloading Apache + action: store_true + diff --git a/yunohost.py b/yunohost.py index bf8f0446..20e36b27 100644 --- a/yunohost.py +++ b/yunohost.py @@ -88,18 +88,6 @@ def pretty_print_dict(d, depth=0): else: print((" ") * depth + "%s: %s" % (str(k), str(v))) -def lvl(*args): - dic = None - for arg in args: - if dic is None: - dic = arg - elif arg in dic: - dic = dic[arg] - else: - return False - - return True - def is_true(arg): true_list = ['yes', 'Yes', 'true', 'True' ] for string in true_list: diff --git a/yunohost_app.py b/yunohost_app.py index d5fb16a7..1e48a774 100644 --- a/yunohost_app.py +++ b/yunohost_app.py @@ -30,7 +30,7 @@ import shutil import stat import yaml import time -from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password, lvl, is_true, lemon_configuration +from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password, is_true, lemon_configuration from yunohost_domain import domain_list, domain_add from yunohost_user import user_info @@ -41,7 +41,6 @@ a2_settings_path = '/etc/yunohost/apache/domains' a2_template_path = '/etc/yunohost/apache/templates' install_tmp = '/tmp/yunohost/install' app_tmp_folder = install_tmp + '/from_file' -lemon_tmp_conf = '/tmp/tmplemonconf' def app_listlists(): """ diff --git a/yunohost_domain.py b/yunohost_domain.py index 84d30813..ad1b0fd4 100644 --- a/yunohost_domain.py +++ b/yunohost_domain.py @@ -117,7 +117,7 @@ def domain_add(domains, raw=False, main=False): ('exportedHeaders', domain, 'Auth-User'): '$uid', ('exportedHeaders', domain, 'Remote-User'): '$uid', ('exportedHeaders', domain, 'Desc'): '$description', - ('exportedHeaders', domain, 'Email'): '$mail', + ('exportedHeaders', domain, 'Email'): "(ref($mail) eq 'ARRAY' ? $mail[0] : $mail)", ('exportedHeaders', domain, 'Name'): '$cn', ('exportedHeaders', domain, 'Authorization'): '"Basic ".encode_base64("$uid:$_password")', ('vhostOptions', domain, 'vhostMaintenance'): 0, diff --git a/yunohost_tools.py b/yunohost_tools.py index 34e04139..385cfbcd 100644 --- a/yunohost_tools.py +++ b/yunohost_tools.py @@ -36,6 +36,8 @@ from yunohost_domain import domain_add from yunohost_dyndns import dyndns_subscribe from yunohost_backup import backup_init +lemon_tmp_conf = '/tmp/tmplemonconf' + def tools_ldapinit(password=None): """ YunoHost LDAP initialization @@ -298,3 +300,64 @@ def tools_postinstall(domain, password, dyndns=False): os.system('service samba restart') win_msg(_("YunoHost has been successfully configured")) + + +def tools_lemonrule(id=None, url=None, key=None, value=None, priority=None, delete=False, apply=False): + """ + + """ + conf_lines = [] + + if delete: line = "delete $tmp" + else: line = "$tmp" + + # locationRule formatter + if url is not None and id is not None: + # Remove potential "http://" or "https://" + if '://' in url: + url = url[url.index('://') + 3:] + + # Split domain and path properly + if '/' in url: + domain = url[:url.index('/')] + path = url[url.index('/'):] + if path[-1:] is not '/': + path = path +'/' + else: + domain = url + path = '/' + + line = line +"->{'locationRules'}->{'"+ domain +"'}" + if priority is not None: + line = line +"->{'(?#"+ priority + id +")^"+ path +"'}" + else: + line = line +"->{'(?#"+ id +"Z)^"+ path +"'}" + + # Free key formatter from tuple + elif key is not None: + if not isinstance(key, tuple): key = (key,) + for level in key: + line = line +"->{'"+ level +"'}" + + # Append value + if value is None: conf_lines.append(line +';') + elif isinstance(value, int): conf_lines.append(line +' = '+ str(value) +';') + else: conf_lines.append(line +' = \''+ value +'\';') + + # Write configuration + with open(lemon_tmp_conf,'a+') as lemon_conf: + for conf_line in conf_lines: + lemon_conf.write(conf_line) + + # Apply & reload configuration + if apply: + os.system('chown www-data '+ lemon_tmp_conf) + if os.system('/usr/share/lemonldap-ng/bin/lmYnhMoulinette') == 0: + os.system('service apache2 reload') + win_msg(_("LemonLDAP configured")) + else: + raise YunoHostError(1, _("An error occured during LemonLDAP configuration")) + + os.system("echo '' > lemon_tmp_conf") + +