You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pw.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "util.h"
  18. #include "ottery.h"
  19. #include "cryptobox.h"
  20. #include "rspamd.h"
  21. #include "rspamadm.h"
  22. #include "unix-std.h"
  23. static void rspamadm_pw(int argc, char **argv,
  24. const struct rspamadm_command *cmd);
  25. static const char *rspamadm_pw_help(gboolean full_help,
  26. const struct rspamadm_command *cmd);
  27. static void rspamadm_pw_lua_subrs(gpointer pL);
  28. static gboolean do_encrypt = FALSE;
  29. static gboolean do_check = FALSE;
  30. static gboolean quiet = FALSE;
  31. static gboolean list = FALSE;
  32. static char *type = "catena";
  33. static char *password = NULL;
  34. struct rspamadm_command pw_command = {
  35. .name = "pw",
  36. .flags = 0,
  37. .help = rspamadm_pw_help,
  38. .run = rspamadm_pw,
  39. .lua_subrs = rspamadm_pw_lua_subrs,
  40. };
  41. static GOptionEntry entries[] = {
  42. {"encrypt", 'e', 0, G_OPTION_ARG_NONE, &do_encrypt,
  43. "Encrypt password", NULL},
  44. {"check", 'c', 0, G_OPTION_ARG_NONE, &do_check,
  45. "Check password", NULL},
  46. {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
  47. "Suppress output", NULL},
  48. {"password", 'p', 0, G_OPTION_ARG_STRING, &password,
  49. "Input password", NULL},
  50. {"type", 't', 0, G_OPTION_ARG_STRING, &type,
  51. "PBKDF type", NULL},
  52. {"list", 'l', 0, G_OPTION_ARG_NONE, &list,
  53. "List available algorithms", NULL},
  54. {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
  55. static const char *
  56. rspamadm_pw_help(gboolean full_help, const struct rspamadm_command *cmd)
  57. {
  58. const char *help_str;
  59. if (full_help) {
  60. help_str = "Manipulate with passwords in Rspamd\n\n"
  61. "Usage: rspamadm pw [command]\n"
  62. "Where commands are:\n\n"
  63. "--encrypt: encrypt password (this is a default command)\n"
  64. "--check: check encrypted password using encrypted password\n"
  65. "--list: list available pbkdf algorithms\n"
  66. "--password: input password\n"
  67. "--type: select the specified pbkdf type\n"
  68. "--help: shows available options and commands";
  69. }
  70. else {
  71. help_str = "Manage rspamd passwords";
  72. }
  73. return help_str;
  74. }
  75. static const struct rspamd_controller_pbkdf *
  76. rspamadm_get_pbkdf(void)
  77. {
  78. const struct rspamd_controller_pbkdf *pbkdf;
  79. unsigned int i;
  80. for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) {
  81. pbkdf = &pbkdf_list[i];
  82. if (strcmp(type, pbkdf->alias) == 0) {
  83. return pbkdf;
  84. }
  85. }
  86. rspamd_fprintf(stderr, "Unknown PKDF type: %s\n", type);
  87. exit(EXIT_FAILURE);
  88. return NULL;
  89. }
  90. static char *
  91. rspamadm_pw_encrypt(char *password)
  92. {
  93. const struct rspamd_controller_pbkdf *pbkdf;
  94. unsigned char *salt, *key;
  95. char *encoded_salt, *encoded_key;
  96. GString *result;
  97. gsize plen;
  98. pbkdf = rspamadm_get_pbkdf();
  99. g_assert(pbkdf != NULL);
  100. if (password == NULL) {
  101. plen = 8192;
  102. password = g_malloc0(plen);
  103. plen = rspamd_read_passphrase(password, plen, 0, NULL);
  104. }
  105. else {
  106. plen = strlen(password);
  107. }
  108. if (plen == 0) {
  109. fprintf(stderr, "Invalid password\n");
  110. exit(EXIT_FAILURE);
  111. }
  112. salt = g_alloca(pbkdf->salt_len);
  113. key = g_alloca(pbkdf->key_len);
  114. ottery_rand_bytes(salt, pbkdf->salt_len);
  115. /* Derive key */
  116. rspamd_cryptobox_pbkdf(password, strlen(password),
  117. salt, pbkdf->salt_len, key, pbkdf->key_len, pbkdf->complexity,
  118. pbkdf->type);
  119. encoded_salt = rspamd_encode_base32(salt, pbkdf->salt_len, RSPAMD_BASE32_DEFAULT);
  120. encoded_key = rspamd_encode_base32(key, pbkdf->key_len, RSPAMD_BASE32_DEFAULT);
  121. result = g_string_new("");
  122. rspamd_printf_gstring(result, "$%d$%s$%s", pbkdf->id, encoded_salt,
  123. encoded_key);
  124. g_free(encoded_salt);
  125. g_free(encoded_key);
  126. rspamd_explicit_memzero(password, plen);
  127. g_free(password);
  128. password = result->str;
  129. g_string_free(result, FALSE); /* Not freeing memory */
  130. return password;
  131. }
  132. static const char *
  133. rspamd_encrypted_password_get_str(const char *password, gsize skip,
  134. gsize *length)
  135. {
  136. const char *str, *start, *end;
  137. gsize size;
  138. start = password + skip;
  139. end = start;
  140. size = 0;
  141. while (*end != '\0' && g_ascii_isalnum(*end)) {
  142. size++;
  143. end++;
  144. }
  145. if (size) {
  146. str = start;
  147. *length = size;
  148. }
  149. else {
  150. str = NULL;
  151. }
  152. return str;
  153. }
  154. static void
  155. rspamadm_pw_check(void)
  156. {
  157. const struct rspamd_controller_pbkdf *pbkdf = NULL;
  158. const char *salt, *hash;
  159. const char *start, *end;
  160. unsigned char *salt_decoded, *key_decoded, *local_key;
  161. gsize salt_len, key_len, size;
  162. char test_password[8192], encrypted_password[8192];
  163. gsize plen, i;
  164. int id;
  165. gboolean ret = FALSE;
  166. if (password == NULL) {
  167. plen = rspamd_read_passphrase_with_prompt("Enter encrypted password: ", encrypted_password,
  168. sizeof(encrypted_password), 1, NULL);
  169. }
  170. else {
  171. plen = rspamd_strlcpy(encrypted_password, password, sizeof(encrypted_password));
  172. }
  173. if (encrypted_password[0] == '$') {
  174. /* Parse id */
  175. start = encrypted_password + 1;
  176. end = start;
  177. size = 0;
  178. while (*end != '\0' && g_ascii_isdigit(*end)) {
  179. size++;
  180. end++;
  181. }
  182. if (size > 0) {
  183. char *endptr;
  184. id = strtoul(start, &endptr, 10);
  185. if ((endptr == NULL || *endptr == *end)) {
  186. for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) {
  187. pbkdf = &pbkdf_list[i];
  188. if (pbkdf->id == id) {
  189. ret = TRUE;
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if (!ret) {
  197. rspamd_fprintf(stderr, "Invalid password format\n");
  198. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  199. exit(EXIT_FAILURE);
  200. }
  201. if (plen < pbkdf->salt_len + pbkdf->key_len + 3) {
  202. msg_err("incorrect salt: password length: %z, must be at least %z characters",
  203. plen, pbkdf->salt_len);
  204. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  205. exit(EXIT_FAILURE);
  206. }
  207. /* get salt */
  208. salt = rspamd_encrypted_password_get_str(encrypted_password, 3, &salt_len);
  209. /* get hash */
  210. hash = rspamd_encrypted_password_get_str(encrypted_password,
  211. 3 + salt_len + 1,
  212. &key_len);
  213. if (salt != NULL && hash != NULL) {
  214. /* decode salt */
  215. salt_decoded = rspamd_decode_base32(salt, salt_len, &salt_len, RSPAMD_BASE32_DEFAULT);
  216. if (salt_decoded == NULL || salt_len != pbkdf->salt_len) {
  217. /* We have some unknown salt here */
  218. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  219. msg_err("incorrect salt: %z, while %z expected",
  220. salt_len, pbkdf->salt_len);
  221. exit(EXIT_FAILURE);
  222. }
  223. key_decoded = rspamd_decode_base32(hash, key_len, &key_len, RSPAMD_BASE32_DEFAULT);
  224. if (key_decoded == NULL || key_len != pbkdf->key_len) {
  225. /* We have some unknown salt here */
  226. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  227. msg_err("incorrect key: %z, while %z expected",
  228. key_len, pbkdf->key_len);
  229. exit(EXIT_FAILURE);
  230. }
  231. plen = rspamd_read_passphrase(test_password, sizeof(test_password),
  232. 0, NULL);
  233. if (plen == 0) {
  234. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  235. fprintf(stderr, "Invalid password\n");
  236. exit(EXIT_FAILURE);
  237. }
  238. local_key = g_alloca(pbkdf->key_len);
  239. rspamd_cryptobox_pbkdf(test_password, plen,
  240. salt_decoded, salt_len,
  241. local_key, pbkdf->key_len,
  242. pbkdf->complexity,
  243. pbkdf->type);
  244. rspamd_explicit_memzero(test_password, plen);
  245. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  246. if (!rspamd_constant_memcmp(key_decoded, local_key, pbkdf->key_len)) {
  247. if (!quiet) {
  248. rspamd_printf("password incorrect\n");
  249. }
  250. exit(EXIT_FAILURE);
  251. }
  252. g_free(salt_decoded);
  253. g_free(key_decoded);
  254. }
  255. else {
  256. msg_err("bad encrypted password format");
  257. rspamd_explicit_memzero(encrypted_password, sizeof(encrypted_password));
  258. exit(EXIT_FAILURE);
  259. }
  260. if (!quiet) {
  261. rspamd_printf("password correct\n");
  262. }
  263. }
  264. static int
  265. rspamadm_pw_lua_encrypt(lua_State *L)
  266. {
  267. const char *pw_in = NULL;
  268. char *ret, *tmp = NULL;
  269. if (lua_type(L, 1) == LUA_TSTRING) {
  270. pw_in = lua_tostring(L, 1);
  271. tmp = g_strdup(pw_in);
  272. }
  273. ret = rspamadm_pw_encrypt(tmp);
  274. lua_pushstring(L, ret);
  275. g_free(ret);
  276. return 1;
  277. }
  278. static void
  279. rspamadm_pw_lua_subrs(gpointer pL)
  280. {
  281. lua_State *L = pL;
  282. lua_pushstring(L, "pw_encrypt");
  283. lua_pushcfunction(L, rspamadm_pw_lua_encrypt);
  284. lua_settable(L, -3);
  285. }
  286. static void
  287. rspamadm_alg_list(void)
  288. {
  289. const struct rspamd_controller_pbkdf *pbkdf;
  290. unsigned int i;
  291. for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i++) {
  292. pbkdf = &pbkdf_list[i];
  293. rspamd_printf("%s: %s - %s\n", pbkdf->alias, pbkdf->name,
  294. pbkdf->description);
  295. }
  296. }
  297. static void
  298. rspamadm_pw(int argc, char **argv, const struct rspamadm_command *cmd)
  299. {
  300. GOptionContext *context;
  301. GError *error = NULL;
  302. context = g_option_context_new("pw [--encrypt | --check] - manage rspamd passwords");
  303. g_option_context_set_summary(context,
  304. "Summary:\n Rspamd administration utility version " RVERSION
  305. "\n Release id: " RID);
  306. g_option_context_add_main_entries(context, entries, NULL);
  307. if (!g_option_context_parse(context, &argc, &argv, &error)) {
  308. fprintf(stderr, "option parsing failed: %s\n", error->message);
  309. g_error_free(error);
  310. g_option_context_free(context);
  311. exit(EXIT_FAILURE);
  312. }
  313. g_option_context_free(context);
  314. if (list) {
  315. rspamadm_alg_list();
  316. exit(EXIT_SUCCESS);
  317. }
  318. if (!do_encrypt && !do_check) {
  319. do_encrypt = TRUE;
  320. }
  321. if (do_encrypt) {
  322. char *encr = rspamadm_pw_encrypt(password);
  323. rspamd_printf("%s\n", encr);
  324. g_free(encr);
  325. }
  326. else if (do_check) {
  327. rspamadm_pw_check();
  328. }
  329. }