Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/utils/nslcd.py
blob: bb720a3635ee32ee2ca6cadcbdaf8b380c029741 (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
146
147
148
149
150
151
152
153
154
155
156
157
# coding: utf-8

# nslcd.py - functions for doing nslcd requests
#
# Copyright (C) 2013-2017 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

import fcntl
import os
import socket
import struct
import sys

import constants


# definition for reading and writing INT32 values
_int32 = struct.Struct('!i')


class NslcdClient(object):

    def __init__(self, action):
        # set up the socket (store in class to avoid closing it)
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        fcntl.fcntl(self.sock, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
        # connect to nslcd
        self.sock.connect(constants.NSLCD_SOCKET)
        #self.sock.setblocking(1)
        self.fp = os.fdopen(self.sock.fileno(), 'r+b', 0)
        # write a request header with a request code
        self.action = action
        self.write_int32(constants.NSLCD_VERSION)
        self.write_int32(action)

    def write(self, value):
        self.fp.write(value)

    def write_int32(self, value):
        self.write(_int32.pack(value))

    def write_bytes(self, value):
        self.write_int32(len(value))
        self.write(value)

    def write_string(self, value):
        if sys.version_info[0] >= 3:
            value = value.encode('utf-8')
        self.write_bytes(value.encode('utf-8'))

    def write_ether(self, value):
        value = struct.pack('BBBBBB', *(int(x, 16) for x in value.split(':')))
        self.write(value)

    def write_address(self, af, value):
        self.write_int32(af)
        self.write_bytes(value)

    def read(self, size):
        value = b''
        while len(value) < size:
            data = self.fp.read(size - len(value))
            if not data:
                raise IOError('NSLCD protocol cut short')
            value += data
        return value

    def read_int32(self):
        return _int32.unpack(self.read(_int32.size))[0]

    def read_bytes(self):
        return self.read(self.read_int32())

    def read_string(self):
        value = self.read_bytes()
        if sys.version_info[0] >= 3:
            value = value.decode('utf-8')
        return value

    def read_stringlist(self):
        num = self.read_int32()
        return [self.read_string() for x in range(num)]

    def read_ether(self):
        value = self.fp.read(6)
        return ':'.join('%x' % x for x in struct.unpack('6B', value))

    def read_address(self):
        af = self.read_int32()
        return af, socket.inet_ntop(af, self.read_bytes())

    def read_addresslist(self):
        num = self.read_int32()
        return [self.read_address() for x in range(num)]

    def get_response(self):
        # complete the request if required and check response header
        if self.action:
            # flush the stream
            self.fp.flush()
            # read and check response version number
            if self.read_int32() != constants.NSLCD_VERSION:
                raise IOError('NSLCD protocol error')
            if self.read_int32() != self.action:
                raise IOError('NSLCD protocol error')
            # reset action to ensure that it is only the first time
            self.action = None
        # get the NSLCD_RESULT_* marker and return it
        return self.read_int32()

    def close(self):
        if hasattr(self, 'fp'):
            try:
                self.fp.close()
            except IOError:
                pass

    def __del__(self):
        self.close()


def usermod(username, asroot=False, password=None, args=None):
    # open a connection to nslcd
    con = NslcdClient(constants.NSLCD_ACTION_USERMOD)
    # write the request information
    con.write_string(username)
    con.write_int32(1 if asroot else 0)
    con.write_string(password)
    for k, v in args.items():
        con.write_int32(k)
        con.write_string(v)
    con.write_int32(constants.NSLCD_USERMOD_END)
    # read the response
    if con.get_response() != constants.NSLCD_RESULT_BEGIN:
        raise IOError('NSLCD protocol error')
    response = {}
    while True:
        key = con.read_int32()
        if key == constants.NSLCD_USERMOD_END:
            break
        response[key] = con.read_string()
    # return the response
    return response