git.fiddlerwoaroof.com
conversation.cc
7684972a
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
 
7699d7ec
 #include <security/pam_modules.h>
01c00cfe
 #include <algorithm>
 #include <vector>
 #include <string>
7699d7ec
 
 #include "conversation.h"
 
2fade7af
 namespace
 {
6f31174c
 conversation_result err = {"",""};
2ff5dc7c
 class impl : public conversation_ifc
 {
 private:
     pam pam_;
 public:
     impl (pam &pam) : pam_ (pam) {}
     conversation_result initiate (const pam_request &request)
2fade7af
     {
2ff5dc7c
         const pam_conv *conv;
         int get_conv_result = pam_.get_conv (request.handle(), &conv);
6f31174c
 
         if (get_conv_result != PAM_SUCCESS) {
             return err;
         }
 
2ff5dc7c
         pam_message msg;
         msg.msg = const_cast<char *> ("Dual control token: ");
         msg.msg_style = PAM_PROMPT_ECHO_OFF;
         std::vector<const pam_message *> messages;
         messages.push_back (&msg);
         std::vector<pam_response *> responses (1);
         int conv_result = conv->conv (1, messages.data(), responses.data(),
                                       conv->appdata_ptr);
 
9708ef79
         if (conv_result != PAM_SUCCESS) {
             return err;
         }
 
aea12a9e
         if (responses[0]->resp_retcode != 0) {
             return err;
         }
 
9708ef79
         std::string answer (responses[0]->resp);
2ff5dc7c
         std::string::iterator delim = std::find (answer.begin(), answer.end(), ':');
 
6f31174c
         if (delim == answer.end()) {
             return err;
2ff5dc7c
         }
 
6f31174c
         return {
             std::string (answer.begin(), delim),
             std::string (delim + 1, answer.end())
         };
 
2ff5dc7c
     }
 };
2fade7af
 }
 
da1b9b25
 conversation conversation::create (pam &pam)
2ff5dc7c
 {
     return conversation (std::shared_ptr<conversation_ifc> (new impl (pam)));
2fade7af
 }
0e218820