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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 (gint argc, gchar **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 gchar *type = "catena";
  33. static gchar *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. };
  56. static const char *
  57. rspamadm_pw_help (gboolean full_help, const struct rspamadm_command *cmd)
  58. {
  59. const char *help_str;
  60. if (full_help) {
  61. help_str = "Manipulate with passwords in rspamd\n\n"
  62. "Usage: rspamadm pw [command]\n"
  63. "Where commands are:\n\n"
  64. "--encrypt: encrypt password (this is a default command)\n"
  65. "--check: check encrypted password using encrypted password\n"
  66. "--list: list available pbkdf algorithms\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. guint 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. guchar *salt, *key;
  95. gchar *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 gchar *
  133. rspamd_encrypted_password_get_str (const gchar *password, gsize skip,
  134. gsize *length)
  135. {
  136. const gchar *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. GIOChannel *in;
  159. GString *encrypted_pwd;
  160. const gchar *salt, *hash;
  161. const gchar *start, *end;
  162. guchar *salt_decoded, *key_decoded, *local_key;
  163. gsize salt_len, key_len, size;
  164. gchar test_password[8192];
  165. gsize plen, term = 0, i;
  166. gint id;
  167. gboolean ret = FALSE;
  168. if (password == NULL) {
  169. encrypted_pwd = g_string_new ("");
  170. in = g_io_channel_unix_new (STDIN_FILENO);
  171. rspamd_printf ("Enter encrypted password: ");
  172. fflush (stdout);
  173. g_io_channel_read_line_string (in, encrypted_pwd, &term, NULL);
  174. if (term != 0) {
  175. g_string_erase (encrypted_pwd, term, encrypted_pwd->len - term);
  176. }
  177. g_io_channel_unref (in);
  178. }
  179. else {
  180. encrypted_pwd = g_string_new (password);
  181. }
  182. if (encrypted_pwd->str[0] == '$') {
  183. /* Parse id */
  184. start = encrypted_pwd->str + 1;
  185. end = start;
  186. size = 0;
  187. while (*end != '\0' && g_ascii_isdigit (*end)) {
  188. size++;
  189. end++;
  190. }
  191. if (size > 0) {
  192. gchar *endptr;
  193. id = strtoul (start, &endptr, 10);
  194. if ((endptr == NULL || *endptr == *end)) {
  195. for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) {
  196. pbkdf = &pbkdf_list[i];
  197. if (pbkdf->id == id) {
  198. ret = TRUE;
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. if (!ret) {
  206. rspamd_fprintf (stderr, "Invalid password format\n");
  207. exit (EXIT_FAILURE);
  208. }
  209. if (encrypted_pwd->len < pbkdf->salt_len + pbkdf->key_len + 3) {
  210. msg_err ("incorrect salt: password length: %z, must be at least %z characters",
  211. encrypted_pwd->len, pbkdf->salt_len);
  212. exit (EXIT_FAILURE);
  213. }
  214. /* get salt */
  215. salt = rspamd_encrypted_password_get_str (encrypted_pwd->str, 3, &salt_len);
  216. /* get hash */
  217. hash = rspamd_encrypted_password_get_str (encrypted_pwd->str,
  218. 3 + salt_len + 1,
  219. &key_len);
  220. if (salt != NULL && hash != NULL) {
  221. /* decode salt */
  222. salt_decoded = rspamd_decode_base32 (salt, salt_len, &salt_len, RSPAMD_BASE32_DEFAULT);
  223. if (salt_decoded == NULL || salt_len != pbkdf->salt_len) {
  224. /* We have some unknown salt here */
  225. msg_err ("incorrect salt: %z, while %z expected",
  226. salt_len, pbkdf->salt_len);
  227. exit (EXIT_FAILURE);
  228. }
  229. key_decoded = rspamd_decode_base32 (hash, key_len, &key_len, RSPAMD_BASE32_DEFAULT);
  230. if (key_decoded == NULL || key_len != pbkdf->key_len) {
  231. /* We have some unknown salt here */
  232. msg_err ("incorrect key: %z, while %z expected",
  233. key_len, pbkdf->key_len);
  234. exit (EXIT_FAILURE);
  235. }
  236. plen = rspamd_read_passphrase (test_password, sizeof (test_password),
  237. 0, NULL);
  238. if (plen == 0) {
  239. fprintf (stderr, "Invalid password\n");
  240. exit (EXIT_FAILURE);
  241. }
  242. local_key = g_alloca (pbkdf->key_len);
  243. rspamd_cryptobox_pbkdf (test_password, plen,
  244. salt_decoded, salt_len,
  245. local_key, pbkdf->key_len,
  246. pbkdf->complexity,
  247. pbkdf->type);
  248. rspamd_explicit_memzero (test_password, plen);
  249. if (!rspamd_constant_memcmp (key_decoded, local_key, pbkdf->key_len)) {
  250. if (!quiet) {
  251. rspamd_printf ("password incorrect\n");
  252. }
  253. exit (EXIT_FAILURE);
  254. }
  255. g_free (salt_decoded);
  256. g_free (key_decoded);
  257. g_string_free (encrypted_pwd, TRUE);
  258. }
  259. else {
  260. msg_err ("bad encrypted password format");
  261. exit (EXIT_FAILURE);
  262. }
  263. if (!quiet) {
  264. rspamd_printf ("password correct\n");
  265. }
  266. }
  267. static gint
  268. rspamadm_pw_lua_encrypt (lua_State *L)
  269. {
  270. const gchar *pw_in = NULL;
  271. gchar *ret, *tmp = NULL;
  272. if (lua_type (L, 1) == LUA_TSTRING) {
  273. pw_in = lua_tostring (L, 1);
  274. tmp = g_strdup (pw_in);
  275. }
  276. ret = rspamadm_pw_encrypt (tmp);
  277. lua_pushstring (L, ret);
  278. g_free (ret);
  279. return 1;
  280. }
  281. static void
  282. rspamadm_pw_lua_subrs (gpointer pL)
  283. {
  284. lua_State *L = pL;
  285. lua_pushstring (L, "pw_encrypt");
  286. lua_pushcfunction (L, rspamadm_pw_lua_encrypt);
  287. lua_settable (L, -3);
  288. }
  289. static void
  290. rspamadm_alg_list (void)
  291. {
  292. const struct rspamd_controller_pbkdf *pbkdf;
  293. guint i;
  294. for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) {
  295. pbkdf = &pbkdf_list[i];
  296. rspamd_printf ("%s: %s - %s\n", pbkdf->alias, pbkdf->name,
  297. pbkdf->description);
  298. }
  299. }
  300. static void
  301. rspamadm_pw (gint argc, gchar **argv, const struct rspamadm_command *cmd)
  302. {
  303. GOptionContext *context;
  304. GError *error = NULL;
  305. context = g_option_context_new ("pw [--encrypt | --check] - manage rspamd passwords");
  306. g_option_context_set_summary (context,
  307. "Summary:\n Rspamd administration utility version "
  308. RVERSION
  309. "\n Release id: "
  310. RID);
  311. g_option_context_add_main_entries (context, entries, NULL);
  312. if (!g_option_context_parse (context, &argc, &argv, &error)) {
  313. fprintf (stderr, "option parsing failed: %s\n", error->message);
  314. g_error_free (error);
  315. g_option_context_free (context);
  316. exit (EXIT_FAILURE);
  317. }
  318. g_option_context_free (context);
  319. if (list) {
  320. rspamadm_alg_list ();
  321. exit (EXIT_SUCCESS);
  322. }
  323. if (!do_encrypt && !do_check) {
  324. do_encrypt = TRUE;
  325. }
  326. if (do_encrypt) {
  327. gchar *encr = rspamadm_pw_encrypt (password);
  328. rspamd_printf ("%s\n", encr);
  329. g_free (encr);
  330. }
  331. else if (do_check) {
  332. rspamadm_pw_check ();
  333. }
  334. }