pam_client.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (c) 1999 Andrew G. Morgan <morgan@linux.kernel.org>
  5. *
  6. * This header file provides the prototypes for the PAM client API
  7. */
  8. #ifndef PAM_CLIENT_H
  9. #define PAM_CLIENT_H
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif /* def __cplusplus */
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdint.h>
  17. #include <sys/types.h>
  18. /* opaque agent handling structure */
  19. typedef struct pamc_handle_s *pamc_handle_t;
  20. /* binary prompt structure pointer */
  21. typedef struct { uint32_t length; uint8_t control; } *pamc_bp_t;
  22. /*
  23. * functions provided by libpamc
  24. */
  25. /*
  26. * Initialize the agent abstraction library
  27. */
  28. pamc_handle_t pamc_start(void);
  29. /*
  30. * Terminate the authentication process
  31. */
  32. int pamc_end(pamc_handle_t *pch);
  33. /*
  34. * force the loading of a specified agent
  35. */
  36. int pamc_load(pamc_handle_t pch, const char *agent_id);
  37. /*
  38. * Single conversation interface for binary prompts
  39. */
  40. int pamc_converse(pamc_handle_t pch, pamc_bp_t *prompt_p);
  41. /*
  42. * disable an agent
  43. */
  44. int pamc_disable(pamc_handle_t pch, const char *agent_id);
  45. /*
  46. * obtain a list of available agents
  47. */
  48. char **pamc_list_agents(pamc_handle_t pch);
  49. /*
  50. * PAM_BP_ MACROS for creating, destroying and manipulating binary prompts
  51. */
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <unistd.h>
  55. #ifndef PAM_BP_ASSERT
  56. # ifdef NDEBUG
  57. # define PAM_BP_ASSERT(x) do {} while (0)
  58. # else
  59. # define PAM_BP_ASSERT(x) do { printf(__FILE__ "(%d): %s\n", \
  60. __LINE__, x) ; exit(1); } while (0)
  61. # endif /* NDEBUG */
  62. #endif /* PAM_BP_ASSERT */
  63. #ifndef PAM_BP_CALLOC
  64. # define PAM_BP_CALLOC calloc
  65. #endif /* PAM_BP_CALLOC */
  66. #ifndef PAM_BP_FREE
  67. # define PAM_BP_FREE free
  68. #endif /* PAM_BP_FREE */
  69. #define __PAM_BP_WOCTET(x,y) (*((y) + (uint8_t *)(x)))
  70. #define __PAM_BP_ROCTET(x,y) (*((y) + (const uint8_t *)(x)))
  71. #define PAM_BP_MIN_SIZE (sizeof(uint32_t) + sizeof(uint8_t))
  72. #define PAM_BP_MAX_LENGTH 0x20000 /* an advisory limit */
  73. #define PAM_BP_WCONTROL(x) (__PAM_BP_WOCTET(x,4))
  74. #define PAM_BP_RCONTROL(x) (__PAM_BP_ROCTET(x,4))
  75. #define PAM_BP_SIZE(x) ((__PAM_BP_ROCTET(x,0)<<24)+ \
  76. (__PAM_BP_ROCTET(x,1)<<16)+ \
  77. (__PAM_BP_ROCTET(x,2)<< 8)+ \
  78. (__PAM_BP_ROCTET(x,3) ))
  79. #define PAM_BP_LENGTH(x) (PAM_BP_SIZE(x) - PAM_BP_MIN_SIZE)
  80. #define PAM_BP_WDATA(x) (PAM_BP_MIN_SIZE + (uint8_t *) (x))
  81. #define PAM_BP_RDATA(x) (PAM_BP_MIN_SIZE + (const uint8_t *) (x))
  82. /* Note, this macro always '\0' terminates renewed packets */
  83. #define PAM_BP_RENEW(old_p, cntrl, data_length) \
  84. do { \
  85. if ((old_p) != NULL) { \
  86. if (*(old_p)) { \
  87. uint32_t __size; \
  88. __size = PAM_BP_SIZE(*(old_p)); \
  89. memset(*(old_p), 0, __size); \
  90. PAM_BP_FREE(*(old_p)); \
  91. } \
  92. if (cntrl) { \
  93. uint32_t __size; \
  94. \
  95. __size = PAM_BP_MIN_SIZE + data_length; \
  96. if ((*(old_p) = PAM_BP_CALLOC(1, 1+__size))) { \
  97. __PAM_BP_WOCTET(*(old_p), 3) = __size & 0xFF; \
  98. __PAM_BP_WOCTET(*(old_p), 2) = (__size>>=8) & 0xFF; \
  99. __PAM_BP_WOCTET(*(old_p), 1) = (__size>>=8) & 0xFF; \
  100. __PAM_BP_WOCTET(*(old_p), 0) = (__size>>=8) & 0xFF; \
  101. (*(old_p))->control = cntrl; \
  102. } else { \
  103. PAM_BP_ASSERT("out of memory for binary prompt"); \
  104. } \
  105. } else { \
  106. *old_p = NULL; \
  107. } \
  108. } else { \
  109. PAM_BP_ASSERT("programming error, invalid binary prompt pointer"); \
  110. } \
  111. } while (0)
  112. #define PAM_BP_FILL(prmpt, offset, length, data) \
  113. do { \
  114. size_t bp_length; \
  115. uint8_t *prompt = (uint8_t *) (prmpt); \
  116. bp_length = PAM_BP_LENGTH(prompt); \
  117. if (bp_length < ((length)+(offset))) { \
  118. PAM_BP_ASSERT("attempt to write over end of prompt"); \
  119. } \
  120. memcpy((offset) + PAM_BP_WDATA(prompt), (data), (length)); \
  121. } while (0)
  122. #define PAM_BP_EXTRACT(prmpt, offset, length, data) \
  123. do { \
  124. size_t __bp_length; \
  125. const uint8_t *__prompt = (const uint8_t *) (prmpt); \
  126. __bp_length = PAM_BP_LENGTH(__prompt); \
  127. if (((offset) < 0) || (__bp_length < ((length)+(offset))) \
  128. || ((length) < 0)) { \
  129. PAM_BP_ASSERT("invalid extraction from prompt"); \
  130. } \
  131. memcpy((data), (offset) + PAM_BP_RDATA(__prompt), (length)); \
  132. } while (0)
  133. /* Control types */
  134. #define PAM_BPC_FALSE 0
  135. #define PAM_BPC_TRUE 1
  136. #define PAM_BPC_OK 0x01 /* continuation packet */
  137. #define PAM_BPC_SELECT 0x02 /* initialization packet */
  138. #define PAM_BPC_DONE 0x03 /* termination packet */
  139. #define PAM_BPC_FAIL 0x04 /* unable to execute */
  140. /* The following control characters are only legal for echanges
  141. between an agent and a client (it is the responsibility of the
  142. client to enforce this rule in the face of a rogue server): */
  143. #define PAM_BPC_GETENV 0x41 /* obtain client env.var */
  144. #define PAM_BPC_PUTENV 0x42 /* set client env.var */
  145. #define PAM_BPC_TEXT 0x43 /* display message */
  146. #define PAM_BPC_ERROR 0x44 /* display error message */
  147. #define PAM_BPC_PROMPT 0x45 /* echo'd text prompt */
  148. #define PAM_BPC_PASS 0x46 /* non-echo'd text prompt*/
  149. /* quick check for prompts that are legal for the client (by
  150. implication the server too) to send to libpamc */
  151. #define PAM_BPC_FOR_CLIENT(/* pamc_bp_t */ prompt) \
  152. (((prompt)->control <= PAM_BPC_FAIL && (prompt)->control >= PAM_BPC_OK) \
  153. ? PAM_BPC_TRUE:PAM_BPC_FALSE)
  154. #ifdef __cplusplus
  155. }
  156. #endif /* def __cplusplus */
  157. #endif /* PAM_CLIENT_H */