You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

932 lines
34 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Mir Calculate. http://www.calculate-linux.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from collections import namedtuple
  16. import sys
  17. from calculate.lib.utils.files import readLinesFile, listDirectory
  18. from calculate.ldap.ldap import Ldap
  19. from calculate.lib.utils.common import getPagesInterval
  20. import os
  21. from os import path
  22. import shutil
  23. _ = lambda x: x
  24. from calculate.lib.cl_lang import (setLocalTranslate, getLazyLocalTranslate)
  25. from calculate.lib.cl_ldap import ldap, LDAPConnectError, LDAPBadSearchFilter
  26. setLocalTranslate('cl_unix3', sys.modules[__name__])
  27. __ = getLazyLocalTranslate(_)
  28. class UnixError(Exception):
  29. pass
  30. UnixGroup = namedtuple("UnixGroup", ['group_name', 'gid', 'user_list',
  31. 'comment'])
  32. class GroupFile(object):
  33. def __init__(self, group_fn='/etc/group'):
  34. self.group_fn = group_fn
  35. def __iter__(self):
  36. for line in (x for x in readLinesFile(self.group_fn)
  37. if not x.startswith("#")):
  38. data = line.strip().split(':')[:4]
  39. if len(data) >= 4 and data[2].isdigit():
  40. yield UnixGroup(data[0], int(data[2]),
  41. filter(None, data[3].split(",")),
  42. "/etc/group")
  43. class UnixGroups(object):
  44. """
  45. Набор методов для нахождения параметров группы и сопаставления GID,
  46. названия
  47. """
  48. groups = GroupFile()
  49. exception = UnixError
  50. ldap_connect = None
  51. groups_dn = ""
  52. def __init__(self, ldap_connect, groups_dn):
  53. self.ldap_connect = ldap_connect
  54. self.groups_dn = groups_dn
  55. def search_system_group_name(self, name):
  56. for group in (x for x in self.groups if x.group_name == name):
  57. return group
  58. else:
  59. return None
  60. def search_system_group_id(self, gid):
  61. for group in (x for x in self.groups if x.gid == gid):
  62. return group
  63. else:
  64. return None
  65. def iterate_ldap_group(self, search_filter, offset=None, count=None):
  66. ldap_connect = self.ldap_connect
  67. if ldap_connect:
  68. base_dn = self.groups_dn
  69. if offset is not None:
  70. below = lambda x: x < offset
  71. else:
  72. below = lambda x: False
  73. if count is not None:
  74. above = lambda x: x >= offset + count
  75. else:
  76. above = lambda x: False
  77. for i, group in enumerate(x[0][1] for x in ldap_connect.ldap_search(
  78. base_dn, ldap.SCOPE_ONELEVEL, search_filter)):
  79. if below(i):
  80. continue
  81. if above(i):
  82. break
  83. yield UnixGroup(
  84. group['cn'][0],
  85. int(group['gidNumber'][0]),
  86. sorted(group.get('memberUid', [])),
  87. group['description'][0])
  88. def search_ldap_group(self, search_filter):
  89. for group in self.iterate_ldap_group(search_filter):
  90. return group
  91. return None
  92. def search_ldap_group_name(self, value):
  93. return self.search_ldap_group("cn=%s" % value)
  94. def gid_to_name(self, value):
  95. if value.isdigit():
  96. value = int(value)
  97. group = (self.search_ldap_group("gidNumber=%d" % value) or
  98. self.search_system_group_id(value))
  99. if group:
  100. return group.group_name
  101. return value
  102. def _groupname(self, name, comment):
  103. if comment != name:
  104. return "{0} ({1})".format(name, comment)
  105. else:
  106. return name
  107. def ldap_group_list(self):
  108. groups = list(x for x in self.iterate_ldap_group("cn=*"))
  109. return [(x.group_name, self._groupname(x.group_name, x.comment))
  110. for x in sorted(groups, key=lambda k: k.gid)]
  111. def ldap_group_names(self):
  112. return list(x.group_name for x in self.iterate_ldap_group("cn=*"))
  113. def check_group(self, value):
  114. ldap_group = self.search_ldap_group_name(value)
  115. unix_group = ldap_group or self.search_system_group_name(value)
  116. if not unix_group:
  117. raise self.exception(_("Group %s not found") % value)
  118. if not ldap_group and unix_group:
  119. raise self.exception(
  120. _("You should create group %s (%d) in Unix service") %
  121. (unix_group.group_name, unix_group.gid))
  122. def group_dn(self, group):
  123. return "cn=%s,%s" % (group, self.groups_dn)
  124. def add_users_to_group(self, users, group):
  125. try:
  126. self.ldap_connect.ldap_modify_attrs(
  127. self.group_dn(group),
  128. ((ldap.MOD_ADD, 'memberUid', user) for user in users))
  129. except LDAPConnectError as e:
  130. raise UnixError(_("Failed to modify group in LDAP: %s") % str(e))
  131. def update_group_id(self, group, gid):
  132. try:
  133. self.ldap_connect.ldap_modify_attrs(
  134. self.group_dn(group),
  135. [(ldap.MOD_REPLACE, 'gidNumber', gid)])
  136. except LDAPConnectError as e:
  137. raise UnixError(_("Failed to update group id in LDAP: %s") % str(e))
  138. def update_group_comment(self, group, comment):
  139. try:
  140. self.ldap_connect.ldap_modify_attrs(
  141. self.group_dn(group),
  142. [(ldap.MOD_REPLACE, 'description', comment)])
  143. except LDAPConnectError as e:
  144. raise UnixError(
  145. _("Failed to update group comment in LDAP: %s") % str(e))
  146. def remove_users_from_group(self, users, group):
  147. try:
  148. self.ldap_connect.ldap_modify_attrs(
  149. self.group_dn(group),
  150. ((ldap.MOD_DELETE, 'memberUid', user) for user in users))
  151. except LDAPConnectError as e:
  152. raise UnixError(
  153. _("Failed to remove users from group in LDAP: %s") % str(e))
  154. def rename_group(self, oldname, newname):
  155. try:
  156. old_dn = self.group_dn(oldname)
  157. new_dn = "cn=%s" % newname
  158. self.ldap_connect.ldap_modify_dn(old_dn, new_dn)
  159. except LDAPConnectError as e:
  160. raise UnixError(
  161. _("Failed to rename the group in LDAP: %s") % str(e))
  162. def remove_group(self, group):
  163. try:
  164. self.ldap_connect.ldap_remove_dn(self.group_dn(group))
  165. except LDAPConnectError as e:
  166. raise UnixError(
  167. _("Failed to remove the group in LDAP: %s") % str(e))
  168. UnixUser = namedtuple(
  169. "UnixUser", ['username', 'uid', 'gid', 'comment', 'homedir', 'shell',
  170. 'visible', 'lock', 'pass_set'])
  171. class PasswdFile(object):
  172. def __init__(self, passwd_fn='/etc/passwd'):
  173. self.passwd_fn = passwd_fn
  174. def __iter__(self):
  175. for line in (x for x in readLinesFile(self.passwd_fn)
  176. if not x.startswith("#")):
  177. data = line.strip().split(':')[:7]
  178. if len(data) >= 7 and data[2].isdigit() and data[3].isdigit():
  179. yield UnixUser(data[0], int(data[2]),
  180. int(data[3]), data[4], data[5], data[6], True,
  181. False, False)
  182. class UnixUsers(object):
  183. """
  184. Набор методов для нахождения параметров пользователя и сопаставления UID,
  185. названия
  186. """
  187. passwd = PasswdFile()
  188. exception = UnixError
  189. ldap_connect = None
  190. users_dn = ""
  191. DeletedPassword = "crypt{xxx}"
  192. def __init__(self, ldap_connect, users_dn):
  193. self.ldap_connect = ldap_connect
  194. self.users_dn = users_dn
  195. def search_system_user_name(self, name):
  196. for user in (x for x in self.passwd if x.username == name):
  197. return user
  198. else:
  199. return None
  200. def search_system_user_id(self, uid):
  201. uid = int(uid)
  202. for user in (x for x in self.passwd if x.uid == uid):
  203. return user
  204. else:
  205. return None
  206. def iterate_ldap_user(self, search_filter, offset=None, count=None):
  207. ldap_connect = self.ldap_connect
  208. if ldap_connect:
  209. base_dn = self.users_dn
  210. if offset is not None:
  211. below = lambda x: x < offset
  212. else:
  213. below = lambda x: False
  214. if count is not None:
  215. above = lambda x: x >= offset + count
  216. else:
  217. above = lambda x: False
  218. for i, user in enumerate(x[0][1] for x in ldap_connect.ldap_search(
  219. base_dn, ldap.SCOPE_ONELEVEL, search_filter)):
  220. if below(i):
  221. continue
  222. if above(i):
  223. break
  224. yield UnixUser(
  225. user['uid'][0],
  226. int(user['uidNumber'][0]),
  227. int(user['gidNumber'][0]),
  228. user['cn'][0],
  229. user['homeDirectory'][0],
  230. user['loginShell'][0],
  231. self.flag_to_visible(user['shadowFlag'][0]),
  232. self.flag_to_lock(user['shadowExpire'][0]),
  233. self.has_password(user['userPassword'][0])
  234. if 'userPassword' in user else None,
  235. )
  236. def search_ldap_user(self, search_filter):
  237. for user in self.iterate_ldap_user(search_filter):
  238. return user
  239. return None
  240. def has_password(self, password):
  241. if password and password != self.DeletedPassword:
  242. return True
  243. return False
  244. def search_ldap_user_name(self, value):
  245. return self.search_ldap_user("uid=%s" % value)
  246. def search_ldap_user_id(self, value):
  247. return self.search_ldap_user("uidNumber=%s" % value)
  248. def get_primary_gids(self):
  249. ldap_connect = self.ldap_connect
  250. if ldap_connect:
  251. base_dn = self.users_dn
  252. return list(ldap_connect.ldap_simple_search(
  253. base_dn, "uid=*", "gidNumber"))
  254. return []
  255. def get_password_hash(self, username):
  256. ldap_connect = self.ldap_connect
  257. if ldap_connect:
  258. base_dn = self.users_dn
  259. for pw in ldap_connect.ldap_simple_search(
  260. self.users_dn, "uid=%s" % username, "userPassword"):
  261. return pw
  262. return ""
  263. def uid_to_name(self, value):
  264. if value.isdigit():
  265. value = int(value)
  266. user = (self.search_ldap_user("uidNumber=%d" % value) or
  267. self.search_system_user_id(value))
  268. if user:
  269. return user.username
  270. return value
  271. def _username(self, name, comment):
  272. if comment != name:
  273. return "{0} ({1})".format(name, comment)
  274. else:
  275. return name
  276. def ldap_user_list(self):
  277. users = list(x for x in self.iterate_ldap_user("uid=*"))
  278. return [(x.username, self._username(x.username, x.comment))
  279. for x in sorted(users, key=lambda y: y.uid)]
  280. def ldap_user_names(self):
  281. return list(x.username for x in self.iterate_ldap_user("uid=*"))
  282. # def check_user(self, value):
  283. # ldap_user = self.search_ldap_user_name(value)
  284. # unix_user = ldap_user or self.search_system_user_name(value)
  285. # if not unix_user:
  286. # raise VariableError(_("User %s not found") % value)
  287. # if not ldap_user and unix_user:
  288. # raise VariableError(
  289. # _("You should create group %s (%d) in Unix service") %
  290. # (unix_group.group_name, unix_group.gid))
  291. def user_dn(self, user):
  292. return "uid=%s,%s" % (user, self.users_dn)
  293. def update_user_group_id(self, user, gid):
  294. try:
  295. self.ldap_connect.ldap_modify_attrs(
  296. self.user_dn(user),
  297. [(ldap.MOD_REPLACE, 'gidNumber', gid)])
  298. except LDAPConnectError as e:
  299. raise UnixError(
  300. _("Failed to modify user primary group in LDAP: %s") % str(e))
  301. def visible_to_flag(self, visible):
  302. return "1" if visible else "0"
  303. def flag_to_visible(self, flag):
  304. return flag == "1"
  305. def lock_to_flag(self, lock):
  306. return "1" if lock else "-1"
  307. def flag_to_lock(self, flag):
  308. return flag == "1"
  309. def modify_user(self, username, pw=None, gid=None, shell=None, visible=None,
  310. lock=None, comment=None, homedir=None):
  311. attributes = []
  312. if pw is not None:
  313. attributes.append((ldap.MOD_REPLACE, 'userPassword', str(pw)))
  314. if gid is not None:
  315. attributes.append((ldap.MOD_REPLACE, 'gidNumber', str(gid)))
  316. if shell is not None:
  317. attributes.append((ldap.MOD_REPLACE, 'loginShell', shell))
  318. if comment is not None:
  319. attributes.append((ldap.MOD_REPLACE, 'cn', comment))
  320. if homedir is not None:
  321. attributes.append((ldap.MOD_REPLACE, 'homeDirectory', homedir))
  322. if visible is not None:
  323. attributes.append(
  324. (ldap.MOD_REPLACE, 'shadowFlag',
  325. self.visible_to_flag(visible)))
  326. if lock is not None:
  327. attributes.append(
  328. (ldap.MOD_REPLACE, 'shadowExpire', self.lock_to_flag(lock)))
  329. try:
  330. self.ldap_connect.ldap_modify_attrs(
  331. self.user_dn(username), attributes)
  332. except LDAPConnectError as e:
  333. raise UnixError(
  334. _("Failed to modify user parameters in LDAP: %s") % str(e))
  335. def remove_user(self, username):
  336. try:
  337. self.ldap_connect.ldap_remove_dn(self.user_dn(username))
  338. except LDAPConnectError as e:
  339. raise UnixError(_("Failed to remove user from LDAP: %s") % str(e))
  340. class Unix(Ldap):
  341. """Основной объект для выполнения действий связанных
  342. с настройкой Unix сервиса
  343. """
  344. class Method(object):
  345. Setup = "unix_setup"
  346. UserAdd = "unix_useradd"
  347. UserMod = "unix_usermod"
  348. UserDel = "unix_userdel"
  349. UserShow = "unix_usershow"
  350. GroupAdd = "unix_groupadd"
  351. GroupMod = "unix_groupmod"
  352. GroupDel = "unix_groupdel"
  353. GroupShow = "unix_groupshow"
  354. Passwd = "unix_passwd"
  355. All = (
  356. Setup, UserAdd, UserMod, UserDel, GroupAdd, GroupMod, GroupDel,
  357. Passwd,
  358. UserShow, GroupShow)
  359. service_name = "unix"
  360. def init(self):
  361. pass
  362. def rename_group(self, oldname, newname):
  363. """
  364. Переименовать группу
  365. """
  366. if oldname != newname and newname:
  367. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  368. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  369. ug = UnixGroups(ldap_connect, groups_dn)
  370. ug.rename_group(oldname, newname)
  371. return True
  372. def update_group_comment(self, group, comment):
  373. """
  374. Переименовать группу
  375. """
  376. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  377. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  378. ug = UnixGroups(ldap_connect, groups_dn)
  379. ug.update_group_comment(group, comment)
  380. return True
  381. def update_group_id(self, group, gid, old_gid):
  382. """
  383. Обновить gid группы
  384. """
  385. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  386. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  387. ug = UnixGroups(ldap_connect, groups_dn)
  388. ug.update_group_id(group, str(gid))
  389. users_dn = self.clVars.Get('ld_unix_users_dn')
  390. uu = UnixUsers(ldap_connect, users_dn)
  391. for user in uu.iterate_ldap_user("gidNumber=%d" % old_gid):
  392. self.printSUCCESS(_("Change primary group for user {user}").format(
  393. user=user.username))
  394. uu.update_user_group_id(user.username, str(gid))
  395. return True
  396. def remove_group(self, groupname):
  397. """
  398. Удалить группу
  399. """
  400. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  401. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  402. ug = UnixGroups(ldap_connect, groups_dn)
  403. ug.remove_group(groupname)
  404. return True
  405. def remove_user(self, username):
  406. """
  407. Удалить пользователя
  408. """
  409. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  410. users_dn = self.clVars.Get('ld_unix_users_dn')
  411. uu = UnixUsers(ldap_connect, users_dn)
  412. uu.remove_user(username)
  413. return True
  414. def _add_users_in_group(self, users, groupname):
  415. """
  416. Добавить пользователей в группу
  417. """
  418. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  419. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  420. ug = UnixGroups(ldap_connect, groups_dn)
  421. group = ug.search_ldap_group_name(groupname)
  422. if group:
  423. exists_users = group.user_list
  424. new_users = set(users) - set(exists_users)
  425. ug.add_users_to_group(new_users, groupname)
  426. return True
  427. return False
  428. def remove_users_from_group(self, users, groupname):
  429. self._remove_users_from_group(users, groupname)
  430. self.printSUCCESS(_("Users {logins} was removed "
  431. "from group {group}").format(
  432. logins=", ".join(users), group=groupname))
  433. return True
  434. def add_users_in_group(self, users, groupname):
  435. self._add_users_in_group(users, groupname)
  436. self.printSUCCESS(_("Users {logins} was added "
  437. "to group {group}").format(
  438. logins=", ".join(users), group=groupname))
  439. return True
  440. def _remove_users_from_group(self, users, groupname):
  441. """
  442. Удалить пользователей из групп
  443. """
  444. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  445. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  446. ug = UnixGroups(ldap_connect, groups_dn)
  447. group = ug.search_ldap_group_name(groupname)
  448. if group:
  449. exists_users = group.user_list
  450. remove_users = set(exists_users) & set(users)
  451. ug.remove_users_from_group(list(remove_users), groupname)
  452. return True
  453. return False
  454. def add_user_in_groups(self, user, groups):
  455. """
  456. Добавить пользователя в группы
  457. """
  458. for group in groups:
  459. self._add_users_in_group([user], group)
  460. self.printSUCCESS(_("User {login} added "
  461. "to groups {groups}").format(
  462. login=user, groups=", ".join(groups)))
  463. return True
  464. def remove_user_from_groups(self, user, groups):
  465. """
  466. Удалить пользователя из группы
  467. """
  468. for group in groups:
  469. self._remove_users_from_group([user], group)
  470. self.printSUCCESS(_("User {login} removed "
  471. "from groups {groups}").format(
  472. login=user, groups=", ".join(groups)))
  473. return True
  474. def modify_user(self, login, pw, pw_delete, gid, shell, visible, lock,
  475. comment, homedir):
  476. """"
  477. Изменить параметры пользователя в LDAP
  478. """
  479. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  480. users_dn = self.clVars.Get('ld_unix_users_dn')
  481. uu = UnixUsers(ldap_connect, users_dn)
  482. user = uu.search_ldap_user_name(login)
  483. if user:
  484. params = {}
  485. if gid is not None and user.gid != gid:
  486. params['gid'] = str(gid)
  487. self.printSUCCESS(
  488. _("Changed primary group for user %s") % user.username)
  489. if comment is not None and user.comment != comment:
  490. params['comment'] = comment
  491. self.printSUCCESS(
  492. _("Changed comment for user %s") % user.username)
  493. if homedir is not None and user.homedir != homedir:
  494. params['homedir'] = homedir
  495. self.printSUCCESS(
  496. _("Changed home directory for user %s") % user.username)
  497. if pw != UnixUsers.DeletedPassword or pw_delete:
  498. params['pw'] = pw
  499. if pw_delete:
  500. self.printSUCCESS(
  501. _("Removed password for user %s") % user.username)
  502. else:
  503. self.printSUCCESS(
  504. _("Changed password for user %s") % user.username)
  505. if shell and user.shell != shell:
  506. params['shell'] = shell
  507. self.printSUCCESS(
  508. _("Changed shell for user %s") % user.username)
  509. if visible is not None:
  510. if user.visible != visible:
  511. params['visible'] = visible
  512. if visible:
  513. self.printSUCCESS(
  514. _("User %s is visible") % user.username)
  515. else:
  516. self.printSUCCESS(
  517. _("User %s is unvisible") % user.username)
  518. else:
  519. if visible:
  520. self.printWARNING(
  521. _("User %s is visible already") % user.username)
  522. else:
  523. self.printWARNING(
  524. _("User %s is unvisible already") % user.username)
  525. if lock is not None:
  526. if user.lock != lock:
  527. params['lock'] = lock
  528. if lock:
  529. self.printSUCCESS(
  530. _("User %s is locked") % user.username)
  531. else:
  532. self.printSUCCESS(
  533. _("User %s is unlocked") % user.username)
  534. else:
  535. if lock:
  536. self.printWARNING(
  537. _("User %s is locked already") % user.username)
  538. else:
  539. self.printWARNING(
  540. _("User %s is unlocked already") % user.username)
  541. uu.modify_user(user.username, **params)
  542. else:
  543. raise UnixError(_("User %s not found") % user)
  544. return True
  545. def move_home_directory(self, homedir, new_homedir):
  546. """
  547. Преместить домашнюю директорию пользователя
  548. """
  549. try:
  550. dirname = path.dirname(new_homedir)
  551. if not path.exists(dirname):
  552. os.makedirs(dirname)
  553. os.rename(homedir, new_homedir)
  554. except (OSError, IOError):
  555. raise UnixError(_("Failed to move home directory"))
  556. return True
  557. def create_home_directory(self, homedir, uid, gid, skel):
  558. """
  559. Создать домашнюю директорию пользователя
  560. """
  561. try:
  562. os.makedirs(homedir, 0700)
  563. if path.exists(skel):
  564. for fn in listDirectory(skel):
  565. src_fn = path.join(skel, fn)
  566. dst_fn = path.join(homedir, fn)
  567. if path.isdir(src_fn):
  568. shutil.copytree(src_fn, dst_fn, True)
  569. elif path.islink(src_fn):
  570. link_src = os.readlink(src_fn)
  571. os.symlink(link_src, dst_fn)
  572. else:
  573. shutil.copy2(src_fn, dst_fn)
  574. for root, dns, fns in os.walk(homedir):
  575. for fn in (path.join(root, x) for x in dns + fns):
  576. os.lchown(fn, uid, gid)
  577. os.lchown(homedir, uid, gid)
  578. except (OSError, IOError) as e:
  579. self.printWARNING(str(e))
  580. raise UnixError(_("Failed to create user home directory"))
  581. return True
  582. def show_groups(self, groupname, fields, count, offset):
  583. dv = self.clVars
  584. fields = ["ur_unix_group_name"] + list(fields)
  585. head = [dv.getInfo(x).label for x in fields]
  586. body = []
  587. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  588. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  589. ug = UnixGroups(ldap_connect, groups_dn)
  590. users_dn = self.clVars.Get('ld_unix_users_dn')
  591. uu = UnixUsers(ldap_connect, users_dn)
  592. filters = (self.clVars.getInfo(x)
  593. for x in ('cl_unix_group_filter_name',
  594. 'cl_unix_group_filter_id',
  595. 'cl_unix_group_filter_comment',
  596. 'cl_unix_group_filter_users'))
  597. filters = [x.test for x in filters if x.enabled()]
  598. def all_users(group):
  599. for user in sorted(uu.iterate_ldap_user("gidNumber=%d" % group.gid),
  600. key=lambda x: x.username):
  601. yield "<%s>" % user.username
  602. for user in group.user_list:
  603. yield user
  604. variables_mapping = {
  605. 'ur_unix_group_name': lambda group: group.group_name,
  606. 'ur_unix_group_id': lambda group: str(group.gid),
  607. 'ur_unix_group_comment': lambda group: group.comment,
  608. 'ur_unix_group_users': lambda group: ", ".join(all_users(group))
  609. }
  610. mapping = {'ur_unix_group_name': 'ur_unix_group_name_exists'}
  611. maxi = 0
  612. try:
  613. for i, group in enumerate(sorted(
  614. (group for group in
  615. ug.iterate_ldap_group("cn=*")
  616. if all(x(group) for x in filters)),
  617. key=lambda x: x.group_name)):
  618. maxi = i
  619. if offset <= i < offset + count:
  620. body.append(
  621. [variables_mapping.get(x)(group) for x in fields])
  622. except LDAPBadSearchFilter:
  623. raise UnixError(_("Wrong group pattern"))
  624. table_fields = [mapping.get(x, '') for x in fields]
  625. if not body:
  626. body = [[]]
  627. dv.Invalidate('ur_unix_group_count')
  628. if any(body):
  629. head_message = _("Groups")
  630. elif dv.GetInteger("ur_unix_group_count"):
  631. head_message = _("Groups not found")
  632. else:
  633. head_message = _("No groups")
  634. self.printTable(
  635. head_message, head, body,
  636. fields=table_fields,
  637. onClick='unix_groupmod' if any(table_fields) else None,
  638. addAction='unix_groupadd',
  639. records=str(maxi))
  640. if any(body):
  641. num_page, count_page = getPagesInterval(
  642. count, offset, maxi)
  643. self.printSUCCESS(_('page %d from ') % num_page + str(count_page))
  644. return True
  645. def show_group(self, groupname):
  646. dv = self.clVars
  647. list_group_name = sorted(dv.Choice('cl_core_group'))
  648. if not list_group_name:
  649. self.printSUCCESS(_("No groups"))
  650. head = [_('Field'), _('Value')]
  651. body = []
  652. fields = [
  653. 'ur_unix_group_name',
  654. 'ur_unix_group_id',
  655. 'ur_unix_group_comment',
  656. 'ur_unix_group_users'
  657. ]
  658. def get_primary_users(gid):
  659. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  660. users_dn = self.clVars.Get('ld_unix_users_dn')
  661. uu = UnixUsers(ldap_connect, users_dn)
  662. for user in sorted(uu.iterate_ldap_user("gidNumber=%d" % gid),
  663. key=lambda x: x.username):
  664. yield "<%s>" % user.username
  665. self.clVars.Set('ur_unix_group_name', groupname, True)
  666. gid = self.clVars.GetInteger('ur_unix_group_id')
  667. for varname in fields:
  668. varval = self.clVars.Get(varname)
  669. varobj = self.clVars.getInfo(varname)
  670. if varname == 'ur_unix_group_users':
  671. varval = (sorted(get_primary_users(gid))
  672. + sorted(varval))
  673. if "list" in varobj.type:
  674. varval = "\n".join(varval)
  675. body.append([varobj.label or "", varval])
  676. if body:
  677. self.printTable(_("Group info"), head, body,
  678. onClick="unix_groupmod",
  679. records="0",
  680. fields=["ur_unix_group_name_exists:%s" % groupname])
  681. return True
  682. def show_users(self, login, fields, count, offset):
  683. dv = self.clVars
  684. fields = ["ur_unix_login"] + list(fields)
  685. head = [dv.getInfo(x).label for x in fields]
  686. body = []
  687. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  688. users_dn = self.clVars.Get('ld_unix_users_dn')
  689. uu = UnixUsers(ldap_connect, users_dn)
  690. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  691. ug = UnixGroups(ldap_connect, groups_dn)
  692. yesno_map = {True: _("Yes"),
  693. False: _("No"),
  694. None: _("Unavailable")}
  695. yesno = lambda x: yesno_map.get(x, _("Failed value"))
  696. filters = (self.clVars.getInfo(x)
  697. for x in ('cl_unix_user_filter_login',
  698. 'cl_unix_user_filter_pw_set',
  699. 'cl_unix_user_filter_comment',
  700. 'cl_unix_user_filter_uid',
  701. 'cl_unix_user_filter_gid',
  702. 'cl_unix_user_filter_home_path',
  703. 'cl_unix_user_filter_shell',
  704. 'cl_unix_user_filter_groups',
  705. 'cl_unix_user_filter_visible_set',
  706. 'cl_unix_user_filter_lock_set',
  707. ))
  708. filters = [x.test for x in filters if x.enabled()]
  709. variables_mapping = {
  710. 'ur_unix_uid': lambda user: user.uid,
  711. 'ur_unix_login': lambda user: user.username,
  712. 'ur_unix_comment': lambda user: user.comment,
  713. 'ur_unix_lock_set': lambda user: yesno(user.lock),
  714. 'ur_unix_visible_set': lambda user: yesno(user.visible),
  715. 'ur_unix_primary_group': lambda user: ug.gid_to_name(str(user.gid)),
  716. 'ur_unix_groups': lambda user: ", ".join(
  717. x.group_name for x in ug.iterate_ldap_group(
  718. "memberUid=%s" % user.username)
  719. ),
  720. 'ur_unix_home_path': lambda user: user.homedir,
  721. 'ur_unix_shell': lambda user: user.shell,
  722. 'ur_unix_pw_set': lambda user: yesno(user.pass_set),
  723. }
  724. mapping = {'ur_unix_login': 'ur_unix_login_exists'}
  725. maxi = 0
  726. try:
  727. for i, user in enumerate(sorted(
  728. (user for user in uu.iterate_ldap_user("uid=*")
  729. if all(x(user) for x in filters)),
  730. key=lambda x: x.username)):
  731. maxi = i
  732. if offset <= i < offset + count:
  733. body.append(
  734. [variables_mapping.get(x)(user) for x in fields])
  735. except LDAPBadSearchFilter:
  736. raise UnixError(_("Wrong user pattern"))
  737. table_fields = [mapping.get(x, '') for x in fields]
  738. if not body:
  739. body = [[]]
  740. dv.Invalidate('ur_unix_user_count')
  741. if any(body):
  742. head_message = _("Users")
  743. elif dv.GetInteger("ur_unix_user_count"):
  744. head_message = _("Users not found")
  745. else:
  746. head_message = _("No users")
  747. self.printTable(head_message,
  748. head, body,
  749. fields=table_fields,
  750. onClick='unix_usermod' if any(table_fields) else None,
  751. addAction='unix_useradd',
  752. records=str(maxi))
  753. if any(body):
  754. num_page, count_page = getPagesInterval(
  755. count, offset, maxi)
  756. self.printSUCCESS(_('page %d from ') % num_page + str(count_page))
  757. return True
  758. def show_user(self, user):
  759. dv = self.clVars
  760. head = [_('Field'), _('Value')]
  761. body = []
  762. fields = [
  763. 'ur_unix_uid',
  764. 'ur_unix_login',
  765. 'ur_unix_comment',
  766. 'ur_unix_lock_set',
  767. 'ur_unix_visible_set',
  768. 'ur_unix_primary_group',
  769. 'ur_unix_groups',
  770. 'ur_unix_home_path',
  771. 'ur_unix_shell',
  772. 'ur_unix_pw_set',
  773. ]
  774. self.clVars.Set('ur_unix_login', user, True)
  775. for varname in fields:
  776. varval = self.clVars.Get(varname)
  777. varobj = self.clVars.getInfo(varname)
  778. if "list" in varobj.type:
  779. varval = "\n".join(varval)
  780. body.append([varobj.label or "", varval])
  781. if body:
  782. self.printTable(_("User info"), head, body,
  783. onClick="unix_usermod",
  784. records="0",
  785. fields=["ur_unix_login_exists:%s" % user])
  786. return True
  787. def try_remove_primary_group(self, user, primary_group):
  788. """
  789. Primary group
  790. :param primary_group:
  791. :return:
  792. """
  793. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  794. groups_dn = self.clVars.Get('ld_unix_groups_dn')
  795. ug = UnixGroups(ldap_connect, groups_dn)
  796. filter_str = "gidNumber={0}".format(primary_group)
  797. filter_str2 = "(&(gidNumber={0})(!(uid={1})))".format(
  798. primary_group, user)
  799. group = ug.search_ldap_group(filter_str)
  800. ldap_connect = self.clVars.Get('ldap.cl_ldap_connect')
  801. users_dn = self.clVars.Get('ld_unix_users_dn')
  802. uu = UnixUsers(ldap_connect, users_dn)
  803. if group:
  804. if not group.user_list and not uu.search_ldap_user(filter_str2):
  805. self.remove_group(group.group_name)
  806. self.printSUCCESS(_("Removed user primary group {0}").format(
  807. group.group_name))
  808. return True