30
30
from __future__ import print_function
31
31
from __future__ import absolute_import
32
32
33
+ from past .builtins import basestring
33
34
from email .utils import parseaddr
35
+ import codecs
34
36
import datetime
37
+ import errno
35
38
import fnmatch
36
39
import os
37
40
import re
44
47
from mig .shared .base import client_id_dir , client_dir_id , client_alias , \
45
48
get_client_id , extract_field , fill_user , fill_distinguished_name , \
46
49
is_gdp_user , mask_creds , sandbox_resource
50
+ from mig .shared .compat import _unicode_string_to_escaped_unicode
47
51
from mig .shared .conf import get_configuration_object
48
52
from mig .shared .configuration import Configuration
49
53
from mig .shared .defaults import user_db_filename , keyword_auto , ssh_conf_dir , \
97
101
https_authdigests = user_db_filename
98
102
99
103
104
+ _USERADM_CONFIG_DIR_KEYS = ('user_db_home' , 'user_home' , 'user_settings' ,
105
+ 'user_cache' , 'mrsl_files_dir' , 'resource_pending' )
106
+
107
+
100
108
def init_user_adm (dynamic_db_path = True ):
101
109
"""Shared init function for all user administration scripts.
102
110
The optional dynamic_db_path argument toggles dynamic user db path lookup
@@ -451,6 +459,21 @@ def verify_user_peers(configuration, db_path, client_id, user, now, verify_peer,
451
459
return accepted_peer_list , effective_expire
452
460
453
461
462
+ def _check_directories_unprovisioned (configuration , db_path ):
463
+ user_db_home = os .path .dirname (db_path )
464
+ return not os .path .exists (db_path ) and not os .path .exists (user_db_home )
465
+
466
+
467
+ def _provision_directories (configuration ):
468
+ for config_attr in _USERADM_CONFIG_DIR_KEYS :
469
+ try :
470
+ dir_to_create = getattr (configuration , config_attr )
471
+ os .mkdir (dir_to_create )
472
+ except OSError as oserr :
473
+ if oserr .errno != errno .ENOENT : # FileNotFoundError
474
+ raise
475
+
476
+
454
477
def create_user_in_db (configuration , db_path , client_id , user , now , authorized ,
455
478
reset_token , reset_auth_type , accepted_peer_list , force ,
456
479
verbose , ask_renew , default_renew , do_lock ,
@@ -463,8 +486,25 @@ def create_user_in_db(configuration, db_path, client_id, user, now, authorized,
463
486
flock = None
464
487
user_db = {}
465
488
renew = default_renew
489
+
490
+ retry_lock = False
466
491
if do_lock :
492
+ try :
493
+ flock = lock_user_db (db_path )
494
+ except (IOError , OSError ) as oserr :
495
+ if oserr .errno != errno .ENOENT : # FileNotFoundError
496
+ raise
497
+
498
+ if _check_directories_unprovisioned (configuration , db_path = db_path ):
499
+ _provision_directories (configuration )
500
+ retry_lock = True
501
+ else :
502
+ raise Exception ("Failed to lock user DB: '%s'" % db_path )
503
+
504
+ if retry_lock :
467
505
flock = lock_user_db (db_path )
506
+ if not flock :
507
+ raise Exception ("Failed to lock user DB: '%s'" % db_path )
468
508
469
509
if not os .path .exists (db_path ):
470
510
# Auto-create missing user DB if either auto_create_db or force is set
@@ -859,7 +899,7 @@ def create_user_in_fs(configuration, client_id, user, now, renew, force, verbose
859
899
# match in htaccess
860
900
861
901
dn_plain = info ['distinguished_name' ]
862
- dn_enc = dn_plain . encode ( 'string_escape' )
902
+ dn_enc = _unicode_string_to_escaped_unicode ( dn_plain )
863
903
864
904
def upper_repl (match ):
865
905
"""Translate hex codes to upper case form"""
@@ -1013,15 +1053,18 @@ def upper_repl(match):
1013
1053
raise Exception ('could not create custom css file: %s' % css_path )
1014
1054
1015
1055
1016
- def create_user (user , conf_path , db_path , force = False , verbose = False ,
1056
+ def create_user (user , conf_path , db_path , configuration = None , force = False , verbose = False ,
1017
1057
ask_renew = True , default_renew = False , do_lock = True ,
1018
1058
verify_peer = None , peer_expire_slack = 0 , from_edit_user = False ,
1019
1059
ask_change_pw = False , auto_create_db = True , create_backup = True ):
1020
1060
"""Add user in database and in file system. Distinguishes on the user ID
1021
1061
format as a first step.
1022
1062
"""
1023
1063
1024
- if conf_path :
1064
+ if configuration is not None :
1065
+ # use it
1066
+ pass
1067
+ elif conf_path :
1025
1068
if isinstance (conf_path , basestring ):
1026
1069
1027
1070
# has been checked for accessibility above...
0 commit comments