This file is indexed.

/usr/lib/python2.7/dist-packages/mysql/fabric/credentials.py is in mysql-utilities 1.6.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
#
# Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
#
# 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; version 2 of the License.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#


import getpass
import hashlib
import logging
import re

from mysql.fabric import (
    config as _config,
    errors as _errors,
    persistence as _persistence,
    executor as _executor,
    command as _command,
)

_LOGGER = logging.getLogger(__name__)

FABRIC_REALM_XMLRPC = 'MySQL Fabric'

FABRIC_DEFAULT_PROTOCOL = 'xmlrpc'
FABRIC_PROTOCOL_DEFAULTS = {
    'protocol.xmlrpc': {
        'realm': FABRIC_REALM_XMLRPC,
    },
    'protocol.mysql': {
    }
}

# This sequence is used when creating an dropping tables
_SQL_TABLES = ('users', 'roles', 'permissions', 'role_permissions',
               'user_roles')

_SQL_CREATE = dict()
_SQL_CREATE['users'] = """
CREATE TABLE `users` (
  `user_id` int unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `protocol` varchar(200) NOT NULL DEFAULT 'xmlrpc',
  `password` varchar(128) DEFAULT NULL,
  PRIMARY KEY (user_id),
  UNIQUE KEY (`username`, `protocol`)
) DEFAULT CHARSET=utf8
"""

_SQL_CREATE['roles'] = """
CREATE TABLE `roles` (
  `role_id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(80) NOT NULL,
  `description` varchar(1000),
  PRIMARY KEY (role_id)
) DEFAULT CHARSET=utf8
"""

_SQL_CREATE['permissions'] = """
CREATE TABLE `permissions` (
  `permission_id` int unsigned NOT NULL AUTO_INCREMENT,
  `subsystem` varchar(60) NOT NULL,
  `component` varchar(60) DEFAULT NULL,
  `function` varchar(60) DEFAULT NULL,
  `description` varchar(1000),
  PRIMARY KEY (permission_id),
  UNIQUE INDEX (`subsystem`, `component`, `function`)
) DEFAULT CHARSET=utf8
"""

_SQL_CREATE['role_permissions'] = """
CREATE TABLE `role_permissions` (
  `role_id` int unsigned NOT NULL,
  `permission_id` int unsigned NOT NULL,
  PRIMARY KEY (`role_id`, `permission_id`)
) DEFAULT CHARSET=utf8
"""

_SQL_CREATE['user_roles'] = """
CREATE TABLE `user_roles` (
  `user_id` int unsigned NOT NULL,
  `role_id` int unsigned NOT NULL,
  PRIMARY KEY (`user_id`, `role_id`)
) DEFAULT CHARSET=utf8
"""

_SQL_CONSTRAINTS = [
    "ALTER TABLE user_roles ADD CONSTRAINT fk_user_roles_user_id "
    "FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE, "
    "ADD CONSTRAINT fk_user_roles_role_id FOREIGN KEY (role_id) "
    "REFERENCES roles (role_id) ON DELETE CASCADE",

    "ALTER TABLE role_permissions ADD CONSTRAINT fk_role_permissions_role_id "
    "FOREIGN KEY (role_id) REFERENCES roles (role_id), ADD CONSTRAINT "
    "fk_role_permissions_permission_id FOREIGN KEY (permission_id) "
    "REFERENCES permissions (permission_id)",
]

_USERS = [
    # user_id, username, protocol, password
    (1, 'admin', 'xmlrpc', None),
    (2, 'admin', 'mysql', None),
]

_PERMISSIONS = [
    (1, 'core', None, None, 'Full access to all core Fabric functionality'),
    (2, 'core', 'dump', None, 'Access to dump commands'),
    (3, 'core', 'user', None, 'User administration'),
    (4, 'core', 'role', None, 'Role administration'),
    (5, 'core', 'threat', None, 'Reporting to Fabric'),
]

_ROLES = [
    (1, 'superadmin', 'Role for Administrative users'),
    (2, 'useradmin', 'Role for users dealing with user administration'),
    (3, 'connector', 'Role for MySQL Connectors'),
]

_ROLE_PERMISSIONS = {
    1: (1,),  # superadmin permissions
    3: (2, 5),  # connector permissions
    2: (3, 4),  # useradmin permissions
}

_USER_ROLES = [
    (1, 1),  # admin in superadmin role
    (2, 1),  # admin in superadmin role
]

_SQL_FETCH_USER = """
SELECT username, protocol, password
FROM users WHERE username = %s AND protocol = %s
"""

_SQL_FETCH_USER_PERMISSIONS = """
SELECT p.subsystem, p.component, p.function
FROM users AS u LEFT JOIN user_roles AS ur USING (user_id)
    LEFT JOIN role_permissions AS rp USING (role_id)
    LEFT JOIN permissions AS p USING (permission_id)
WHERE
    u.username = %s AND protocol = %s
"""


class User(_persistence.Persistable):
    """Class defining a user connecting to a Fabric instance
    """

    def __init__(self, username, protocol, password_hash):
        self._username = username
        self._protocol = protocol
        self._password_hash = password_hash
        self._permissions = User.fetch_permissions(username, protocol)

    @property
    def username(self):
        return self._username

    @property
    def protocol(self):
        return self._protocol

    @property
    def password_hash(self):
        return self._password_hash

    @staticmethod
    def add_user(username, password, protocol, user_id=None,
                 config=None, persister=None):
        stmt = (
            "INSERT INTO users (user_id, username, password, protocol) "
            "VALUES (%(user_id)s, %(username)s, %(password)s, %(protocol)s)"
        )

        hashed_password = _hash_password(username, password, protocol, config)

        options = {
            "fetch": False,
            "params": {
                'user_id': user_id,
                'username': username,
                'protocol': protocol,
                'password': hashed_password,
            },
        }
        cur = persister.exec_stmt(stmt, options)
        return cur.lastrowid

    @staticmethod
    def add_permission(subsystem, component=None, function=None,
                       description=None, permission_id=None, persister=None):
        stmt = (
            "INSERT INTO permissions (permission_id, subsystem, "
            "component, function, description) "
            "VALUES (%(permission_id)s, %(subsystem)s, %(component)s, "
            "%(function)s, %(description)s)"
        )

        options = {
            "fetch": False,
            "params": {
                'permission_id': permission_id,
                'subsystem': subsystem,
                'function': function,
                'component': component,
                'description': description,
            },
        }
        persister.exec_stmt(stmt, options)

    @staticmethod
    def add_role(name, description, role_id=None, persister=None):
        stmt = (
            "INSERT INTO roles (role_id, name, description) "
            "VALUES (%(role_id)s, %(name)s, %(description)s)"
        )

        options = {
            "fetch": False,
            "params": {
                'role_id': role_id,
                'name': name,
                'description': description,
            },
        }
        persister.exec_stmt(stmt, options)

    @staticmethod
    def add_role_permission(role_id, permission_id, persister=None):
        stmt = (
            "INSERT INTO role_permissions (role_id, permission_id) "
            "VALUES (%(role_id)s, %(permission_id)s)"
        )

        options = {
            "fetch": False,
            "params": {
                'role_id': role_id,
                'permission_id': permission_id,
            },
        }
        persister.exec_stmt(stmt, options)

    @staticmethod
    def add_user_role(user_id, role_id, persister=None):
        stmt = (
            "INSERT INTO user_roles (user_id, role_id) "
            "VALUES (%(user_id)s, %(role_id)s)"
        )

        options = {
            "fetch": False,
            "params": {
                'role_id': role_id,
                'user_id': user_id,
            },
        }
        persister.exec_stmt(stmt, options)

    @staticmethod
    def delete_user(user_id=None, username=None, protocol=None, persister=None):
        """Delete a Fabric user"""
        if (user_id and username) or (not user_id and not username):
            raise AttributeError("Use user_id or username, not both")

        if username and not protocol:
            raise AttributeError(
                "protocol is required when username is provided")

        options = {
            "fetch": False,
            "params": {},
        }
        if user_id:
            options['params']['user_id'] = user_id
            sql = "DELETE FROM users WHERE user_id = %(user_id)s"
        else:
            options['params']['username'] = username
            options['params']['protocol'] = protocol
            sql = ("DELETE FROM users WHERE username = %(username)s "
                   "AND protocol = %(protocol)s")

        persister.exec_stmt(sql, options)

    @staticmethod
    def create(persister=None, config=None):
        """Create table for FabricUser

        :param persister: A valid handle to the state store.
        """
        for table_name in _SQL_TABLES:
            persister.exec_stmt(_SQL_CREATE[table_name])

        for statement in _SQL_CONSTRAINTS:
            persister.exec_stmt(statement)

        for user_id, username, protocol, password in _USERS:
            User.add_user(username, password, protocol,
                          user_id=user_id,
                          persister=persister,
                          config=config)

        for permission_id, subsystem, component, function, description in \
                _PERMISSIONS:
            User.add_permission(
                subsystem, component, function, description,
                permission_id=permission_id, persister=persister)

        for role_id, name, description in _ROLES:
            User.add_role(name, description, role_id=role_id,
                          persister=persister)

        for role_id, permissions in _ROLE_PERMISSIONS.items():
            for permission_id in permissions:
                User.add_role_permission(
                    role_id, permission_id, persister=persister)

        for user_id, role_id in _USER_ROLES:
            User.add_user_role(user_id, role_id,
                               persister=persister)

    @staticmethod
    def fetch_user(username, protocol, persister=None):
        """Fetch the information about a user using particular protocol

        :param username: Username being queried.
        :param protocol: Protocol with which the user connect.
        :param realm: Realm which the user belongs to.
        :param persister: A valid handle to the state store.
        :return: Returns user information including password hash.
        """
        if not protocol:
            protocol = FABRIC_DEFAULT_PROTOCOL

        options = {
            "fetch": False,
            "params": (username, protocol),
            "columns": True,
        }

        cur = persister.exec_stmt(_SQL_FETCH_USER, options)
        row = cur.fetchone()
        if row:
            return User(username=row.username,
                        protocol=row.protocol,
                        password_hash=row.password)

        return None

    @staticmethod
    def fetch_permissions(username, protocol, persister=None):
        """Fetch permissions for given user and protocol

        :param username: Username being queried.
        :param protocol: Protocol in which the user lives
        :param persister: A valid handle to the state store.
        :return: Returns user permissions as a list of tuples
        """
        if not protocol:
            protocol = FABRIC_DEFAULT_PROTOCOL

        options = {
            "fetch": False,
            "params": (username, protocol),
        }

        cur = persister.exec_stmt(_SQL_FETCH_USER_PERMISSIONS, options)
        rows = cur.fetchall()
        if rows:
            return rows

        return None

    def has_permission(self, subsystem, component=None, function=None):
        """Check if user has permission"""
        needles = [
            (subsystem, None, None),  # full access to subsystem
            (subsystem, component, None),  # access to all component's functions
            (subsystem, component, function)  # access to function
        ]

        for needle in needles:
            if needle in self._permissions:
                return True

        return False


def _hash_password(username, password, protocol, config, realm=None):
    """Hash password base on protocol.

    If the OpenSSL is not installed, the function uses built-in functions
    from hashlib.

    :raises errors.CredentialError: if protocol is not implemented
    """
    if not password:
        return
    if protocol in ('xmlrpc',) and not realm and config:
        section = 'protocol.' + protocol
        realm = config.get(section, 'realm',
                           vars=FABRIC_PROTOCOL_DEFAULTS[section])

    if protocol == 'xmlrpc':
        return hashlib.md5('{user}:{realm}:{secret}'.format(
            user=username, realm=FABRIC_REALM_XMLRPC,
            secret=password)).hexdigest()
    elif protocol == 'mysql':
        return hashlib.sha1(hashlib.sha1(password).digest()).hexdigest().upper()

    raise _errors.CredentialError(
        "Password hasing for protocol '{0}' is not implemented.".format(
            protocol
        ))


def get_user(username, protocol, persister=None):
    """Get user information from data store

    :param username: User to query for.
    :param protocol: Protocol used to connect.
    :param persister: A valid handle to the state store.
    :return: namedtuple with password, username, protocol and user_id
    :rtype: namedtuple
    """
    if not persister:
        persister = _persistence.current_persister()

    options = {
        "fetch": False,
        "params": (username, protocol),
        "columns": True,
    }

    cur = persister.exec_stmt(
        "SELECT password, username, protocol, user_id "
        "FROM users WHERE username = %s "
        "AND protocol = %s", options)
    row = cur.fetchone()
    if row:
        return row
    return None


def _get_password(prompt=None):
    """Get password from using STDIN

    :param prompt: String describing what input is required.
    :returns: Password as string.
    :rtype: str
    :raises CredentialError: if passwords did not match
    """
    if not prompt:
        prompt = 'Password: '
    password = getpass.getpass(prompt).strip()
    repeat = getpass.getpass('Repeat Password: ').strip()
    if password != repeat:
        raise _errors.CredentialError("Passwords did not match!")
    return password


def check_initial_setup(config, persister, check_only=False):
    """Check if admin user has password and if not sets it

    :param persister: A valid handle to the state store.
    """

    # Fetch which protocols have no passwords set for user 'admin'
    protocols = []
    for key in FABRIC_PROTOCOL_DEFAULTS.keys():
        if key.startswith('protocol.'):
            tmp = key.split('.', 2)[1]
            if tmp not in protocols:
                user = get_user('admin', tmp, persister)
                if not user or not user.password:
                    protocols.append(tmp)

    # Try setting password for 'admin' user from configuration file
    for protocol in tuple(protocols):  # we change protocols, loop over copy
        section = 'protocol.' + protocol
        try:
            username = config.get(section, 'user')
        except _config.NoOptionError:
            username = 'admin'

        # If there is no password, we have to ask for one.
        try:
            password = config.get(section, 'password').strip()

            # No password, so we have to ask for one
            if password == '':
                break
        except _config.NoOptionError:
            # No password, so we have to ask for one
            continue

        persister.begin()
        try:
            if username != 'admin':
                _LOGGER.info("Adding user %s/%s", username, protocol)
                user_id = User.add_user(username, password, protocol,
                                        persister=persister)
            else:
                _LOGGER.info("Initial password for %s/%s set", username,
                             protocol)
                _change_password(username, password, protocol, config,
                                 persister)
        except _errors.CredentialError as error:
            print "Setting password cancelled."
            _LOGGER.debug(str(error))
            print error
            persister.rollback()
        else:
            persister.commit()
            msg = (
                "Password set for {user}/{protocol} from configuration file."
            ).format(user=username, protocol=protocol)
            print msg
            _LOGGER.info(msg)

        if username != 'admin':
            print "Note: {user}/{protocol} has no roles set!".format(
                user=username, protocol=protocol
            )
        else:
            # No need to ask for password later for this protocol
            protocols.remove(protocol)


    if not protocols:
        # Passwords are set
        return

    if check_only and protocols:
        print (
            "\nPassword for admin user is empty. Please run\n\n"
            "  shell> mysqlfabric user password admin\n\n"
            "Make sure the password is empty or commented in section\n"
            "[protocol.xmlrpc] of the configuration file before executing the\n"
            "above command."
        )
        raise _errors.CredentialError("Check empty admin password failed")

    print "Finishing initial setup"
    print "======================="
    print "Password for admin user is not yet set."
    password = None

    while True:
        password = _get_password("Password for {user}/{protocol}: ".format(
            user='admin', protocol='xmlrpc'))
        if password:
            break

    if password:
        for protocol in protocols:
            try:
                _change_password('admin', password, protocol, config,
                                 persister)
            except _errors.CredentialError as error:
                print "Setting password cancelled."
                _LOGGER.debug(str(error))
                print error
            else:
                print "Password set."
    else:
        # Making sure password is set and there is an error message
        raise _errors.CredentialError(
            "Password for admin can not be empty. Use `user password` command.")

    persister.commit()


def _configure_connections(config):
    """Configure information on database connection and remote servers.

    :param config: Configuration read from configuration file.
    """
    # Configure the number of concurrent executors.
    executor = _executor.Executor()
    executor.set_number_executors(1)

    # Fetch options to configure the state store.
    address = config.get('storage', 'address')

    try:
        host, port = address.split(':')
        port = int(port)
    except ValueError:
        host = address
        port = 3306

    user = config.get('storage', 'user')
    database = config.get('storage', 'database')

    try:
        password = config.get('storage', 'password')
    except _config.NoOptionError:
        password = _get_password("Storage password: ")

    try:
        connection_timeout = config.get("storage", "connection_timeout")
        connection_timeout = float(connection_timeout)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        connection_timeout = None

    try:
        connection_attempts = config.get("storage", "connection_attempts")
        connection_attempts = int(connection_attempts)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        connection_attempts = None

    try:
        connection_delay = config.get("storage", "connection_delay")
        connection_delay = int(connection_delay)
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        connection_delay = None

    try:
        auth_plugin = config.get("storage", "auth_plugin")
    except (_config.NoOptionError, _config.NoSectionError, ValueError):
        auth_plugin = None

    # Define state store configuration.
    _persistence.init(
        host=host, port=port, user=user, password=password, database=database,
        connection_timeout=connection_timeout,
        connection_attempts=connection_attempts,
        connection_delay=connection_delay,
        auth_plugin=auth_plugin
    )


def confirm(message, default='y'):
    """Prompt confirming using Yes or No

    :param message: Message to be shown.
    :param default: The default answer, either 'y' or 'n'.
    :returns: True if answer was 'y', False if 'n'.
    :rtype: bool
    :raises ValueError: if invalid choice has been given.
    """
    if not isinstance(default, str):
        raise AttributeError("default argument should be a string")
    if default:
        default = default[0].lower()

    yes = 'Y' if default == 'y' else 'y'
    no = 'N' if default == 'n' else 'n'

    msg = message + " [{0}/{1}] ".format(yes, no)
    try:
        choice = raw_input(msg)[0].lower()
    except IndexError:
        choice = default

    if choice == 'y':
        return True
    elif choice == 'n':
        return False

    raise ValueError("Invalid choice")


def _role_listing(selected=None, marker=None, persister=None):
    """Display list of roles

    :param selected: Sequence of selected roles.
    :param marker: Marker used for showing selected roles.

    The selected argument should be a list of role IDs. The marker will be
    shown in front of the role ID when it is in the selected sequence.
    """
    if not persister:
        persister = _persistence.current_persister()

    if not selected:
        selected = []

    if marker:
        marker = marker[0]
    else:
        marker = 'X'

    options = {
        "fetch": False,
        "columns": True,
    }

    roleperms = (
        "SELECT r.role_id, r.name, r.description AS role_desc, "
        "p.permission_id, p.description AS perm_desc, p.subsystem, "
        "p.component, p.function "
        "FROM roles AS r LEFT JOIN role_permissions USING (role_id) "
        "LEFT JOIN permissions AS p USING (permission_id) ORDER BY r.role_id"
    )
    cur = persister.exec_stmt(roleperms, options)
    roles = {}
    max_name_len = 0
    max_rowid = 0
    for row in cur:
        if len(row.name) > max_name_len:
            max_name_len = len(row.name)
        if row.role_id > max_rowid:
            max_rowid = row.role_id

        if row.role_id not in roles:
            roles[row.role_id] = [row.name, row.role_desc, [row.perm_desc]]
        else:
            roles[row.role_id][2].append(row.perm_desc)

    # Minimum sizes
    max_rowid_len = max(2, len(str(max_rowid)))
    max_name_len = max(9, max_name_len)

    sel = ' '

    role_fmt = ("{{sel}} {{role_id:{idlen}}}  {{name:{namelen}}}"
                "  {{desc}}".format(idlen=max_rowid_len, namelen=max_name_len))

    label_desc = "Description and Permissions"
    header = role_fmt.format(role_id="ID", name="Role Name", desc=label_desc,
                             sel=sel)
    print header
    print role_fmt.format(role_id='-' * max_rowid_len, name='-' * max_name_len,
                          desc='-' * len(label_desc), sel=sel)

    fmt_perm = '{0}+ {{perm}}'.format(
        (2 + max_rowid_len + 2 + max_name_len + 2) * ' ')

    for role_id in sorted(roles.keys()):
        if role_id in selected:
            sel = marker
        else:
            sel = ' '
        name, role_desc, permissions = roles[role_id]
        print role_fmt.format(role_id=role_id, name=name, desc=role_desc,
                              sel=sel)
        for perm in permissions:
            print fmt_perm.format(perm=perm)


def _role_selection(message=None, choices=None, persister=None):
    """Offers user to select roles on the console

    :param persister: A valid handle to the state store.
    :param message: Message shown just before prompt.
    :param choices: Do not ask, just process choices (string or sequence).
    :return: List of role IDs or role names.
    :rtype: list
    :raises errors.CredentialError: if invalid role was given
    """
    if not persister:
        persister = _persistence.current_persister()

    if not choices:
        if not message:
            message = "\nEnter comma separated list of role IDs or names: "

        choices = raw_input(message)
        if not choices.strip():
            return []

    if isinstance(choices, str):
        choices = choices.split(',')

    options = {
        "fetch": False,
        "columns": True,
    }
    cur = persister.exec_stmt("SELECT role_id, name FROM roles", options)

    valid_role_ids = []
    roles = {}
    for row in cur:
        roles[row.name] = row.role_id
        if str(row.role_id) not in valid_role_ids:
            valid_role_ids.extend([str(row.role_id), row.name])

    try:
        if not all(rid.strip() in valid_role_ids for rid in choices):
            raise ValueError
    except ValueError:
        raise _errors.CredentialError("Found invalid role.")

    # Only return role IDs
    result = []
    for rid in choices:
        try:
            result.append(int(rid))
        except ValueError:
            # We got name
            result.append(roles[rid.strip()])

    return result


def _change_password(username, password, protocol, config, persister):
    """Change password of a Fabric user

    :param username: Username of Fabric user.
    :param password: Password to which we change.
    :param protocol: Protocol for this user.
    :param config: Fabric configuration.
    :param persister: A valid handle to the state store.

    :raise _errors.CredentialError: if any error occurs while updating data
    """
    try:
        persister.begin()
        options = {
            "fetch": False,
            "params": (),
            "columns": True,
        }
        update = ("UPDATE users SET password = %s WHERE username = %s"
                  " AND protocol = %s")
        hashed = _hash_password(username, password, protocol,
                                config)
        options['params'] = (hashed, username, protocol)
        persister.exec_stmt(update, options)
    except Exception as error:
        # We rollback and re-raise
        persister.rollback()
        raise _errors.CredentialError("Error updating password: {0}".format(
            str(error)
        ))
    persister.commit()


def validate_username(username, allow_empty=False):
    """Validates a username

    :param username: The username we need to check.
    :param allow_empty: Whether to allow empty username.
    :returns: Stripped username or None when username was empty
    :rtype: str
    :raises ValueError: if username is not acceptable.
    """
    username = username.strip()
    if not username and allow_empty:
        return None
    valid = r"[a-zA-Z0-9_@.-]"
    match = re.match(r"^" + valid + "+$", username)
    if not match:
        raise _errors.CredentialError("Invalid username (was not {0})".format(
            valid
        ))

    return username


def validate_protocol(protocol, allow_empty=False):
    """Validates a protocol

    :param protocol: The protocol we need to check.
    :param allow_empty: Whether to allow empty protocol.
    :returns: Stripped protocol or None when protocol was empty
    :rtype: str
    :raises ValueError: if protocol is not acceptable.
    """
    protocol = protocol.strip()
    if not protocol and allow_empty:
        return None
    valid = r"[a-zA-Z0-9_@.-]"
    match = re.match(r"^" + valid + "+$", protocol)
    if not match:
        raise _errors.CredentialError("Invalid protocol (was not {0})".format(
            valid
        ))

    return protocol


class UserCommand(_command.Command):
    """Base class for all user commands"""

    group_name = 'user'
    command_name = ''
    description = ''
    persister = None

    def dispatch(self, *args):
        """Setup Fabric Storage System.
        """
        # Configure connections.
        _configure_connections(self.config)
        _persistence.init_thread()
        self.persister = _persistence.current_persister()
        self.execute(*args)

    def execute(self, *args):
        raise NotImplementedError

    def _ask_credentials(self, username, ask_password=True, title=None):
        """Ask for credentials

        :param username: Username will not be asked if given.
        :param ask_password: Whether to ask for password or not.
        :param title: The title to show.
        :return: Tuple containing username, password and protocol
        :rtype: tuple
        """
        if not title:
            title = self.description

        print title
        print "=" * len(title)

        if username:
            print "Username: {0}".format(username)
        else:
            username = validate_username(raw_input("Enter username: "))

        if self.options.protocol:
            protocol = self.options.protocol
            print "Protocol: {0}".format(protocol)
        else:
            prompt = "Protocol (default {0}): ".format(FABRIC_DEFAULT_PROTOCOL)
            protocol = validate_protocol(raw_input(prompt),
                                         allow_empty=True)
            if not protocol:
                protocol = FABRIC_DEFAULT_PROTOCOL

        if ask_password:
            password = _get_password()
        else:
            password = None

        return username, password, protocol


class UserAdd(UserCommand):

    """Add a new Fabric user.

    * protocol: Protocol of the user (for example 'xmlrpc')
    * roles: Comma separated list of roles, IDs or names (see `role list`)

    """

    command_name = 'add'
    description = 'Add a new Fabric user'

    def execute(self, username, protocol=None, roles=None):
        """Add a new Fabric user"""
        username, password, protocol = self._ask_credentials(
            username, ask_password=False)

        # Check if user exists
        if get_user(username, protocol, persister=self.persister):
            raise _errors.CredentialError(
                "User {user}/{protocol} already exists".format(
                    user=username, protocol=protocol))

        password = _get_password('Password: ')

        if not password:
            raise _errors.CredentialError("Can not set empty password")

        role_list = []
        if roles:
            role_list = [role.strip() for role in roles.split(',')]
        else:
            print "\nSelect role(s) for new user"
            _role_listing()
        role_list = _role_selection(persister=self.persister, choices=role_list)

        try:
            self.persister.begin()
            user_id = User.add_user(username, password, protocol)
            if not role_list:
                print ("You can always assign roles later using the "
                       "'user addrole' command")
            else:
                for role_id in role_list:
                    User.add_user_role(user_id, int(role_id))
        except Exception:
            # Whatever happens, we rollback and re-raise
            self.persister.rollback()
            print "Adding user cancelled."
            raise

        self.persister.commit()
        print "Fabric user added."


class UserDelete(UserCommand):

    """Delete a Fabric user.

    * protocol: Protocol of the user (for example 'xmlrpc')
    * force: Do not ask for confirmation

    """

    command_name = 'delete'
    description = 'Delete a Fabric user'

    def execute(self, username, protocol=None, force=False):
        """Delete a Fabric user
        """
        username, password, protocol = self._ask_credentials(
            username, ask_password=False)

        # Check if user exists
        if not get_user(username, protocol, persister=self.persister):
            raise _errors.CredentialError(
                "No user {user}/{protocol}".format(
                    user=username, protocol=protocol))

        if not self.options.force:
            result = confirm(
                "Really remove user {user}/{protocol}?".format(
                    user=username, protocol=protocol), default='n')
            if result is not True:
                return

        try:
            self.persister.begin()
            User.delete_user(username=username, protocol=protocol)
        except Exception:
            # We rollback and re-raise
            self.persister.rollback()
            print "Removing user cancelled."
            raise

        self.persister.commit()
        print "Fabric user deleted."


class UserPassword(UserCommand):

    """Change password of a Fabric user.

    * protocol: Protocol of the user (for example 'xmlrpc')

    """

    command_name = 'password'
    description = 'Change password a Fabric user'

    def execute(self, username, protocol=None):
        """Change password a Fabric user
        """
        username, password, protocol = self._ask_credentials(
            username, ask_password=False)

        # Check if user exists
        if not get_user(username, protocol):
            raise _errors.CredentialError(
                "No user {user}/{protocol}".format(
                    user=username, protocol=protocol))

        password = _get_password('New password: ')

        if password:
            try:
                _change_password(username, password, protocol, self.config,
                                 self.persister)
            except _errors.CredentialError as error:
                print "Changing password cancelled."
                _LOGGER.debug(str(error))
                print error
            else:
                print "Password changed."
        else:
            raise _errors.CredentialError("Can not set empty password")


class UserRoles(UserCommand):

    """Change roles for a Fabric user

    * protocol: Protocol of the user (for example 'xmlrpc')
    * roles: Comma separated list of roles, IDs or names (see `role list`)

    """

    command_name = 'roles'
    description = 'Change roles for a Fabric user'

    def execute(self, username, protocol=None, roles=None):
        """Change roles for a Fabric user
        """
        username, password, protocol = self._ask_credentials(
            username, ask_password=False)

        # Check if user exists
        user = get_user(username, protocol)
        if not user:
            raise _errors.CredentialError(
                "No user {user}/{protocol}".format(user=username,
                                                   protocol=protocol))

        exit_text_removed = (
            "Roles for {user}/{protocol} removed."
        ).format(user=username, protocol=protocol)
        exit_text_updated = (
            "Roles for {user}/{protocol} updated."
        ).format(user=username, protocol=protocol)

        confirmed = False
        role_list = []
        if not roles:
            options = {
                "fetch": False,
                "params": (user.user_id, ),
                "columns": True,
            }
            cur = self.persister.exec_stmt(
                "SELECT role_id FROM user_roles WHERE user_id = %s",
                options)
            current_roles = [row.role_id for row in cur]

            print "\nSelect new role(s) for user, replacing current roles."
            print "Current roles are marke with an X."
            _role_listing(selected=current_roles)
            role_list = _role_selection(persister=self.persister)

            if not role_list:
                confirm_text = (
                    "Remove all roles of user {user}/{protocol}?"
                ).format(user=username, protocol=protocol)
                exit_text = exit_text_removed
                default = 'n'
            else:
                confirm_text = (
                    "Replace roles of user {user}/{protocol}?"
                ).format(user=username, protocol=protocol)
                exit_text = exit_text_updated
                default = 'y'

            confirmed = confirm(confirm_text.format(username=user.username),
                                default=default)
        else:
            # From command line option --roles
            confirmed = True
            if roles.strip() == "0":
                exit_text = exit_text_removed
                role_list = []
            else:
                exit_text = exit_text_updated
                role_list = [role.strip() for role in roles.split(',')]

                role_list = _role_selection(persister=self.persister,
                                            choices=role_list)

        if confirmed:
            try:
                self.persister.begin()
                options = {
                    "fetch": False,
                    "params": (user.user_id, ),
                    "columns": True,
                }
                self.persister.exec_stmt(
                    "DELETE FROM user_roles WHERE user_id = %s", options)
                for role_id in role_list:
                    User.add_user_role(user.user_id, int(role_id))
            except Exception:
                # Whatever happens, we rollback and re-raise
                self.persister.rollback()
                print "Changing roles for user cancelled."
                raise
            else:
                self.persister.commit()
                print exit_text
        else:
            print "Changing roles cancelled."


class UserList(UserCommand):
    """List users and their roles
    """

    group_name = 'user'
    command_name = 'list'
    description = 'List roles and their permissions'

    def execute(self):
        """Display list of users
        """
        persister = _persistence.current_persister()

        options = {
            "fetch": False,
            "columns": True,
        }

        role_perms = (
            "SELECT u.username, u.protocol, r.name as role_name, "
            "r.description AS role_desc,"
            "p.permission_id, p.description AS perm_desc, p.subsystem, "
            "p.component, p.function "
            "FROM users as u LEFT JOIN user_roles AS ur USING (user_id) "
            "LEFT JOIN roles AS r USING (role_id) "
            "LEFT JOIN role_permissions USING (role_id) "
            "LEFT JOIN permissions AS p USING (permission_id) "
            "ORDER BY u.username, u.protocol"
        )
        cur = persister.exec_stmt(role_perms, options)
        roles = {}
        max_username_len = 0
        max_protocol_len = 0

        user_info = {}
        for row in cur:
            if len(row.username) > max_username_len:
                max_username_len = len(row.username)
            if len(row.protocol) > max_protocol_len:
                max_protocol_len = len(row.protocol)

            user_tuple = (row.username, row.protocol)
            if user_tuple not in user_info:
                user_info[user_tuple] = []
            if row.role_name and row.role_name not in user_info[user_tuple]:
                user_info[user_tuple].append(row.role_name)

        # Minimum sizes
        max_username_len = max(9, max_username_len)
        max_protocol_len = max(12, max_protocol_len)

        role_fmt = ("{{username:{userlen}}} {{protocol:{protlen}}} "
                    "{{roles}}".format(userlen=max_username_len,
                                       protlen=max_protocol_len))

        header = role_fmt.format(username="Username", protocol="Protocol",
                                 roles="Roles")
        print header
        print role_fmt.format(username='-' * max_username_len,
                              protocol='-' * max_protocol_len,
                              roles='-' * 20)

        for user_tuple, roles in user_info.iteritems():
            username, protocol = user_tuple
            if roles:
                role_list = ', '.join(roles)
            else:
                role_list = '(no roles set)'
            print role_fmt.format(username=username,
                                  protocol=protocol,
                                  roles=role_list)


class RoleList(UserCommand):
    """List roles and associated permissions
    """

    group_name = 'role'
    command_name = 'list'
    description = 'List roles and their permissions'

    def execute(self):
        _role_listing(persister=self.persister)


def check_credentials(group, command, config, protocol):
    """Check credentials using configuration

    :raises errors.CredentialError: if login failed, or if user has no
                                    permission
    """
    if group not in ('user', 'role'):
        return

    _configure_connections(config)
    _persistence.init_thread()

    if not protocol:
        protocol = FABRIC_DEFAULT_PROTOCOL

    section = 'protocol.' + protocol

    username = config.get(section, 'user')
    password = config.get(section, 'password')
    realm = config.get(section, 'realm', vars=FABRIC_PROTOCOL_DEFAULTS)

    user = User.fetch_user(username, protocol=protocol)
    password_hash = _hash_password(username, password, protocol, config, realm)

    if user is None or user.password_hash != password_hash:
        _LOGGER.info("Failed login for user %s/%s", username, protocol)
        raise _errors.CredentialError("Login failed")
    elif not user.has_permission('core', group, command):
        _LOGGER.info("Permission denied for user %s/%s", username, protocol)
        raise _errors.CredentialError("No permission")