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