auth.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> */
  2. #include <linux/sched.h>
  3. #include "nfsd.h"
  4. #include "auth.h"
  5. int nfsexp_flags(struct svc_rqst *rqstp, struct svc_export *exp)
  6. {
  7. struct exp_flavor_info *f;
  8. struct exp_flavor_info *end = exp->ex_flavors + exp->ex_nflavors;
  9. for (f = exp->ex_flavors; f < end; f++) {
  10. if (f->pseudoflavor == rqstp->rq_flavor)
  11. return f->flags;
  12. }
  13. return exp->ex_flags;
  14. }
  15. int nfsd_setuser(struct svc_rqst *rqstp, struct svc_export *exp)
  16. {
  17. struct group_info *rqgi;
  18. struct group_info *gi;
  19. struct cred *new;
  20. int i;
  21. int flags = nfsexp_flags(rqstp, exp);
  22. int ret;
  23. validate_process_creds();
  24. /* discard any old override before preparing the new set */
  25. revert_creds(get_cred(current->real_cred));
  26. new = prepare_creds();
  27. if (!new)
  28. return -ENOMEM;
  29. new->fsuid = rqstp->rq_cred.cr_uid;
  30. new->fsgid = rqstp->rq_cred.cr_gid;
  31. rqgi = rqstp->rq_cred.cr_group_info;
  32. if (flags & NFSEXP_ALLSQUASH) {
  33. new->fsuid = exp->ex_anon_uid;
  34. new->fsgid = exp->ex_anon_gid;
  35. gi = groups_alloc(0);
  36. if (!gi)
  37. goto oom;
  38. } else if (flags & NFSEXP_ROOTSQUASH) {
  39. if (!new->fsuid)
  40. new->fsuid = exp->ex_anon_uid;
  41. if (!new->fsgid)
  42. new->fsgid = exp->ex_anon_gid;
  43. gi = groups_alloc(rqgi->ngroups);
  44. if (!gi)
  45. goto oom;
  46. for (i = 0; i < rqgi->ngroups; i++) {
  47. if (!GROUP_AT(rqgi, i))
  48. GROUP_AT(gi, i) = exp->ex_anon_gid;
  49. else
  50. GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
  51. }
  52. } else {
  53. gi = get_group_info(rqgi);
  54. }
  55. if (new->fsuid == (uid_t) -1)
  56. new->fsuid = exp->ex_anon_uid;
  57. if (new->fsgid == (gid_t) -1)
  58. new->fsgid = exp->ex_anon_gid;
  59. ret = set_groups(new, gi);
  60. put_group_info(gi);
  61. if (ret < 0)
  62. goto error;
  63. if (new->fsuid)
  64. new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
  65. else
  66. new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
  67. new->cap_permitted);
  68. validate_process_creds();
  69. put_cred(override_creds(new));
  70. put_cred(new);
  71. validate_process_creds();
  72. return 0;
  73. oom:
  74. ret = -ENOMEM;
  75. error:
  76. abort_creds(new);
  77. return ret;
  78. }