+# Handle a ConfirmSudoPassword request
+def DoConfirmSudopassword(Str):
+ Match = re.compile('^confirm sudopassword ('+UUID_FORMAT+') ([a-z0-9.,*]+) ([0-9a-f]{40})$').match(Str)
+ if Match == None:
+ return None
+
+ uuid = Match.group(1)
+ hosts = Match.group(2)
+ hmac = Match.group(3)
+
+ global SudoPasswd
+ SudoPasswd[uuid] = (hosts, hmac)
+ return "got confirm for sudo password %s on host(s) %s, auth code %s" % (uuid,hosts, hmac)
+
+def FinishConfirmSudopassword(l, uid, Attrs):
+ global SudoPasswd
+ result = "\n"
+
+ res = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"uid="+uid, ['sudoPassword']);
+ if len(res) != 1:
+ raise UDFormatError, "Not exactly one hit when searching for user"
+ if res[0][1].has_key('sudoPassword'):
+ inldap = res[0][1]['sudoPassword']
+ else:
+ inldap = []
+
+ newldap = []
+ for entry in inldap:
+ Match = re.compile('^('+UUID_FORMAT+') (confirmed:[0-9a-f]{40}|unconfirmed) ([a-z0-9.,*]+) ([^ ]+)$').match(entry)
+ if Match == None:
+ raise UDFormatError, "Could not parse existing sudopasswd entry"
+ uuid = Match.group(1)
+ status = Match.group(2)
+ hosts = Match.group(3)
+ cryptedpass = Match.group(4)
+
+ if SudoPasswd.has_key(uuid):
+ confirmedHosts = SudoPasswd[uuid][0]
+ confirmedHmac = SudoPasswd[uuid][1]
+ if status.startswith('confirmed:'):
+ if status == 'confirmed:'+make_passwd_hmac('password-is-confirmed', 'sudo', uid, uuid, hosts, cryptedpass):
+ result = result + "Entry %s for sudo password on hosts %s already confirmed.\n"%(uuid, hosts)
+ else:
+ result = result + "Entry %s for sudo password on hosts %s is listed as confirmed, but HMAC does not verify.\n"%(uuid, hosts)
+ elif confirmedHosts != hosts:
+ result = result + "Entry %s hostlist mismatch (%s vs. %s).\n"%(uuid, hosts, confirmedHosts)
+ elif make_passwd_hmac('confirm-new-password', 'sudo', uid, uuid, hosts, cryptedpass) == confirmedHmac:
+ result = result + "Entry %s for sudo password on hosts %s now confirmed.\n"%(uuid, hosts)
+ status = 'confirmed:'+make_passwd_hmac('password-is-confirmed', 'sudo', uid, uuid, hosts, cryptedpass)
+ else:
+ result = result + "Entry %s for sudo password on hosts %s HMAC verify failed.\n"%(uuid, hosts)
+ del SudoPasswd[uuid]
+
+ newentry = " ".join([uuid, status, hosts, cryptedpass])
+ if len(newldap) == 0:
+ newldap.append((ldap.MOD_REPLACE,"sudoPassword",newentry))
+ else:
+ newldap.append((ldap.MOD_ADD,"sudoPassword",newentry))
+
+ for entry in SudoPasswd:
+ result = result + "Entry %s that you confirm is not listed in ldap."%(entry)
+
+ for entry in newldap:
+ Attrs.append(entry)
+
+ return result
+