protocol.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. protocol.c -- handle the meta-protocol, basic functions
  3. Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
  4. 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
  5. This program 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 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. $Id: protocol.c,v 1.28.4.122 2002/02/10 21:57:54 guus Exp $
  17. */
  18. #include "config.h"
  19. #include <sys/types.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <syslog.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <errno.h>
  26. #include <utils.h>
  27. #include "conf.h"
  28. #include "protocol.h"
  29. #include "meta.h"
  30. #include "connection.h"
  31. #include "system.h"
  32. int check_id(char *id)
  33. {
  34. int i;
  35. for (i = 0; i < strlen(id); i++)
  36. if(!isalnum(id[i]) && id[i] != '_')
  37. return -1;
  38. return 0;
  39. }
  40. /* Generic request routines - takes care of logging and error
  41. detection as well */
  42. int send_request(connection_t *c, const char *format, ...)
  43. {
  44. va_list args;
  45. char buffer[MAXBUFSIZE];
  46. int len, request;
  47. cp
  48. /* Use vsnprintf instead of vasprintf: faster, no memory
  49. fragmentation, cleanup is automatic, and there is a limit on the
  50. input buffer anyway */
  51. va_start(args, format);
  52. len = vsnprintf(buffer, MAXBUFSIZE, format, args);
  53. request = va_arg(args, int);
  54. va_end(args);
  55. if(len < 0 || len > MAXBUFSIZE-1)
  56. {
  57. syslog(LOG_ERR, _("Output buffer overflow while sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
  58. return -1;
  59. }
  60. if(debug_lvl >= DEBUG_PROTOCOL)
  61. {
  62. if(debug_lvl >= DEBUG_META)
  63. syslog(LOG_DEBUG, _("Sending %s to %s (%s): %s"), request_name[request], c->name, c->hostname, buffer);
  64. else
  65. syslog(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request], c->name, c->hostname);
  66. }
  67. buffer[len++] = '\n';
  68. cp
  69. return send_meta(c, buffer, len);
  70. }
  71. int receive_request(connection_t *c)
  72. {
  73. int request;
  74. cp
  75. if(sscanf(c->buffer, "%d", &request) == 1)
  76. {
  77. if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
  78. {
  79. if(debug_lvl >= DEBUG_META)
  80. syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
  81. c->name, c->hostname, c->buffer);
  82. else
  83. syslog(LOG_ERR, _("Unknown request from %s (%s)"),
  84. c->name, c->hostname);
  85. return -1;
  86. }
  87. else
  88. {
  89. if(debug_lvl >= DEBUG_PROTOCOL)
  90. {
  91. if(debug_lvl >= DEBUG_META)
  92. syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
  93. request_name[request], c->name, c->hostname, c->buffer);
  94. else
  95. syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
  96. request_name[request], c->name, c->hostname);
  97. }
  98. }
  99. if((c->allow_request != ALL) && (c->allow_request != request))
  100. {
  101. syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
  102. return -1;
  103. }
  104. if(request_handlers[request](c))
  105. /* Something went wrong. Probably scriptkiddies. Terminate. */
  106. {
  107. syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
  108. request_name[request], c->name, c->hostname);
  109. return -1;
  110. }
  111. }
  112. else
  113. {
  114. syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
  115. c->name, c->hostname);
  116. return -1;
  117. }
  118. cp
  119. return 0;
  120. }
  121. /* Jumptable for the request handlers */
  122. int (*request_handlers[])(connection_t*) = {
  123. id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
  124. status_h, error_h, termreq_h,
  125. ping_h, pong_h,
  126. // add_node_h, del_node_h,
  127. add_subnet_h, del_subnet_h,
  128. add_edge_h, del_edge_h,
  129. key_changed_h, req_key_h, ans_key_h,
  130. tcppacket_h,
  131. };
  132. /* Request names */
  133. char (*request_name[]) = {
  134. "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
  135. "STATUS", "ERROR", "TERMREQ",
  136. "PING", "PONG",
  137. // "ADD_NODE", "DEL_NODE",
  138. "ADD_SUBNET", "DEL_SUBNET",
  139. "ADD_EDGE", "DEL_EDGE",
  140. "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
  141. "PACKET",
  142. };