package.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #!/usr/bin/python
  2. import os
  3. import grp
  4. import pwd
  5. import shutil
  6. import libuser
  7. ### Helper methods
  8. def hav(method, *args):
  9. try:
  10. call("baselayout", "User.Manager", method, args)
  11. except:
  12. pass
  13. def deleteGroup(group):
  14. try:
  15. gid = grp.getgrnam(group)[2]
  16. # deleteGroup(gid)
  17. hav("deleteGroup", gid)
  18. except KeyError:
  19. pass
  20. def deleteUser(user):
  21. try:
  22. uid = pwd.getpwnam(user)[2]
  23. # deleteUser(uid, delete_files)
  24. hav("deleteUser", uid, False)
  25. except KeyError:
  26. pass
  27. def setGroupId(group_name, gid):
  28. ctx = libuser.admin()
  29. group = ctx.lookupGroupByName(group_name)
  30. if group:
  31. group.set(libuser.GIDNUMBER, [gid])
  32. ctx.modifyGroup(group)
  33. def setUserId(user_name, uid):
  34. ctx = libuser.admin()
  35. user = ctx.lookupUserByName(user_name)
  36. if user:
  37. user.set(libuser.UIDNUMBER, [uid])
  38. ctx.modifyUser(user)
  39. def migrateUsers():
  40. # build user -> group map for migration (hopefully we'll drop this in 2012)
  41. migration = []
  42. migrationMap = {
  43. "removable" : ["cdrom", "plugdev"],
  44. "pnp" : ["lp", "floppy"],
  45. "pnpadmin" : ["lpadmin"],
  46. }
  47. for user in pwd.getpwall():
  48. groups = set()
  49. if 1000 <= user.pw_uid < 65534:
  50. for group in grp.getgrall():
  51. if user.pw_name in group.gr_mem:
  52. groups.add(group.gr_name)
  53. for oldGroup, newGroups in migrationMap.items():
  54. if oldGroup in groups:
  55. #groups.remove(oldGroup)
  56. groups.update(newGroups)
  57. if groups:
  58. migration.append((user.pw_uid, list(groups)))
  59. # Migrate regular user groups
  60. for user, group in migration:
  61. # setUser(uid, realname, homedir, shell, passwd, groups)
  62. hav("setUser", user, "", "", "", "", group)
  63. # Big ugly zemberek-openoffice hack
  64. def zemberek_hack():
  65. import re
  66. f = "/var/db/comar3/scripts/System.Package/zemberek_openoffice.py"
  67. if os.path.exists(f):
  68. postContent = open(f).read()
  69. pattern = re.compile('oxt"\)\[0\]$', re.M)
  70. postContent = re.sub(pattern, 'oxt")', postContent)
  71. postContent = re.sub("raise Exception", "print", postContent)
  72. postFile = open(f, 'w')
  73. postFile.write(postContent)
  74. ### COMAR methods
  75. def postInstall(fromVersion, fromRelease, toVersion, toRelease):
  76. # We don't want to overwrite an existing file during upgrade
  77. specialFiles = ["passwd", "shadow", "group", "fstab", "hosts", "ld.so.conf", "resolv.conf"]
  78. for specialFile in specialFiles:
  79. if not os.path.exists("/etc/%s" % specialFile):
  80. shutil.copy("/usr/share/baselayout/%s" % specialFile, "/etc")
  81. shutil.copy("/etc/passwd", "/usr/share/baselayout/passwd.backup")
  82. shutil.copy("/etc/group", "/usr/share/baselayout/group.backup")
  83. if fromRelease and int(fromRelease) < 143:
  84. # Release 143 starts using /etc/ld.so.conf.d. Copy ld.so.conf
  85. # for "include" statement.
  86. shutil.copy("/usr/share/baselayout/ld.so.conf", "/etc")
  87. ##################################
  88. # Merge new system groups
  89. # addGroup(gid, name)
  90. groups = (
  91. (7, "lp"),
  92. (10, "wheel"),
  93. (11, "cdrom"),
  94. (14, "lpadmin"),
  95. (19, "floppy"),
  96. (20, "dialout"),
  97. (22, "sshd"),
  98. (30, "squid"),
  99. (32, "rpc"),
  100. # libftdi need plugdev
  101. (46, "plugdev"),
  102. (50, "named"),
  103. # For systemd/var-lock.mount
  104. (54, "lock"),
  105. (60, "mysql"),
  106. (70, "postgres"),
  107. (80, "apache"),
  108. (90, "dovecot"),
  109. (100, "users"),
  110. (102, "input"),
  111. (103, "polkitd"),
  112. (104, "postfix"),
  113. (105, "postdrop"),
  114. (106, "smmsp"),
  115. (107, "locate"),
  116. (108, "utmp"),
  117. (109, "firebird"),
  118. (110, "dhcp"),
  119. (111, "ldap"),
  120. (112, "clamav"),
  121. (113, "ntlmaps"),
  122. (116, "colord"),
  123. (120, "avahi"),
  124. (121, "avahi-autoipd"),
  125. (123, "ntp"),
  126. (124, "gdm"),
  127. (130, "tss"),
  128. (131, "ejabberd"),
  129. (132, "tomcat"),
  130. (133, "ups"),
  131. (134, "partimag"),
  132. (135, "radiusd"),
  133. (136, "oprofile"),
  134. (137, "mediatomb"),
  135. # 'pulse' is for system wide PA daemon.
  136. (138, "pulse"),
  137. # In order to access to a system wide PA daemon,
  138. # a user should be a member of the 'pulse-access' group.
  139. (139, "pulse-access"),
  140. (141, "italc"),
  141. (142, "quassel"),
  142. (143, "bitlbee"),
  143. (144, "icecast"),
  144. (145, "virt"),
  145. (995, "vboxusers"),
  146. # Gnokii system user for the SMS daemon
  147. (146, "gnokii"),
  148. (150, "svn"),
  149. (151, "memcached"),
  150. (152, "rtkit"),
  151. # NetworkManager user for OpenConnect VPN helper
  152. (153, "nm-openconnect"),
  153. (160, "usbmuxd"),
  154. (161, "openvpn"),
  155. (162, "privoxy"),
  156. (163, "kvm"),
  157. (164, "qemu"),
  158. (165, "kdm"),
  159. (166, "polipo"),
  160. (167, "nginx"),
  161. (168, "guests"),
  162. (169, "ntop"),
  163. # COMAR profile groups
  164. (200, "pnp"),
  165. (201, "removable"),
  166. (204, "power"),
  167. (205, "pnpadmin"),
  168. # for RT jackaudio
  169. (206, "jackuser"),
  170. (207, "wireshark"),
  171. (209, "vdr"),
  172. (210, "ecryptfs"),
  173. (211, "slocate"),
  174. (212, "dansguardian"),
  175. )
  176. for gid, groupName in groups:
  177. try:
  178. group = grp.getgrnam(groupName)
  179. except KeyError:
  180. hav("addGroup", gid, groupName)
  181. else:
  182. if group.gr_gid != gid:
  183. setGroupId(groupName, gid)
  184. ##################################
  185. # Merge new system users
  186. # addUser(uid, nick, realname, homedir, shell, password, groups, grantedauths, blockedauths)
  187. users = (
  188. (4, "lp", "CUPS user", "/var/spool/cups", "/sbin/nologin", "", ["lp"], [], []),
  189. (15, "lpadmin", "CUPS administrator", "/var/spool/cups", "/sbin/nologin", "", ["lpadmin"], [], []),
  190. (20, "dialout", "Dialout", "/dev/null", "/bin/false", "", ["dialout"], [], []),
  191. (22, "sshd", "Privilege-separated SSH", "/var/empty/sshd", "/sbin/nologin", "", ["sshd"], [], []),
  192. (30, "squid", "Squid", "/var/cache/squid", "/bin/false", "", ["squid"], [], []),
  193. (32, "rpc", "Rpcbind daemon", "/var/lib/rpcbind", "/sbin/nologin", "", ["rpc"], [], []),
  194. (40, "named", "Bind", "/var/named", "/bin/false", "", ["named"], [], []),
  195. (60, "mysql", "MySQL", "/var/lib/mysql", "/bin/false", "", ["mysql"], [], []),
  196. (70, "postgres", "PostgreSQL", "/var/lib/postgresql", "/bin/bash", "", ["postgres"], [], []),
  197. (80, "apache", "Apache", "/dev/null", "/bin/false", "", ["apache", "svn"], [], []),
  198. (90, "dovecot", "Dovecot", "/dev/null", "/bin/false", "", ["dovecot"], [], []),
  199. (103, "polkitd", "PolicyKit", "/var/lib/polkit-1", "/bin/false", "", ["polkitd"], [], []),
  200. (104, "postfix", "Postfix", "/var/spool/postfix", "/bin/false", "", ["postfix"], [], []),
  201. (106, "smmsp", "smmsp", "/var/spool/mqueue", "/bin/false", "", ["smmsp"], [], []),
  202. (107, "colord", "colord colour management daemon", "/var/lib/colord", "/bin/false", "", ["colord"], [], []),
  203. (109, "firebird", "Firebird", "/opt/firebird", "/bin/false", "", ["firebird"], [], []),
  204. (110, "dhcp", "DHCP", "/dev/null", "/bin/false", "", ["dhcp"], [], []),
  205. (111, "ldap", "OpenLDAP", "/dev/null", "/bin/false", "", ["ldap"], [], []),
  206. (112, "clamav", "Clamav", "/dev/null", "/bin/false", "", ["clamav"], [], []),
  207. (113, "ntlmaps", "NTLMaps", "/dev/null", "/bin/false", "", ["ntlmaps"], [], []),
  208. (120, "avahi", "Avahi mDNS/DNS-SD Stack", "/run/avahi-daemon", "/sbin/nologin", "", ["avahi"], [], []),
  209. (121, "avahi-autoipd", "Avahi IPv4LL Stack", "/var/lib/avahi-autoipd", "/sbin/nologin", "", ["avahi-autoipd"], [], []),
  210. (123, "ntp", "NTP", "/dev/null", "/bin/false", "", ["ntp"], [], []),
  211. (124, "gdm", "gdm", "/var/lib/gdm", "/sbin/nologin", "", ["gdm"], [], []),
  212. (130, "tss", "tss", "/var/lib/tpm", "/bin/false", "", ["tss"], [], []),
  213. (131, "ejabberd", "Ejabberd", "/var/lib/ejabberd", "/bin/false", "", ["ejabberd"], [], []),
  214. (132, "tomcat", "Tomcat", "/var/lib/tomcat", "/bin/false", "", ["tomcat"], [], []),
  215. (133, "ups", "UPS", "/var/lib/nut", "/bin/false", "", ["ups", "dialout", "tty", "pnp"], [], []),
  216. (134, "partimag", "Partimage", "/var/lib/partimaged", "/bin/false", "", ["partimag"], [], []),
  217. (135, "radiusd", "Freeradius", "/dev/null", "/bin/false", "", ["radiusd"], [], []),
  218. (136, "oprofile", "oprofile", "/dev/null", "/bin/false", "", ["oprofile"], [], []),
  219. (137, "mediatomb", "mediatomb", "/dev/null", "/bin/false", "", ["mediatomb"], [], []),
  220. (138, "pulse", "PulseAudio System Daemon", "/run/pulse", "/bin/false", "", ["pulse", "pulse-access", "pulse-rt", "audio"], [], []),
  221. (139, "quasselcore", "Quassel IRC System", "/var/cache/quassel", "/bin/false", "", ["quassel"], [], []),
  222. (140, "bitlbee", "Bitlbee Gateway", "/var/lib/bitlbee", "/bin/false", "", ["bitlbee"], [], []),
  223. (141, "spamd", "Spamassassin Daemon", "/var/lib/spamd", "/bin/false", "", [], [], []),
  224. (145, "vboxadd", "VirtualBox Guest Additions", "/dev/null", "/bin/false", "", [], [], []),
  225. (146, "gnokii", "Gnokii system user", "/", "/sbin/nologin", "", ["gnokii"], [], []),
  226. (150, "svn", "Subversion", "/dev/null", "/bin/false", "", ["svn"], [], []),
  227. (151, "icecast", "Icecast Server", "/dev/null", "/bin/false", "", ["icecast"], [], []),
  228. (152, "memcached", "Memcached daemon", "/run/memcached", "/bin/false", "", ["memcached"], [], []),
  229. (153, "rtkit", "RealtimeKit", "/proc", "/sbin/nologin", "", ["rtkit"], [], []),
  230. (154, "nm-openconnect", "NetworkManager user for OpenConnect", "/", "/sbin/nologin", "", ["nm-openconnect"], [], []),
  231. (160, "usbmuxd", "usbmuxd daemon", "/", "/sbin/nologin", "", ["usbmuxd"], [], []),
  232. (161, "openvpn", "OpenVPN", "/etc/openvpn", "/sbin/nologin", "", ["openvpn"], [], []),
  233. (162, "privoxy", "Privoxy", "/etc/privoxy", "/sbin/nologin", "", ["privoxy"], [], []),
  234. (163, "qemu", "qemu user", "/", "/sbin/nologin", "", ["qemu", "kvm"], [], []),
  235. (164, "polipo", "polipo user", "/", "/sbin/nologin", "", ["polipo"], [], []),
  236. (166, "nginx", "nginx user", "/etc/nginx", "/sbin/nologin", "", ["nginx"], [], []),
  237. (167, "ntop", "ntop user", "/var/lib/ntop", "/sbin/nologin", "", ["ntop"], [], []),
  238. (168, "smolt", "smolt user", "/dev/null", "/bin/false", "", [], [], []),
  239. (169, "svxlink", "Svxlink Daemon", "/", "/sbin/nologin", "", ["daemon", "audio", "dialout"], [], []),
  240. (170, "dansguardian", "Dansguardian web content filter", "/usr/share/dansguardian", "/sbin/nologin", "", ["dansguardian"], [], []),
  241. (200, "pnp", "PnP", "/dev/null", "/bin/false", "", ["pnp"], [], []),
  242. (250, "mpd", "Music Player Daemon", "/var/lib/mpd", "/bin/false", "", ["audio", "pulse", "pulse-access", "pulse-rt"], [], []),
  243. (252, "vdr", "VDR User", "/var/vdr", "/bin/false", "", ["audio", "video", "cdrom", "dialout"], [], []),
  244. )
  245. for uid, nick, realname, homedir, shell, password, groups, grantedauths, blockedauths in users:
  246. try:
  247. user = pwd.getpwnam(nick)
  248. except KeyError:
  249. hav("addUser", uid, nick, realname, homedir, shell, password, groups, grantedauths, blockedauths)
  250. else:
  251. if user.pw_uid == uid:
  252. # setUser(uid, realname, homedir, shell, passwd, groups)
  253. hav("setUser", uid, realname, homedir, shell, password, groups)
  254. else:
  255. setUserId(nick, uid)
  256. # Migrate users to their new groups if any
  257. migrateUsers()
  258. # We should only install empty files if these files don't already exist.
  259. if not os.path.exists("/var/log/lastlog"):
  260. os.system("/bin/touch /var/log/lastlog")
  261. if not os.path.exists("/run/utmp"):
  262. os.system("/usr/bin/install -m 0664 -g utmp /dev/null /run/utmp")
  263. if not os.path.exists("/var/log/wtmp"):
  264. os.system("/usr/bin/install -m 0664 -g utmp /dev/null /var/log/wtmp")
  265. # Enable shadow groups
  266. os.system("/usr/sbin/grpconv")
  267. os.system("/usr/sbin/grpck -r &>/dev/null")
  268. # Create /root if not exists
  269. if not os.path.exists("/root/"):
  270. shutil.copytree("/etc/skel", "/root")
  271. os.chown("/root", 0, 0)
  272. os.chmod("/root", 0700)
  273. # Tell init to reload new inittab
  274. os.system("/sbin/telinit q")
  275. # Save user defined DNS
  276. if not os.access("/etc/resolv.default.conf", os.R_OK):
  277. os.system("cp /etc/resolv.conf /etc/resolv.default.conf")
  278. # Apply zemberek hack
  279. zemberek_hack()
  280. # Fix permissions of /var/lock folder
  281. os.chown("/var/lock", 0, 54)
  282. os.chmod("/var/lock", 0775)