pam_auth.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef PAM_AUTH_H
  2. #define PAM_AUTH_H
  3. #include <string>
  4. #include <memory>
  5. /**
  6. * @brief PAM authentication result structure
  7. */
  8. struct PamAuthResult {
  9. bool success; ///< Authentication success
  10. std::string userId; ///< User ID if successful
  11. std::string username; ///< Username if successful
  12. std::string errorMessage; ///< Error message if failed
  13. std::string errorCode; ///< Error code for API responses
  14. };
  15. /**
  16. * @brief PAM authentication class
  17. *
  18. * This class provides PAM (Pluggable Authentication Modules) authentication
  19. * functionality for the stable-diffusion.cpp-rest server. It allows authentication
  20. * against system PAM modules, enabling integration with various authentication
  21. * backends (LDAP, Kerberos, local accounts, etc.).
  22. */
  23. class PamAuth {
  24. public:
  25. /**
  26. * @brief Construct a new Pam Auth object
  27. *
  28. * @param serviceName PAM service name (default: "stable-diffusion-rest")
  29. */
  30. explicit PamAuth(const std::string& serviceName = "stable-diffusion-rest");
  31. /**
  32. * @brief Destroy the Pam Auth object
  33. */
  34. ~PamAuth();
  35. /**
  36. * @brief Initialize PAM authentication
  37. *
  38. * @return true if initialization successful, false otherwise
  39. */
  40. bool initialize();
  41. /**
  42. * @brief Authenticate user with PAM
  43. *
  44. * @param username Username to authenticate
  45. * @param password Plain text password
  46. * @return PamAuthResult Authentication result
  47. */
  48. PamAuthResult authenticate(const std::string& username, const std::string& password);
  49. /**
  50. * @brief Check if PAM authentication is available
  51. *
  52. * @return true if PAM is available and initialized, false otherwise
  53. */
  54. bool isAvailable() const;
  55. /**
  56. * @brief Get PAM service name
  57. *
  58. * @return std::string PAM service name
  59. */
  60. std::string getServiceName() const;
  61. /**
  62. * @brief Set PAM service name
  63. *
  64. * @param serviceName PAM service name
  65. */
  66. void setServiceName(const std::string& serviceName);
  67. private:
  68. std::string m_serviceName; ///< PAM service name
  69. bool m_initialized; ///< Initialization status
  70. /**
  71. * @brief PAM conversation function
  72. *
  73. * This static function handles the conversation between PAM and the application
  74. * for password input and other authentication prompts.
  75. *
  76. * @param num_msg Number of messages
  77. * @param msg Messages from PAM
  78. * @param resp Response to PAM
  79. * @param appdata_ptr Application data pointer
  80. * @return int PAM return code
  81. */
  82. static int conversationFunction(int num_msg, const struct pam_message** msg,
  83. struct pam_response** resp, void* appdata_ptr);
  84. /**
  85. * @brief Internal PAM authentication implementation
  86. *
  87. * @param username Username to authenticate
  88. * @param password Plain text password
  89. * @return PamAuthResult Authentication result
  90. */
  91. PamAuthResult authenticateInternal(const std::string& username, const std::string& password);
  92. /**
  93. * @brief Convert PAM error code to error message
  94. *
  95. * @param pamError PAM error code
  96. * @return std::string Human-readable error message
  97. */
  98. std::string pamErrorToString(int pamError);
  99. /**
  100. * @brief Convert PAM error code to API error code
  101. *
  102. * @param pamError PAM error code
  103. * @return std::string API error code
  104. */
  105. std::string pamErrorToErrorCode(int pamError);
  106. };
  107. #endif // PAM_AUTH_H