passwd.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <iostream>
  19. #include <kopano/charset/convert.h>
  20. #include <climits>
  21. #include <cmath>
  22. #include <getopt.h>
  23. #include <kopano/memory.hpp>
  24. #include <mapidefs.h>
  25. #include <mapispi.h>
  26. #include <mapix.h>
  27. #include <mapiutil.h>
  28. #include <kopano/IECServiceAdmin.h>
  29. #include <kopano/IECUnknown.h>
  30. #include <kopano/ECTags.h>
  31. #include <kopano/ECGuid.h>
  32. #include <kopano/CommonUtil.h>
  33. #include <kopano/ecversion.h>
  34. #include <kopano/stringutil.h>
  35. #include <kopano/MAPIErrors.h>
  36. #include <kopano/ECLogger.h>
  37. using namespace std;
  38. using namespace KCHL;
  39. static bool verbose = false;
  40. enum modes {
  41. MODE_INVALID = 0, MODE_CHANGE_PASSWD, MODE_HELP
  42. };
  43. enum {
  44. OPT_HELP = UCHAR_MAX + 1, // high to avoid clashes with modes
  45. OPT_HOST
  46. };
  47. static const struct option long_options[] = {
  48. { "help", 0, NULL, OPT_HELP },
  49. { "host", 1, NULL, OPT_HOST }
  50. };
  51. static void print_help(const char *name)
  52. {
  53. cout << "Usage:" << endl;
  54. cout << name << " [action] [options]" << endl << endl;
  55. cout << "Actions: [-u] " << endl;
  56. cout << "\t" << " -u user" << "\t" << "update user password, -p or -P" << endl;
  57. cout << endl;
  58. cout << "Options: [-u username] [-p password] [-o oldpassword] [-h path]" << endl;
  59. cout << "\t" << " -o oldpass" << "\t\t" << "old password to login" << endl;
  60. cout << "\t" << " -p pass" << "\t\t" << "set password to pass" << endl;
  61. cout << endl;
  62. cout << "Global options: [-h|--host path]" << endl;
  63. cout << "\t" << " -h path" << "\t\t" << "connect through <path>, e.g. file:///var/run/socket" << endl;
  64. cout << "\t" << " -v\t\tenable verbosity" << endl;
  65. cout << "\t" << " -V Print version info." << endl;
  66. cout << "\t" << " --help" << "\t\t" << "show this help text." << endl;
  67. cout << endl;
  68. }
  69. static HRESULT UpdatePassword(const char *lpPath, const char *lpUsername,
  70. const char *lpPassword, const char *lpNewPassword)
  71. {
  72. HRESULT hr = hrSuccess;
  73. object_ptr<IMAPISession> lpSession;
  74. object_ptr<IECUnknown> lpECMsgStore;
  75. object_ptr<IMsgStore> lpMsgStore;
  76. object_ptr<IECServiceAdmin> lpServiceAdmin;
  77. ULONG cbUserId = 0;
  78. memory_ptr<ENTRYID> lpUserId;
  79. memory_ptr<SPropValue> lpPropValue;
  80. memory_ptr<ECUSER> lpECUser;
  81. convert_context converter;
  82. std::wstring strwUsername, strwPassword;
  83. strwUsername = converter.convert_to<wstring>(lpUsername);
  84. strwPassword = converter.convert_to<wstring>(lpPassword);
  85. ECLogger *lpLogger = NULL;
  86. if (verbose)
  87. lpLogger = new ECLogger_File(EC_LOGLEVEL_FATAL, 0, "-", false);
  88. else
  89. lpLogger = new ECLogger_Null();
  90. ec_log_set(lpLogger);
  91. hr = HrOpenECSession(&~lpSession, "kopano-passwd", PROJECT_SVN_REV_STR,
  92. strwUsername.c_str(), strwPassword.c_str(), lpPath,
  93. EC_PROFILE_FLAGS_NO_NOTIFICATIONS | EC_PROFILE_FLAGS_NO_PUBLIC_STORE,
  94. NULL, NULL);
  95. lpLogger->Release();
  96. if(hr != hrSuccess) {
  97. cerr << "Wrong username or password." << endl;
  98. return hr;
  99. }
  100. hr = HrOpenDefaultStore(lpSession, &~lpMsgStore);
  101. if(hr != hrSuccess) {
  102. cerr << "Unable to open store." << endl;
  103. return hr;
  104. }
  105. hr = HrGetOneProp(lpMsgStore, PR_EC_OBJECT, &~lpPropValue);
  106. if(hr != hrSuccess || !lpPropValue)
  107. return hr;
  108. lpECMsgStore.reset(reinterpret_cast<IECUnknown *>(lpPropValue->Value.lpszA), false);
  109. if(!lpECMsgStore)
  110. return hr;
  111. lpECMsgStore->AddRef();
  112. hr = lpECMsgStore->QueryInterface(IID_IECServiceAdmin, &~lpServiceAdmin);
  113. if(hr != hrSuccess)
  114. return hr;
  115. hr = lpServiceAdmin->ResolveUserName((LPTSTR)lpUsername, 0, &cbUserId, &~lpUserId);
  116. if (hr != hrSuccess) {
  117. cerr << "Unable to update password, user not found." << endl;
  118. return hr;
  119. }
  120. // get old features. we need these, because not setting them would mean: remove them
  121. hr = lpServiceAdmin->GetUser(cbUserId, lpUserId, 0, &~lpECUser);
  122. if (hr != hrSuccess) {
  123. cerr << "Unable to get user details, " << getMapiCodeString(hr, lpUsername) << endl;
  124. return hr;
  125. }
  126. lpECUser->lpszPassword = (LPTSTR)lpNewPassword;
  127. hr = lpServiceAdmin->SetUser(lpECUser, 0);
  128. if(hr != hrSuccess) {
  129. cerr << "Unable to update user password." << endl;
  130. return hr;
  131. }
  132. return hrSuccess;
  133. }
  134. int main(int argc, char* argv[])
  135. {
  136. HRESULT hr = hrSuccess;
  137. const char *username = NULL;
  138. const char *newpassword = NULL;
  139. char szOldPassword[80];
  140. char szNewPassword[80];
  141. const char *oldpassword = NULL;
  142. const char *repassword = NULL;
  143. const char *path = NULL;
  144. modes mode = MODE_INVALID;
  145. int passprompt = 1;
  146. setlocale(LC_MESSAGES, "");
  147. setlocale(LC_CTYPE, "");
  148. if(argc < 2) {
  149. print_help(argv[0]);
  150. return 1;
  151. }
  152. int c;
  153. while (1) {
  154. c = getopt_long(argc, argv, "u:Pp:h:o:Vv", long_options, NULL);
  155. if (c == -1)
  156. break;
  157. switch (c) {
  158. case 'u':
  159. mode = MODE_CHANGE_PASSWD;
  160. username = optarg;
  161. break;
  162. case 'p':
  163. newpassword = optarg;
  164. passprompt = 0;
  165. break;
  166. case 'o':
  167. oldpassword = optarg;
  168. passprompt = 0;
  169. break;
  170. // error handling?
  171. case '?':
  172. break;
  173. case OPT_HOST:
  174. case 'h':
  175. path = optarg;
  176. break;
  177. case 'V':
  178. cout << "Product version:\t" <<
  179. PROJECT_VERSION_PASSWD_STR << endl <<
  180. "File version:\t\t" << PROJECT_SVN_REV_STR <<
  181. endl;
  182. return 1;
  183. case 'v':
  184. verbose = true;
  185. break;
  186. case OPT_HELP:
  187. mode = MODE_HELP;
  188. break;
  189. default:
  190. break;
  191. };
  192. }
  193. // check parameters
  194. if (optind < argc) {
  195. cerr << "Too many options given." << endl;
  196. return 1;
  197. }
  198. if (mode == MODE_INVALID) {
  199. cerr << "No correct command given." << endl;
  200. return 1;
  201. }
  202. if (mode == MODE_HELP) {
  203. print_help(argv[0]);
  204. return 0;
  205. }
  206. if (mode == MODE_CHANGE_PASSWD && ((newpassword == NULL && passprompt == 0) ||
  207. username == NULL || (oldpassword == NULL && passprompt == 0)) ) {
  208. cerr << "Missing information to update user password." << endl;
  209. return 1;
  210. }
  211. //Init mapi
  212. hr = MAPIInitialize(NULL);
  213. if (hr != hrSuccess) {
  214. cerr << "Unable to initialize" << endl;
  215. goto exit;
  216. }
  217. // fully logged on, action!
  218. switch(mode) {
  219. case MODE_CHANGE_PASSWD:
  220. if(passprompt)
  221. {
  222. oldpassword = get_password("Enter old password:");
  223. if(oldpassword == NULL)
  224. {
  225. cerr << "Wrong old password" << endl;
  226. goto exit;
  227. }
  228. cout << endl;
  229. strcpy(szOldPassword, oldpassword);
  230. newpassword = get_password("Enter new password:");
  231. if(oldpassword == NULL)
  232. {
  233. cerr << "Wrong new password" << endl;
  234. goto exit;
  235. }
  236. cout << endl;
  237. kc_strlcpy(szNewPassword, newpassword, sizeof(szNewPassword));
  238. repassword = get_password("Re-Enter password:");
  239. if (strcmp(newpassword, repassword) != 0)
  240. cerr << "Passwords don't match" << endl;
  241. cout << endl;
  242. oldpassword = szOldPassword;
  243. newpassword = szNewPassword;
  244. }
  245. hr = UpdatePassword(path, username, oldpassword, newpassword);
  246. if (hr != hrSuccess)
  247. goto exit;
  248. case MODE_INVALID:
  249. case MODE_HELP:
  250. // happy compiler
  251. break;
  252. };
  253. exit:
  254. MAPIUninitialize();
  255. if (hr == hrSuccess)
  256. return 0;
  257. else
  258. return 1;
  259. }