vcc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* vcc/vcc.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <signal.h>
  31. #include <klist.h>
  32. #include <vcc/vcc.h>
  33. #include <vcc/plugin.h>
  34. #include <vcc/version.h>
  35. #define clear_req(req) memset(req, 0, sizeof(*req))
  36. extern struct klist_node banned_people;
  37. extern int fd;
  38. extern char usrname[];
  39. int current_sid = 0;
  40. int vcc_login_bh(struct vcc_request *);
  41. int do_cmd_ls_bh(struct vcc_request *);
  42. int do_cmd_lsse_bh(struct vcc_request *);
  43. int do_cmd_uinfo_bh(struct vcc_request *);
  44. int do_cmd_incr_bh(struct vcc_request *);
  45. int init_socket(const char *server_addr, int server_port) {
  46. struct sockaddr_in addr, server;
  47. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  48. perror("socket()");
  49. exit(1);
  50. }
  51. memset(&addr, 0, sizeof(struct sockaddr_in));
  52. addr.sin_family = AF_INET;
  53. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  54. addr.sin_port = htons(0);
  55. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  56. perror("bind()");
  57. exit(1);
  58. }
  59. memset(&server, 0, sizeof(struct sockaddr_in));
  60. server.sin_family = AF_INET;
  61. server.sin_port = htons(server_port);
  62. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  63. fprintf(stderr, _("invalid ip address. "
  64. "leave blank if you want to connect the default server. \n"));
  65. exit(1);
  66. }
  67. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  68. perror("connect()");
  69. exit(1);
  70. }
  71. return fd;
  72. }
  73. int __send_msg(const char *msg, const char *usrname, int flags) {
  74. struct vcc_request req;
  75. clear_req(&req);
  76. req.magic = htonl(VCC_MAGIC);
  77. req.reqtype = htonl(REQ_MSG_SEND);
  78. req.uid = 0;
  79. req.session = htonl(current_sid);
  80. req.flags = htonl(flags);
  81. strncpy(req.usrname, usrname, USRNAME_SIZE);
  82. strncpy(req.msg, msg, MSG_SIZE);
  83. write(fd, &req, REQ_SIZE);
  84. return 0;
  85. }
  86. extern inline int send_msg(const char *msg, const char *usrname) {
  87. return __send_msg(msg, usrname, 0);
  88. }
  89. extern inline int send_msg_encrypted(const char *msg, const char *usrname) {
  90. return __send_msg(msg, usrname, FLAG_ENCRYPTED);
  91. }
  92. int send_relay_msg(const char *msg, const char *usrname, const char *visible) {
  93. struct vcc_relay_header req;
  94. int len;
  95. len = strlen(msg);
  96. clear_req(&req);
  97. req.magic = htonl(VCC_MAGIC_RL);
  98. req.reqtype = htonl(REQ_REL_MSG);
  99. req.uid = 0;
  100. req.session = htonl(current_sid);
  101. req.size = htonl(sizeof(struct vcc_relay_header) + len);
  102. strncpy(req.usrname, usrname, USRNAME_SIZE);
  103. if (visible)
  104. strncpy(req.visible, visible, USRNAME_SIZE);
  105. else
  106. memset(req.visible, 0, USRNAME_SIZE);
  107. write(fd, &req, sizeof(struct vcc_relay_header));
  108. write(fd, msg, len);
  109. return 0;
  110. }
  111. int read_until(int fd, void *p, int until) {
  112. int did_read = 0;
  113. int n;
  114. while ((did_read += n = read(fd, p, until)) < until) {
  115. if (!n)
  116. return 0;
  117. p += n;
  118. }
  119. return until;
  120. }
  121. char *relay_get_msg(int fd, struct vcc_relay_header *hdr) {
  122. char *p;
  123. int size;
  124. size = ntohl(hdr->size);
  125. if (unlikely(!(p = malloc(size)))) {
  126. fprintf(stderr, "*** malloc() failed\n");
  127. return NULL;
  128. }
  129. memset(p, 0, size);
  130. read_until(fd, p, size - sizeof(struct vcc_relay_header));
  131. return p;
  132. }
  133. int get_user_info(char *usrname) {
  134. struct vcc_request req;
  135. clear_req(&req);
  136. req.magic = htonl(VCC_MAGIC);
  137. req.reqtype = htonl(REQ_CTL_UINFO);
  138. req.uid = 0;
  139. strncpy(req.msg, usrname, USRNAME_SIZE);
  140. write(fd, &req, REQ_SIZE);
  141. return 0;
  142. }
  143. int get_users(void) {
  144. struct vcc_request req;
  145. clear_req(&req);
  146. req.magic = htonl(VCC_MAGIC);
  147. req.reqtype = htonl(REQ_CTL_USRS);
  148. req.uid = 0;
  149. write(fd, &req, REQ_SIZE);
  150. return 0;
  151. }
  152. int get_sessions(void) {
  153. struct vcc_request req;
  154. clear_req(&req);
  155. req.magic = htonl(VCC_MAGIC);
  156. req.reqtype = htonl(REQ_CTL_SESS);
  157. req.uid = 0;
  158. write(fd, &req, REQ_SIZE);
  159. return 0;
  160. }
  161. int do_bh(struct vcc_request *req) {
  162. switch (ntohl(req->reqtype)) {
  163. case REQ_CTL_LOGIN:
  164. vcc_login_bh(req);
  165. break;
  166. case REQ_CTL_USRS:
  167. do_cmd_ls_bh(req);
  168. break;
  169. case REQ_CTL_SESS:
  170. do_cmd_lsse_bh(req);
  171. break;
  172. case REQ_CTL_UINFO:
  173. do_cmd_uinfo_bh(req);
  174. break;
  175. case REQ_SYS_SCRINC:
  176. do_cmd_incr_bh(req);
  177. break;
  178. default:
  179. // fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  180. break;
  181. }
  182. return 0;
  183. }
  184. int __login(char *usrname, char *passwd, int type) {
  185. struct vcc_request req;
  186. clear_req(&req);
  187. req.magic = htonl(VCC_MAGIC);
  188. /* REQ_CTL_LOGIN or REQ_CTL_IALOG */
  189. req.reqtype = htonl(type);
  190. strncpy(req.usrname, usrname, USRNAME_SIZE);
  191. strncpy(req.msg, passwd, MSG_SIZE);
  192. write(fd, &req, REQ_SIZE);
  193. return 0;
  194. }
  195. int vcc_login(char *usrname, char *passwd) {
  196. return __login(usrname, passwd, REQ_CTL_LOGIN);
  197. }
  198. int ia_login(char *session) {
  199. return __login(usrname, session, REQ_CTL_IALOG);
  200. }
  201. int vcc_login_bh(struct vcc_request *req) {
  202. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  203. fprintf(stderr, "*** bad package. \n");
  204. return 1;
  205. }
  206. if (ntohl(req->uid))
  207. printf(_("ready. \n"));
  208. else {
  209. fprintf(stderr, _("login failed: wrong password or user doesn't exists. \n"));
  210. fprintf(stderr, _("if you haven't got an account on this server, ask the administrator "
  211. "and you can get one. \n"));
  212. exit(1);
  213. }
  214. return 0;
  215. }
  216. int vcc_create_session(char *name) {
  217. struct vcc_request req;
  218. clear_req(&req);
  219. req.magic = htonl(VCC_MAGIC);
  220. req.reqtype = htonl(REQ_CTL_NEWSE);
  221. strncpy(req.usrname, name, USRNAME_SIZE);
  222. write(fd, &req, REQ_SIZE);
  223. return 0;
  224. }
  225. int join_session(int sid) {
  226. struct vcc_request req;
  227. clear_req(&req);
  228. req.magic = htonl(VCC_MAGIC);
  229. req.reqtype = htonl(REQ_CTL_JOINS);
  230. req.session = htonl(sid);
  231. write(fd, &req, REQ_SIZE);
  232. return 0;
  233. }
  234. int incr_score(char *name, int incr) {
  235. struct vcc_request req;
  236. clear_req(&req);
  237. req.magic = htonl(VCC_MAGIC);
  238. req.reqtype = htonl(REQ_SYS_SCRINC);
  239. req.session = htonl(incr);
  240. strcpy(req.usrname, name);
  241. write(fd, &req, REQ_SIZE);
  242. return 0;
  243. }
  244. int is_user_banned(char *usrname) {
  245. struct klist_node *item;
  246. for (item = banned_people.next; item != &banned_people; item = item->next) {
  247. if (item->usrname && !strcmp(usrname, item->usrname)) {
  248. return 1;
  249. }
  250. }
  251. return 0;
  252. }