auth_userpasswd.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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, 8);
  17. sprint((char *)buf, "%lud", chal);
  18. if(encrypt(key, buf, 8) < 0)
  19. abort();
  20. chal = (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+buf[3];
  21. sprint(answer, "%.8lux", chal);
  22. }
  23. AuthInfo*
  24. auth_userpasswd(char *user, char *passwd)
  25. {
  26. char key[DESKEYLEN], resp[16];
  27. AuthInfo *ai;
  28. Chalstate *ch;
  29. /*
  30. * Probably we should have a factotum protocol
  31. * to check a raw password. For now, we use
  32. * p9cr, which is simplest to speak.
  33. */
  34. if((ch = auth_challenge("user=%q proto=p9cr role=server", user)) == nil)
  35. return nil;
  36. passtokey(key, passwd);
  37. netresp(key, atol(ch->chal), resp);
  38. memset(key, 0, sizeof key);
  39. ch->resp = resp;
  40. ch->nresp = strlen(resp);
  41. ai = auth_response(ch);
  42. auth_freechal(ch);
  43. return ai;
  44. }