private-lwsgs.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 Lesser 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 lwsgs_user u;
  61. struct lws_context *context;
  62. char session_db[256];
  63. char admin_user[32];
  64. char confounder[32];
  65. char email_contact_person[128];
  66. char email_title[128];
  67. char email_template[128];
  68. char email_confirm_url[128];
  69. lwsgw_hash admin_password_sha1;
  70. sqlite3 *pdb;
  71. int timeout_idle_secs;
  72. int timeout_absolute_secs;
  73. int timeout_anon_absolute_secs;
  74. int timeout_email_secs;
  75. time_t last_session_expire;
  76. char email_inited;
  77. };
  78. struct per_session_data__gs {
  79. struct lws_spa *spa;
  80. lwsgw_hash login_session;
  81. lwsgw_hash delete_session;
  82. unsigned int login_expires;
  83. char onward[256];
  84. char result[500 + LWS_PRE];
  85. char urldec[500 + LWS_PRE];
  86. int result_len;
  87. char ip[46];
  88. struct lws_process_html_state phs;
  89. int spos;
  90. unsigned int logging_out:1;
  91. };
  92. /* utils.c */
  93. int
  94. lwsgs_lookup_callback_user(void *priv, int cols, char **col_val,
  95. char **col_name);
  96. void
  97. lwsgw_cookie_from_session(lwsgw_hash *sid, time_t expires, char **p, char *end);
  98. int
  99. lwsgs_get_sid_from_wsi(struct lws *wsi, lwsgw_hash *sid);
  100. int
  101. lwsgs_lookup_session(struct per_vhost_data__gs *vhd,
  102. const lwsgw_hash *sid, char *username, int len);
  103. int
  104. lwsgs_get_auth_level(struct per_vhost_data__gs *vhd,
  105. const char *username);
  106. int
  107. lwsgs_check_credentials(struct per_vhost_data__gs *vhd,
  108. const char *username, const char *password);
  109. void
  110. sha1_to_lwsgw_hash(unsigned char *hash, lwsgw_hash *shash);
  111. unsigned int
  112. lwsgs_now_secs(void);
  113. int
  114. lwsgw_check_admin(struct per_vhost_data__gs *vhd,
  115. const char *username, const char *password);
  116. int
  117. lwsgs_hash_password(struct per_vhost_data__gs *vhd,
  118. const char *password, struct lwsgs_user *u);
  119. int
  120. lwsgs_new_session_id(struct per_vhost_data__gs *vhd,
  121. lwsgw_hash *sid, const char *username, int exp);
  122. int
  123. lwsgs_lookup_user(struct per_vhost_data__gs *vhd,
  124. const char *username, struct lwsgs_user *u);
  125. int
  126. lwsgw_update_session(struct per_vhost_data__gs *vhd,
  127. lwsgw_hash *hash, const char *user);
  128. int
  129. lwsgw_expire_old_sessions(struct per_vhost_data__gs *vhd);
  130. /* handlers.c */
  131. int
  132. lwsgs_handler_confirm(struct per_vhost_data__gs *vhd, struct lws *wsi,
  133. struct per_session_data__gs *pss);
  134. int
  135. lwsgs_handler_forgot(struct per_vhost_data__gs *vhd, struct lws *wsi,
  136. struct per_session_data__gs *pss);
  137. int
  138. lwsgs_handler_check(struct per_vhost_data__gs *vhd, struct lws *wsi,
  139. struct per_session_data__gs *pss);
  140. int
  141. lwsgs_handler_change_password(struct per_vhost_data__gs *vhd, struct lws *wsi,
  142. struct per_session_data__gs *pss);
  143. int
  144. lwsgs_handler_forgot_pw_form(struct per_vhost_data__gs *vhd, struct lws *wsi,
  145. struct per_session_data__gs *pss);
  146. int
  147. lwsgs_handler_register_form(struct per_vhost_data__gs *vhd, struct lws *wsi,
  148. struct per_session_data__gs *pss);