Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "challenge-email.hpp" |
| 22 | #include "../logging.hpp" |
| 23 | #include <boost/regex.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndncert { |
| 27 | |
| 28 | _LOG_INIT(ndncert.ChallengeEmail); |
| 29 | |
| 30 | NDNCERT_REGISTER_CHALLENGE(ChallengeEmail, "Email"); |
| 31 | |
| 32 | const std::string ChallengeEmail::NEED_CODE = "need-code"; |
| 33 | const std::string ChallengeEmail::WRONG_CODE = "wrong-code"; |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 34 | |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 35 | const std::string ChallengeEmail::FAILURE_INVALID_EMAIL = "failure-invalid-email"; |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 36 | const std::string ChallengeEmail::FAILURE_TIMEOUT = "timeout"; |
| 37 | const std::string ChallengeEmail::FAILURE_MAXRETRY = "max-retry"; |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 38 | |
| 39 | const std::string ChallengeEmail::JSON_EMAIL = "email"; |
| 40 | const std::string ChallengeEmail::JSON_CODE_TP = "code-timepoint"; |
| 41 | const std::string ChallengeEmail::JSON_CODE = "code"; |
| 42 | const std::string ChallengeEmail::JSON_ATTEMPT_TIMES = "attempt-times"; |
| 43 | |
| 44 | ChallengeEmail::ChallengeEmail(const std::string& scriptPath, |
| 45 | const size_t& maxAttemptTimes, |
| 46 | const time::seconds secretLifetime) |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 47 | : ChallengeModule("Email") |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 48 | , m_sendEmailScript(scriptPath) |
| 49 | , m_maxAttemptTimes(maxAttemptTimes) |
| 50 | , m_secretLifetime(secretLifetime) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | JsonSection |
| 55 | ChallengeEmail::processSelectInterest(const Interest& interest, CertificateRequest& request) |
| 56 | { |
| 57 | // interest format: /caName/CA/_SELECT/{"request-id":"id"}/EMAIL/{"Email":"email"}/<signature> |
| 58 | JsonSection emailJson = getJsonFromNameComponent(interest.getName(), |
| 59 | request.getCaName().size() + 4); |
| 60 | std::string emailAddress = emailJson.get<std::string>(JSON_EMAIL); |
| 61 | if (!isValidEmailAddress(emailAddress)) { |
| 62 | request.setStatus(FAILURE_INVALID_EMAIL); |
| 63 | request.setChallengeType(CHALLENGE_TYPE); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 64 | return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_INVALID_EMAIL); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::string emailCode = generateSecretCode(); |
| 68 | sendEmail(emailAddress, emailCode); |
| 69 | |
| 70 | request.setStatus(NEED_CODE); |
| 71 | request.setChallengeType(CHALLENGE_TYPE); |
| 72 | request.setChallengeSecrets(generateStoredSecrets(time::system_clock::now(), |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 73 | emailCode, m_maxAttemptTimes)); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 74 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, NEED_CODE); |
| 75 | } |
| 76 | |
| 77 | JsonSection |
| 78 | ChallengeEmail::processValidateInterest(const Interest& interest, CertificateRequest& request) |
| 79 | { |
| 80 | // interest format: /caName/CA/_VALIDATION/{"request-id":"id"}/EMAIL/{"code":"code"}/<signature> |
| 81 | JsonSection infoJson = getJsonFromNameComponent(interest.getName(), request.getCaName().size() + 4); |
| 82 | std::string givenCode = infoJson.get<std::string>(JSON_CODE); |
| 83 | |
| 84 | const auto parsedSecret = parseStoredSecrets(request.getChallengeSecrets()); |
| 85 | if (time::system_clock::now() - std::get<0>(parsedSecret) >= m_secretLifetime) { |
| 86 | // secret expires |
| 87 | request.setStatus(FAILURE_TIMEOUT); |
| 88 | request.setChallengeSecrets(JsonSection()); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 89 | return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_TIMEOUT); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 90 | } |
| 91 | else if (givenCode == std::get<1>(parsedSecret)) { |
| 92 | request.setStatus(SUCCESS); |
| 93 | request.setChallengeSecrets(JsonSection()); |
| 94 | Name downloadName = genDownloadName(request.getCaName(), request.getRequestId()); |
| 95 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, SUCCESS, downloadName); |
| 96 | } |
| 97 | else { |
| 98 | // check rest attempt times |
| 99 | if (std::get<2>(parsedSecret) > 1) { |
| 100 | int restAttemptTimes = std::get<2>(parsedSecret) - 1; |
| 101 | request.setStatus(WRONG_CODE); |
| 102 | request.setChallengeSecrets(generateStoredSecrets(std::get<0>(parsedSecret), |
| 103 | std::get<1>(parsedSecret), |
| 104 | restAttemptTimes)); |
| 105 | return genResponseChallengeJson(request.getRequestId(), CHALLENGE_TYPE, WRONG_CODE); |
| 106 | } |
| 107 | else { |
| 108 | // run out times |
| 109 | request.setStatus(FAILURE_MAXRETRY); |
| 110 | request.setChallengeSecrets(JsonSection()); |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 111 | return genFailureJson(request.getRequestId(), CHALLENGE_TYPE, FAILURE, FAILURE_MAXRETRY); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | std::list<std::string> |
| 117 | ChallengeEmail::getSelectRequirements() |
| 118 | { |
| 119 | std::list<std::string> result; |
| 120 | result.push_back("Please input your email address:"); |
| 121 | return result; |
| 122 | } |
| 123 | |
| 124 | std::list<std::string> |
| 125 | ChallengeEmail::getValidateRequirements(const std::string& status) |
| 126 | { |
| 127 | std::list<std::string> result; |
| 128 | if (status == NEED_CODE) { |
| 129 | result.push_back("Please input your verification code:"); |
| 130 | } |
| 131 | else if (status == WRONG_CODE) { |
Zhiyi Zhang | a9bda73 | 2017-05-20 22:58:55 -0700 | [diff] [blame^] | 132 | result.push_back("Incorrect PIN code, please try again and input your verification code:"); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | JsonSection |
| 138 | ChallengeEmail::doGenSelectParamsJson(const std::string& status, |
| 139 | const std::list<std::string>& paramList) |
| 140 | { |
| 141 | JsonSection result; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 142 | BOOST_ASSERT(status == WAIT_SELECTION); |
Zhiyi Zhang | defa959 | 2017-02-21 10:56:22 -0800 | [diff] [blame] | 143 | BOOST_ASSERT(paramList.size() == 1); |
| 144 | result.put(JSON_EMAIL, paramList.front()); |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | JsonSection |
| 149 | ChallengeEmail::doGenValidateParamsJson(const std::string& status, |
| 150 | const std::list<std::string>& paramList) |
| 151 | { |
| 152 | JsonSection result; |
| 153 | BOOST_ASSERT(paramList.size() == 1); |
| 154 | result.put(JSON_CODE, paramList.front()); |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | std::tuple<time::system_clock::TimePoint, std::string, int> |
| 159 | ChallengeEmail::parseStoredSecrets(const JsonSection& storedSecrets) |
| 160 | { |
| 161 | auto tp = time::fromIsoString(storedSecrets.get<std::string>(JSON_CODE_TP)); |
| 162 | std::string rightCode= storedSecrets.get<std::string>(JSON_CODE); |
| 163 | int attemptTimes = std::stoi(storedSecrets.get<std::string>(JSON_ATTEMPT_TIMES)); |
| 164 | |
| 165 | return std::make_tuple(tp, rightCode, attemptTimes); |
| 166 | } |
| 167 | |
| 168 | JsonSection |
| 169 | ChallengeEmail::generateStoredSecrets(const time::system_clock::TimePoint& tp, |
| 170 | const std::string& secretCode, int attempTimes) |
| 171 | { |
| 172 | JsonSection json; |
| 173 | json.put(JSON_CODE_TP, time::toIsoString(tp)); |
| 174 | json.put(JSON_CODE, secretCode); |
| 175 | json.put(JSON_ATTEMPT_TIMES, std::to_string(attempTimes)); |
| 176 | return json; |
| 177 | } |
| 178 | |
| 179 | bool |
| 180 | ChallengeEmail::isValidEmailAddress(const std::string& emailAddress) |
| 181 | { |
| 182 | std::string pattern = R"_REGEX_((^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9\-\.]+$))_REGEX_"; |
| 183 | boost::regex emailPattern(pattern); |
| 184 | return boost::regex_match(emailAddress, emailPattern); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | ChallengeEmail::sendEmail(const std::string& emailAddress, const std::string& secret) const |
| 189 | { |
| 190 | pid_t pid = fork(); |
| 191 | |
| 192 | if (pid < 0) { |
| 193 | _LOG_TRACE("Cannot fork before trying to call email sending script"); |
| 194 | } |
| 195 | else if (pid == 0) { |
| 196 | int ret; |
| 197 | std::vector<char> emailParam(emailAddress.begin(), emailAddress.end()); |
| 198 | emailParam.push_back('\0'); |
| 199 | |
| 200 | std::vector<char> secretParam(secret.begin(), secret.end()); |
| 201 | secretParam.push_back('\0'); |
| 202 | |
| 203 | std::vector<char> defaultParam(m_sendEmailScript.begin(), m_sendEmailScript.end()); |
| 204 | defaultParam.push_back('\0'); |
| 205 | |
| 206 | char* argv[] = {&defaultParam[0], &emailParam[0], &secretParam[0], nullptr}; |
| 207 | ret = execve(m_sendEmailScript.c_str(), argv, nullptr); |
| 208 | |
| 209 | BOOST_THROW_EXCEPTION(Error("Email sending script went wrong, error code: " + std::to_string(ret))); |
| 210 | } |
| 211 | |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | } // namespace ndncert |
| 216 | } // namespace ndn |