auth.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/include/linux/sunrpc/auth.h
  4. *
  5. * Declarations for the RPC client authentication machinery.
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #ifndef _LINUX_SUNRPC_AUTH_H
  10. #define _LINUX_SUNRPC_AUTH_H
  11. #ifdef __KERNEL__
  12. #include <linux/sunrpc/sched.h>
  13. #include <linux/sunrpc/msg_prot.h>
  14. #include <linux/sunrpc/xdr.h>
  15. #include <linux/atomic.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/uidgid.h>
  18. #include <linux/utsname.h>
  19. /*
  20. * Maximum size of AUTH_NONE authentication information, in XDR words.
  21. */
  22. #define NUL_CALLSLACK (4)
  23. #define NUL_REPLYSLACK (2)
  24. /*
  25. * Size of the nodename buffer. RFC1831 specifies a hard limit of 255 bytes,
  26. * but Linux hostnames are actually limited to __NEW_UTS_LEN bytes.
  27. */
  28. #define UNX_MAXNODENAME __NEW_UTS_LEN
  29. #define UNX_CALLSLACK (21 + XDR_QUADLEN(UNX_MAXNODENAME))
  30. #define UNX_NGROUPS 16
  31. struct rpcsec_gss_info;
  32. /* auth_cred ac_flags bits */
  33. enum {
  34. RPC_CRED_KEY_EXPIRE_SOON = 1, /* underlying cred key will expire soon */
  35. RPC_CRED_NOTIFY_TIMEOUT = 2, /* nofity generic cred when underlying
  36. key will expire soon */
  37. };
  38. /* Work around the lack of a VFS credential */
  39. struct auth_cred {
  40. kuid_t uid;
  41. kgid_t gid;
  42. struct group_info *group_info;
  43. const char *principal;
  44. unsigned long ac_flags;
  45. unsigned char machine_cred : 1;
  46. };
  47. /*
  48. * Client user credentials
  49. */
  50. struct rpc_auth;
  51. struct rpc_credops;
  52. struct rpc_cred {
  53. struct hlist_node cr_hash; /* hash chain */
  54. struct list_head cr_lru; /* lru garbage collection */
  55. struct rcu_head cr_rcu;
  56. struct rpc_auth * cr_auth;
  57. const struct rpc_credops *cr_ops;
  58. unsigned long cr_expire; /* when to gc */
  59. unsigned long cr_flags; /* various flags */
  60. atomic_t cr_count; /* ref count */
  61. kuid_t cr_uid;
  62. /* per-flavor data */
  63. };
  64. #define RPCAUTH_CRED_NEW 0
  65. #define RPCAUTH_CRED_UPTODATE 1
  66. #define RPCAUTH_CRED_HASHED 2
  67. #define RPCAUTH_CRED_NEGATIVE 3
  68. /* rpc_auth au_flags */
  69. #define RPCAUTH_AUTH_NO_CRKEY_TIMEOUT 0x0001 /* underlying cred has no key timeout */
  70. /*
  71. * Client authentication handle
  72. */
  73. struct rpc_cred_cache;
  74. struct rpc_authops;
  75. struct rpc_auth {
  76. unsigned int au_cslack; /* call cred size estimate */
  77. /* guess at number of u32's auth adds before
  78. * reply data; normally the verifier size: */
  79. unsigned int au_rslack;
  80. /* for gss, used to calculate au_rslack: */
  81. unsigned int au_verfsize;
  82. unsigned int au_flags; /* various flags */
  83. const struct rpc_authops *au_ops; /* operations */
  84. rpc_authflavor_t au_flavor; /* pseudoflavor (note may
  85. * differ from the flavor in
  86. * au_ops->au_flavor in gss
  87. * case) */
  88. atomic_t au_count; /* Reference counter */
  89. struct rpc_cred_cache * au_credcache;
  90. /* per-flavor data */
  91. };
  92. /* rpc_auth au_flags */
  93. #define RPCAUTH_AUTH_DATATOUCH 0x00000002
  94. struct rpc_auth_create_args {
  95. rpc_authflavor_t pseudoflavor;
  96. const char *target_name;
  97. };
  98. /* Flags for rpcauth_lookupcred() */
  99. #define RPCAUTH_LOOKUP_NEW 0x01 /* Accept an uninitialised cred */
  100. #define RPCAUTH_LOOKUP_RCU 0x02 /* lock-less lookup */
  101. /*
  102. * Client authentication ops
  103. */
  104. struct rpc_authops {
  105. struct module *owner;
  106. rpc_authflavor_t au_flavor; /* flavor (RPC_AUTH_*) */
  107. char * au_name;
  108. struct rpc_auth * (*create)(const struct rpc_auth_create_args *,
  109. struct rpc_clnt *);
  110. void (*destroy)(struct rpc_auth *);
  111. int (*hash_cred)(struct auth_cred *, unsigned int);
  112. struct rpc_cred * (*lookup_cred)(struct rpc_auth *, struct auth_cred *, int);
  113. struct rpc_cred * (*crcreate)(struct rpc_auth*, struct auth_cred *, int, gfp_t);
  114. int (*list_pseudoflavors)(rpc_authflavor_t *, int);
  115. rpc_authflavor_t (*info2flavor)(struct rpcsec_gss_info *);
  116. int (*flavor2info)(rpc_authflavor_t,
  117. struct rpcsec_gss_info *);
  118. int (*key_timeout)(struct rpc_auth *,
  119. struct rpc_cred *);
  120. };
  121. struct rpc_credops {
  122. const char * cr_name; /* Name of the auth flavour */
  123. int (*cr_init)(struct rpc_auth *, struct rpc_cred *);
  124. void (*crdestroy)(struct rpc_cred *);
  125. int (*crmatch)(struct auth_cred *, struct rpc_cred *, int);
  126. struct rpc_cred * (*crbind)(struct rpc_task *, struct rpc_cred *, int);
  127. __be32 * (*crmarshal)(struct rpc_task *, __be32 *);
  128. int (*crrefresh)(struct rpc_task *);
  129. __be32 * (*crvalidate)(struct rpc_task *, __be32 *);
  130. int (*crwrap_req)(struct rpc_task *, kxdreproc_t,
  131. void *, __be32 *, void *);
  132. int (*crunwrap_resp)(struct rpc_task *, kxdrdproc_t,
  133. void *, __be32 *, void *);
  134. int (*crkey_timeout)(struct rpc_cred *);
  135. bool (*crkey_to_expire)(struct rpc_cred *);
  136. char * (*crstringify_acceptor)(struct rpc_cred *);
  137. };
  138. extern const struct rpc_authops authunix_ops;
  139. extern const struct rpc_authops authnull_ops;
  140. int __init rpc_init_authunix(void);
  141. int __init rpc_init_generic_auth(void);
  142. int __init rpcauth_init_module(void);
  143. void rpcauth_remove_module(void);
  144. void rpc_destroy_generic_auth(void);
  145. void rpc_destroy_authunix(void);
  146. struct rpc_cred * rpc_lookup_cred(void);
  147. struct rpc_cred * rpc_lookup_cred_nonblock(void);
  148. struct rpc_cred * rpc_lookup_generic_cred(struct auth_cred *, int, gfp_t);
  149. struct rpc_cred * rpc_lookup_machine_cred(const char *service_name);
  150. int rpcauth_register(const struct rpc_authops *);
  151. int rpcauth_unregister(const struct rpc_authops *);
  152. struct rpc_auth * rpcauth_create(const struct rpc_auth_create_args *,
  153. struct rpc_clnt *);
  154. void rpcauth_release(struct rpc_auth *);
  155. rpc_authflavor_t rpcauth_get_pseudoflavor(rpc_authflavor_t,
  156. struct rpcsec_gss_info *);
  157. int rpcauth_get_gssinfo(rpc_authflavor_t,
  158. struct rpcsec_gss_info *);
  159. int rpcauth_list_flavors(rpc_authflavor_t *, int);
  160. struct rpc_cred * rpcauth_lookup_credcache(struct rpc_auth *, struct auth_cred *, int, gfp_t);
  161. void rpcauth_init_cred(struct rpc_cred *, const struct auth_cred *, struct rpc_auth *, const struct rpc_credops *);
  162. struct rpc_cred * rpcauth_lookupcred(struct rpc_auth *, int);
  163. struct rpc_cred * rpcauth_generic_bind_cred(struct rpc_task *, struct rpc_cred *, int);
  164. void put_rpccred(struct rpc_cred *);
  165. __be32 * rpcauth_marshcred(struct rpc_task *, __be32 *);
  166. __be32 * rpcauth_checkverf(struct rpc_task *, __be32 *);
  167. int rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp, __be32 *data, void *obj);
  168. int rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp, __be32 *data, void *obj);
  169. int rpcauth_refreshcred(struct rpc_task *);
  170. void rpcauth_invalcred(struct rpc_task *);
  171. int rpcauth_uptodatecred(struct rpc_task *);
  172. int rpcauth_init_credcache(struct rpc_auth *);
  173. void rpcauth_destroy_credcache(struct rpc_auth *);
  174. void rpcauth_clear_credcache(struct rpc_cred_cache *);
  175. int rpcauth_key_timeout_notify(struct rpc_auth *,
  176. struct rpc_cred *);
  177. bool rpcauth_cred_key_to_expire(struct rpc_auth *, struct rpc_cred *);
  178. char * rpcauth_stringify_acceptor(struct rpc_cred *);
  179. static inline
  180. struct rpc_cred * get_rpccred(struct rpc_cred *cred)
  181. {
  182. if (cred != NULL)
  183. atomic_inc(&cred->cr_count);
  184. return cred;
  185. }
  186. /**
  187. * get_rpccred_rcu - get a reference to a cred using rcu-protected pointer
  188. * @cred: cred of which to take a reference
  189. *
  190. * In some cases, we may have a pointer to a credential to which we
  191. * want to take a reference, but don't already have one. Because these
  192. * objects are freed using RCU, we can access the cr_count while its
  193. * on its way to destruction and only take a reference if it's not already
  194. * zero.
  195. */
  196. static inline struct rpc_cred *
  197. get_rpccred_rcu(struct rpc_cred *cred)
  198. {
  199. if (atomic_inc_not_zero(&cred->cr_count))
  200. return cred;
  201. return NULL;
  202. }
  203. #endif /* __KERNEL__ */
  204. #endif /* _LINUX_SUNRPC_AUTH_H */