This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / include / linux / key.h
1 /* key.h: authentication token and access key management
2  *
3  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  *
12  * See Documentation/keys.txt for information on keys/keyrings.
13  */
14
15 #ifndef _LINUX_KEY_H
16 #define _LINUX_KEY_H
17
18 #include <linux/types.h>
19 #include <linux/list.h>
20 #include <linux/rbtree.h>
21 #include <linux/spinlock.h>
22 #include <asm/atomic.h>
23
24 #ifdef __KERNEL__
25
26 /* key handle serial number */
27 typedef int32_t key_serial_t;
28
29 /* key handle permissions mask */
30 typedef uint32_t key_perm_t;
31
32 struct key;
33
34 #ifdef CONFIG_KEYS
35
36 #undef KEY_DEBUGGING
37
38 #define KEY_USR_VIEW    0x00010000      /* user can view a key's attributes */
39 #define KEY_USR_READ    0x00020000      /* user can read key payload / view keyring */
40 #define KEY_USR_WRITE   0x00040000      /* user can update key payload / add link to keyring */
41 #define KEY_USR_SEARCH  0x00080000      /* user can find a key in search / search a keyring */
42 #define KEY_USR_LINK    0x00100000      /* user can create a link to a key/keyring */
43 #define KEY_USR_ALL     0x001f0000
44
45 #define KEY_GRP_VIEW    0x00000100      /* group permissions... */
46 #define KEY_GRP_READ    0x00000200
47 #define KEY_GRP_WRITE   0x00000400
48 #define KEY_GRP_SEARCH  0x00000800
49 #define KEY_GRP_LINK    0x00001000
50 #define KEY_GRP_ALL     0x00001f00
51
52 #define KEY_OTH_VIEW    0x00000001      /* third party permissions... */
53 #define KEY_OTH_READ    0x00000002
54 #define KEY_OTH_WRITE   0x00000004
55 #define KEY_OTH_SEARCH  0x00000008
56 #define KEY_OTH_LINK    0x00000010
57 #define KEY_OTH_ALL     0x0000001f
58
59 struct seq_file;
60 struct user_struct;
61
62 struct key_type;
63 struct key_owner;
64 struct keyring_list;
65 struct keyring_name;
66
67 /*****************************************************************************/
68 /*
69  * authentication token / access credential / keyring
70  * - types of key include:
71  *   - keyrings
72  *   - disk encryption IDs
73  *   - Kerberos TGTs and tickets
74  */
75 struct key {
76         atomic_t                usage;          /* number of references */
77         key_serial_t            serial;         /* key serial number */
78         struct rb_node          serial_node;
79         struct key_type         *type;          /* type of key */
80         rwlock_t                lock;           /* examination vs change lock */
81         struct rw_semaphore     sem;            /* change vs change sem */
82         struct key_user         *user;          /* owner of this key */
83         time_t                  expiry;         /* time at which key expires (or 0) */
84         uid_t                   uid;
85         gid_t                   gid;
86         key_perm_t              perm;           /* access permissions */
87         unsigned short          quotalen;       /* length added to quota */
88         unsigned short          datalen;        /* payload data length */
89         unsigned short          flags;          /* status flags (change with lock writelocked) */
90 #define KEY_FLAG_INSTANTIATED   0x00000001      /* set if key has been instantiated */
91 #define KEY_FLAG_DEAD           0x00000002      /* set if key type has been deleted */
92 #define KEY_FLAG_REVOKED        0x00000004      /* set if key had been revoked */
93 #define KEY_FLAG_IN_QUOTA       0x00000008      /* set if key consumes quota */
94 #define KEY_FLAG_USER_CONSTRUCT 0x00000010      /* set if key is being constructed in userspace */
95 #define KEY_FLAG_NEGATIVE       0x00000020      /* set if key is negative */
96
97 #ifdef KEY_DEBUGGING
98         unsigned                magic;
99 #define KEY_DEBUG_MAGIC         0x18273645u
100 #define KEY_DEBUG_MAGIC_X       0xf8e9dacbu
101 #endif
102
103         /* the description string
104          * - this is used to match a key against search criteria
105          * - this should be a printable string
106          * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
107          */
108         char                    *description;
109
110         /* type specific data
111          * - this is used by the keyring type to index the name
112          */
113         union {
114                 struct list_head        link;
115         } type_data;
116
117         /* key data
118          * - this is used to hold the data actually used in cryptography or
119          *   whatever
120          */
121         union {
122                 unsigned long           value;
123                 void                    *data;
124                 struct keyring_list     *subscriptions;
125         } payload;
126 };
127
128 /*****************************************************************************/
129 /*
130  * kernel managed key type definition
131  */
132 struct key_type {
133         /* name of the type */
134         const char *name;
135
136         /* default payload length for quota precalculation (optional)
137          * - this can be used instead of calling key_payload_reserve(), that
138          *   function only needs to be called if the real datalen is different
139          */
140         size_t def_datalen;
141
142         /* instantiate a key of this type
143          * - this method should call key_payload_reserve() to determine if the
144          *   user's quota will hold the payload
145          */
146         int (*instantiate)(struct key *key, const void *data, size_t datalen);
147
148         /* duplicate a key of this type (optional)
149          * - the source key will be locked against change
150          * - the new description will be attached
151          * - the quota will have been adjusted automatically from
152          *   source->quotalen
153          */
154         int (*duplicate)(struct key *key, const struct key *source);
155
156         /* update a key of this type (optional)
157          * - this method should call key_payload_reserve() to recalculate the
158          *   quota consumption
159          * - the key must be locked against read when modifying
160          */
161         int (*update)(struct key *key, const void *data, size_t datalen);
162
163         /* match a key against a description */
164         int (*match)(const struct key *key, const void *desc);
165
166         /* clear the data from a key (optional) */
167         void (*destroy)(struct key *key);
168
169         /* describe a key */
170         void (*describe)(const struct key *key, struct seq_file *p);
171
172         /* read a key's data (optional)
173          * - permission checks will be done by the caller
174          * - the key's semaphore will be readlocked by the caller
175          * - should return the amount of data that could be read, no matter how
176          *   much is copied into the buffer
177          * - shouldn't do the copy if the buffer is NULL
178          */
179         long (*read)(const struct key *key, char __user *buffer, size_t buflen);
180
181         /* internal fields */
182         struct list_head        link;           /* link in types list */
183 };
184
185 extern struct key_type key_type_keyring;
186
187 extern int register_key_type(struct key_type *ktype);
188 extern void unregister_key_type(struct key_type *ktype);
189
190 extern struct key *key_alloc(struct key_type *type,
191                              const char *desc,
192                              uid_t uid, gid_t gid, key_perm_t perm,
193                              int not_in_quota);
194 extern int key_payload_reserve(struct key *key, size_t datalen);
195 extern int key_instantiate_and_link(struct key *key,
196                                     const void *data,
197                                     size_t datalen,
198                                     struct key *keyring);
199 extern int key_negate_and_link(struct key *key,
200                                unsigned timeout,
201                                struct key *keyring);
202 extern void key_revoke(struct key *key);
203 extern void key_put(struct key *key);
204
205 static inline struct key *key_get(struct key *key)
206 {
207         if (key)
208                 atomic_inc(&key->usage);
209         return key;
210 }
211
212 extern struct key *request_key(struct key_type *type,
213                                const char *description,
214                                const char *callout_info);
215
216 extern int key_validate(struct key *key);
217
218 extern struct key *key_create_or_update(struct key *keyring,
219                                         const char *type,
220                                         const char *description,
221                                         const void *payload,
222                                         size_t plen,
223                                         int not_in_quota);
224
225 extern int key_update(struct key *key,
226                       const void *payload,
227                       size_t plen);
228
229 extern int key_link(struct key *keyring,
230                     struct key *key);
231
232 extern int key_unlink(struct key *keyring,
233                       struct key *key);
234
235 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
236                                  int not_in_quota, struct key *dest);
237
238 extern int keyring_clear(struct key *keyring);
239
240 extern struct key *keyring_search(struct key *keyring,
241                                   struct key_type *type,
242                                   const char *description);
243
244 extern struct key *search_process_keyrings(struct key_type *type,
245                                            const char *description);
246
247 extern int keyring_add_key(struct key *keyring,
248                            struct key *key);
249
250 extern struct key *key_lookup(key_serial_t id);
251
252 #define key_serial(key) ((key) ? (key)->serial : 0)
253
254 /*
255  * the userspace interface
256  */
257 extern struct key root_user_keyring, root_session_keyring;
258 extern int alloc_uid_keyring(struct user_struct *user);
259 extern void switch_uid_keyring(struct user_struct *new_user);
260 extern int copy_keys(unsigned long clone_flags, struct task_struct *tsk);
261 extern void exit_keys(struct task_struct *tsk);
262 extern int suid_keys(struct task_struct *tsk);
263 extern int exec_keys(struct task_struct *tsk);
264 extern void key_fsuid_changed(struct task_struct *tsk);
265 extern void key_fsgid_changed(struct task_struct *tsk);
266 extern void key_init(void);
267
268 #else /* CONFIG_KEYS */
269
270 #define key_validate(k)                 0
271 #define key_serial(k)                   0
272 #define key_get(k)                      NULL
273 #define key_put(k)                      do { } while(0)
274 #define alloc_uid_keyring(u)            0
275 #define switch_uid_keyring(u)           do { } while(0)
276 #define copy_keys(f,t)                  0
277 #define exit_keys(t)                    do { } while(0)
278 #define suid_keys(t)                    do { } while(0)
279 #define exec_keys(t)                    do { } while(0)
280 #define key_fsuid_changed(t)            do { } while(0)
281 #define key_fsgid_changed(t)            do { } while(0)
282 #define key_init()                      do { } while(0)
283
284 #endif /* CONFIG_KEYS */
285 #endif /* __KERNEL__ */
286 #endif /* _LINUX_KEY_H */