auth_userpasswd.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include <authsrv.h>
  5. #include "authlocal.h"
  6. /*
  7. * compute the proper response. We encrypt the ascii of
  8. * challenge number, with trailing binary zero fill.
  9. * This process was derived empirically.
  10. * this was copied from inet's guard.
  11. */
  12. static void
  13. netresp(char *key, long chal, char *answer)
  14. {
  15. uchar buf[8];
  16. memset(buf, 0, sizeof buf);
  17. snprint((char *)buf, sizeof buf, "%lud", chal);
  18. if(encrypt(key, buf, 8) < 0)
  19. abort();
  20. sprint(answer, "%.8ux", buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
  21. }
  22. AuthInfo*
  23. auth_userpasswd(char *user, char *passwd)
  24. {
  25. char key[DESKEYLEN], resp[16];
  26. AuthInfo *ai;
  27. Chalstate *ch;
  28. /*
  29. * Probably we should have a factotum protocol
  30. * to check a raw password. For now, we use
  31. * p9cr, which is simplest to speak.
  32. */
  33. if((ch = auth_challenge("user=%q proto=p9cr role=server", user)) == nil)
  34. return nil;
  35. passtokey(key, passwd);
  36. netresp(key, atol(ch->chal), resp);
  37. memset(key, 0, sizeof key);
  38. ch->resp = resp;
  39. ch->nresp = strlen(resp);
  40. ai = auth_response(ch);
  41. auth_freechal(ch);
  42. return ai;
  43. }