【问题标题】:Find Max UID using Python-LDAP使用 Python-LDAP 查找最大 UID
【发布时间】:2018-04-02 09:04:39
【问题描述】:

我正在尝试使用 python 模块查找/搜索 LDAP 条目中的最大 UID 值。我的代码暂时看起来是这样的

def search_max_uid():
    filter_uid = 'uid=*'
    attributes = ['uidNumber']
    resulting = l.search_ext(base_dn,ldap.SCOPE_SUBTREE,filter_uid,attributes)
    print resulting

一旦我从整个服务器获得最大 UID,我就可以 +1 并将新用户添加到组中。我看到一些类似http://www.openldap.org/lists/openldap-software/200110/msg00539.htmlhttp://www.perlmonks.org/?node_id=457108 的帖子与我的问题非常相似

谁能帮我找到最大 UID 以便我解决这个问题。

【问题讨论】:

    标签: python python-2.7 ldap openldap python-ldap


    【解决方案1】:

    尝试解决类似的问题,我认为这可以获取下一个可用的 uidNumber:

    import ldap
    
    l = ldap.initialize("ldap://localhost")
    l.simple_bind_s("cn=blah,dc=blah,dc=blah", supersecretpassword)
    res = l.search_s("dc=blah,dc=blah", ldap.SCOPE_SUBTREE, 'objectclass=posixaccount', ['uidNumber'])
    uidNum = 0
    
    for a in res:
        uidNumtemp = a[1].get('uidNumber')[0]
        if uidNumtemp > uidNum:
           uidNum = uidNumtemp
    print "Highest:", uidNum
    nextNum = int(uidNum) + 1
    print "next uidNumber:", nextNum
    

    【讨论】:

      【解决方案2】:

      如果您使用支持服务器端排序的 LDAP 服务器(请参阅 RFC 2891),例如带有 slapo-sssvlv 的 OpenLDAP,您可以通过反向排序仅搜索一个搜索结果来搜索最大数字。

      基于python-ldap 的Python sn-p(摘自Æ-DIR's CLI 工具之一):

      import ldap
      from ldap.controls.sss import SSSRequestControl
      
      def highest_id(ldap_conn, searchbase, id_attr):
          """
          search the highest value of `id_attr' by using server-side (reverse) sorting
          """
          # reverse sorting request control
          sss_control = SSSRequestControl(criticality=True, ordering_rules=['-'+id_attr])
          # send search request
          msg_id = ldap_conn.search(
              searchbase,
              ldap.SCOPE_SUBTREE,
              '({0}=*)'.format(id_attr),
              attrlist=[id_attr],
              sizelimit=1,
              serverctrls=[sss_control],
          )
          # collect result
          ldap_result = []
          try:
              for _, res_data, _, res_controls in ldap_conn.results(
                      msg_id,
                      add_ctrls=0
                  ):
                  ldap_result.extend(res_data)
          except ldap.SIZELIMIT_EXCEEDED:
              pass
      
          if not ldap_result:
              logging.error('No entry with attribute %r found!', id_attr)
              raise ValueError('No LDAP result!')
      
          highest_id_number = int(ldap_result[0][1][id_attr][0])
          logging.debug('Highest %r value found: %d', id_attr, highest_id_number)
          return highest_id_number
      

      请注意,在分配新 ID 时,这并不总是您想要的,因为 ID 号空间中的间隙不会(重新)使用。

      还要确保使用服务器端唯一约束插件,例如OpenLDAP 的覆盖slapo-unique。这样可以避免在并发客户端添加新条目时出现重复。

      【讨论】:

      • 请注意,这个 sn-p 不起作用。 “搜索库”未在任何地方定义
      • 感谢您指出这一点。它是由复制代码引起的,该代码是一种方法,因此使用了实例属性。编辑了我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 2017-12-17
      • 1970-01-01
      相关资源
      最近更新 更多