key-ui.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* key-ui.h: key userspace interface stuff for use by keyfs
  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. #ifndef _LINUX_KEY_UI_H
  12. #define _LINUX_KEY_UI_H
  13. #include <linux/key.h>
  14. /* the key tree */
  15. extern struct rb_root key_serial_tree;
  16. extern spinlock_t key_serial_lock;
  17. /* required permissions */
  18. #define KEY_VIEW 0x01 /* require permission to view attributes */
  19. #define KEY_READ 0x02 /* require permission to read content */
  20. #define KEY_WRITE 0x04 /* require permission to update / modify */
  21. #define KEY_SEARCH 0x08 /* require permission to search (keyring) or find (key) */
  22. #define KEY_LINK 0x10 /* require permission to link */
  23. #define KEY_ALL 0x1f /* all the above permissions */
  24. /*
  25. * the keyring payload contains a list of the keys to which the keyring is
  26. * subscribed
  27. */
  28. struct keyring_list {
  29. unsigned maxkeys; /* max keys this list can hold */
  30. unsigned nkeys; /* number of keys currently held */
  31. struct key *keys[0];
  32. };
  33. /*
  34. * check to see whether permission is granted to use a key in the desired way
  35. */
  36. static inline int key_permission(const struct key *key, key_perm_t perm)
  37. {
  38. key_perm_t kperm;
  39. if (key->uid == current->fsuid)
  40. kperm = key->perm >> 16;
  41. else if (key->gid != -1 &&
  42. key->perm & KEY_GRP_ALL &&
  43. in_group_p(key->gid)
  44. )
  45. kperm = key->perm >> 8;
  46. else
  47. kperm = key->perm;
  48. kperm = kperm & perm & KEY_ALL;
  49. return kperm == perm;
  50. }
  51. /*
  52. * check to see whether permission is granted to use a key in at least one of
  53. * the desired ways
  54. */
  55. static inline int key_any_permission(const struct key *key, key_perm_t perm)
  56. {
  57. key_perm_t kperm;
  58. if (key->uid == current->fsuid)
  59. kperm = key->perm >> 16;
  60. else if (key->gid != -1 &&
  61. key->perm & KEY_GRP_ALL &&
  62. in_group_p(key->gid)
  63. )
  64. kperm = key->perm >> 8;
  65. else
  66. kperm = key->perm;
  67. kperm = kperm & perm & KEY_ALL;
  68. return kperm != 0;
  69. }
  70. extern struct key *lookup_user_key(key_serial_t id, int create, int part,
  71. key_perm_t perm);
  72. extern long join_session_keyring(const char *name);
  73. extern struct key_type *key_type_lookup(const char *type);
  74. extern void key_type_put(struct key_type *ktype);
  75. #define key_negative_timeout 60 /* default timeout on a negative key's existence */
  76. #endif /* _LINUX_KEY_UI_H */