external.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* SASL server API implementation
  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 <stdlib.h>
  48. #include <limits.h>
  49. #include <ctype.h>
  50. #include <string.h>
  51. #include <sasl.h>
  52. #include <saslplug.h>
  53. #include "saslint.h"
  54. #include "../common/plugin_common.h"
  55. /***************************** Common Section *****************************/
  56. /***************************** Server Section *****************************/
  57. static int
  58. external_server_mech_new(void *glob_context __attribute__((unused)),
  59. sasl_server_params_t *sparams,
  60. const char *challenge __attribute__((unused)),
  61. unsigned challen __attribute__((unused)),
  62. void **conn_context)
  63. {
  64. if (!conn_context
  65. || !sparams
  66. || !sparams->utils
  67. || !sparams->utils->conn)
  68. return SASL_BADPARAM;
  69. if (!sparams->utils->conn->external.auth_id)
  70. return SASL_NOMECH;
  71. *conn_context = NULL;
  72. return SASL_OK;
  73. }
  74. static int
  75. external_server_mech_step(void *conn_context __attribute__((unused)),
  76. sasl_server_params_t *sparams,
  77. const char *clientin,
  78. unsigned clientinlen,
  79. const char **serverout,
  80. unsigned *serveroutlen,
  81. sasl_out_params_t *oparams)
  82. {
  83. int result;
  84. if (!sparams
  85. || !sparams->utils
  86. || !sparams->utils->conn
  87. || !sparams->utils->getcallback
  88. || !serverout
  89. || !serveroutlen
  90. || !oparams)
  91. return SASL_BADPARAM;
  92. if (!sparams->utils->conn->external.auth_id)
  93. return SASL_BADPROT;
  94. /* xxx arbitrary limit here */
  95. if (clientinlen > 16384) return SASL_BADPROT;
  96. if ((sparams->props.security_flags & SASL_SEC_NOANONYMOUS) &&
  97. (!strcmp(sparams->utils->conn->external.auth_id, "anonymous"))) {
  98. sasl_seterror(sparams->utils->conn,0,"anonymous login not allowed");
  99. return SASL_NOAUTHZ;
  100. }
  101. *serverout = NULL;
  102. *serveroutlen = 0;
  103. if (!clientin) {
  104. /* No initial data; we're in a protocol which doesn't support it.
  105. * So we let the server app know that we need some... */
  106. return SASL_CONTINUE;
  107. }
  108. if (clientinlen) { /* if we have a non-zero authorization id */
  109. /* The user's trying to authorize as someone they didn't
  110. * authenticate as */
  111. result = sparams->canon_user(sparams->utils->conn,
  112. clientin, clientinlen,
  113. SASL_CU_AUTHZID, oparams);
  114. if(result != SASL_OK) return result;
  115. result = sparams->canon_user(sparams->utils->conn,
  116. sparams->utils->conn->external.auth_id, 0,
  117. SASL_CU_AUTHID | SASL_CU_EXTERNALLY_VERIFIED, oparams);
  118. } else {
  119. result = sparams->canon_user(sparams->utils->conn,
  120. sparams->utils->conn->external.auth_id, 0,
  121. SASL_CU_AUTHID | SASL_CU_EXTERNALLY_VERIFIED | SASL_CU_AUTHZID, oparams);
  122. }
  123. if (result != SASL_OK) return result;
  124. /* set oparams */
  125. oparams->doneflag = 1;
  126. oparams->mech_ssf = 0;
  127. oparams->maxoutbuf = 0;
  128. oparams->encode_context = NULL;
  129. oparams->encode = NULL;
  130. oparams->decode_context = NULL;
  131. oparams->decode = NULL;
  132. oparams->param_version = 0;
  133. return SASL_OK;
  134. }
  135. static int
  136. external_server_mech_avail(void *glob_context __attribute__((unused)),
  137. sasl_server_params_t *sparams,
  138. void **conn_context __attribute__((unused)))
  139. {
  140. if (!sparams->utils->conn->external.auth_id) {
  141. /* Return Temporary Failure */
  142. return SASL_NOTDONE;
  143. }
  144. return SASL_OK;
  145. }
  146. static sasl_server_plug_t external_server_plugins[] =
  147. {
  148. {
  149. "EXTERNAL", /* mech_name */
  150. 0, /* max_ssf */
  151. SASL_SEC_NOPLAINTEXT
  152. | SASL_SEC_NOANONYMOUS
  153. | SASL_SEC_NODICTIONARY, /* security_flags */
  154. SASL_FEAT_WANT_CLIENT_FIRST
  155. | SASL_FEAT_ALLOWS_PROXY, /* features */
  156. NULL, /* glob_context */
  157. &external_server_mech_new, /* mech_new */
  158. &external_server_mech_step, /* mech_step */
  159. NULL, /* mech_dispose */
  160. NULL, /* mech_free */
  161. NULL, /* setpass */
  162. NULL, /* user_query */
  163. NULL, /* idle */
  164. &external_server_mech_avail, /* mech_avail */
  165. NULL /* spare */
  166. }
  167. };
  168. int external_server_plug_init(const sasl_utils_t *utils,
  169. int max_version,
  170. int *out_version,
  171. sasl_server_plug_t **pluglist,
  172. int *plugcount)
  173. {
  174. if (!out_version || !pluglist || !plugcount)
  175. return SASL_BADPARAM;
  176. if (max_version != SASL_SERVER_PLUG_VERSION) {
  177. SETERROR( utils, "EXTERNAL version mismatch" );
  178. return SASL_BADVERS;
  179. }
  180. *out_version = SASL_SERVER_PLUG_VERSION;
  181. *pluglist = external_server_plugins;
  182. *plugcount = 1;
  183. return SASL_OK;
  184. }
  185. /***************************** Client Section *****************************/
  186. typedef struct client_context
  187. {
  188. char *out_buf;
  189. size_t out_buf_len;
  190. } client_context_t;
  191. static int external_client_mech_new(void *glob_context __attribute__((unused)),
  192. sasl_client_params_t *params,
  193. void **conn_context)
  194. {
  195. client_context_t *text;
  196. if (!params
  197. || !params->utils
  198. || !params->utils->conn
  199. || !conn_context)
  200. return SASL_BADPARAM;
  201. if (!params->utils->conn->external.auth_id)
  202. return SASL_NOMECH;
  203. text = sasl_ALLOC(sizeof(client_context_t));
  204. if(!text) return SASL_NOMEM;
  205. memset(text, 0, sizeof(client_context_t));
  206. *conn_context = text;
  207. return SASL_OK;
  208. }
  209. static int
  210. external_client_mech_step(void *conn_context,
  211. sasl_client_params_t *params,
  212. const char *serverin __attribute__((unused)),
  213. unsigned serverinlen,
  214. sasl_interact_t **prompt_need,
  215. const char **clientout,
  216. unsigned *clientoutlen,
  217. sasl_out_params_t *oparams)
  218. {
  219. client_context_t *text = (client_context_t *)conn_context;
  220. const char *user = NULL;
  221. int user_result = SASL_OK;
  222. int result;
  223. if (!params
  224. || !params->utils
  225. || !params->utils->conn
  226. || !params->utils->getcallback
  227. || !clientout
  228. || !clientoutlen
  229. || !oparams)
  230. return SASL_BADPARAM;
  231. if (!params->utils->conn->external.auth_id)
  232. return SASL_BADPROT;
  233. if (serverinlen != 0)
  234. return SASL_BADPROT;
  235. *clientout = NULL;
  236. *clientoutlen = 0;
  237. /* try to get the userid */
  238. if (user == NULL) {
  239. user_result = _plug_get_userid(params->utils, &user, prompt_need);
  240. if ((user_result != SASL_OK) && (user_result != SASL_INTERACT))
  241. return user_result;
  242. }
  243. /* free prompts we got */
  244. if (prompt_need && *prompt_need) {
  245. params->utils->free(*prompt_need);
  246. *prompt_need = NULL;
  247. }
  248. /* if there are prompts not filled in */
  249. if (user_result == SASL_INTERACT) {
  250. /* make the prompt list */
  251. int result =
  252. _plug_make_prompts(params->utils, prompt_need,
  253. "Please enter your authorization name",
  254. "",
  255. NULL, NULL,
  256. NULL, NULL,
  257. NULL, NULL, NULL,
  258. NULL, NULL, NULL);
  259. if (result != SASL_OK) return result;
  260. return SASL_INTERACT;
  261. }
  262. *clientoutlen = user ? (unsigned) strlen(user) : 0;
  263. result = _buf_alloc(&text->out_buf, &text->out_buf_len, *clientoutlen + 1);
  264. if (result != SASL_OK) return result;
  265. if (user && *user) {
  266. result = params->canon_user(params->utils->conn,
  267. user, 0, SASL_CU_AUTHZID, oparams);
  268. if (result != SASL_OK) return result;
  269. result = params->canon_user(params->utils->conn,
  270. params->utils->conn->external.auth_id, 0,
  271. SASL_CU_AUTHID, oparams);
  272. if (result != SASL_OK) return result;
  273. memcpy(text->out_buf, user, *clientoutlen);
  274. } else {
  275. result = params->canon_user(params->utils->conn,
  276. params->utils->conn->external.auth_id, 0,
  277. SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
  278. if (result != SASL_OK) return result;
  279. }
  280. text->out_buf[*clientoutlen] = '\0';
  281. *clientout = text->out_buf;
  282. /* set oparams */
  283. oparams->doneflag = 1;
  284. oparams->mech_ssf = 0;
  285. oparams->maxoutbuf = 0;
  286. oparams->encode_context = NULL;
  287. oparams->encode = NULL;
  288. oparams->decode_context = NULL;
  289. oparams->decode = NULL;
  290. oparams->param_version = 0;
  291. return SASL_OK;
  292. }
  293. static void
  294. external_client_mech_dispose(void *conn_context,
  295. const sasl_utils_t *utils __attribute__((unused)))
  296. {
  297. client_context_t *text = (client_context_t *) conn_context;
  298. if (!text) return;
  299. if(text->out_buf) sasl_FREE(text->out_buf);
  300. sasl_FREE(text);
  301. }
  302. static const unsigned long external_required_prompts[] = {
  303. SASL_CB_LIST_END
  304. };
  305. static sasl_client_plug_t external_client_plugins[] =
  306. {
  307. {
  308. "EXTERNAL", /* mech_name */
  309. 0, /* max_ssf */
  310. SASL_SEC_NOPLAINTEXT
  311. | SASL_SEC_NOANONYMOUS
  312. | SASL_SEC_NODICTIONARY, /* security_flags */
  313. SASL_FEAT_WANT_CLIENT_FIRST
  314. | SASL_FEAT_ALLOWS_PROXY, /* features */
  315. external_required_prompts, /* required_prompts */
  316. NULL, /* glob_context */
  317. &external_client_mech_new, /* mech_new */
  318. &external_client_mech_step, /* mech_step */
  319. &external_client_mech_dispose, /* mech_dispose */
  320. NULL, /* mech_free */
  321. NULL, /* idle */
  322. NULL, /* spare */
  323. NULL /* spare */
  324. }
  325. };
  326. int external_client_plug_init(const sasl_utils_t *utils,
  327. int max_version,
  328. int *out_version,
  329. sasl_client_plug_t **pluglist,
  330. int *plugcount)
  331. {
  332. if (!utils || !out_version || !pluglist || !plugcount)
  333. return SASL_BADPARAM;
  334. if (max_version != SASL_CLIENT_PLUG_VERSION) {
  335. SETERROR( utils, "EXTERNAL version mismatch" );
  336. return SASL_BADVERS;
  337. }
  338. *out_version = SASL_CLIENT_PLUG_VERSION;
  339. *pluglist = external_client_plugins;
  340. *plugcount = 1;
  341. return SASL_OK;
  342. }