pam_client.h 7.1 KB

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