login.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <json-c/json.h>
  6. #include <json-c/json_util.h>
  7. #include <json-c/json_object.h>
  8. #include <mosquitto.h>
  9. #include "common.h"
  10. #include "command.h"
  11. static volatile bool run =true;
  12. char *inputbuffer=NULL;
  13. void connect_callback(struct mosquitto *mosq, void *obj, int rc)
  14. {
  15. run = true;
  16. }
  17. void disconnect_callback(struct mosquitto *mosq, void *obj, int result)
  18. {
  19. run = false;
  20. }
  21. void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  22. {
  23. struct json_object *proot=NULL, *payload_obj=NULL, *item_obj=NULL, *user_obj=NULL, *temp_obj=NULL, *cookied_obj=NULL, *rsp_obj=NULL;
  24. const char *proot_string=NULL, *rsp_string=NULL;
  25. const char *user=NULL;
  26. const char *passwd=NULL;
  27. const char *load_user=NULL;
  28. const char *load_passwd=NULL;
  29. char timestamp[100];
  30. proot = json_tokener_parse(msg->payload);
  31. if(proot == NULL)
  32. {
  33. printf("ERROR: login proot is NULL!\r\n");
  34. run =false;
  35. return ;
  36. }
  37. json_object_object_get_ex(proot, "user", &user_obj);
  38. json_object_object_get_ex(user_obj, "user", &temp_obj);
  39. user = json_object_get_string(temp_obj);
  40. json_object_object_get_ex(user_obj, "passwd", &temp_obj);
  41. passwd = json_object_get_string(temp_obj);
  42. payload_obj = json_tokener_parse(inputbuffer);
  43. if(payload_obj == NULL)
  44. {
  45. printf("ERROR: inputbuffer is error!\r\n");
  46. json_object_put(proot);
  47. run =false;
  48. return;
  49. }
  50. json_object_object_get_ex(payload_obj, "user", &temp_obj);
  51. load_user = json_object_get_string(temp_obj);
  52. json_object_object_get_ex(payload_obj, "passwd", &temp_obj);
  53. load_passwd = json_object_get_string(temp_obj);
  54. if(strncmp(user,load_user, STR_MAX_LEN(user, load_user)) ==0 &&
  55. strncmp(passwd, load_passwd,STR_MAX_LEN(passwd,load_passwd)) ==0)
  56. {
  57. rsp_obj = json_object_new_object();
  58. json_object_object_add(rsp_obj,"state", json_object_new_string("success"));
  59. json_object_object_add(rsp_obj,"user", json_object_new_string(user));
  60. json_object_object_add(rsp_obj,"passwd", json_object_new_string(passwd));
  61. timestamp_get(timestamp);
  62. cookied_obj = json_object_new_object();
  63. json_object_object_add(cookied_obj, "cookied", json_object_new_string(timestamp));
  64. json_object_to_file_ext("./cookie/cookied.json", cookied_obj, JSON_C_TO_STRING_PRETTY);
  65. printf("Set-Cookie:cookied=%s;path=/;",timestamp);
  66. json_object_put(cookied_obj);
  67. }
  68. else{
  69. rsp_obj = json_object_new_object();
  70. json_object_object_add(rsp_obj,"state", json_object_new_string("fail"));
  71. json_object_object_add(rsp_obj,"user", json_object_new_string(load_user));
  72. json_object_object_add(rsp_obj,"passwd", json_object_new_string(load_passwd));
  73. }
  74. printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
  75. rsp_string = json_object_to_json_string(rsp_obj);
  76. printf("%s",rsp_string);
  77. json_object_put(proot);
  78. json_object_put(payload_obj);
  79. json_object_put(rsp_obj);
  80. run =false;
  81. }
  82. void subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  83. {
  84. run = true;
  85. }
  86. int main(int argc, char *argv[])
  87. {
  88. char *info=NULL, *p=NULL, *pRequestMethod=NULL;
  89. int ContentLength,x,i=0;
  90. struct mosquitto *http_mosq=NULL;
  91. int max =0;
  92. char id[50];
  93. setvbuf(stdin, NULL , _IONBF, 0);
  94. pRequestMethod = getenv("REQUEST_METHOD");
  95. if(pRequestMethod == NULL)
  96. {
  97. printf("<p> request = null </p>");
  98. return 0;
  99. }
  100. if(strcasecmp(pRequestMethod, "POST") == 0)
  101. {
  102. p = getenv("CONTENT_LENGTH");
  103. if(p!=NULL)
  104. {
  105. ContentLength =atoi(p);
  106. }
  107. else
  108. {
  109. ContentLength = 0;
  110. }
  111. inputbuffer = (char *)malloc(ContentLength + 1);
  112. while(i < ContentLength)
  113. {
  114. x = fgetc(stdin);
  115. if(x == EOF)
  116. break;
  117. *(inputbuffer+i) =x;
  118. i++;
  119. }
  120. *(inputbuffer+i) = '\0';
  121. ContentLength = i;
  122. mosquitto_lib_init();
  123. snprintf(id, 50, "login_%d", getpid());
  124. http_mosq = mosquitto_new(id, true, NULL);
  125. mosquitto_connect_callback_set(http_mosq, connect_callback);
  126. mosquitto_disconnect_callback_set(http_mosq, disconnect_callback);
  127. mosquitto_message_callback_set(http_mosq, message_callback);
  128. mosquitto_subscribe_callback_set(http_mosq, subscribe_callback);
  129. mosquitto_connect(http_mosq, "localhost", 1883, 600);
  130. mosquitto_subscribe(http_mosq, NULL, SUB_GET_USER_NONE_HTTP, 1);
  131. mosquitto_publish(http_mosq, NULL, PUB_GET_USER_NONE_HTTP, 0, "", 1, false);
  132. while(run ==true && max < MAX_TIMEOUT)
  133. {
  134. mosquitto_loop(http_mosq, 500, 1);
  135. max++;
  136. }
  137. mosquitto_disconnect(http_mosq);
  138. mosquitto_destroy(http_mosq);
  139. mosquitto_lib_cleanup();
  140. free(inputbuffer);
  141. }
  142. return 0;
  143. }