auth.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_cred.cr_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. validate_process_creds();
  23. /* discard any old override before preparing the new set */
  24. revert_creds(get_cred(current_real_cred()));
  25. new = prepare_creds();
  26. if (!new)
  27. return -ENOMEM;
  28. new->fsuid = rqstp->rq_cred.cr_uid;
  29. new->fsgid = rqstp->rq_cred.cr_gid;
  30. rqgi = rqstp->rq_cred.cr_group_info;
  31. if (flags & NFSEXP_ALLSQUASH) {
  32. new->fsuid = exp->ex_anon_uid;
  33. new->fsgid = exp->ex_anon_gid;
  34. gi = groups_alloc(0);
  35. if (!gi)
  36. goto oom;
  37. } else if (flags & NFSEXP_ROOTSQUASH) {
  38. if (uid_eq(new->fsuid, GLOBAL_ROOT_UID))
  39. new->fsuid = exp->ex_anon_uid;
  40. if (gid_eq(new->fsgid, GLOBAL_ROOT_GID))
  41. new->fsgid = exp->ex_anon_gid;
  42. gi = groups_alloc(rqgi->ngroups);
  43. if (!gi)
  44. goto oom;
  45. for (i = 0; i < rqgi->ngroups; i++) {
  46. if (gid_eq(GLOBAL_ROOT_GID, GROUP_AT(rqgi, i)))
  47. GROUP_AT(gi, i) = exp->ex_anon_gid;
  48. else
  49. GROUP_AT(gi, i) = GROUP_AT(rqgi, i);
  50. }
  51. /* Each thread allocates its own gi, no race */
  52. groups_sort(gi);
  53. } else {
  54. gi = get_group_info(rqgi);
  55. }
  56. if (uid_eq(new->fsuid, INVALID_UID))
  57. new->fsuid = exp->ex_anon_uid;
  58. if (gid_eq(new->fsgid, INVALID_GID))
  59. new->fsgid = exp->ex_anon_gid;
  60. set_groups(new, gi);
  61. put_group_info(gi);
  62. if (!uid_eq(new->fsuid, GLOBAL_ROOT_UID))
  63. new->cap_effective = cap_drop_nfsd_set(new->cap_effective);
  64. else
  65. new->cap_effective = cap_raise_nfsd_set(new->cap_effective,
  66. new->cap_permitted);
  67. validate_process_creds();
  68. put_cred(override_creds(new));
  69. put_cred(new);
  70. validate_process_creds();
  71. return 0;
  72. oom:
  73. abort_creds(new);
  74. return -ENOMEM;
  75. }