plain.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* Plain SASL plugin
  2. * Rob Siemborski
  3. * Tim Martin
  4. */
  5. /*
  6. * Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The name "Carnegie Mellon University" must not be used to
  21. * endorse or promote products derived from this software without
  22. * prior written permission. For permission or any other legal
  23. * details, please contact
  24. * Carnegie Mellon University
  25. * Center for Technology Transfer and Enterprise Creation
  26. * 4615 Forbes Avenue
  27. * Suite 302
  28. * Pittsburgh, PA 15213
  29. * (412) 268-7393, fax: (412) 268-7395
  30. * innovation@andrew.cmu.edu
  31. *
  32. * 4. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by Computing Services
  35. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  36. *
  37. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  38. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  39. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  40. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  41. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  42. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  43. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  44. */
  45. #include <config.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <sasl.h>
  49. #include <saslplug.h>
  50. #include "plugin_common.h"
  51. #ifdef macintosh
  52. #error #include <sasl_plain_plugin_decl.h>
  53. #endif
  54. /***************************** Common Section *****************************/
  55. /***************************** Server Section *****************************/
  56. static int plain_server_mech_new(void *glob_context __attribute__((unused)),
  57. sasl_server_params_t *sparams,
  58. const char *challenge __attribute__((unused)),
  59. unsigned challen __attribute__((unused)),
  60. void **conn_context)
  61. {
  62. /* holds state are in */
  63. if (!conn_context) {
  64. PARAMERROR( sparams->utils );
  65. return SASL_BADPARAM;
  66. }
  67. *conn_context = NULL;
  68. return SASL_OK;
  69. }
  70. static int plain_server_mech_step(void *conn_context __attribute__((unused)),
  71. sasl_server_params_t *params,
  72. const char *clientin,
  73. unsigned clientinlen,
  74. const char **serverout,
  75. unsigned *serveroutlen,
  76. sasl_out_params_t *oparams)
  77. {
  78. const char *author;
  79. const char *authen;
  80. const char *password;
  81. unsigned password_len;
  82. unsigned lup = 0;
  83. int result;
  84. char *passcopy;
  85. unsigned canon_flags = 0;
  86. *serverout = NULL;
  87. *serveroutlen = 0;
  88. /* should have received author-id NUL authen-id NUL password */
  89. /* get author */
  90. author = clientin;
  91. while ((lup < clientinlen) && (clientin[lup] != 0)) ++lup;
  92. if (lup >= clientinlen) {
  93. SETERROR(params->utils, "Can only find author (no password)");
  94. return SASL_BADPROT;
  95. }
  96. /* get authen */
  97. ++lup;
  98. authen = clientin + lup;
  99. while ((lup < clientinlen) && (clientin[lup] != 0)) ++lup;
  100. if (lup >= clientinlen) {
  101. params->utils->seterror(params->utils->conn, 0,
  102. "Can only find author/en (no password)");
  103. return SASL_BADPROT;
  104. }
  105. /* get password */
  106. lup++;
  107. password = clientin + lup;
  108. while ((lup < clientinlen) && (clientin[lup] != 0)) ++lup;
  109. password_len = (unsigned) (clientin + lup - password);
  110. if (lup != clientinlen) {
  111. SETERROR(params->utils,
  112. "Got more data than we were expecting in the PLAIN plugin\n");
  113. return SASL_BADPROT;
  114. }
  115. /* this kinda sucks. we need password to be null terminated
  116. but we can't assume there is an allocated byte at the end
  117. of password so we have to copy it */
  118. passcopy = params->utils->malloc(password_len + 1);
  119. if (passcopy == NULL) {
  120. MEMERROR(params->utils);
  121. return SASL_NOMEM;
  122. }
  123. strncpy(passcopy, password, password_len);
  124. passcopy[password_len] = '\0';
  125. /* Canonicalize userid first, so that password verification is only
  126. * against the canonical id */
  127. if (!author || !*author) {
  128. author = authen;
  129. canon_flags = SASL_CU_AUTHZID;
  130. } else if (strcmp(author, authen) == 0) {
  131. /* While this isn't going to find out that <user> and <user>@<defaultdomain>
  132. are the same thing, this is good enough for many cases */
  133. canon_flags = SASL_CU_AUTHZID;
  134. }
  135. result = params->canon_user(params->utils->conn,
  136. authen,
  137. 0,
  138. SASL_CU_AUTHID | canon_flags | SASL_CU_EXTERNALLY_VERIFIED,
  139. oparams);
  140. if (result != SASL_OK) {
  141. _plug_free_string(params->utils, &passcopy);
  142. return result;
  143. }
  144. /* verify password (and possibly fetch both authentication and
  145. authorization identity related properties) - return SASL_OK
  146. on success */
  147. result = params->utils->checkpass(params->utils->conn,
  148. oparams->authid,
  149. oparams->alen,
  150. passcopy,
  151. password_len);
  152. _plug_free_string(params->utils, &passcopy);
  153. if (result != SASL_OK) {
  154. params->utils->seterror(params->utils->conn, 0,
  155. "Password verification failed");
  156. return result;
  157. }
  158. /* Canonicalize and store the authorization ID */
  159. /* We need to do this after calling verify_user just in case verify_user
  160. * needed to get auxprops itself */
  161. if (canon_flags == 0) {
  162. const struct propval *pr;
  163. int i;
  164. pr = params->utils->prop_get(params->propctx);
  165. if (!pr) {
  166. return SASL_FAIL;
  167. }
  168. /* params->utils->checkpass() might have fetched authorization identity related properties
  169. for the wrong user name. Free these values. */
  170. for (i = 0; pr[i].name; i++) {
  171. if (pr[i].name[0] == '*') {
  172. continue;
  173. }
  174. if (pr[i].values) {
  175. params->utils->prop_erase(params->propctx, pr[i].name);
  176. }
  177. }
  178. result = params->canon_user(params->utils->conn,
  179. author,
  180. 0,
  181. SASL_CU_AUTHZID,
  182. oparams);
  183. if (result != SASL_OK) {
  184. return result;
  185. }
  186. }
  187. /* set oparams */
  188. oparams->doneflag = 1;
  189. oparams->mech_ssf = 0;
  190. oparams->maxoutbuf = 0;
  191. oparams->encode_context = NULL;
  192. oparams->encode = NULL;
  193. oparams->decode_context = NULL;
  194. oparams->decode = NULL;
  195. oparams->param_version = 0;
  196. return SASL_OK;
  197. }
  198. static sasl_server_plug_t plain_server_plugins[] =
  199. {
  200. {
  201. "PLAIN", /* mech_name */
  202. 0, /* max_ssf */
  203. SASL_SEC_NOANONYMOUS
  204. | SASL_SEC_PASS_CREDENTIALS, /* security_flags */
  205. SASL_FEAT_WANT_CLIENT_FIRST
  206. | SASL_FEAT_ALLOWS_PROXY, /* features */
  207. NULL, /* glob_context */
  208. &plain_server_mech_new, /* mech_new */
  209. &plain_server_mech_step, /* mech_step */
  210. NULL, /* mech_dispose */
  211. NULL, /* mech_free */
  212. NULL, /* setpass */
  213. NULL, /* user_query */
  214. NULL, /* idle */
  215. NULL, /* mech_avail */
  216. NULL /* spare */
  217. }
  218. };
  219. int plain_server_plug_init(const sasl_utils_t *utils,
  220. int maxversion,
  221. int *out_version,
  222. sasl_server_plug_t **pluglist,
  223. int *plugcount)
  224. {
  225. if (maxversion < SASL_SERVER_PLUG_VERSION) {
  226. SETERROR(utils, "PLAIN version mismatch");
  227. return SASL_BADVERS;
  228. }
  229. *out_version = SASL_SERVER_PLUG_VERSION;
  230. *pluglist = plain_server_plugins;
  231. *plugcount = 1;
  232. return SASL_OK;
  233. }
  234. /***************************** Client Section *****************************/
  235. typedef struct client_context {
  236. char *out_buf;
  237. unsigned out_buf_len;
  238. } client_context_t;
  239. static int plain_client_mech_new(void *glob_context __attribute__((unused)),
  240. sasl_client_params_t *params,
  241. void **conn_context)
  242. {
  243. client_context_t *text;
  244. /* holds state are in */
  245. text = params->utils->malloc(sizeof(client_context_t));
  246. if (text == NULL) {
  247. MEMERROR( params->utils );
  248. return SASL_NOMEM;
  249. }
  250. memset(text, 0, sizeof(client_context_t));
  251. *conn_context = text;
  252. return SASL_OK;
  253. }
  254. static int plain_client_mech_step(void *conn_context,
  255. sasl_client_params_t *params,
  256. const char *serverin __attribute__((unused)),
  257. unsigned serverinlen __attribute__((unused)),
  258. sasl_interact_t **prompt_need,
  259. const char **clientout,
  260. unsigned *clientoutlen,
  261. sasl_out_params_t *oparams)
  262. {
  263. client_context_t *text = (client_context_t *) conn_context;
  264. const char *user = NULL, *authid = NULL;
  265. sasl_secret_t *password = NULL;
  266. unsigned int free_password = 0; /* set if we need to free password */
  267. int user_result = SASL_OK;
  268. int auth_result = SASL_OK;
  269. int pass_result = SASL_OK;
  270. int result;
  271. char *p;
  272. *clientout = NULL;
  273. *clientoutlen = 0;
  274. /* doesn't really matter how the server responds */
  275. /* check if sec layer strong enough */
  276. if (params->props.min_ssf > params->external_ssf) {
  277. SETERROR( params->utils, "SSF requested of PLAIN plugin");
  278. return SASL_TOOWEAK;
  279. }
  280. /* try to get the authid */
  281. if (oparams->authid == NULL) {
  282. auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
  283. if ((auth_result != SASL_OK) && (auth_result != SASL_INTERACT))
  284. return auth_result;
  285. }
  286. /* try to get the userid */
  287. if (oparams->user == NULL) {
  288. user_result = _plug_get_userid(params->utils, &user, prompt_need);
  289. if ((user_result != SASL_OK) && (user_result != SASL_INTERACT))
  290. return user_result;
  291. }
  292. /* try to get the password */
  293. if (password == NULL) {
  294. pass_result = _plug_get_password(params->utils, &password,
  295. &free_password, prompt_need);
  296. if ((pass_result != SASL_OK) && (pass_result != SASL_INTERACT))
  297. return pass_result;
  298. }
  299. /* free prompts we got */
  300. if (prompt_need && *prompt_need) {
  301. params->utils->free(*prompt_need);
  302. *prompt_need = NULL;
  303. }
  304. /* if there are prompts not filled in */
  305. if ((user_result == SASL_INTERACT) || (auth_result == SASL_INTERACT) ||
  306. (pass_result == SASL_INTERACT)) {
  307. /* make the prompt list */
  308. result =
  309. _plug_make_prompts(params->utils, prompt_need,
  310. user_result == SASL_INTERACT ?
  311. "Please enter your authorization name" : NULL,
  312. NULL,
  313. auth_result == SASL_INTERACT ?
  314. "Please enter your authentication name" : NULL,
  315. NULL,
  316. pass_result == SASL_INTERACT ?
  317. "Please enter your password" : NULL, NULL,
  318. NULL, NULL, NULL,
  319. NULL, NULL, NULL);
  320. if (result != SASL_OK) goto cleanup;
  321. return SASL_INTERACT;
  322. }
  323. if (!password) {
  324. PARAMERROR(params->utils);
  325. return SASL_BADPARAM;
  326. }
  327. if (!user || !*user) {
  328. result = params->canon_user(params->utils->conn, authid, 0,
  329. SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
  330. }
  331. else {
  332. result = params->canon_user(params->utils->conn, user, 0,
  333. SASL_CU_AUTHZID, oparams);
  334. if (result != SASL_OK) goto cleanup;
  335. result = params->canon_user(params->utils->conn, authid, 0,
  336. SASL_CU_AUTHID, oparams);
  337. }
  338. if (result != SASL_OK) goto cleanup;
  339. /* send authorized id NUL authentication id NUL password */
  340. *clientoutlen = ((user && *user ? oparams->ulen : 0) +
  341. 1 + oparams->alen +
  342. 1 + password->len);
  343. /* remember the extra NUL on the end for stupid clients */
  344. result = _plug_buf_alloc(params->utils, &(text->out_buf),
  345. &(text->out_buf_len), *clientoutlen + 1);
  346. if (result != SASL_OK) goto cleanup;
  347. memset(text->out_buf, 0, *clientoutlen + 1);
  348. p = text->out_buf;
  349. if (user && *user) {
  350. memcpy(p, oparams->user, oparams->ulen);
  351. p += oparams->ulen;
  352. }
  353. memcpy(++p, oparams->authid, oparams->alen);
  354. p += oparams->alen;
  355. memcpy(++p, password->data, password->len);
  356. *clientout = text->out_buf;
  357. /* set oparams */
  358. oparams->doneflag = 1;
  359. oparams->mech_ssf = 0;
  360. oparams->maxoutbuf = 0;
  361. oparams->encode_context = NULL;
  362. oparams->encode = NULL;
  363. oparams->decode_context = NULL;
  364. oparams->decode = NULL;
  365. oparams->param_version = 0;
  366. result = SASL_OK;
  367. cleanup:
  368. /* free sensitive info */
  369. if (free_password) _plug_free_secret(params->utils, &password);
  370. return result;
  371. }
  372. static void plain_client_mech_dispose(void *conn_context,
  373. const sasl_utils_t *utils)
  374. {
  375. client_context_t *text = (client_context_t *) conn_context;
  376. if (!text) return;
  377. if (text->out_buf) utils->free(text->out_buf);
  378. utils->free(text);
  379. }
  380. static sasl_client_plug_t plain_client_plugins[] =
  381. {
  382. {
  383. "PLAIN", /* mech_name */
  384. 0, /* max_ssf */
  385. SASL_SEC_NOANONYMOUS
  386. | SASL_SEC_PASS_CREDENTIALS, /* security_flags */
  387. SASL_FEAT_WANT_CLIENT_FIRST
  388. | SASL_FEAT_ALLOWS_PROXY, /* features */
  389. NULL, /* required_prompts */
  390. NULL, /* glob_context */
  391. &plain_client_mech_new, /* mech_new */
  392. &plain_client_mech_step, /* mech_step */
  393. &plain_client_mech_dispose, /* mech_dispose */
  394. NULL, /* mech_free */
  395. NULL, /* idle */
  396. NULL, /* spare */
  397. NULL /* spare */
  398. }
  399. };
  400. int plain_client_plug_init(sasl_utils_t *utils,
  401. int maxversion,
  402. int *out_version,
  403. sasl_client_plug_t **pluglist,
  404. int *plugcount)
  405. {
  406. if (maxversion < SASL_CLIENT_PLUG_VERSION) {
  407. SETERROR(utils, "PLAIN version mismatch");
  408. return SASL_BADVERS;
  409. }
  410. *out_version = SASL_CLIENT_PLUG_VERSION;
  411. *pluglist = plain_client_plugins;
  412. *plugcount = 1;
  413. return SASL_OK;
  414. }