auth_userpasswd.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. #include <authsrv.h>
  13. #include "authlocal.h"
  14. /*
  15. * compute the proper response. We encrypt the ascii of
  16. * challenge number, with trailing binary zero fill.
  17. * This process was derived empirically.
  18. * this was copied from inet's guard.
  19. */
  20. static void
  21. netresp(char *key, int32_t chal, char *answer)
  22. {
  23. uint8_t buf[8];
  24. memset(buf, 0, sizeof buf);
  25. snprint((char *)buf, sizeof buf, "%lud", chal);
  26. if(encrypt(key, buf, 8) < 0)
  27. abort();
  28. sprint(answer, "%.8ux", buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
  29. }
  30. AuthInfo*
  31. auth_userpasswd(char *user, char *passwd)
  32. {
  33. char key[DESKEYLEN], resp[16];
  34. AuthInfo *ai;
  35. Chalstate *ch;
  36. /*
  37. * Probably we should have a factotum protocol
  38. * to check a raw password. For now, we use
  39. * p9cr, which is simplest to speak.
  40. */
  41. if((ch = auth_challenge("user=%q proto=p9cr role=server", user)) == nil)
  42. return nil;
  43. passtokey(key, passwd);
  44. netresp(key, atol(ch->chal), resp);
  45. memset(key, 0, sizeof key);
  46. ch->resp = resp;
  47. ch->nresp = strlen(resp);
  48. ai = auth_response(ch);
  49. auth_freechal(ch);
  50. return ai;
  51. }