auth.c 1.9 KB

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