daemon.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. /*
  2. * Copyright (c) 2002 - 2003
  3. * NetGroup, Politecnico di Torino (Italy)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the Politecnico di Torino nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. #include <config.h>
  33. #endif
  34. #include "ftmacros.h"
  35. #include "varattrs.h"
  36. #include <errno.h> // for the errno variable
  37. #include <stdlib.h> // for malloc(), free(), ...
  38. #include <string.h> // for strlen(), ...
  39. #ifdef _WIN32
  40. #include <process.h> // for threads
  41. #else
  42. #include <unistd.h>
  43. #include <pthread.h>
  44. #include <sys/time.h>
  45. #include <sys/types.h> // for select() and such
  46. #include <pwd.h> // for password management
  47. #endif
  48. #ifdef HAVE_GETSPNAM
  49. #include <shadow.h> // for password management
  50. #endif
  51. #include <pcap.h> // for libpcap/WinPcap calls
  52. #include "fmtutils.h"
  53. #include "sockutils.h" // for socket calls
  54. #include "portability.h"
  55. #include "rpcap-protocol.h"
  56. #include "daemon.h"
  57. #include "log.h"
  58. #define RPCAP_TIMEOUT_INIT 90 /* Initial timeout for RPCAP connections (default: 90 sec) */
  59. #define RPCAP_TIMEOUT_RUNTIME 180 /* Run-time timeout for RPCAP connections (default: 3 min) */
  60. #define RPCAP_SUSPEND_WRONGAUTH 1 /* If the authentication is wrong, stops 1 sec before accepting a new auth message */
  61. // Parameters for the service loop.
  62. struct daemon_slpars
  63. {
  64. SOCKET sockctrl_in; //!< SOCKET ID of the input side of the control connection
  65. SOCKET sockctrl_out; //!< SOCKET ID of the output side of the control connection
  66. uint8 protocol_version; //!< negotiated protocol version
  67. int isactive; //!< Not null if the daemon has to run in active mode
  68. int nullAuthAllowed; //!< '1' if we permit NULL authentication, '0' otherwise
  69. };
  70. /*
  71. * Data for a session managed by a thread.
  72. */
  73. struct session {
  74. SOCKET sockctrl_out;
  75. SOCKET sockdata;
  76. uint8 protocol_version;
  77. pcap_t *fp;
  78. unsigned int TotCapt;
  79. };
  80. //
  81. // Structure to refer to a thread.
  82. // It includes both a Boolean indicating whether we *have* a thread,
  83. // and a platform-dependent (UN*X vs. Windows) identifier for the
  84. // thread; on Windows, we could use an invalid handle to indicate
  85. // that we don't have a thread, but there *is* no portable "no thread"
  86. // value for a pthread_t on UN*X.
  87. //
  88. struct thread_handle {
  89. int have_thread;
  90. #ifdef _WIN32
  91. HANDLE thread;
  92. #else
  93. pthread_t thread;
  94. #endif
  95. };
  96. // Locally defined functions
  97. static int daemon_msg_err(SOCKET sockctrl_in, uint32 plen);
  98. static int daemon_msg_auth_req(struct daemon_slpars *pars, uint32 plen);
  99. static int daemon_AuthUserPwd(char *username, char *password, char *errbuf);
  100. static int daemon_msg_findallif_req(struct daemon_slpars *pars, uint32 plen);
  101. static int daemon_msg_open_req(struct daemon_slpars *pars, uint32 plen, char *source, size_t sourcelen);
  102. static int daemon_msg_startcap_req(struct daemon_slpars *pars, uint32 plen, struct thread_handle *threaddata, char *source, struct session **sessionp, struct rpcap_sampling *samp_param);
  103. static int daemon_msg_endcap_req(struct daemon_slpars *pars, struct session *session, struct thread_handle *threaddata);
  104. static int daemon_msg_updatefilter_req(struct daemon_slpars *pars, struct session *session, uint32 plen);
  105. static int daemon_unpackapplyfilter(SOCKET sockctrl_in, struct session *session, uint32 *plenp, char *errbuf);
  106. static int daemon_msg_stats_req(struct daemon_slpars *pars, struct session *session, uint32 plen, struct pcap_stat *stats, unsigned int svrcapt);
  107. static int daemon_msg_setsampling_req(struct daemon_slpars *pars, uint32 plen, struct rpcap_sampling *samp_param);
  108. static void daemon_seraddr(struct sockaddr_storage *sockaddrin, struct rpcap_sockaddr *sockaddrout);
  109. #ifdef _WIN32
  110. static unsigned __stdcall daemon_thrdatamain(void *ptr);
  111. #else
  112. static void *daemon_thrdatamain(void *ptr);
  113. #endif
  114. static int rpcapd_recv_msg_header(SOCKET sock, struct rpcap_header *headerp);
  115. static int rpcapd_recv(SOCKET sock, char *buffer, size_t toread, uint32 *plen, char *errmsgbuf);
  116. static int rpcapd_discard(SOCKET sock, uint32 len);
  117. int
  118. daemon_serviceloop(SOCKET sockctrl_in, SOCKET sockctrl_out, int isactive, int nullAuthAllowed)
  119. {
  120. struct daemon_slpars pars; // service loop parameters
  121. char errbuf[PCAP_ERRBUF_SIZE + 1]; // keeps the error string, prior to be printed
  122. char errmsgbuf[PCAP_ERRBUF_SIZE + 1]; // buffer for errors to send to the client
  123. int nrecv;
  124. struct rpcap_header header; // RPCAP message general header
  125. uint32 plen; // payload length from header
  126. int authenticated = 0; // 1 if the client has successfully authenticated
  127. char source[PCAP_BUF_SIZE+1]; // keeps the string that contains the interface to open
  128. int got_source = 0; // 1 if we've gotten the source from an open request
  129. struct session *session = NULL; // struct session main variable
  130. const char *msg_type_string; // string for message type
  131. int client_told_us_to_close = 0; // 1 if the client told us to close the capture
  132. struct thread_handle threaddata; // 'read from daemon and send to client' thread
  133. // needed to save the values of the statistics
  134. struct pcap_stat stats;
  135. unsigned int svrcapt;
  136. struct rpcap_sampling samp_param; // in case sampling has been requested
  137. // Structures needed for the select() call
  138. fd_set rfds; // set of socket descriptors we have to check
  139. struct timeval tv; // maximum time the select() can block waiting for data
  140. int retval; // select() return value
  141. // Set parameters structure
  142. pars.sockctrl_in = sockctrl_in;
  143. pars.sockctrl_out = sockctrl_out;
  144. pars.protocol_version = 0; // not yet known
  145. pars.isactive = isactive; // active mode
  146. pars.nullAuthAllowed = nullAuthAllowed;
  147. // We don't have a thread yet.
  148. threaddata.have_thread = 0;
  149. //
  150. // We *shouldn't* have to initialize the thread indicator
  151. // itself, because the compiler *should* realize that we
  152. // only use this if have_thread isn't 0, but we *do* have
  153. // to do it, because not all compilers *do* realize that.
  154. //
  155. // There is no "invalid thread handle" value for a UN*X
  156. // pthread_t, so we just zero it out.
  157. //
  158. #ifdef _WIN32
  159. threaddata.thread = INVALID_HANDLE_VALUE;
  160. #else
  161. memset(&threaddata.thread, 0, sizeof(threaddata.thread));
  162. #endif
  163. *errbuf = 0; // Initialize errbuf
  164. //
  165. // The client must first authenticate; loop until they send us a
  166. // message with a version we support and credentials we accept,
  167. // they send us a close message indicating that they're giving up,
  168. // or we get a network error or other fatal error.
  169. //
  170. while (!authenticated)
  171. {
  172. //
  173. // If we're in active mode, we have to check for the
  174. // initial timeout.
  175. //
  176. // XXX - do this on *every* trip through the loop?
  177. //
  178. if (!pars.isactive)
  179. {
  180. FD_ZERO(&rfds);
  181. // We do not have to block here
  182. tv.tv_sec = RPCAP_TIMEOUT_INIT;
  183. tv.tv_usec = 0;
  184. FD_SET(pars.sockctrl_in, &rfds);
  185. retval = select(pars.sockctrl_in + 1, &rfds, NULL, NULL, &tv);
  186. if (retval == -1)
  187. {
  188. sock_geterror("select failed: ", errmsgbuf, PCAP_ERRBUF_SIZE);
  189. if (rpcap_senderror(pars.sockctrl_out, 0, PCAP_ERR_NETW, errmsgbuf, errbuf) == -1)
  190. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  191. goto end;
  192. }
  193. // The timeout has expired
  194. // So, this was a fake connection. Drop it down
  195. if (retval == 0)
  196. {
  197. if (rpcap_senderror(pars.sockctrl_out, 0, PCAP_ERR_INITTIMEOUT, "The RPCAP initial timeout has expired", errbuf) == -1)
  198. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  199. goto end;
  200. }
  201. }
  202. //
  203. // Read the message header from the client.
  204. //
  205. nrecv = rpcapd_recv_msg_header(pars.sockctrl_in, &header);
  206. if (nrecv == -1)
  207. {
  208. // Fatal error.
  209. goto end;
  210. }
  211. if (nrecv == -2)
  212. {
  213. // Client closed the connection.
  214. goto end;
  215. }
  216. plen = header.plen;
  217. //
  218. // Did the client specify a version we can handle?
  219. //
  220. if (!RPCAP_VERSION_IS_SUPPORTED(header.ver))
  221. {
  222. //
  223. // Tell them it's not a valid protocol version.
  224. //
  225. uint8 reply_version;
  226. //
  227. // If RPCAP_MIN_VERSION is 0, no version is too
  228. // old, as the oldest supported version is 0,
  229. // and there are no negative versions.
  230. //
  231. #if RPCAP_MIN_VERSION != 0
  232. if (header.ver < RPCAP_MIN_VERSION)
  233. {
  234. //
  235. // Their maximum version is too old;
  236. // there *is* no version we can both
  237. // handle, and they might reject
  238. // an error with a version they don't
  239. // understand, so reply with the
  240. // version they sent. That may
  241. // make them retry with that version,
  242. // but they'll give up on that
  243. // failure.
  244. //
  245. reply_version = header.ver;
  246. }
  247. else
  248. #endif
  249. {
  250. //
  251. // Their maximum version is too new,
  252. // but they might be able to handle
  253. // *our* maximum version, so reply
  254. // with that version.
  255. //
  256. reply_version = RPCAP_MAX_VERSION;
  257. }
  258. if (rpcap_senderror(pars.sockctrl_out, reply_version,
  259. PCAP_ERR_WRONGVER, "RPCAP version number mismatch",
  260. errbuf) == -1)
  261. {
  262. // That failed; log a message and give up.
  263. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  264. goto end;
  265. }
  266. // Discard the rest of the message.
  267. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  268. {
  269. // Network error.
  270. goto end;
  271. }
  272. // Let them try again.
  273. continue;
  274. }
  275. //
  276. // OK, we use the version the client specified.
  277. //
  278. pars.protocol_version = header.ver;
  279. switch (header.type)
  280. {
  281. case RPCAP_MSG_AUTH_REQ:
  282. retval = daemon_msg_auth_req(&pars, plen);
  283. if (retval == -1)
  284. {
  285. // Fatal error; a message has
  286. // been logged, so just give up.
  287. goto end;
  288. }
  289. if (retval == -2)
  290. {
  291. // Non-fatal error; we sent back
  292. // an error message, so let them
  293. // try again.
  294. continue;
  295. }
  296. // OK, we're authenticated; we sent back
  297. // a reply, so start serving requests.
  298. authenticated = 1;
  299. break;
  300. case RPCAP_MSG_CLOSE:
  301. //
  302. // The client is giving up.
  303. // Discard the rest of the message, if
  304. // there is anything more.
  305. //
  306. (void)rpcapd_discard(pars.sockctrl_in, plen);
  307. // We're done with this client.
  308. goto end;
  309. case RPCAP_MSG_ERROR:
  310. // Log this and close the connection?
  311. // XXX - is this what happens in active
  312. // mode, where *we* initiate the
  313. // connection, and the client gives us
  314. // an error message rather than a "let
  315. // me log in" message, indicating that
  316. // we're not allowed to connect to them?
  317. (void)daemon_msg_err(pars.sockctrl_in, plen);
  318. goto end;
  319. case RPCAP_MSG_FINDALLIF_REQ:
  320. case RPCAP_MSG_OPEN_REQ:
  321. case RPCAP_MSG_STARTCAP_REQ:
  322. case RPCAP_MSG_UPDATEFILTER_REQ:
  323. case RPCAP_MSG_STATS_REQ:
  324. case RPCAP_MSG_ENDCAP_REQ:
  325. case RPCAP_MSG_SETSAMPLING_REQ:
  326. //
  327. // These requests can't be sent until
  328. // the client is authenticated.
  329. //
  330. msg_type_string = rpcap_msg_type_string(header.type);
  331. if (msg_type_string != NULL)
  332. {
  333. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "%s request sent before authentication was completed", msg_type_string);
  334. }
  335. else
  336. {
  337. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Message of type %u sent before authentication was completed", header.type);
  338. }
  339. if (rpcap_senderror(pars.sockctrl_out,
  340. pars.protocol_version, PCAP_ERR_WRONGMSG,
  341. errmsgbuf, errbuf) == -1)
  342. {
  343. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  344. goto end;
  345. }
  346. // Discard the rest of the message.
  347. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  348. {
  349. // Network error.
  350. goto end;
  351. }
  352. break;
  353. case RPCAP_MSG_PACKET:
  354. case RPCAP_MSG_FINDALLIF_REPLY:
  355. case RPCAP_MSG_OPEN_REPLY:
  356. case RPCAP_MSG_STARTCAP_REPLY:
  357. case RPCAP_MSG_UPDATEFILTER_REPLY:
  358. case RPCAP_MSG_AUTH_REPLY:
  359. case RPCAP_MSG_STATS_REPLY:
  360. case RPCAP_MSG_ENDCAP_REPLY:
  361. case RPCAP_MSG_SETSAMPLING_REPLY:
  362. //
  363. // These are server-to-client messages.
  364. //
  365. msg_type_string = rpcap_msg_type_string(header.type);
  366. if (msg_type_string != NULL)
  367. {
  368. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message %s received from client", msg_type_string);
  369. }
  370. else
  371. {
  372. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message of type %u received from client", header.type);
  373. }
  374. if (rpcap_senderror(pars.sockctrl_out,
  375. pars.protocol_version, PCAP_ERR_WRONGMSG,
  376. errmsgbuf, errbuf) == -1)
  377. {
  378. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  379. goto end;
  380. }
  381. // Discard the rest of the message.
  382. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  383. {
  384. // Fatal error.
  385. goto end;
  386. }
  387. break;
  388. default:
  389. //
  390. // Unknown message type.
  391. //
  392. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Unknown message type %u", header.type);
  393. if (rpcap_senderror(pars.sockctrl_out,
  394. pars.protocol_version, PCAP_ERR_WRONGMSG,
  395. errmsgbuf, errbuf) == -1)
  396. {
  397. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  398. goto end;
  399. }
  400. // Discard the rest of the message.
  401. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  402. {
  403. // Fatal error.
  404. goto end;
  405. }
  406. break;
  407. }
  408. }
  409. //
  410. // OK, the client has authenticated itself, and we can start
  411. // processing regular requests from it.
  412. //
  413. //
  414. // We don't have any statistics yet.
  415. //
  416. stats.ps_ifdrop = 0;
  417. stats.ps_recv = 0;
  418. stats.ps_drop = 0;
  419. svrcapt = 0;
  420. //
  421. // Service requests.
  422. //
  423. for (;;)
  424. {
  425. errbuf[0] = 0; // clear errbuf
  426. // Avoid zombies connections; check if the connection is opens but no commands are performed
  427. // from more than RPCAP_TIMEOUT_RUNTIME
  428. // Conditions:
  429. // - I have to be in normal mode (no active mode)
  430. // - if the device is open, I don't have to be in the middle of a capture (session->sockdata)
  431. // - if the device is closed, I have always to check if a new command arrives
  432. //
  433. // Be carefully: the capture can have been started, but an error occurred (so session != NULL, but
  434. // sockdata is 0
  435. if ((!pars.isactive) && ((session == NULL) || ((session != NULL) && (session->sockdata == 0))))
  436. {
  437. // Check for the initial timeout
  438. FD_ZERO(&rfds);
  439. // We do not have to block here
  440. tv.tv_sec = RPCAP_TIMEOUT_RUNTIME;
  441. tv.tv_usec = 0;
  442. FD_SET(pars.sockctrl_in, &rfds);
  443. retval = select(pars.sockctrl_in + 1, &rfds, NULL, NULL, &tv);
  444. if (retval == -1)
  445. {
  446. sock_geterror("select failed: ", errmsgbuf, PCAP_ERRBUF_SIZE);
  447. if (rpcap_senderror(pars.sockctrl_out,
  448. pars.protocol_version, PCAP_ERR_NETW,
  449. errmsgbuf, errbuf) == -1)
  450. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  451. goto end;
  452. }
  453. // The timeout has expired
  454. // So, this was a fake connection. Drop it down
  455. if (retval == 0)
  456. {
  457. if (rpcap_senderror(pars.sockctrl_out,
  458. pars.protocol_version,
  459. PCAP_ERR_INITTIMEOUT,
  460. "The RPCAP initial timeout has expired",
  461. errbuf) == -1)
  462. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  463. goto end;
  464. }
  465. }
  466. //
  467. // Read the message header from the client.
  468. //
  469. nrecv = rpcapd_recv_msg_header(pars.sockctrl_in, &header);
  470. if (nrecv == -1)
  471. {
  472. // Fatal error.
  473. goto end;
  474. }
  475. if (nrecv == -2)
  476. {
  477. // Client closed the connection.
  478. goto end;
  479. }
  480. plen = header.plen;
  481. //
  482. // Did the client specify the version we negotiated?
  483. //
  484. // For now, there's only one version.
  485. //
  486. if (header.ver != pars.protocol_version)
  487. {
  488. //
  489. // Tell them it's not the negotiated version.
  490. // Send the error message with their version,
  491. // so they don't reject it as having the wrong
  492. // version.
  493. //
  494. if (rpcap_senderror(pars.sockctrl_out,
  495. header.ver, PCAP_ERR_WRONGVER,
  496. "RPCAP version in message isn't the negotiated version",
  497. errbuf) == -1)
  498. {
  499. // That failed; log a message and give up.
  500. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  501. goto end;
  502. }
  503. // Discard the rest of the message.
  504. (void)rpcapd_discard(pars.sockctrl_in, plen);
  505. // Give up on them.
  506. goto end;
  507. }
  508. switch (header.type)
  509. {
  510. case RPCAP_MSG_ERROR: // The other endpoint reported an error
  511. {
  512. (void)daemon_msg_err(pars.sockctrl_in, plen);
  513. // Do nothing; just exit; the error code is already into the errbuf
  514. // XXX - actually exit....
  515. break;
  516. }
  517. case RPCAP_MSG_FINDALLIF_REQ:
  518. {
  519. if (daemon_msg_findallif_req(&pars, plen) == -1)
  520. {
  521. // Fatal error; a message has
  522. // been logged, so just give up.
  523. goto end;
  524. }
  525. break;
  526. }
  527. case RPCAP_MSG_OPEN_REQ:
  528. {
  529. //
  530. // Process the open request, and keep
  531. // the source from it, for use later
  532. // when the capture is started.
  533. //
  534. // XXX - we don't care if the client sends
  535. // us multiple open requests, the last
  536. // one wins.
  537. //
  538. retval = daemon_msg_open_req(&pars, plen, source, sizeof(source));
  539. if (retval == -1)
  540. {
  541. // Fatal error; a message has
  542. // been logged, so just give up.
  543. goto end;
  544. }
  545. got_source = 1;
  546. break;
  547. }
  548. case RPCAP_MSG_STARTCAP_REQ:
  549. {
  550. if (!got_source)
  551. {
  552. // They never told us what device
  553. // to capture on!
  554. if (rpcap_senderror(pars.sockctrl_out,
  555. pars.protocol_version,
  556. PCAP_ERR_STARTCAPTURE,
  557. "No capture device was specified",
  558. errbuf) == -1)
  559. {
  560. // Fatal error; log an
  561. // error and give up.
  562. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  563. goto end;
  564. }
  565. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  566. {
  567. goto end;
  568. }
  569. break;
  570. }
  571. if (daemon_msg_startcap_req(&pars, plen, &threaddata, source, &session, &samp_param) == -1)
  572. {
  573. // Fatal error; a message has
  574. // been logged, so just give up.
  575. goto end;
  576. }
  577. break;
  578. }
  579. case RPCAP_MSG_UPDATEFILTER_REQ:
  580. {
  581. if (session)
  582. {
  583. if (daemon_msg_updatefilter_req(&pars, session, plen) == -1)
  584. {
  585. // Fatal error; a message has
  586. // been logged, so just give up.
  587. goto end;
  588. }
  589. }
  590. else
  591. {
  592. if (rpcap_senderror(pars.sockctrl_out,
  593. pars.protocol_version,
  594. PCAP_ERR_UPDATEFILTER,
  595. "Device not opened. Cannot update filter",
  596. errbuf) == -1)
  597. {
  598. // That failed; log a message and give up.
  599. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  600. goto end;
  601. }
  602. }
  603. break;
  604. }
  605. case RPCAP_MSG_CLOSE: // The other endpoint close the pcap session
  606. {
  607. //
  608. // Indicate to our caller that the client
  609. // closed the control connection.
  610. // This is used only in case of active mode.
  611. //
  612. client_told_us_to_close = 1;
  613. SOCK_DEBUG_MESSAGE("The other end system asked to close the connection.");
  614. goto end;
  615. }
  616. case RPCAP_MSG_STATS_REQ:
  617. {
  618. if (daemon_msg_stats_req(&pars, session, plen, &stats, svrcapt) == -1)
  619. {
  620. // Fatal error; a message has
  621. // been logged, so just give up.
  622. goto end;
  623. }
  624. break;
  625. }
  626. case RPCAP_MSG_ENDCAP_REQ: // The other endpoint close the current capture session
  627. {
  628. if (session)
  629. {
  630. // Save statistics (we can need them in the future)
  631. if (pcap_stats(session->fp, &stats))
  632. {
  633. svrcapt = session->TotCapt;
  634. }
  635. else
  636. {
  637. stats.ps_ifdrop = 0;
  638. stats.ps_recv = 0;
  639. stats.ps_drop = 0;
  640. svrcapt = 0;
  641. }
  642. if (daemon_msg_endcap_req(&pars, session, &threaddata) == -1)
  643. {
  644. free(session);
  645. session = NULL;
  646. // Fatal error; a message has
  647. // been logged, so just give up.
  648. goto end;
  649. }
  650. free(session);
  651. session = NULL;
  652. }
  653. else
  654. {
  655. rpcap_senderror(pars.sockctrl_out,
  656. pars.protocol_version,
  657. PCAP_ERR_ENDCAPTURE,
  658. "Device not opened. Cannot close the capture",
  659. errbuf);
  660. }
  661. break;
  662. }
  663. case RPCAP_MSG_SETSAMPLING_REQ:
  664. {
  665. if (daemon_msg_setsampling_req(&pars, plen, &samp_param) == -1)
  666. {
  667. // Fatal error; a message has
  668. // been logged, so just give up.
  669. goto end;
  670. }
  671. break;
  672. }
  673. case RPCAP_MSG_AUTH_REQ:
  674. {
  675. //
  676. // We're already authenticated; you don't
  677. // get to reauthenticate.
  678. //
  679. rpcapd_log(LOGPRIO_INFO, "The client sent an RPCAP_MSG_AUTH_REQ message after authentication was completed");
  680. if (rpcap_senderror(pars.sockctrl_out,
  681. pars.protocol_version,
  682. PCAP_ERR_WRONGMSG,
  683. "RPCAP_MSG_AUTH_REQ request sent after authentication was completed",
  684. errbuf) == -1)
  685. {
  686. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  687. goto end;
  688. }
  689. // Discard the rest of the message.
  690. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  691. {
  692. // Fatal error.
  693. goto end;
  694. }
  695. goto end;
  696. case RPCAP_MSG_PACKET:
  697. case RPCAP_MSG_FINDALLIF_REPLY:
  698. case RPCAP_MSG_OPEN_REPLY:
  699. case RPCAP_MSG_STARTCAP_REPLY:
  700. case RPCAP_MSG_UPDATEFILTER_REPLY:
  701. case RPCAP_MSG_AUTH_REPLY:
  702. case RPCAP_MSG_STATS_REPLY:
  703. case RPCAP_MSG_ENDCAP_REPLY:
  704. case RPCAP_MSG_SETSAMPLING_REPLY:
  705. //
  706. // These are server-to-client messages.
  707. //
  708. msg_type_string = rpcap_msg_type_string(header.type);
  709. if (msg_type_string != NULL)
  710. {
  711. rpcapd_log(LOGPRIO_INFO, "The client sent a %s server-to-client message", msg_type_string);
  712. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message %s received from client", msg_type_string);
  713. }
  714. else
  715. {
  716. rpcapd_log(LOGPRIO_INFO, "The client sent a server-to-client message of type %u", header.type);
  717. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Server-to-client message of type %u received from client", header.type);
  718. }
  719. if (rpcap_senderror(pars.sockctrl_out,
  720. pars.protocol_version, PCAP_ERR_WRONGMSG,
  721. errmsgbuf, errbuf) == -1)
  722. {
  723. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  724. goto end;
  725. }
  726. // Discard the rest of the message.
  727. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  728. {
  729. // Fatal error.
  730. goto end;
  731. }
  732. goto end;
  733. default:
  734. //
  735. // Unknown message type.
  736. //
  737. rpcapd_log(LOGPRIO_INFO, "The client sent a message of type %u", header.type);
  738. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Unknown message type %u", header.type);
  739. if (rpcap_senderror(pars.sockctrl_out,
  740. pars.protocol_version, PCAP_ERR_WRONGMSG,
  741. errbuf, errmsgbuf) == -1)
  742. {
  743. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  744. goto end;
  745. }
  746. // Discard the rest of the message.
  747. if (rpcapd_discard(pars.sockctrl_in, plen) == -1)
  748. {
  749. // Fatal error.
  750. goto end;
  751. }
  752. goto end;
  753. }
  754. }
  755. }
  756. end:
  757. // The child thread is about to end
  758. // perform pcap_t cleanup, in case it has not been done
  759. if (session)
  760. {
  761. if (threaddata.have_thread)
  762. {
  763. #ifdef _WIN32
  764. //
  765. // Tell the data connection thread main capture
  766. // loop to break out of that loop.
  767. //
  768. pcap_breakloop(session->fp);
  769. //
  770. // If it's currently blocked waiting for packets
  771. // to arrive, try to wake it up, so it can see
  772. // the "break out of the loop" indication.
  773. //
  774. SetEvent(pcap_getevent(session->fp));
  775. //
  776. // Wait for the thread to exit, so we don't close
  777. // sockets out from under it.
  778. //
  779. // XXX - have a timeout, so we don't wait forever?
  780. //
  781. WaitForSingleObject(threaddata.thread, INFINITE);
  782. //
  783. // Release the thread handle, as we're done with
  784. // it.
  785. //
  786. CloseHandle(threaddata.thread);
  787. #else
  788. pthread_cancel(threaddata.thread);
  789. #endif
  790. threaddata.have_thread = 0;
  791. }
  792. if (session->sockdata)
  793. {
  794. sock_close(session->sockdata, NULL, 0);
  795. session->sockdata = 0;
  796. }
  797. pcap_close(session->fp);
  798. free(session);
  799. session = NULL;
  800. }
  801. // Print message and return
  802. SOCK_DEBUG_MESSAGE("I'm exiting from the child loop");
  803. SOCK_DEBUG_MESSAGE(errbuf);
  804. return client_told_us_to_close;
  805. }
  806. /*
  807. * This handles the RPCAP_MSG_ERR message.
  808. */
  809. static int
  810. daemon_msg_err(SOCKET sockctrl_in, uint32 plen)
  811. {
  812. char errbuf[PCAP_ERRBUF_SIZE];
  813. char remote_errbuf[PCAP_ERRBUF_SIZE];
  814. if (plen >= PCAP_ERRBUF_SIZE)
  815. {
  816. /*
  817. * Message is too long; just read as much of it as we
  818. * can into the buffer provided, and discard the rest.
  819. */
  820. if (sock_recv(sockctrl_in, remote_errbuf, PCAP_ERRBUF_SIZE - 1,
  821. SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
  822. PCAP_ERRBUF_SIZE) == -1)
  823. {
  824. // Network error.
  825. rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
  826. return -1;
  827. }
  828. if (rpcapd_discard(sockctrl_in, plen - (PCAP_ERRBUF_SIZE - 1)) == -1)
  829. {
  830. // Network error.
  831. return -1;
  832. }
  833. /*
  834. * Null-terminate it.
  835. */
  836. remote_errbuf[PCAP_ERRBUF_SIZE - 1] = '\0';
  837. }
  838. else if (plen == 0)
  839. {
  840. /* Empty error string. */
  841. remote_errbuf[0] = '\0';
  842. }
  843. else
  844. {
  845. if (sock_recv(sockctrl_in, remote_errbuf, plen,
  846. SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf,
  847. PCAP_ERRBUF_SIZE) == -1)
  848. {
  849. // Network error.
  850. rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
  851. return -1;
  852. }
  853. /*
  854. * Null-terminate it.
  855. */
  856. remote_errbuf[plen] = '\0';
  857. }
  858. // Log the message
  859. rpcapd_log(LOGPRIO_ERROR, "Error from client: %s", remote_errbuf);
  860. return 0;
  861. }
  862. /*
  863. * This handles the RPCAP_MSG_AUTH_REQ message.
  864. * It checks if the authentication credentials supplied by the user are valid.
  865. *
  866. * This function is called if the daemon receives a RPCAP_MSG_AUTH_REQ
  867. * message in its authentication loop. It reads the body of the
  868. * authentication message from the network and checks whether the
  869. * credentials are valid.
  870. *
  871. * \param sockctrl: the socket for the control connection.
  872. *
  873. * \param nullAuthAllowed: '1' if the NULL authentication is allowed.
  874. *
  875. * \param errbuf: a user-allocated buffer in which the error message
  876. * (if one) has to be written. It must be at least PCAP_ERRBUF_SIZE
  877. * bytes long.
  878. *
  879. * \return '0' if everything is fine, '-1' if an unrecoverable error occurred,
  880. * or '-2' if the authentication failed. For errors, an error message is
  881. * returned in the 'errbuf' variable; this gives a message for the
  882. * unrecoverable error or for the authentication failure.
  883. */
  884. static int
  885. daemon_msg_auth_req(struct daemon_slpars *pars, uint32 plen)
  886. {
  887. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  888. char errmsgbuf[PCAP_ERRBUF_SIZE]; // buffer for errors to send to the client
  889. struct rpcap_header header; // RPCAP message general header
  890. int status;
  891. struct rpcap_auth auth; // RPCAP authentication header
  892. status = rpcapd_recv(pars->sockctrl_in, (char *) &auth, sizeof(struct rpcap_auth), &plen, errmsgbuf);
  893. if (status == -1)
  894. {
  895. return -1;
  896. }
  897. if (status == -2)
  898. {
  899. goto error;
  900. }
  901. switch (ntohs(auth.type))
  902. {
  903. case RPCAP_RMTAUTH_NULL:
  904. {
  905. if (!pars->nullAuthAllowed)
  906. {
  907. // Send the client an error reply.
  908. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Authentication failed; NULL authentication not permitted.");
  909. goto error;
  910. }
  911. break;
  912. }
  913. case RPCAP_RMTAUTH_PWD:
  914. {
  915. char *username, *passwd;
  916. uint32 usernamelen, passwdlen;
  917. usernamelen = ntohs(auth.slen1);
  918. username = (char *) malloc (usernamelen + 1);
  919. if (username == NULL)
  920. {
  921. pcap_fmt_errmsg_for_errno(errmsgbuf,
  922. PCAP_ERRBUF_SIZE, errno, "malloc() failed");
  923. goto error;
  924. }
  925. status = rpcapd_recv(pars->sockctrl_in, username, usernamelen, &plen, errmsgbuf);
  926. if (status == -1)
  927. {
  928. free(username);
  929. return -1;
  930. }
  931. if (status == -2)
  932. {
  933. free(username);
  934. goto error;
  935. }
  936. username[usernamelen] = '\0';
  937. passwdlen = ntohs(auth.slen2);
  938. passwd = (char *) malloc (passwdlen + 1);
  939. if (passwd == NULL)
  940. {
  941. pcap_fmt_errmsg_for_errno(errmsgbuf,
  942. PCAP_ERRBUF_SIZE, errno, "malloc() failed");
  943. free(username);
  944. goto error;
  945. }
  946. status = rpcapd_recv(pars->sockctrl_in, passwd, passwdlen, &plen, errmsgbuf);
  947. if (status == -1)
  948. {
  949. free(username);
  950. free(passwd);
  951. return -1;
  952. }
  953. if (status == -2)
  954. {
  955. free(username);
  956. free(passwd);
  957. goto error;
  958. }
  959. passwd[passwdlen] = '\0';
  960. if (daemon_AuthUserPwd(username, passwd, errmsgbuf))
  961. {
  962. //
  963. // Authentication failed. Let the client
  964. // know.
  965. //
  966. free(username);
  967. free(passwd);
  968. if (rpcap_senderror(pars->sockctrl_out,
  969. pars->protocol_version,
  970. PCAP_ERR_AUTH, errmsgbuf, errbuf) == -1)
  971. {
  972. // That failed; log a message and give up.
  973. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  974. return -1;
  975. }
  976. //
  977. // Suspend for 1 second, so that they can't
  978. // hammer us with repeated tries with an
  979. // attack such as a dictionary attack.
  980. //
  981. // WARNING: this delay is inserted only
  982. // at this point; if the client closes the
  983. // connection and reconnects, the suspension
  984. // time does not have any effect.
  985. //
  986. sleep_secs(RPCAP_SUSPEND_WRONGAUTH);
  987. goto error_noreply;
  988. }
  989. free(username);
  990. free(passwd);
  991. break;
  992. }
  993. default:
  994. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Authentication type not recognized.");
  995. goto error;
  996. }
  997. // The authentication succeeded; let the client know.
  998. rpcap_createhdr(&header, pars->protocol_version, RPCAP_MSG_AUTH_REPLY, 0, 0);
  999. // Send the ok message back
  1000. if (sock_send(pars->sockctrl_out, (char *) &header, sizeof (struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE) == -1)
  1001. {
  1002. // That failed; log a messsage and give up.
  1003. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1004. return -1;
  1005. }
  1006. // Check if all the data has been read; if not, discard the data in excess
  1007. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1008. {
  1009. return -1;
  1010. }
  1011. return 0;
  1012. error:
  1013. if (rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1014. PCAP_ERR_AUTH, errmsgbuf, errbuf) == -1)
  1015. {
  1016. // That failed; log a message and give up.
  1017. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1018. return -1;
  1019. }
  1020. error_noreply:
  1021. // Check if all the data has been read; if not, discard the data in excess
  1022. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1023. {
  1024. return -1;
  1025. }
  1026. return -2;
  1027. }
  1028. static int
  1029. daemon_AuthUserPwd(char *username, char *password, char *errbuf)
  1030. {
  1031. #ifdef _WIN32
  1032. /*
  1033. * Warning: the user which launches the process must have the
  1034. * SE_TCB_NAME right.
  1035. * This corresponds to have the "Act as part of the Operating System"
  1036. * turned on (administrative tools, local security settings, local
  1037. * policies, user right assignment)
  1038. * However, it seems to me that if you run it as a service, this
  1039. * right should be provided by default.
  1040. */
  1041. HANDLE Token;
  1042. if (LogonUser(username, ".", password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &Token) == 0)
  1043. {
  1044. int error;
  1045. error = GetLastError();
  1046. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
  1047. PCAP_ERRBUF_SIZE, NULL);
  1048. return -1;
  1049. }
  1050. // This call should change the current thread to the selected user.
  1051. // I didn't test it.
  1052. if (ImpersonateLoggedOnUser(Token) == 0)
  1053. {
  1054. int error;
  1055. error = GetLastError();
  1056. FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
  1057. PCAP_ERRBUF_SIZE, NULL);
  1058. CloseHandle(Token);
  1059. return -1;
  1060. }
  1061. CloseHandle(Token);
  1062. return 0;
  1063. #else
  1064. /*
  1065. * See
  1066. *
  1067. * http://www.unixpapa.com/incnote/passwd.html
  1068. *
  1069. * We use the Solaris/Linux shadow password authentication if
  1070. * we have getspnam(), otherwise we just do traditional
  1071. * authentication, which, on some platforms, might work, even
  1072. * with shadow passwords, if we're running as root. Traditional
  1073. * authenticaion won't work if we're not running as root, as
  1074. * I think these days all UN*Xes either won't return the password
  1075. * at all with getpwnam() or will only do so if you're root.
  1076. *
  1077. * XXX - perhaps what we *should* be using is PAM, if we have
  1078. * it. That might hide all the details of username/password
  1079. * authentication, whether it's done with a visible-to-root-
  1080. * only password database or some other authentication mechanism,
  1081. * behind its API.
  1082. */
  1083. struct passwd *user;
  1084. char *user_password;
  1085. #ifdef HAVE_GETSPNAM
  1086. struct spwd *usersp;
  1087. #endif
  1088. // This call is needed to get the uid
  1089. if ((user = getpwnam(username)) == NULL)
  1090. {
  1091. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed: no such user");
  1092. return -1;
  1093. }
  1094. #ifdef HAVE_GETSPNAM
  1095. // This call is needed to get the password; otherwise 'x' is returned
  1096. if ((usersp = getspnam(username)) == NULL)
  1097. {
  1098. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed: no such user");
  1099. return -1;
  1100. }
  1101. user_password = usersp->sp_pwdp;
  1102. #else
  1103. /*
  1104. * XXX - what about other platforms?
  1105. * The unixpapa.com page claims this Just Works on *BSD if you're
  1106. * running as root - it's from 2000, so it doesn't indicate whether
  1107. * macOS (which didn't come out until 2001, under the name Mac OS
  1108. * X) behaves like the *BSDs or not, and might also work on AIX.
  1109. * HP-UX does something else.
  1110. *
  1111. * Again, hopefully PAM hides all that.
  1112. */
  1113. user_password = user->pw_passwd;
  1114. #endif
  1115. if (strcmp(user_password, (char *) crypt(password, user_password)) != 0)
  1116. {
  1117. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Authentication failed: password incorrect");
  1118. return -1;
  1119. }
  1120. if (setuid(user->pw_uid))
  1121. {
  1122. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1123. errno, "setuid");
  1124. return -1;
  1125. }
  1126. /* if (setgid(user->pw_gid))
  1127. {
  1128. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1129. errno, "setgid");
  1130. return -1;
  1131. }
  1132. */
  1133. return 0;
  1134. #endif
  1135. }
  1136. static int
  1137. daemon_msg_findallif_req(struct daemon_slpars *pars, uint32 plen)
  1138. {
  1139. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  1140. char errmsgbuf[PCAP_ERRBUF_SIZE]; // buffer for errors to send to the client
  1141. char sendbuf[RPCAP_NETBUF_SIZE]; // temporary buffer in which data to be sent is buffered
  1142. int sendbufidx = 0; // index which keeps the number of bytes currently buffered
  1143. pcap_if_t *alldevs = NULL; // pointer to the header of the interface chain
  1144. pcap_if_t *d; // temp pointer needed to scan the interface chain
  1145. struct pcap_addr *address; // pcap structure that keeps a network address of an interface
  1146. struct rpcap_findalldevs_if *findalldevs_if;// rpcap structure that packet all the data of an interface together
  1147. uint16 nif = 0; // counts the number of interface listed
  1148. // Discard the rest of the message; there shouldn't be any payload.
  1149. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1150. {
  1151. // Network error.
  1152. return -1;
  1153. }
  1154. // Retrieve the device list
  1155. if (pcap_findalldevs(&alldevs, errmsgbuf) == -1)
  1156. goto error;
  1157. if (alldevs == NULL)
  1158. {
  1159. if (rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1160. PCAP_ERR_NOREMOTEIF,
  1161. "No interfaces found! Make sure libpcap/WinPcap is properly installed"
  1162. " and you have the right to access to the remote device.",
  1163. errbuf) == -1)
  1164. {
  1165. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1166. return -1;
  1167. }
  1168. return 0;
  1169. }
  1170. // checks the number of interfaces and it computes the total length of the payload
  1171. for (d = alldevs; d != NULL; d = d->next)
  1172. {
  1173. nif++;
  1174. if (d->description)
  1175. plen+= strlen(d->description);
  1176. if (d->name)
  1177. plen+= strlen(d->name);
  1178. plen+= sizeof(struct rpcap_findalldevs_if);
  1179. for (address = d->addresses; address != NULL; address = address->next)
  1180. {
  1181. /*
  1182. * Send only IPv4 and IPv6 addresses over the wire.
  1183. */
  1184. switch (address->addr->sa_family)
  1185. {
  1186. case AF_INET:
  1187. #ifdef AF_INET6
  1188. case AF_INET6:
  1189. #endif
  1190. plen+= (sizeof(struct rpcap_sockaddr) * 4);
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. // RPCAP findalldevs command
  1198. if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
  1199. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf,
  1200. PCAP_ERRBUF_SIZE) == -1)
  1201. goto error;
  1202. rpcap_createhdr((struct rpcap_header *) sendbuf, pars->protocol_version,
  1203. RPCAP_MSG_FINDALLIF_REPLY, nif, plen);
  1204. // send the interface list
  1205. for (d = alldevs; d != NULL; d = d->next)
  1206. {
  1207. uint16 lname, ldescr;
  1208. findalldevs_if = (struct rpcap_findalldevs_if *) &sendbuf[sendbufidx];
  1209. if (sock_bufferize(NULL, sizeof(struct rpcap_findalldevs_if), NULL,
  1210. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1211. goto error;
  1212. memset(findalldevs_if, 0, sizeof(struct rpcap_findalldevs_if));
  1213. if (d->description) ldescr = (short) strlen(d->description);
  1214. else ldescr = 0;
  1215. if (d->name) lname = (short) strlen(d->name);
  1216. else lname = 0;
  1217. findalldevs_if->desclen = htons(ldescr);
  1218. findalldevs_if->namelen = htons(lname);
  1219. findalldevs_if->flags = htonl(d->flags);
  1220. for (address = d->addresses; address != NULL; address = address->next)
  1221. {
  1222. /*
  1223. * Send only IPv4 and IPv6 addresses over the wire.
  1224. */
  1225. switch (address->addr->sa_family)
  1226. {
  1227. case AF_INET:
  1228. #ifdef AF_INET6
  1229. case AF_INET6:
  1230. #endif
  1231. findalldevs_if->naddr++;
  1232. break;
  1233. default:
  1234. break;
  1235. }
  1236. }
  1237. findalldevs_if->naddr = htons(findalldevs_if->naddr);
  1238. if (sock_bufferize(d->name, lname, sendbuf, &sendbufidx,
  1239. RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf,
  1240. PCAP_ERRBUF_SIZE) == -1)
  1241. goto error;
  1242. if (sock_bufferize(d->description, ldescr, sendbuf, &sendbufidx,
  1243. RPCAP_NETBUF_SIZE, SOCKBUF_BUFFERIZE, errmsgbuf,
  1244. PCAP_ERRBUF_SIZE) == -1)
  1245. goto error;
  1246. // send all addresses
  1247. for (address = d->addresses; address != NULL; address = address->next)
  1248. {
  1249. struct rpcap_sockaddr *sockaddr;
  1250. /*
  1251. * Send only IPv4 and IPv6 addresses over the wire.
  1252. */
  1253. switch (address->addr->sa_family)
  1254. {
  1255. case AF_INET:
  1256. #ifdef AF_INET6
  1257. case AF_INET6:
  1258. #endif
  1259. sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
  1260. if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
  1261. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1262. goto error;
  1263. daemon_seraddr((struct sockaddr_storage *) address->addr, sockaddr);
  1264. sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
  1265. if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
  1266. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1267. goto error;
  1268. daemon_seraddr((struct sockaddr_storage *) address->netmask, sockaddr);
  1269. sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
  1270. if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
  1271. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1272. goto error;
  1273. daemon_seraddr((struct sockaddr_storage *) address->broadaddr, sockaddr);
  1274. sockaddr = (struct rpcap_sockaddr *) &sendbuf[sendbufidx];
  1275. if (sock_bufferize(NULL, sizeof(struct rpcap_sockaddr), NULL,
  1276. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1277. goto error;
  1278. daemon_seraddr((struct sockaddr_storage *) address->dstaddr, sockaddr);
  1279. break;
  1280. default:
  1281. break;
  1282. }
  1283. }
  1284. }
  1285. // We no longer need the device list. Free it.
  1286. pcap_freealldevs(alldevs);
  1287. // Send a final command that says "now send it!"
  1288. if (sock_send(pars->sockctrl_out, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
  1289. {
  1290. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1291. return -1;
  1292. }
  1293. return 0;
  1294. error:
  1295. if (alldevs)
  1296. pcap_freealldevs(alldevs);
  1297. if (rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1298. PCAP_ERR_FINDALLIF, errmsgbuf, errbuf) == -1)
  1299. {
  1300. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1301. return -1;
  1302. }
  1303. return 0;
  1304. }
  1305. /*
  1306. \param plen: the length of the current message (needed in order to be able
  1307. to discard excess data in the message, if present)
  1308. */
  1309. static int
  1310. daemon_msg_open_req(struct daemon_slpars *pars, uint32 plen, char *source, size_t sourcelen)
  1311. {
  1312. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  1313. char errmsgbuf[PCAP_ERRBUF_SIZE]; // buffer for errors to send to the client
  1314. pcap_t *fp; // pcap_t main variable
  1315. int nread;
  1316. char sendbuf[RPCAP_NETBUF_SIZE]; // temporary buffer in which data to be sent is buffered
  1317. int sendbufidx = 0; // index which keeps the number of bytes currently buffered
  1318. struct rpcap_openreply *openreply; // open reply message
  1319. if (plen > sourcelen - 1)
  1320. {
  1321. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Source string too long");
  1322. goto error;
  1323. }
  1324. nread = sock_recv(pars->sockctrl_in, source, plen,
  1325. SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf, PCAP_ERRBUF_SIZE);
  1326. if (nread == -1)
  1327. {
  1328. rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
  1329. return -1;
  1330. }
  1331. source[nread] = '\0';
  1332. plen -= nread;
  1333. // XXX - make sure it's *not* a URL; we don't support opening
  1334. // remote devices here.
  1335. // Open the selected device
  1336. // This is a fake open, since we do that only to get the needed parameters, then we close the device again
  1337. if ((fp = pcap_open_live(source,
  1338. 1500 /* fake snaplen */,
  1339. 0 /* no promis */,
  1340. 1000 /* fake timeout */,
  1341. errmsgbuf)) == NULL)
  1342. goto error;
  1343. // Now, I can send a RPCAP open reply message
  1344. if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
  1345. RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1346. goto error;
  1347. rpcap_createhdr((struct rpcap_header *) sendbuf, pars->protocol_version,
  1348. RPCAP_MSG_OPEN_REPLY, 0, sizeof(struct rpcap_openreply));
  1349. openreply = (struct rpcap_openreply *) &sendbuf[sendbufidx];
  1350. if (sock_bufferize(NULL, sizeof(struct rpcap_openreply), NULL, &sendbufidx,
  1351. RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1352. goto error;
  1353. memset(openreply, 0, sizeof(struct rpcap_openreply));
  1354. openreply->linktype = htonl(pcap_datalink(fp));
  1355. openreply->tzoff = 0; /* This is always 0 for live captures */
  1356. // We're done with the pcap_t.
  1357. pcap_close(fp);
  1358. // Send the reply.
  1359. if (sock_send(pars->sockctrl_out, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
  1360. {
  1361. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1362. return -1;
  1363. }
  1364. return 0;
  1365. error:
  1366. if (rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1367. PCAP_ERR_OPEN, errmsgbuf, errbuf) == -1)
  1368. {
  1369. // That failed; log a message and give up.
  1370. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1371. return -1;
  1372. }
  1373. // Check if all the data has been read; if not, discard the data in excess
  1374. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1375. {
  1376. return -1;
  1377. }
  1378. return 0;
  1379. }
  1380. /*
  1381. \param plen: the length of the current message (needed in order to be able
  1382. to discard excess data in the message, if present)
  1383. */
  1384. static int
  1385. daemon_msg_startcap_req(struct daemon_slpars *pars, uint32 plen, struct thread_handle *threaddata, char *source, struct session **sessionp, struct rpcap_sampling *samp_param _U_)
  1386. {
  1387. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  1388. char errmsgbuf[PCAP_ERRBUF_SIZE]; // buffer for errors to send to the client
  1389. char portdata[PCAP_BUF_SIZE]; // temp variable needed to derive the data port
  1390. char peerhost[PCAP_BUF_SIZE]; // temp variable needed to derive the host name of our peer
  1391. struct session *session = NULL; // saves state of session
  1392. int status;
  1393. char sendbuf[RPCAP_NETBUF_SIZE]; // temporary buffer in which data to be sent is buffered
  1394. int sendbufidx = 0; // index which keeps the number of bytes currently buffered
  1395. // socket-related variables
  1396. SOCKET sockdata = INVALID_SOCKET; // socket descriptor of the data connection
  1397. struct addrinfo hints; // temp, needed to open a socket connection
  1398. struct addrinfo *addrinfo; // temp, needed to open a socket connection
  1399. struct sockaddr_storage saddr; // temp, needed to retrieve the network data port chosen on the local machine
  1400. socklen_t saddrlen; // temp, needed to retrieve the network data port chosen on the local machine
  1401. int ret; // return value from functions
  1402. #ifndef _WIN32
  1403. pthread_attr_t detachedAttribute; // temp, needed to set the created thread as detached
  1404. #endif
  1405. // RPCAP-related variables
  1406. struct rpcap_startcapreq startcapreq; // start capture request message
  1407. struct rpcap_startcapreply *startcapreply; // start capture reply message
  1408. int serveropen_dp; // keeps who is going to open the data connection
  1409. addrinfo = NULL;
  1410. status = rpcapd_recv(pars->sockctrl_in, (char *) &startcapreq,
  1411. sizeof(struct rpcap_startcapreq), &plen, errmsgbuf);
  1412. if (status == -1)
  1413. {
  1414. goto fatal_error;
  1415. }
  1416. if (status == -2)
  1417. {
  1418. goto error;
  1419. }
  1420. startcapreq.flags = ntohs(startcapreq.flags);
  1421. // Create a session structure
  1422. session = malloc(sizeof(struct session));
  1423. if (session == NULL)
  1424. {
  1425. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Can't allocate session structure");
  1426. goto error;
  1427. }
  1428. // Open the selected device
  1429. if ((session->fp = pcap_open_live(source,
  1430. ntohl(startcapreq.snaplen),
  1431. (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_PROMISC) ? 1 : 0 /* local device, other flags not needed */,
  1432. ntohl(startcapreq.read_timeout),
  1433. errmsgbuf)) == NULL)
  1434. goto error;
  1435. #if 0
  1436. // Apply sampling parameters
  1437. fp->rmt_samp.method = samp_param->method;
  1438. fp->rmt_samp.value = samp_param->value;
  1439. #endif
  1440. /*
  1441. We're in active mode if:
  1442. - we're using TCP, and the user wants us to be in active mode
  1443. - we're using UDP
  1444. */
  1445. serveropen_dp = (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_SERVEROPEN) || (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM) || pars->isactive;
  1446. /*
  1447. Gets the sockaddr structure referred to the other peer in the ctrl connection
  1448. We need that because:
  1449. - if we're in passive mode, we need to know the address family we want to use
  1450. (the same used for the ctrl socket)
  1451. - if we're in active mode, we need to know the network address of the other host
  1452. we want to connect to
  1453. */
  1454. saddrlen = sizeof(struct sockaddr_storage);
  1455. if (getpeername(pars->sockctrl_in, (struct sockaddr *) &saddr, &saddrlen) == -1)
  1456. {
  1457. sock_geterror("getpeername(): ", errmsgbuf, PCAP_ERRBUF_SIZE);
  1458. goto error;
  1459. }
  1460. memset(&hints, 0, sizeof(struct addrinfo));
  1461. hints.ai_socktype = (startcapreq.flags & RPCAP_STARTCAPREQ_FLAG_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
  1462. hints.ai_family = saddr.ss_family;
  1463. // Now we have to create a new socket to send packets
  1464. if (serveropen_dp) // Data connection is opened by the server toward the client
  1465. {
  1466. pcap_snprintf(portdata, sizeof portdata, "%d", ntohs(startcapreq.portdata));
  1467. // Get the name of the other peer (needed to connect to that specific network address)
  1468. if (getnameinfo((struct sockaddr *) &saddr, saddrlen, peerhost,
  1469. sizeof(peerhost), NULL, 0, NI_NUMERICHOST))
  1470. {
  1471. sock_geterror("getnameinfo(): ", errmsgbuf, PCAP_ERRBUF_SIZE);
  1472. goto error;
  1473. }
  1474. if (sock_initaddress(peerhost, portdata, &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1475. goto error;
  1476. if ((sockdata = sock_open(addrinfo, SOCKOPEN_CLIENT, 0, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
  1477. goto error;
  1478. }
  1479. else // Data connection is opened by the client toward the server
  1480. {
  1481. hints.ai_flags = AI_PASSIVE;
  1482. // Let's the server socket pick up a free network port for us
  1483. if (sock_initaddress(NULL, "0", &hints, &addrinfo, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1484. goto error;
  1485. if ((sockdata = sock_open(addrinfo, SOCKOPEN_SERVER, 1 /* max 1 connection in queue */, errmsgbuf, PCAP_ERRBUF_SIZE)) == INVALID_SOCKET)
  1486. goto error;
  1487. // get the complete sockaddr structure used in the data connection
  1488. saddrlen = sizeof(struct sockaddr_storage);
  1489. if (getsockname(sockdata, (struct sockaddr *) &saddr, &saddrlen) == -1)
  1490. {
  1491. sock_geterror("getsockname(): ", errmsgbuf, PCAP_ERRBUF_SIZE);
  1492. goto error;
  1493. }
  1494. // Get the local port the system picked up
  1495. if (getnameinfo((struct sockaddr *) &saddr, saddrlen, NULL,
  1496. 0, portdata, sizeof(portdata), NI_NUMERICSERV))
  1497. {
  1498. sock_geterror("getnameinfo(): ", errmsgbuf, PCAP_ERRBUF_SIZE);
  1499. goto error;
  1500. }
  1501. }
  1502. // addrinfo is no longer used
  1503. freeaddrinfo(addrinfo);
  1504. addrinfo = NULL;
  1505. // Needed to send an error on the ctrl connection
  1506. session->sockctrl_out = pars->sockctrl_out;
  1507. session->protocol_version = pars->protocol_version;
  1508. // Now I can set the filter
  1509. ret = daemon_unpackapplyfilter(pars->sockctrl_in, session, &plen, errmsgbuf);
  1510. if (ret == -1)
  1511. {
  1512. // Fatal error. A message has been logged; just give up.
  1513. goto fatal_error;
  1514. }
  1515. if (ret == -2)
  1516. {
  1517. // Non-fatal error. Send an error message to the client.
  1518. goto error;
  1519. }
  1520. // Now, I can send a RPCAP start capture reply message
  1521. if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL, &sendbufidx,
  1522. RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1523. goto error;
  1524. rpcap_createhdr((struct rpcap_header *) sendbuf, pars->protocol_version,
  1525. RPCAP_MSG_STARTCAP_REPLY, 0, sizeof(struct rpcap_startcapreply));
  1526. startcapreply = (struct rpcap_startcapreply *) &sendbuf[sendbufidx];
  1527. if (sock_bufferize(NULL, sizeof(struct rpcap_startcapreply), NULL,
  1528. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1529. goto error;
  1530. memset(startcapreply, 0, sizeof(struct rpcap_startcapreply));
  1531. startcapreply->bufsize = htonl(pcap_bufsize(session->fp));
  1532. if (!serveropen_dp)
  1533. {
  1534. unsigned short port = (unsigned short)strtoul(portdata,NULL,10);
  1535. startcapreply->portdata = htons(port);
  1536. }
  1537. if (sock_send(pars->sockctrl_out, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
  1538. {
  1539. // That failed; log a message and give up.
  1540. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1541. goto fatal_error;
  1542. }
  1543. if (!serveropen_dp)
  1544. {
  1545. SOCKET socktemp; // We need another socket, since we're going to accept() a connection
  1546. // Connection creation
  1547. saddrlen = sizeof(struct sockaddr_storage);
  1548. socktemp = accept(sockdata, (struct sockaddr *) &saddr, &saddrlen);
  1549. if (socktemp == INVALID_SOCKET)
  1550. {
  1551. sock_geterror("accept(): ", errbuf, PCAP_ERRBUF_SIZE);
  1552. rpcapd_log(LOGPRIO_ERROR, "Accept of data connection failed: %s",
  1553. errbuf);
  1554. goto error;
  1555. }
  1556. // Now that I accepted the connection, the server socket is no longer needed
  1557. sock_close(sockdata, NULL, 0);
  1558. sockdata = socktemp;
  1559. }
  1560. session->sockdata = sockdata;
  1561. // Now we have to create a new thread to receive packets
  1562. #ifdef _WIN32
  1563. threaddata->thread = (HANDLE)_beginthreadex(NULL, 0, daemon_thrdatamain,
  1564. (void *) session, 0, NULL);
  1565. if (threaddata->thread == 0)
  1566. {
  1567. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error creating the data thread");
  1568. goto error;
  1569. }
  1570. #else
  1571. /* GV we need this to create the thread as detached. */
  1572. /* GV otherwise, the thread handle is not destroyed */
  1573. pthread_attr_init(&detachedAttribute);
  1574. pthread_attr_setdetachstate(&detachedAttribute, PTHREAD_CREATE_DETACHED);
  1575. ret = pthread_create(&threaddata->thread, &detachedAttribute,
  1576. daemon_thrdatamain, (void *) session);
  1577. if (ret != 0)
  1578. {
  1579. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1580. ret, "Error creating the data thread");
  1581. pthread_attr_destroy(&detachedAttribute);
  1582. goto error;
  1583. }
  1584. pthread_attr_destroy(&detachedAttribute);
  1585. #endif
  1586. threaddata->have_thread = 1;
  1587. // Check if all the data has been read; if not, discard the data in excess
  1588. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1589. goto fatal_error;
  1590. *sessionp = session;
  1591. return 0;
  1592. error:
  1593. //
  1594. // Not a fatal error, so send the client an error message and
  1595. // keep serving client requests.
  1596. //
  1597. *sessionp = NULL;
  1598. if (addrinfo)
  1599. freeaddrinfo(addrinfo);
  1600. if (threaddata->have_thread)
  1601. {
  1602. #ifdef _WIN32
  1603. if (session->fp)
  1604. {
  1605. pcap_breakloop(session->fp);
  1606. SetEvent(pcap_getevent(session->fp));
  1607. }
  1608. CloseHandle(threaddata->thread);
  1609. #else
  1610. pthread_cancel(threaddata->thread);
  1611. #endif
  1612. threaddata->have_thread = 0;
  1613. }
  1614. if (sockdata != INVALID_SOCKET)
  1615. sock_close(sockdata, NULL, 0);
  1616. if (session)
  1617. {
  1618. if (session->fp)
  1619. pcap_close(session->fp);
  1620. free(session);
  1621. }
  1622. if (rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1623. PCAP_ERR_STARTCAPTURE, errmsgbuf, errbuf) == -1)
  1624. {
  1625. // That failed; log a message and give up.
  1626. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1627. return -1;
  1628. }
  1629. // Check if all the data has been read; if not, discard the data in excess
  1630. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1631. {
  1632. // Network error.
  1633. return -1;
  1634. }
  1635. return 0;
  1636. fatal_error:
  1637. //
  1638. // Fatal network error, so don't try to communicate with
  1639. // the client, just give up.
  1640. //
  1641. *sessionp = NULL;
  1642. if (threaddata->have_thread)
  1643. {
  1644. #ifdef _WIN32
  1645. if (session && session->fp)
  1646. {
  1647. //
  1648. // Tell the data connection thread main capture
  1649. // loop to break out of that loop.
  1650. //
  1651. pcap_breakloop(session->fp);
  1652. //
  1653. // If it's currently blocked waiting for packets
  1654. // to arrive, try to wake it up, so it can see
  1655. // the "break out of the loop" indication.
  1656. //
  1657. SetEvent(pcap_getevent(session->fp));
  1658. }
  1659. //
  1660. // Wait for the thread to exit, so we don't close
  1661. // sockets out from under it.
  1662. //
  1663. // XXX - have a timeout, so we don't wait forever?
  1664. //
  1665. WaitForSingleObject(threaddata->thread, INFINITE);
  1666. //
  1667. // Release the thread handle, as we're done with
  1668. // it.
  1669. //
  1670. CloseHandle(threaddata->thread);
  1671. #else
  1672. pthread_cancel(threaddata->thread);
  1673. #endif
  1674. threaddata->have_thread = 0;
  1675. }
  1676. if (sockdata != INVALID_SOCKET)
  1677. sock_close(sockdata, NULL, 0);
  1678. if (session)
  1679. {
  1680. if (session->fp)
  1681. pcap_close(session->fp);
  1682. free(session);
  1683. }
  1684. return -1;
  1685. }
  1686. static int
  1687. daemon_msg_endcap_req(struct daemon_slpars *pars, struct session *session, struct thread_handle *threaddata)
  1688. {
  1689. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  1690. struct rpcap_header header;
  1691. if (threaddata->have_thread)
  1692. {
  1693. #ifdef _WIN32
  1694. //
  1695. // Tell the data connection thread main capture loop to
  1696. // break out of that loop.
  1697. //
  1698. pcap_breakloop(session->fp);
  1699. //
  1700. // If it's currently blocked waiting for packets to
  1701. // arrive, try to wake it up, so it can see the "break
  1702. // out of the loop" indication.
  1703. //
  1704. SetEvent(pcap_getevent(session->fp));
  1705. //
  1706. // Wait for the thread to exit, so we don't close
  1707. // sockets out from under it.
  1708. //
  1709. // XXX - have a timeout, so we don't wait forever?
  1710. //
  1711. WaitForSingleObject(threaddata->thread, INFINITE);
  1712. //
  1713. // Release the thread handle, as we're done with
  1714. // it.
  1715. //
  1716. CloseHandle(threaddata->thread);
  1717. #else
  1718. pthread_cancel(threaddata->thread);
  1719. #endif
  1720. threaddata->have_thread = 0;
  1721. }
  1722. if (session->sockdata)
  1723. {
  1724. sock_close(session->sockdata, NULL, 0);
  1725. session->sockdata = 0;
  1726. }
  1727. pcap_close(session->fp);
  1728. rpcap_createhdr(&header, pars->protocol_version,
  1729. RPCAP_MSG_ENDCAP_REPLY, 0, 0);
  1730. if (sock_send(pars->sockctrl_out, (char *) &header, sizeof(struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE) == -1)
  1731. {
  1732. // That failed; log a message and give up.
  1733. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1734. return -1;
  1735. }
  1736. return 0;
  1737. }
  1738. static int
  1739. daemon_unpackapplyfilter(SOCKET sockctrl_in, struct session *session, uint32 *plenp, char *errmsgbuf)
  1740. {
  1741. int status;
  1742. struct rpcap_filter filter;
  1743. struct rpcap_filterbpf_insn insn;
  1744. struct bpf_insn *bf_insn;
  1745. struct bpf_program bf_prog;
  1746. unsigned int i;
  1747. status = rpcapd_recv(sockctrl_in, (char *) &filter,
  1748. sizeof(struct rpcap_filter), plenp, errmsgbuf);
  1749. if (status == -1)
  1750. {
  1751. return -1;
  1752. }
  1753. if (status == -2)
  1754. {
  1755. return -2;
  1756. }
  1757. bf_prog.bf_len = ntohl(filter.nitems);
  1758. if (ntohs(filter.filtertype) != RPCAP_UPDATEFILTER_BPF)
  1759. {
  1760. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Only BPF/NPF filters are currently supported");
  1761. return -2;
  1762. }
  1763. bf_insn = (struct bpf_insn *) malloc (sizeof(struct bpf_insn) * bf_prog.bf_len);
  1764. if (bf_insn == NULL)
  1765. {
  1766. pcap_fmt_errmsg_for_errno(errmsgbuf, PCAP_ERRBUF_SIZE,
  1767. errno, "malloc() failed");
  1768. return -2;
  1769. }
  1770. bf_prog.bf_insns = bf_insn;
  1771. for (i = 0; i < bf_prog.bf_len; i++)
  1772. {
  1773. status = rpcapd_recv(sockctrl_in, (char *) &insn,
  1774. sizeof(struct rpcap_filterbpf_insn), plenp, errmsgbuf);
  1775. if (status == -1)
  1776. {
  1777. return -1;
  1778. }
  1779. if (status == -2)
  1780. {
  1781. return -2;
  1782. }
  1783. bf_insn->code = ntohs(insn.code);
  1784. bf_insn->jf = insn.jf;
  1785. bf_insn->jt = insn.jt;
  1786. bf_insn->k = ntohl(insn.k);
  1787. bf_insn++;
  1788. }
  1789. if (bpf_validate(bf_prog.bf_insns, bf_prog.bf_len) == 0)
  1790. {
  1791. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "The filter contains bogus instructions");
  1792. return -2;
  1793. }
  1794. if (pcap_setfilter(session->fp, &bf_prog))
  1795. {
  1796. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "RPCAP error: %s", pcap_geterr(session->fp));
  1797. return -2;
  1798. }
  1799. return 0;
  1800. }
  1801. static int
  1802. daemon_msg_updatefilter_req(struct daemon_slpars *pars, struct session *session, uint32 plen)
  1803. {
  1804. char errbuf[PCAP_ERRBUF_SIZE];
  1805. char errmsgbuf[PCAP_ERRBUF_SIZE]; // buffer for errors to send to the client
  1806. int ret; // status of daemon_unpackapplyfilter()
  1807. struct rpcap_header header; // keeps the answer to the updatefilter command
  1808. ret = daemon_unpackapplyfilter(pars->sockctrl_in, session, &plen, errmsgbuf);
  1809. if (ret == -1)
  1810. {
  1811. // Fatal error. A message has been logged; just give up.
  1812. return -1;
  1813. }
  1814. if (ret == -2)
  1815. {
  1816. // Non-fatal error. Send an error reply to the client.
  1817. goto error;
  1818. }
  1819. // Check if all the data has been read; if not, discard the data in excess
  1820. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1821. {
  1822. // Network error.
  1823. return -1;
  1824. }
  1825. // A response is needed, otherwise the other host does not know that everything went well
  1826. rpcap_createhdr(&header, pars->protocol_version,
  1827. RPCAP_MSG_UPDATEFILTER_REPLY, 0, 0);
  1828. if (sock_send(pars->sockctrl_out, (char *) &header, sizeof (struct rpcap_header), pcap_geterr(session->fp), PCAP_ERRBUF_SIZE))
  1829. {
  1830. // That failed; log a messsage and give up.
  1831. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1832. return -1;
  1833. }
  1834. return 0;
  1835. error:
  1836. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1837. {
  1838. return -1;
  1839. }
  1840. rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1841. PCAP_ERR_UPDATEFILTER, errmsgbuf, NULL);
  1842. return 0;
  1843. }
  1844. /*!
  1845. \brief Received the sampling parameters from remote host and it stores in the pcap_t structure.
  1846. */
  1847. static int
  1848. daemon_msg_setsampling_req(struct daemon_slpars *pars, uint32 plen, struct rpcap_sampling *samp_param)
  1849. {
  1850. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  1851. char errmsgbuf[PCAP_ERRBUF_SIZE];
  1852. struct rpcap_header header;
  1853. struct rpcap_sampling rpcap_samp;
  1854. int status;
  1855. status = rpcapd_recv(pars->sockctrl_in, (char *) &rpcap_samp, sizeof(struct rpcap_sampling), &plen, errmsgbuf);
  1856. if (status == -1)
  1857. {
  1858. return -1;
  1859. }
  1860. if (status == -2)
  1861. {
  1862. goto error;
  1863. }
  1864. // Save these settings in the pcap_t
  1865. samp_param->method = rpcap_samp.method;
  1866. samp_param->value = ntohl(rpcap_samp.value);
  1867. // A response is needed, otherwise the other host does not know that everything went well
  1868. rpcap_createhdr(&header, pars->protocol_version,
  1869. RPCAP_MSG_SETSAMPLING_REPLY, 0, 0);
  1870. if (sock_send(pars->sockctrl_out, (char *) &header, sizeof (struct rpcap_header), errbuf, PCAP_ERRBUF_SIZE) == -1)
  1871. {
  1872. // That failed; log a messsage and give up.
  1873. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1874. return -1;
  1875. }
  1876. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1877. {
  1878. return -1;
  1879. }
  1880. return 0;
  1881. error:
  1882. if (rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1883. PCAP_ERR_AUTH, errmsgbuf, errbuf) == -1)
  1884. {
  1885. // That failed; log a message and give up.
  1886. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1887. return -1;
  1888. }
  1889. // Check if all the data has been read; if not, discard the data in excess
  1890. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1891. {
  1892. return -1;
  1893. }
  1894. return 0;
  1895. }
  1896. static int
  1897. daemon_msg_stats_req(struct daemon_slpars *pars, struct session *session, uint32 plen, struct pcap_stat *stats, unsigned int svrcapt)
  1898. {
  1899. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  1900. char errmsgbuf[PCAP_ERRBUF_SIZE]; // buffer for errors to send to the client
  1901. char sendbuf[RPCAP_NETBUF_SIZE]; // temporary buffer in which data to be sent is buffered
  1902. int sendbufidx = 0; // index which keeps the number of bytes currently buffered
  1903. struct rpcap_stats *netstats; // statistics sent on the network
  1904. // Checks that the header does not contain other data; if so, discard it
  1905. if (rpcapd_discard(pars->sockctrl_in, plen) == -1)
  1906. {
  1907. // Network error.
  1908. return -1;
  1909. }
  1910. if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
  1911. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1912. goto error;
  1913. rpcap_createhdr((struct rpcap_header *) sendbuf, pars->protocol_version,
  1914. RPCAP_MSG_STATS_REPLY, 0, (uint16) sizeof(struct rpcap_stats));
  1915. netstats = (struct rpcap_stats *) &sendbuf[sendbufidx];
  1916. if (sock_bufferize(NULL, sizeof(struct rpcap_stats), NULL,
  1917. &sendbufidx, RPCAP_NETBUF_SIZE, SOCKBUF_CHECKONLY, errmsgbuf, PCAP_ERRBUF_SIZE) == -1)
  1918. goto error;
  1919. if (session && session->fp)
  1920. {
  1921. if (pcap_stats(session->fp, stats) == -1)
  1922. {
  1923. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "%s", pcap_geterr(session->fp));
  1924. goto error;
  1925. }
  1926. netstats->ifdrop = htonl(stats->ps_ifdrop);
  1927. netstats->ifrecv = htonl(stats->ps_recv);
  1928. netstats->krnldrop = htonl(stats->ps_drop);
  1929. netstats->svrcapt = htonl(session->TotCapt);
  1930. }
  1931. else
  1932. {
  1933. // We have to keep compatibility with old applications,
  1934. // which ask for statistics also when the capture has
  1935. // already stopped.
  1936. netstats->ifdrop = htonl(stats->ps_ifdrop);
  1937. netstats->ifrecv = htonl(stats->ps_recv);
  1938. netstats->krnldrop = htonl(stats->ps_drop);
  1939. netstats->svrcapt = htonl(svrcapt);
  1940. }
  1941. // Send the packet
  1942. if (sock_send(pars->sockctrl_out, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE) == -1)
  1943. {
  1944. rpcapd_log(LOGPRIO_ERROR, "Send to client failed: %s", errbuf);
  1945. return -1;
  1946. }
  1947. return 0;
  1948. error:
  1949. rpcap_senderror(pars->sockctrl_out, pars->protocol_version,
  1950. PCAP_ERR_GETSTATS, errmsgbuf, NULL);
  1951. return 0;
  1952. }
  1953. #ifdef _WIN32
  1954. static unsigned __stdcall
  1955. #else
  1956. static void *
  1957. #endif
  1958. daemon_thrdatamain(void *ptr)
  1959. {
  1960. char errbuf[PCAP_ERRBUF_SIZE + 1]; // error buffer
  1961. struct session *session; // pointer to the struct session for this session
  1962. int retval; // general variable used to keep the return value of other functions
  1963. struct rpcap_pkthdr *net_pkt_header;// header of the packet
  1964. struct pcap_pkthdr *pkt_header; // pointer to the buffer that contains the header of the current packet
  1965. u_char *pkt_data; // pointer to the buffer that contains the current packet
  1966. size_t sendbufsize; // size for the send buffer
  1967. char *sendbuf; // temporary buffer in which data to be sent is buffered
  1968. int sendbufidx; // index which keeps the number of bytes currently buffered
  1969. int status;
  1970. session = (struct session *) ptr;
  1971. session->TotCapt = 0; // counter which is incremented each time a packet is received
  1972. // Initialize errbuf
  1973. memset(errbuf, 0, sizeof(errbuf));
  1974. //
  1975. // We need a buffer large enough to hold a buffer large enough
  1976. // for a maximum-size packet for this pcap_t.
  1977. //
  1978. if (pcap_snapshot(session->fp) < 0)
  1979. {
  1980. //
  1981. // The snapshot length is negative.
  1982. // This "should not happen".
  1983. //
  1984. rpcapd_log(LOGPRIO_ERROR,
  1985. "Unable to allocate the buffer for this child thread: snapshot length of %d is negative",
  1986. pcap_snapshot(session->fp));
  1987. sendbuf = NULL; // we can't allocate a buffer, so nothing to free
  1988. goto error;
  1989. }
  1990. //
  1991. // size_t is unsigned, and the result of pcap_snapshot() is signed;
  1992. // on no platform that we support is int larger than size_t.
  1993. // This means that, unless the extra information we prepend to
  1994. // a maximum-sized packet is impossibly large, the sum of the
  1995. // snapshot length and the size of that extra information will
  1996. // fit in a size_t.
  1997. //
  1998. // So we don't need to make sure that sendbufsize will overflow.
  1999. //
  2000. sendbufsize = sizeof(struct rpcap_header) + sizeof(struct rpcap_pkthdr) + pcap_snapshot(session->fp);
  2001. sendbuf = (char *) malloc (sendbufsize);
  2002. if (sendbuf == NULL)
  2003. {
  2004. rpcapd_log(LOGPRIO_ERROR,
  2005. "Unable to allocate the buffer for this child thread");
  2006. goto error;
  2007. }
  2008. #ifndef _WIN32
  2009. // Modify thread params so that it can be killed at any time
  2010. retval = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  2011. if (retval != 0)
  2012. {
  2013. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  2014. retval, "pthread_setcancelstate");
  2015. rpcapd_log(LOGPRIO_ERROR,
  2016. "Can't set cancel state on data thread: %s", errbuf);
  2017. goto error;
  2018. }
  2019. retval = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  2020. if (retval != 0)
  2021. {
  2022. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  2023. retval, "pthread_setcanceltype");
  2024. rpcapd_log(LOGPRIO_ERROR,
  2025. "Can't set cancel type on data thread: %s", errbuf);
  2026. goto error;
  2027. }
  2028. #endif
  2029. // Retrieve the packets
  2030. while ((retval = pcap_next_ex(session->fp, &pkt_header, (const u_char **) &pkt_data)) >= 0) // cast to avoid a compiler warning
  2031. {
  2032. if (retval == 0) // Read timeout elapsed
  2033. continue;
  2034. sendbufidx = 0;
  2035. // Bufferize the general header
  2036. if (sock_bufferize(NULL, sizeof(struct rpcap_header), NULL,
  2037. &sendbufidx, sendbufsize, SOCKBUF_CHECKONLY, errbuf,
  2038. PCAP_ERRBUF_SIZE) == -1)
  2039. {
  2040. rpcapd_log(LOGPRIO_ERROR,
  2041. "sock_bufferize() error sending packet message: %s",
  2042. errbuf);
  2043. goto error;
  2044. }
  2045. rpcap_createhdr((struct rpcap_header *) sendbuf,
  2046. session->protocol_version, RPCAP_MSG_PACKET, 0,
  2047. (uint16) (sizeof(struct rpcap_pkthdr) + pkt_header->caplen));
  2048. net_pkt_header = (struct rpcap_pkthdr *) &sendbuf[sendbufidx];
  2049. // Bufferize the pkt header
  2050. if (sock_bufferize(NULL, sizeof(struct rpcap_pkthdr), NULL,
  2051. &sendbufidx, sendbufsize, SOCKBUF_CHECKONLY, errbuf,
  2052. PCAP_ERRBUF_SIZE) == -1)
  2053. {
  2054. rpcapd_log(LOGPRIO_ERROR,
  2055. "sock_bufferize() error sending packet message: %s",
  2056. errbuf);
  2057. goto error;
  2058. }
  2059. net_pkt_header->caplen = htonl(pkt_header->caplen);
  2060. net_pkt_header->len = htonl(pkt_header->len);
  2061. net_pkt_header->npkt = htonl(++(session->TotCapt));
  2062. net_pkt_header->timestamp_sec = htonl(pkt_header->ts.tv_sec);
  2063. net_pkt_header->timestamp_usec = htonl(pkt_header->ts.tv_usec);
  2064. // Bufferize the pkt data
  2065. if (sock_bufferize((char *) pkt_data, pkt_header->caplen,
  2066. sendbuf, &sendbufidx, sendbufsize, SOCKBUF_BUFFERIZE,
  2067. errbuf, PCAP_ERRBUF_SIZE) == -1)
  2068. {
  2069. rpcapd_log(LOGPRIO_ERROR,
  2070. "sock_bufferize() error sending packet message: %s",
  2071. errbuf);
  2072. goto error;
  2073. }
  2074. // Send the packet
  2075. // If the client dropped the connection, don't report an
  2076. // error, just quit.
  2077. status = sock_send(session->sockdata, sendbuf, sendbufidx, errbuf, PCAP_ERRBUF_SIZE);
  2078. if (status < 0)
  2079. {
  2080. if (status == -1)
  2081. {
  2082. //
  2083. // Error other than "client closed the
  2084. // connection out from under us"; report
  2085. // it.
  2086. //
  2087. rpcapd_log(LOGPRIO_ERROR,
  2088. "Send of packet to client failed: %s",
  2089. errbuf);
  2090. }
  2091. //
  2092. // Give up in either case.
  2093. //
  2094. goto error;
  2095. }
  2096. }
  2097. if (retval == -1)
  2098. {
  2099. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "Error reading the packets: %s", pcap_geterr(session->fp));
  2100. rpcap_senderror(session->sockctrl_out, session->protocol_version,
  2101. PCAP_ERR_READEX, errbuf, NULL);
  2102. goto error;
  2103. }
  2104. error:
  2105. closesocket(session->sockdata);
  2106. session->sockdata = 0;
  2107. free(sendbuf);
  2108. return 0;
  2109. }
  2110. /*!
  2111. \brief It serializes a network address.
  2112. It accepts a 'sockaddr_storage' structure as input, and it converts it appropriately into a format
  2113. that can be used to be sent on the network. Basically, it applies all the hton()
  2114. conversion required to the input variable.
  2115. \param sockaddrin a 'sockaddr_storage' pointer to the variable that has to be
  2116. serialized. This variable can be both a 'sockaddr_in' and 'sockaddr_in6'.
  2117. \param sockaddrout an 'rpcap_sockaddr' pointer to the variable that will contain
  2118. the serialized data. This variable has to be allocated by the user.
  2119. \warning This function supports only AF_INET and AF_INET6 address families.
  2120. */
  2121. static void
  2122. daemon_seraddr(struct sockaddr_storage *sockaddrin, struct rpcap_sockaddr *sockaddrout)
  2123. {
  2124. memset(sockaddrout, 0, sizeof(struct sockaddr_storage));
  2125. // There can be the case in which the sockaddrin is not available
  2126. if (sockaddrin == NULL) return;
  2127. // Warning: we support only AF_INET and AF_INET6
  2128. switch (sockaddrin->ss_family)
  2129. {
  2130. case AF_INET:
  2131. {
  2132. struct sockaddr_in *sockaddrin_ipv4;
  2133. struct rpcap_sockaddr_in *sockaddrout_ipv4;
  2134. sockaddrin_ipv4 = (struct sockaddr_in *) sockaddrin;
  2135. sockaddrout_ipv4 = (struct rpcap_sockaddr_in *) sockaddrout;
  2136. sockaddrout_ipv4->family = htons(RPCAP_AF_INET);
  2137. sockaddrout_ipv4->port = htons(sockaddrin_ipv4->sin_port);
  2138. memcpy(&sockaddrout_ipv4->addr, &sockaddrin_ipv4->sin_addr, sizeof(sockaddrout_ipv4->addr));
  2139. memset(sockaddrout_ipv4->zero, 0, sizeof(sockaddrout_ipv4->zero));
  2140. break;
  2141. }
  2142. #ifdef AF_INET6
  2143. case AF_INET6:
  2144. {
  2145. struct sockaddr_in6 *sockaddrin_ipv6;
  2146. struct rpcap_sockaddr_in6 *sockaddrout_ipv6;
  2147. sockaddrin_ipv6 = (struct sockaddr_in6 *) sockaddrin;
  2148. sockaddrout_ipv6 = (struct rpcap_sockaddr_in6 *) sockaddrout;
  2149. sockaddrout_ipv6->family = htons(RPCAP_AF_INET6);
  2150. sockaddrout_ipv6->port = htons(sockaddrin_ipv6->sin6_port);
  2151. sockaddrout_ipv6->flowinfo = htonl(sockaddrin_ipv6->sin6_flowinfo);
  2152. memcpy(&sockaddrout_ipv6->addr, &sockaddrin_ipv6->sin6_addr, sizeof(sockaddrout_ipv6->addr));
  2153. sockaddrout_ipv6->scope_id = htonl(sockaddrin_ipv6->sin6_scope_id);
  2154. break;
  2155. }
  2156. #endif
  2157. }
  2158. }
  2159. /*!
  2160. \brief Suspends a thread for secs seconds.
  2161. */
  2162. void sleep_secs(int secs)
  2163. {
  2164. #ifdef _WIN32
  2165. Sleep(secs*1000);
  2166. #else
  2167. unsigned secs_remaining;
  2168. if (secs <= 0)
  2169. return;
  2170. secs_remaining = secs;
  2171. while (secs_remaining != 0)
  2172. secs_remaining = sleep(secs_remaining);
  2173. #endif
  2174. }
  2175. /*
  2176. * Read the header of a message.
  2177. */
  2178. static int
  2179. rpcapd_recv_msg_header(SOCKET sock, struct rpcap_header *headerp)
  2180. {
  2181. int nread;
  2182. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  2183. nread = sock_recv(sock, (char *) headerp, sizeof(struct rpcap_header),
  2184. SOCK_RECEIVEALL_YES|SOCK_EOF_ISNT_ERROR, errbuf, PCAP_ERRBUF_SIZE);
  2185. if (nread == -1)
  2186. {
  2187. // Network error.
  2188. rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
  2189. return -1;
  2190. }
  2191. if (nread == 0)
  2192. {
  2193. // Immediate EOF; that's treated like a close message.
  2194. return -2;
  2195. }
  2196. headerp->plen = ntohl(headerp->plen);
  2197. return 0;
  2198. }
  2199. /*
  2200. * Read data from a message.
  2201. * If we're trying to read more data that remains, puts an error
  2202. * message into errmsgbuf and returns -2. Otherwise, tries to read
  2203. * the data and, if that succeeds, subtracts the amount read from
  2204. * the number of bytes of data that remains.
  2205. * Returns 0 on success, logs a message and returns -1 on a network
  2206. * error.
  2207. */
  2208. static int
  2209. rpcapd_recv(SOCKET sock, char *buffer, size_t toread, uint32 *plen, char *errmsgbuf)
  2210. {
  2211. int nread;
  2212. char errbuf[PCAP_ERRBUF_SIZE]; // buffer for network errors
  2213. if (toread > *plen)
  2214. {
  2215. // Tell the client and continue.
  2216. pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Message payload is too short");
  2217. return -2;
  2218. }
  2219. nread = sock_recv(sock, buffer, toread,
  2220. SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf, PCAP_ERRBUF_SIZE);
  2221. if (nread == -1)
  2222. {
  2223. rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
  2224. return -1;
  2225. }
  2226. *plen -= nread;
  2227. return 0;
  2228. }
  2229. /*
  2230. * Discard data from a connection.
  2231. * Mostly used to discard wrong-sized messages.
  2232. * Returns 0 on success, logs a message and returns -1 on a network
  2233. * error.
  2234. */
  2235. static int
  2236. rpcapd_discard(SOCKET sock, uint32 len)
  2237. {
  2238. char errbuf[PCAP_ERRBUF_SIZE + 1]; // keeps the error string, prior to be printed
  2239. if (len != 0)
  2240. {
  2241. if (sock_discard(sock, len, errbuf, PCAP_ERRBUF_SIZE) == -1)
  2242. {
  2243. // Network error.
  2244. rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
  2245. return -1;
  2246. }
  2247. }
  2248. return 0;
  2249. }