private-lwsgs.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * ws protocol handler plugin for "generic sessions"
  3. *
  4. * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #define LWS_DLL
  22. #define LWS_INTERNAL
  23. #include "../lib/libwebsockets.h"
  24. #include <sqlite3.h>
  25. #include <string.h>
  26. #define LWSGS_VERIFIED_ACCEPTED 100
  27. enum {
  28. FGS_USERNAME,
  29. FGS_PASSWORD,
  30. FGS_PASSWORD2,
  31. FGS_EMAIL,
  32. FGS_REGISTER,
  33. FGS_GOOD,
  34. FGS_BAD,
  35. FGS_REG_GOOD,
  36. FGS_REG_BAD,
  37. FGS_ADMIN,
  38. FGS_FORGOT,
  39. FGS_FORGOT_GOOD,
  40. FGS_FORGOT_BAD,
  41. FGS_FORGOT_POST_GOOD,
  42. FGS_FORGOT_POST_BAD,
  43. FGS_CHANGE,
  44. FGS_CURPW,
  45. FGS_DELETE,
  46. };
  47. struct lwsgs_user {
  48. char username[32];
  49. char ip[16];
  50. lwsgw_hash pwhash;
  51. lwsgw_hash pwsalt;
  52. lwsgw_hash token;
  53. time_t created;
  54. time_t last_forgot_validated;
  55. char email[100];
  56. int verified;
  57. };
  58. struct per_vhost_data__gs {
  59. struct lws_email email;
  60. struct lws_context *context;
  61. char session_db[256];
  62. char admin_user[32];
  63. char confounder[32];
  64. char email_contact_person[128];
  65. char email_title[128];
  66. char email_template[128];
  67. char email_confirm_url[128];
  68. lwsgw_hash admin_password_sha1;
  69. sqlite3 *pdb;
  70. int timeout_idle_secs;
  71. int timeout_absolute_secs;
  72. int timeout_anon_absolute_secs;
  73. int timeout_email_secs;
  74. time_t last_session_expire;
  75. struct lwsgs_user u;
  76. };
  77. struct per_session_data__gs {
  78. struct lws_spa *spa;
  79. lwsgw_hash login_session;
  80. lwsgw_hash delete_session;
  81. unsigned int login_expires;
  82. char onward[256];
  83. char result[500 + LWS_PRE];
  84. char urldec[500 + LWS_PRE];
  85. int result_len;
  86. char ip[46];
  87. struct lws_process_html_state phs;
  88. int spos;
  89. unsigned int logging_out:1;
  90. };
  91. /* utils.c */
  92. int
  93. lwsgs_lookup_callback_user(void *priv, int cols, char **col_val,
  94. char **col_name);
  95. void
  96. lwsgw_cookie_from_session(lwsgw_hash *sid, time_t expires, char **p, char *end);
  97. int
  98. lwsgs_get_sid_from_wsi(struct lws *wsi, lwsgw_hash *sid);
  99. int
  100. lwsgs_lookup_session(struct per_vhost_data__gs *vhd,
  101. const lwsgw_hash *sid, char *username, int len);
  102. int
  103. lwsgs_get_auth_level(struct per_vhost_data__gs *vhd,
  104. const char *username);
  105. int
  106. lwsgs_check_credentials(struct per_vhost_data__gs *vhd,
  107. const char *username, const char *password);
  108. void
  109. sha1_to_lwsgw_hash(unsigned char *hash, lwsgw_hash *shash);
  110. unsigned int
  111. lwsgs_now_secs(void);
  112. int
  113. lwsgw_check_admin(struct per_vhost_data__gs *vhd,
  114. const char *username, const char *password);
  115. int
  116. lwsgs_hash_password(struct per_vhost_data__gs *vhd,
  117. const char *password, struct lwsgs_user *u);
  118. int
  119. lwsgs_new_session_id(struct per_vhost_data__gs *vhd,
  120. lwsgw_hash *sid, const char *username, int exp);
  121. int
  122. lwsgs_lookup_user(struct per_vhost_data__gs *vhd,
  123. const char *username, struct lwsgs_user *u);
  124. int
  125. lwsgw_update_session(struct per_vhost_data__gs *vhd,
  126. lwsgw_hash *hash, const char *user);
  127. int
  128. lwsgw_expire_old_sessions(struct per_vhost_data__gs *vhd);
  129. /* handlers.c */
  130. int
  131. lwsgs_handler_confirm(struct per_vhost_data__gs *vhd, struct lws *wsi,
  132. struct per_session_data__gs *pss);
  133. int
  134. lwsgs_handler_forgot(struct per_vhost_data__gs *vhd, struct lws *wsi,
  135. struct per_session_data__gs *pss);
  136. int
  137. lwsgs_handler_check(struct per_vhost_data__gs *vhd, struct lws *wsi,
  138. struct per_session_data__gs *pss);
  139. int
  140. lwsgs_handler_change_password(struct per_vhost_data__gs *vhd, struct lws *wsi,
  141. struct per_session_data__gs *pss);
  142. int
  143. lwsgs_handler_forgot_pw_form(struct per_vhost_data__gs *vhd, struct lws *wsi,
  144. struct per_session_data__gs *pss);
  145. int
  146. lwsgs_handler_register_form(struct per_vhost_data__gs *vhd, struct lws *wsi,
  147. struct per_session_data__gs *pss);