anonymous.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* Anonymous 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. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #include <sasl.h>
  52. #include <saslplug.h>
  53. #include "plugin_common.h"
  54. #ifdef macintosh
  55. #error #include <sasl_anonymous_plugin_decl.h>
  56. #endif
  57. /***************************** Common Section *****************************/
  58. static const char anonymous_id[] = "anonymous";
  59. /***************************** Server Section *****************************/
  60. static int
  61. anonymous_server_mech_new(void *glob_context __attribute__((unused)),
  62. sasl_server_params_t *sparams,
  63. const char *challenge __attribute__((unused)),
  64. unsigned challen __attribute__((unused)),
  65. void **conn_context)
  66. {
  67. /* holds state are in */
  68. if (!conn_context) {
  69. PARAMERROR( sparams->utils );
  70. return SASL_BADPARAM;
  71. }
  72. *conn_context = NULL;
  73. return SASL_OK;
  74. }
  75. static int
  76. anonymous_server_mech_step(void *conn_context __attribute__((unused)),
  77. sasl_server_params_t *sparams,
  78. const char *clientin,
  79. unsigned clientinlen,
  80. const char **serverout,
  81. unsigned *serveroutlen,
  82. sasl_out_params_t *oparams)
  83. {
  84. char *clientdata;
  85. int result;
  86. if (!sparams
  87. || !serverout
  88. || !serveroutlen
  89. || !oparams) {
  90. if (sparams) PARAMERROR( sparams->utils );
  91. return SASL_BADPARAM;
  92. }
  93. *serverout = NULL;
  94. *serveroutlen = 0;
  95. if (!clientin) {
  96. return SASL_CONTINUE;
  97. }
  98. /* We force a truncation 255 characters (specified by RFC 2245) */
  99. if (clientinlen > 255) clientinlen = 255;
  100. /* NULL-terminate the clientin... */
  101. clientdata = sparams->utils->malloc(clientinlen + 1);
  102. if (!clientdata) {
  103. MEMERROR(sparams->utils);
  104. return SASL_NOMEM;
  105. }
  106. strncpy(clientdata, clientin, clientinlen);
  107. clientdata[clientinlen] = '\0';
  108. sparams->utils->log(sparams->utils->conn,
  109. SASL_LOG_NOTE,
  110. "ANONYMOUS login: \"%s\"",
  111. clientdata);
  112. if (clientdata != clientin)
  113. sparams->utils->free(clientdata);
  114. result = sparams->canon_user(sparams->utils->conn,
  115. anonymous_id, 0,
  116. SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
  117. if (result != SASL_OK) return result;
  118. /* set oparams */
  119. oparams->doneflag = 1;
  120. oparams->mech_ssf = 0;
  121. oparams->maxoutbuf = 0;
  122. oparams->encode_context = NULL;
  123. oparams->encode = NULL;
  124. oparams->decode_context = NULL;
  125. oparams->decode = NULL;
  126. oparams->param_version = 0;
  127. return SASL_OK;
  128. }
  129. static sasl_server_plug_t anonymous_server_plugins[] =
  130. {
  131. {
  132. "ANONYMOUS", /* mech_name */
  133. 0, /* max_ssf */
  134. SASL_SEC_NOPLAINTEXT, /* security_flags */
  135. SASL_FEAT_WANT_CLIENT_FIRST
  136. | SASL_FEAT_DONTUSE_USERPASSWD, /* features */
  137. NULL, /* glob_context */
  138. &anonymous_server_mech_new, /* mech_new */
  139. &anonymous_server_mech_step, /* mech_step */
  140. NULL, /* mech_dispose */
  141. NULL, /* mech_free */
  142. NULL, /* setpass */
  143. NULL, /* user_query */
  144. NULL, /* idle */
  145. NULL, /* mech_avail */
  146. NULL /* spare */
  147. }
  148. };
  149. int anonymous_server_plug_init(const sasl_utils_t *utils,
  150. int maxversion,
  151. int *out_version,
  152. sasl_server_plug_t **pluglist,
  153. int *plugcount)
  154. {
  155. if (maxversion < SASL_SERVER_PLUG_VERSION) {
  156. SETERROR( utils, "ANONYMOUS version mismatch" );
  157. return SASL_BADVERS;
  158. }
  159. *out_version = SASL_SERVER_PLUG_VERSION;
  160. *pluglist = anonymous_server_plugins;
  161. *plugcount = 1;
  162. return SASL_OK;
  163. }
  164. /***************************** Client Section *****************************/
  165. typedef struct client_context {
  166. char *out_buf;
  167. unsigned out_buf_len;
  168. } client_context_t;
  169. static int
  170. anonymous_client_mech_new(void *glob_context __attribute__((unused)),
  171. sasl_client_params_t *cparams,
  172. void **conn_context)
  173. {
  174. client_context_t *text;
  175. if (!conn_context) {
  176. PARAMERROR(cparams->utils);
  177. return SASL_BADPARAM;
  178. }
  179. /* holds state are in */
  180. text = cparams->utils->malloc(sizeof(client_context_t));
  181. if (text == NULL) {
  182. MEMERROR(cparams->utils);
  183. return SASL_NOMEM;
  184. }
  185. memset(text, 0, sizeof(client_context_t));
  186. *conn_context = text;
  187. return SASL_OK;
  188. }
  189. static int
  190. anonymous_client_mech_step(void *conn_context,
  191. sasl_client_params_t *cparams,
  192. const char *serverin __attribute__((unused)),
  193. unsigned serverinlen,
  194. sasl_interact_t **prompt_need,
  195. const char **clientout,
  196. unsigned *clientoutlen,
  197. sasl_out_params_t *oparams)
  198. {
  199. client_context_t *text = (client_context_t *) conn_context;
  200. size_t userlen;
  201. char hostname[256];
  202. const char *user = NULL;
  203. int user_result = SASL_OK;
  204. int result;
  205. if (!cparams
  206. || !clientout
  207. || !clientoutlen
  208. || !oparams) {
  209. if (cparams) PARAMERROR( cparams->utils );
  210. return SASL_BADPARAM;
  211. }
  212. *clientout = NULL;
  213. *clientoutlen = 0;
  214. if (serverinlen != 0) {
  215. SETERROR( cparams->utils,
  216. "Nonzero serverinlen in ANONYMOUS continue_step" );
  217. return SASL_BADPROT;
  218. }
  219. /* check if sec layer strong enough */
  220. if (cparams->props.min_ssf > cparams->external_ssf) {
  221. SETERROR( cparams->utils, "SSF requested of ANONYMOUS plugin");
  222. return SASL_TOOWEAK;
  223. }
  224. /* try to get the trace info */
  225. if (user == NULL) {
  226. user_result = _plug_get_userid(cparams->utils, &user, prompt_need);
  227. if ((user_result != SASL_OK) && (user_result != SASL_INTERACT)) {
  228. return user_result;
  229. }
  230. }
  231. /* free prompts we got */
  232. if (prompt_need && *prompt_need) {
  233. cparams->utils->free(*prompt_need);
  234. *prompt_need = NULL;
  235. }
  236. /* if there are prompts not filled in */
  237. if (user_result == SASL_INTERACT) {
  238. /* make the prompt list */
  239. result =
  240. _plug_make_prompts(cparams->utils, prompt_need,
  241. "Please enter anonymous identification",
  242. "",
  243. NULL, NULL,
  244. NULL, NULL,
  245. NULL, NULL, NULL,
  246. NULL, NULL, NULL);
  247. if (result != SASL_OK) return result;
  248. return SASL_INTERACT;
  249. }
  250. if (!user || !*user) {
  251. user = anonymous_id;
  252. }
  253. userlen = strlen(user);
  254. result = cparams->canon_user(cparams->utils->conn,
  255. anonymous_id, 0,
  256. SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
  257. if (result != SASL_OK) return result;
  258. memset(hostname, 0, sizeof(hostname));
  259. gethostname(hostname, sizeof(hostname));
  260. hostname[sizeof(hostname)-1] = '\0';
  261. *clientoutlen = (unsigned) (userlen + strlen(hostname) + 1);
  262. result = _plug_buf_alloc(cparams->utils, &text->out_buf,
  263. &text->out_buf_len, *clientoutlen);
  264. if (result != SASL_OK) return result;
  265. strcpy(text->out_buf, user);
  266. text->out_buf[userlen] = '@';
  267. /* use memcpy() instead of strcpy() so we don't add the NUL */
  268. memcpy(text->out_buf + userlen + 1, hostname, strlen(hostname));
  269. *clientout = text->out_buf;
  270. /* set oparams */
  271. oparams->doneflag = 1;
  272. oparams->mech_ssf = 0;
  273. oparams->maxoutbuf = 0;
  274. oparams->encode_context = NULL;
  275. oparams->encode = NULL;
  276. oparams->decode_context = NULL;
  277. oparams->decode = NULL;
  278. oparams->param_version = 0;
  279. return SASL_OK;
  280. }
  281. static void anonymous_client_dispose(void *conn_context,
  282. const sasl_utils_t *utils)
  283. {
  284. client_context_t *text = (client_context_t *) conn_context;
  285. if(!text) return;
  286. if (text->out_buf) utils->free(text->out_buf);
  287. utils->free(text);
  288. }
  289. static const unsigned long anonymous_required_prompts[] = {
  290. SASL_CB_LIST_END
  291. };
  292. static sasl_client_plug_t anonymous_client_plugins[] =
  293. {
  294. {
  295. "ANONYMOUS", /* mech_name */
  296. 0, /* max_ssf */
  297. SASL_SEC_NOPLAINTEXT, /* security_flags */
  298. SASL_FEAT_WANT_CLIENT_FIRST, /* features */
  299. anonymous_required_prompts, /* required_prompts */
  300. NULL, /* glob_context */
  301. &anonymous_client_mech_new, /* mech_new */
  302. &anonymous_client_mech_step, /* mech_step */
  303. &anonymous_client_dispose, /* mech_dispose */
  304. NULL, /* mech_free */
  305. NULL, /* idle */
  306. NULL, /* spare */
  307. NULL /* spare */
  308. }
  309. };
  310. int anonymous_client_plug_init(const sasl_utils_t *utils,
  311. int maxversion,
  312. int *out_version,
  313. sasl_client_plug_t **pluglist,
  314. int *plugcount)
  315. {
  316. if (maxversion < SASL_CLIENT_PLUG_VERSION) {
  317. SETERROR( utils, "ANONYMOUS version mismatch" );
  318. return SASL_BADVERS;
  319. }
  320. *out_version = SASL_CLIENT_PLUG_VERSION;
  321. *pluglist = anonymous_client_plugins;
  322. *plugcount = 1;
  323. return SASL_OK;
  324. }