_asrdresp.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <authsrv.h>
  12. static char *pbmsg = "AS protocol botch";
  13. int
  14. _asrdresp(int fd, char *buf, int len)
  15. {
  16. int n;
  17. char error[64];
  18. if(read(fd, buf, 1) != 1){
  19. werrstr(pbmsg);
  20. return -1;
  21. }
  22. n = len;
  23. switch(buf[0]){
  24. case AuthOK:
  25. if(readn(fd, buf, len) != len){
  26. werrstr(pbmsg);
  27. return -1;
  28. }
  29. break;
  30. case AuthErr:
  31. if(readn(fd, error, sizeof error) != sizeof error){
  32. werrstr(pbmsg);
  33. return -1;
  34. }
  35. error[sizeof error-1] = '\0';
  36. werrstr("remote: %s", error);
  37. return -1;
  38. case AuthOKvar:
  39. if(readn(fd, error, 5) != 5){
  40. werrstr(pbmsg);
  41. return -1;
  42. }
  43. error[5] = 0;
  44. n = atoi(error);
  45. if(n <= 0 || n > len){
  46. werrstr(pbmsg);
  47. return -1;
  48. }
  49. memset(buf, 0, len);
  50. if(readn(fd, buf, n) != n){
  51. werrstr(pbmsg);
  52. return -1;
  53. }
  54. break;
  55. default:
  56. werrstr(pbmsg);
  57. return -1;
  58. }
  59. return n;
  60. }