vcc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. #include <vcc/interfaces.h>
  36. #define clear_req(req) memset(req, 0, sizeof(*req))
  37. int current_sid = 0;
  38. int init_socket(const char *server_addr, int server_port) {
  39. struct sockaddr_in addr, server;
  40. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  41. perror("socket()");
  42. exit(1);
  43. }
  44. memset(&addr, 0, sizeof(struct sockaddr_in));
  45. addr.sin_family = AF_INET;
  46. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  47. addr.sin_port = htons(0);
  48. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  49. perror("bind()");
  50. exit(1);
  51. }
  52. memset(&server, 0, sizeof(struct sockaddr_in));
  53. server.sin_family = AF_INET;
  54. server.sin_port = htons(server_port);
  55. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  56. fprintf(stderr, _("invalid ip address. "
  57. "leave blank if you want to connect the default server. \n"));
  58. exit(1);
  59. }
  60. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  61. perror("connect()");
  62. exit(1);
  63. }
  64. return fd;
  65. }
  66. int __send_msg(const char *msg, const char *usrname, int flags) {
  67. struct vcc_request req;
  68. clear_req(&req);
  69. req.magic = htonl(VCC_MAGIC);
  70. req.reqtype = htonl(REQ_MSG_SEND);
  71. req.uid = 0;
  72. req.session = htonl(current_sid);
  73. req.flags = htonl(flags);
  74. strncpy(req.usrname, usrname, USRNAME_SIZE);
  75. strncpy(req.msg, msg, MSG_SIZE);
  76. write(fd, &req, REQ_SIZE);
  77. return 0;
  78. }
  79. extern inline int send_msg(const char *msg, const char *usrname) {
  80. return __send_msg(msg, usrname, 0);
  81. }
  82. extern inline int send_msg_encrypted(const char *msg, const char *usrname) {
  83. return __send_msg(msg, usrname, FLAG_ENCRYPTED);
  84. }
  85. int send_relay_msg(const char *msg, const char *usrname, const char *visible) {
  86. struct vcc_relay_header req;
  87. int len;
  88. len = strlen(msg);
  89. clear_req(&req);
  90. req.magic = htonl(VCC_MAGIC_RL);
  91. req.reqtype = htonl(REQ_REL_MSG);
  92. req.uid = 0;
  93. req.session = htonl(current_sid);
  94. req.size = htonl(sizeof(struct vcc_relay_header) + len);
  95. strncpy(req.usrname, usrname, USRNAME_SIZE);
  96. if (visible)
  97. strncpy(req.visible, visible, USRNAME_SIZE);
  98. else
  99. memset(req.visible, 0, USRNAME_SIZE);
  100. write(fd, &req, sizeof(struct vcc_relay_header));
  101. write(fd, msg, len);
  102. return 0;
  103. }
  104. int read_until(int fd, void *p, int until) {
  105. int did_read = 0;
  106. int n;
  107. while ((did_read += n = read(fd, p, until)) < until) {
  108. if (!n)
  109. return 0;
  110. p += n;
  111. }
  112. return until;
  113. }
  114. char *relay_get_msg(int fd, struct vcc_relay_header *hdr) {
  115. char *p;
  116. int size;
  117. size = ntohl(hdr->size);
  118. if (unlikely(!(p = malloc(size)))) {
  119. fprintf(stderr, "*** malloc() failed\n");
  120. return NULL;
  121. }
  122. memset(p, 0, size);
  123. read_until(fd, p, size - sizeof(struct vcc_relay_header));
  124. return p;
  125. }
  126. int get_user_info(char *usrname) {
  127. struct vcc_request req;
  128. clear_req(&req);
  129. req.magic = htonl(VCC_MAGIC);
  130. req.reqtype = htonl(REQ_CTL_UINFO);
  131. req.uid = 0;
  132. strncpy(req.msg, usrname, USRNAME_SIZE);
  133. write(fd, &req, REQ_SIZE);
  134. return 0;
  135. }
  136. int get_users(void) {
  137. struct vcc_request req;
  138. clear_req(&req);
  139. req.magic = htonl(VCC_MAGIC);
  140. req.reqtype = htonl(REQ_CTL_USRS);
  141. req.uid = 0;
  142. write(fd, &req, REQ_SIZE);
  143. return 0;
  144. }
  145. int get_sessions(void) {
  146. struct vcc_request req;
  147. clear_req(&req);
  148. req.magic = htonl(VCC_MAGIC);
  149. req.reqtype = htonl(REQ_CTL_SESS);
  150. req.uid = 0;
  151. write(fd, &req, REQ_SIZE);
  152. return 0;
  153. }
  154. int do_bh(struct vcc_request *req) {
  155. switch (ntohl(req->reqtype)) {
  156. case REQ_CTL_LOGIN:
  157. vcc_login_bh(req);
  158. break;
  159. #if 0
  160. /* obsolete, see do_relay_bh() */
  161. case REQ_CTL_USRS:
  162. do_cmd_ls_bh(req);
  163. break;
  164. #endif
  165. case REQ_CTL_SESS:
  166. do_cmd_lsse_bh(req);
  167. break;
  168. case REQ_CTL_UINFO:
  169. do_cmd_uinfo_bh(req);
  170. break;
  171. case REQ_SYS_SCRINC:
  172. do_cmd_incr_bh(req);
  173. break;
  174. default:
  175. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  176. break;
  177. }
  178. return 0;
  179. }
  180. int do_relay_bh(struct vcc_relay_header *req) {
  181. switch (ntohl(req->reqtype)) {
  182. case REQ_CTL_USRS:
  183. do_cmd_ls_bh(req);
  184. break;
  185. default:
  186. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  187. break;
  188. }
  189. return 0;
  190. }
  191. int __login(char *usrname, char *passwd, int type) {
  192. struct vcc_request req;
  193. clear_req(&req);
  194. req.magic = htonl(VCC_MAGIC);
  195. /* REQ_CTL_LOGIN or REQ_CTL_IALOG */
  196. req.reqtype = htonl(type);
  197. strncpy(req.usrname, usrname, USRNAME_SIZE);
  198. strncpy(req.msg, passwd, MSG_SIZE);
  199. write(fd, &req, REQ_SIZE);
  200. return 0;
  201. }
  202. int vcc_login(char *usrname, char *passwd) {
  203. return __login(usrname, passwd, REQ_CTL_LOGIN);
  204. }
  205. int ia_login(char *session) {
  206. return __login(usrname, session, REQ_CTL_IALOG);
  207. }
  208. int vcc_login_bh(struct vcc_request *req) {
  209. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  210. fprintf(stderr, "*** bad package. \n");
  211. return 1;
  212. }
  213. if (ntohl(req->uid))
  214. printf(_("ready. \n"));
  215. else {
  216. fprintf(stderr, _("login failed: wrong password or user doesn't exists. \n"));
  217. fprintf(stderr, _("if you haven't got an account on this server, ask the administrator "
  218. "and you can get one. \n"));
  219. exit(1);
  220. }
  221. return 0;
  222. }
  223. int vcc_create_session(char *name) {
  224. struct vcc_request req;
  225. clear_req(&req);
  226. req.magic = htonl(VCC_MAGIC);
  227. req.reqtype = htonl(REQ_CTL_NEWSE);
  228. strncpy(req.usrname, name, USRNAME_SIZE);
  229. write(fd, &req, REQ_SIZE);
  230. return 0;
  231. }
  232. int join_session(int sid) {
  233. struct vcc_request req;
  234. clear_req(&req);
  235. req.magic = htonl(VCC_MAGIC);
  236. req.reqtype = htonl(REQ_CTL_JOINS);
  237. req.session = htonl(sid);
  238. write(fd, &req, REQ_SIZE);
  239. return 0;
  240. }
  241. int incr_score(char *name, int incr) {
  242. struct vcc_request req;
  243. clear_req(&req);
  244. req.magic = htonl(VCC_MAGIC);
  245. req.reqtype = htonl(REQ_SYS_SCRINC);
  246. req.session = htonl(incr);
  247. strcpy(req.usrname, name);
  248. write(fd, &req, REQ_SIZE);
  249. return 0;
  250. }
  251. int is_user_banned(char *usrname) {
  252. struct klist_node *item;
  253. struct banlist *ban;
  254. for (item = banned_people.next; item != &banned_people; item = item->next) {
  255. ban = banlist_of(item);
  256. if (ban->usrname && !strcmp(usrname, ban->usrname)) {
  257. return 1;
  258. }
  259. }
  260. return 0;
  261. }
  262. int get_sys_info(void) {
  263. struct vcc_request req;
  264. clear_req(&req);
  265. req.magic = htonl(VCC_MAGIC);
  266. req.reqtype = htonl(REQ_SYS_INFO);
  267. strcpy(req.usrname, usrname);
  268. write(fd, &req, REQ_SIZE);
  269. return 0;
  270. }