-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautolabel.py
173 lines (135 loc) · 4.47 KB
/
autolabel.py
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
#!/usr/bin/env python
##############################################################################
# -*- coding: utf-8 -*-
# Project: Autolabel
# Module: autolabel.py
# Purpose: Auto labeled for laptops
# Language: Python 2.5
# Date: 16-Oct-2014
# Ver: 16-Oct-2014
# Author: Manuel Mora Gordillo
# Copyright: 2014 - Manuel Mora Gordillo <manuito @nospam@ gmail.com>
#
# Autolabel 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, either version 3 of the License, or
# (at your option) any later version.
# Autolabel 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 Autolabel. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from LdapConnection import LdapConnection
import fcntl, socket, struct
import fileinput
import os
import time
import ConfigParser
time.sleep(20)
dir = "/usr/share/autolabel"
defaultImage = "/usr/share/icons/gnome/48x48/places/icono-inicio.png"
def getHwAddr(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
def removeWhitespaces():
for lines in fileinput.FileInput('/etc/gdm3/greeter.gsettings', inplace=1):
lines = lines.strip()
print lines
def removePhoto():
try:
os.remove(dir+"/userphoto.jpg")
except:
pass
def getConfigUsername():
parser = ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read(dir+'/config')
username = parser.get('main', 'username')
return username
def setConfigUsername(user):
parser = ConfigParser.SafeConfigParser(allow_no_value=True)
parser.read(dir+'/config')
parser.set('main', 'username',user)
with open(dir+'/config', 'w') as fp:
parser.write(fp)
return username
def setData(name, photo):
parser = ConfigParser.SafeConfigParser(allow_no_value=True)
try:
parser.read('/etc/gdm3/greeter.gsettings')
except:
removeWhitespaces()
parser.read('/etc/gdm3/greeter.gsettings')
parser.set('org.gnome.login-screen', 'logo', '\''+photo+'\'')
parser.set('org.gnome.login-screen', 'fallback-logo', '\''+photo+'\'')
parser.set('org.gnome.login-screen', 'banner-message-enable', 'true')
parser.set('org.gnome.login-screen', 'banner-message-text', '\''+name+'\'')
with open('/etc/gdm3/greeter.gsettings', 'w') as fp:
parser.write(fp)
def setDefault():
setData("IES Sta Eulalia",defaultImage)
setConfigUsername("default")
removePhoto()
# try connection
response = os.system("ping -c 1 ldap")
if response != 0:
exit()
# get mac
mac = getHwAddr('eth0')
# get hostname
hostname = socket.gethostname()
# get username
l = LdapConnection("ldap", "", "")
l.connectauth()
search = l.search("cn=DHCP Config","(dhcpHWAddress=ethernet " + mac + ")",["cn","uniqueIdentifier"])
try:
username = search[0][0][1]['uniqueIdentifier'][0].replace("user-name ","")
except:
username = ""
try:
ldap_hostname = search[0][0][1]['cn'][0]
except:
ldap_hostname = ""
u = getConfigUsername()
if username == "" and u!="default":
setDefault()
elif username!="" and username!=u:
# get user data
search = l.search("ou=People","(uid="+username+")",["cn","jpegPhoto"])
try:
name = search[0][0][1]['cn'][0]
except:
name = username
try:
photo = search[0][0][1]['jpegPhoto'][0]
except:
photo = ""
setConfigUsername(username)
if photo=="":
photo = defaultImage
removePhoto()
elif photo!="":
f = open(dir+"/userphoto.jpg","w+")
f.write(photo)
f.close()
photo = dir+"/userphoto.jpg"
# Set gdm banner
setData(name, photo)
if hostname != ldap_hostname:
f = open("/etc/hostname","w")
f.write(ldap_hostname)
f.close()
f = open("/etc/hosts","w")
f.write("127.0.0.1 localhost\n")
f.write("127.0.1.1 "+ldap_hostname+"\n\n")
f.write("# The following lines are desirable for IPv6 capable hosts\n")
f.write("::1 localhost ip6-localhost ip6-loopback\n")
f.write("fe00::0 ip6-localnet\n")
f.write("ff00::0 ip6-mcastprefix\n")
f.write("ff02::1 ip6-allnodes\n")
f.write("ff02::2 ip6-allrouters")
f.close()
os.system("hostname -F /etc/hostname")