sasl.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <sasl/sasl.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. static int mgo_sasl_simple(void *context, int id, const char **result, unsigned int *len)
  6. {
  7. if (!result) {
  8. return SASL_BADPARAM;
  9. }
  10. switch (id) {
  11. case SASL_CB_USER:
  12. *result = (char *) context;
  13. break;
  14. case SASL_CB_AUTHNAME:
  15. *result = (char *) context;
  16. break;
  17. case SASL_CB_LANGUAGE:
  18. *result = NULL;
  19. break;
  20. default:
  21. return SASL_BADPARAM;
  22. }
  23. if (len) {
  24. *len = *result ? strlen(*result) : 0;
  25. }
  26. return SASL_OK;
  27. }
  28. typedef int (*callback)(void);
  29. static int mgo_sasl_secret(sasl_conn_t *conn, void *context, int id, sasl_secret_t **result)
  30. {
  31. if (!conn || !result || id != SASL_CB_PASS) {
  32. return SASL_BADPARAM;
  33. }
  34. *result = (sasl_secret_t *)context;
  35. return SASL_OK;
  36. }
  37. sasl_callback_t *mgo_sasl_callbacks(const char *username, const char *password)
  38. {
  39. sasl_callback_t *cb = malloc(4 * sizeof(sasl_callback_t));
  40. int n = 0;
  41. size_t len = strlen(password);
  42. sasl_secret_t *secret = (sasl_secret_t*)malloc(sizeof(sasl_secret_t) + len);
  43. if (!secret) {
  44. free(cb);
  45. return NULL;
  46. }
  47. strcpy((char *)secret->data, password);
  48. secret->len = len;
  49. cb[n].id = SASL_CB_PASS;
  50. cb[n].proc = (callback)&mgo_sasl_secret;
  51. cb[n].context = secret;
  52. n++;
  53. cb[n].id = SASL_CB_USER;
  54. cb[n].proc = (callback)&mgo_sasl_simple;
  55. cb[n].context = (char*)username;
  56. n++;
  57. cb[n].id = SASL_CB_AUTHNAME;
  58. cb[n].proc = (callback)&mgo_sasl_simple;
  59. cb[n].context = (char*)username;
  60. n++;
  61. cb[n].id = SASL_CB_LIST_END;
  62. cb[n].proc = NULL;
  63. cb[n].context = NULL;
  64. return cb;
  65. }