summaryrefslogtreecommitdiff
path: root/src/conf_mode/system-login.py
blob: 2c1e4dc3efc7cfd321c2b39f5b6211cedf73d356 (plain)
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
#!/usr/bin/env python3
#
# Copyright (C) 2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# 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, see <http://www.gnu.org/licenses/>.

import sys
import os

from vyos.config import Config
from vyos import ConfigError

default_config_data = {
    'deleted': False,
    'radius_server': [],
    'radius_source': '',
    'user': []
}

def get_config():
    login = default_config_data
    conf = Config()
    base_level = ['system', 'login']

    if not conf.exists(base_level):
        login['deleted'] = True
        return login

    if conf.exists(base_level + ['radius', 'source-address']):
        login['radius_source'] = conf.return_value(['radius', 'source-address'])

    # Read in all RADIUS servers and store to list
    for server in conf.list_nodes(base_level + ['radius', 'server']):
        radius = {
            'address': server,
            'key': '',
            'port': '1812',
            'timeout': '2'
        }
        conf.set_level(base_level + ['radius', 'server', server])

        # RADIUS shared secret
        if conf.exists(['key']):
            radius['key'] = conf.return_value(['key'])

        # RADIUS authentication port
        if conf.exists(['port']):
            radius['port'] = conf.return_value(['port'])

        # RADIUS session timeout
        if conf.exists(['timeout']):
            radius['timeout'] = conf.return_value(['timeout'])

        # Append individual RADIUS server configuration to global server list
        login['radius_server'].append(radius)

    # Read in all local users and store to list
    for username in conf.list_nodes(base_level + ['user']):
        user = {
            'name': username,
            'password_plaintext': '',
            'password_encrypted': '',
            'public_keys': [],
            'full_name': '',
            'home_dir': '/home/' + username,
        }
        conf.set_level(base_level + ['user', username])

        # Plaintext password
        if conf.exists(['authentication', 'plaintext-password']):
            user['password_plaintext'] = conf.return_value(['authentication', 'plaintext-password'])

        # Encrypted password
        if conf.exists(['authentication', 'encrypted-password']):
            user['password_encrypted'] = conf.return_value(['authentication', 'encrypted-password'])

        # Read in public keys
        for id in conf.list_nodes(['authentication', 'public-keys']):
            key = {
                'name': id,
                'key': '',
                'options': '',
                'type': ''
            }
            conf.set_level(base_level + ['user', username, 'authentication', 'public-keys', id])

            # Public Key portion
            if conf.exists(['key']):
                user['key'] = conf.return_value(['key'])

            # Options for individual public key
            if conf.exists(['options']):
                user['options'] = conf.return_value(['options'])

            # Type of public key
            if conf.exists(['type']):
                user['type'] = conf.return_value(['type'])

            # Append individual public key to list of user keys
            user['public_keys'].append(key)

        # set proper config level
        conf.set_level(base_level + ['user', username])

        # User real name
        if conf.exists(['full-name']):
            user['full_name'] = conf.return_value(['full-name'])

        # User home-directory
        if conf.exists(['home-directory']):
            user['home_dir'] = conf.return_value(['home-directory'])

    return login

def verify(login):
    pass

def generate(login):
    import pprint
    pprint.pprint(login)

    pass

def apply(login):
    pass

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        sys.exit(1)