mainrelay.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 Citrix Systems
  3. *
  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. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include "mainrelay.h"
  31. #include "dbdrivers/dbdriver.h"
  32. #include "prom_server.h"
  33. #if defined(WINDOWS)
  34. #include <iphlpapi.h>
  35. #define WORKING_BUFFER_SIZE 15000
  36. #define MAX_TRIES 3
  37. #endif
  38. #if (!defined OPENSSL_VERSION_1_1_1)
  39. #define OPENSSL_VERSION_1_1_1 0x10101000L
  40. #endif
  41. ////// TEMPORARY data //////////
  42. static int use_lt_credentials = 0;
  43. static int anon_credentials = 0;
  44. // long term credential
  45. static int use_ltc = 0;
  46. // timelimited long term credential
  47. static int use_tltc = 0;
  48. ////// ALPN //////////
  49. #if ALPN_SUPPORTED
  50. char STUN_ALPN[128] = "stun.nat-discovery";
  51. char TURN_ALPN[128] = "stun.turn";
  52. char HTTP_ALPN[128] = "http/1.1";
  53. #endif
  54. ////// TURNDB //////////////
  55. #if defined(Q)
  56. #undef Q
  57. #endif
  58. #define Q(x) #x
  59. #if defined(QUOTE)
  60. #undef QUOTE
  61. #endif
  62. #define QUOTE(x) Q(x)
  63. #define DEFAULT_USERDB_FILE QUOTE(TURNDB)
  64. //////TURN PARAMS STRUCTURE DEFINITION //////
  65. #define DEFAULT_GENERAL_RELAY_SERVERS_NUMBER (1)
  66. turn_params_t turn_params = {
  67. //////////////// OpenSSL group //////////////////////
  68. NULL, /* tls_ctx */
  69. NULL, /* dtls_ctx */
  70. DH_2066, /*dh_key_size*/
  71. "", /*cipher_list*/
  72. "", /*ec_curve_name*/
  73. "", /*ca_cert_file*/
  74. "turn_server_cert.pem", /*cert_file*/
  75. "turn_server_pkey.pem", /*pkey_file*/
  76. "", /*tls_password*/
  77. "", /*dh_file*/
  78. 0, /*no_tlsv1*/
  79. 0, /*no_tlsv1_1*/
  80. 0, /*no_tlsv1_2*/
  81. /*no_tls*/
  82. #if !TLS_SUPPORTED
  83. 1,
  84. #else
  85. 0,
  86. #endif
  87. /*no_dtls*/
  88. #if !DTLS_SUPPORTED
  89. 1,
  90. #else
  91. 0,
  92. #endif
  93. NULL, /*tls_ctx_update_ev*/
  94. {0, NULL}, /*tls_mutex*/
  95. //////////////// Common params ////////////////////
  96. TURN_VERBOSE_NONE, /* verbose */
  97. 0, /* turn_daemon */
  98. 0, /* no_software_attribute */
  99. 0, /* web_admin_listen_on_workers */
  100. 0, /* do_not_use_config_file */
  101. "/var/run/turnserver.pid", /* pidfile */
  102. "", /* acme_redirect */
  103. //////////////// Listener server /////////////////
  104. DEFAULT_STUN_PORT, /* listener_port*/
  105. DEFAULT_STUN_TLS_PORT, /* tls_listener_port */
  106. 0, /* alt_listener_port */
  107. 0, /* alt_tls_listener_port */
  108. 0, /* tcp_proxy_port */
  109. 1, /* rfc5780 */
  110. 0, /* no_udp */
  111. 0, /* no_tcp */
  112. 0, /* tcp_use_proxy */
  113. 0, /* no_tcp_relay */
  114. 0, /* no_udp_relay */
  115. "", /*listener_ifname*/
  116. {"", ""}, /*redis_statsdb*/
  117. 0, /*use_redis_statsdb*/
  118. {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, NULL, NULL, NULL}, /*listener*/
  119. {NULL, 0}, /*ip_whitelist*/
  120. {NULL, 0}, /*ip_blacklist*/
  121. NEV_UNKNOWN, /*net_engine_version*/
  122. {"Unknown", "UDP listening socket per session", "UDP thread per network endpoint",
  123. "UDP thread per CPU core"}, /*net_engine_version_txt*/
  124. //////////////// Relay servers //////////////////////////////////
  125. LOW_DEFAULT_PORTS_BOUNDARY, /*min_port*/
  126. HIGH_DEFAULT_PORTS_BOUNDARY, /*max_port*/
  127. 0, /*check_origin*/
  128. 0, /*no_multicast_peers*/
  129. 0, /*allow_loopback_peers*/
  130. "", /*relay_ifname*/
  131. 0, /*relays_number*/
  132. NULL, /*relay_addrs*/
  133. 0, /*default_relays*/
  134. NULL, /*external_ip*/
  135. DEFAULT_GENERAL_RELAY_SERVERS_NUMBER, /*general_relay_servers_number*/
  136. 0, /*udp_relay_servers_number*/
  137. ////////////// Auth server /////////////////////////////////////
  138. "",
  139. "",
  140. 0,
  141. /////////////// AUX SERVERS ////////////////
  142. {NULL, 0, {0, NULL}}, /*aux_servers_list*/
  143. 0, /*udp_self_balance*/
  144. /////////////// ALTERNATE SERVERS ////////////////
  145. {NULL, 0, {0, NULL}}, /*alternate_servers_list*/
  146. {NULL, 0, {0, NULL}}, /*tls_alternate_servers_list*/
  147. /////////////// stop server ////////////////
  148. 0, /*stop_turn_server*/
  149. /////////////// MISC PARAMS ////////////////
  150. 0, /* stun_only */
  151. 0, /* no_stun */
  152. 0, /* secure_stun */
  153. 0, /* server_relay */
  154. 0, /* fingerprint */
  155. ':', /* rest_api_separator */
  156. STUN_DEFAULT_NONCE_EXPIRATION_TIME, /* stale_nonce */
  157. STUN_DEFAULT_MAX_ALLOCATE_LIFETIME, /* max_allocate_lifetime */
  158. STUN_DEFAULT_CHANNEL_LIFETIME, /* channel_lifetime */
  159. STUN_DEFAULT_PERMISSION_LIFETIME, /* permission_lifetime */
  160. 0, /* mobility */
  161. TURN_CREDENTIALS_NONE, /* ct */
  162. 0, /* use_auth_secret_with_timestamp */
  163. 0, /* max_bps */
  164. 0, /* bps_capacity */
  165. 0, /* bps_capacity_allocated */
  166. 0, /* total_quota */
  167. 0, /* user_quota */
  168. 0, /* prometheus disabled by default */
  169. DEFAULT_PROM_SERVER_PORT, /* prometheus port */
  170. 0, /* prometheus username labelling disabled by default when prometheus is enabled */
  171. ///////////// Users DB //////////////
  172. {(TURN_USERDB_TYPE)0, {"\0", "\0"}, {0, NULL, {NULL, 0}}},
  173. ///////////// CPUs //////////////////
  174. DEFAULT_CPUS_NUMBER,
  175. ///////// Encryption /////////
  176. "", /* secret_key_file */
  177. "", /* secret_key */
  178. ALLOCATION_DEFAULT_ADDRESS_FAMILY_IPV4, /* allocation_default_address_family */
  179. 0, /* no_auth_pings */
  180. 0, /* no_dynamic_ip_list */
  181. 0, /* no_dynamic_realms */
  182. 0, /* log_binding */
  183. 0, /* no_stun_backward_compatibility */
  184. 0, /* response_origin_only_with_rfc5780 */
  185. 0 /* respond_http_unsupported */
  186. };
  187. //////////////// OpenSSL Init //////////////////////
  188. static void openssl_setup(void);
  189. /*
  190. * openssl genrsa -out pkey 2048
  191. * openssl req -new -key pkey -out cert.req
  192. * openssl x509 -req -days 365 -in cert.req -signkey pkey -out cert
  193. *
  194. */
  195. //////////// Common static process params ////////
  196. #if defined(WINDOWS)
  197. // TODO: implement it!!!
  198. #else
  199. static gid_t procgroupid = 0;
  200. static uid_t procuserid = 0;
  201. static gid_t procgroupid_set = 0;
  202. static uid_t procuserid_set = 0;
  203. static char procusername[1025] = "\0";
  204. static char procgroupname[1025] = "\0";
  205. #endif
  206. ////////////// Configuration functionality ////////////////////////////////
  207. static void read_config_file(int argc, char **argv, int pass);
  208. static void reload_ssl_certs(evutil_socket_t sock, short events, void *args);
  209. static void shutdown_handler(evutil_socket_t sock, short events, void *args);
  210. //////////////////////////////////////////////////
  211. static int make_local_listeners_list(void) {
  212. int ret = 0;
  213. #if defined(WINDOWS)
  214. DWORD dwSize = 0;
  215. DWORD dwRetVal = 0;
  216. unsigned int i = 0;
  217. // Set the flags to pass to GetAdaptersAddresses
  218. ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
  219. // default to unspecified address family (both)
  220. ULONG family = AF_UNSPEC;
  221. LPVOID lpMsgBuf = NULL;
  222. PIP_ADAPTER_ADDRESSES pAddresses = NULL;
  223. ULONG outBufLen = 0;
  224. ULONG Iterations = 0;
  225. PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
  226. PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
  227. PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
  228. PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
  229. IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = NULL;
  230. IP_ADAPTER_PREFIX *pPrefix = NULL;
  231. // Allocate a 15 KB buffer to start with.
  232. outBufLen = WORKING_BUFFER_SIZE;
  233. do {
  234. pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
  235. if (pAddresses == NULL) {
  236. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
  237. return -1;
  238. }
  239. dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
  240. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  241. free(pAddresses);
  242. pAddresses = NULL;
  243. } else {
  244. break;
  245. }
  246. Iterations++;
  247. } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES));
  248. if (dwRetVal == NO_ERROR) {
  249. // If successful, output some information from the data we received
  250. pCurrAddresses = pAddresses;
  251. while (pCurrAddresses) {
  252. /*
  253. printf("\tLength of the IP_ADAPTER_ADDRESS struct: %ld\n",
  254. pCurrAddresses->Length);
  255. printf("\tIfIndex (IPv4 interface): %u\n", pCurrAddresses->IfIndex);
  256. printf("\tAdapter name: %s\n", pCurrAddresses->AdapterName);//*/
  257. pUnicast = pCurrAddresses->FirstUnicastAddress;
  258. if (pUnicast != NULL) {
  259. // printf("\tNumber of Unicast Addresses:\n");
  260. for (i = 0; pUnicast != NULL; pUnicast = pUnicast->Next) {
  261. char saddr[INET6_ADDRSTRLEN] = "";
  262. if (AF_INET == pUnicast->Address.lpSockaddr->sa_family) // IPV4
  263. {
  264. if (!inet_ntop(PF_INET, &((struct sockaddr_in *)pUnicast->Address.lpSockaddr)->sin_addr, saddr,
  265. INET6_ADDRSTRLEN)) {
  266. continue;
  267. }
  268. if (strstr(saddr, "169.254.") == saddr) {
  269. continue;
  270. }
  271. if (!strcmp(saddr, "0.0.0.0")) {
  272. continue;
  273. }
  274. } else if (AF_INET6 == pUnicast->Address.lpSockaddr->sa_family) // IPV6
  275. {
  276. if (!inet_ntop(PF_INET6, &((struct sockaddr_in6 *)pUnicast->Address.lpSockaddr)->sin6_addr, saddr,
  277. INET6_ADDRSTRLEN)) {
  278. continue;
  279. }
  280. if (strstr(saddr, "fe80") == saddr) {
  281. continue;
  282. }
  283. if (!strcmp(saddr, "::")) {
  284. continue;
  285. }
  286. } else {
  287. continue;
  288. }
  289. // printf("\t\tIP: %s\n", saddr);
  290. add_listener_addr(saddr);
  291. if (MIB_IF_TYPE_LOOPBACK != pCurrAddresses->IfType) {
  292. ret++;
  293. }
  294. }
  295. }
  296. /*
  297. else
  298. printf("\tNo Unicast Addresses\n");
  299. pAnycast = pCurrAddresses->FirstAnycastAddress;
  300. if (pAnycast) {
  301. for (i = 0; pAnycast != NULL; i++)
  302. pAnycast = pAnycast->Next;
  303. printf("\tNumber of Anycast Addresses: %d\n", i);
  304. }
  305. else
  306. printf("\tNo Anycast Addresses\n");
  307. pMulticast = pCurrAddresses->FirstMulticastAddress;
  308. if (pMulticast) {
  309. for (i = 0; pMulticast != NULL; i++)
  310. pMulticast = pMulticast->Next;
  311. printf("\tNumber of Multicast Addresses: %d\n", i);
  312. }
  313. else
  314. printf("\tNo Multicast Addresses\n");
  315. pDnServer = pCurrAddresses->FirstDnsServerAddress;
  316. if (pDnServer) {
  317. for (i = 0; pDnServer != NULL; i++)
  318. pDnServer = pDnServer->Next;
  319. printf("\tNumber of DNS Server Addresses: %d\n", i);
  320. }
  321. else
  322. printf("\tNo DNS Server Addresses\n");
  323. printf("\tDNS Suffix: %wS\n", pCurrAddresses->DnsSuffix);
  324. printf("\tDescription: %wS\n", pCurrAddresses->Description);
  325. printf("\tFriendly name: %wS\n", pCurrAddresses->FriendlyName);
  326. if (pCurrAddresses->PhysicalAddressLength != 0) {
  327. printf("\tPhysical address: ");
  328. for (i = 0; i < (int)pCurrAddresses->PhysicalAddressLength;
  329. i++) {
  330. if (i == (pCurrAddresses->PhysicalAddressLength - 1))
  331. printf("%.2X\n",
  332. (int)pCurrAddresses->PhysicalAddress[i]);
  333. else
  334. printf("%.2X-",
  335. (int)pCurrAddresses->PhysicalAddress[i]);
  336. }
  337. }
  338. printf("\tFlags: %ld\n", pCurrAddresses->Flags);
  339. printf("\tMtu: %lu\n", pCurrAddresses->Mtu);
  340. char* pType = NULL;
  341. switch (pCurrAddresses->IfType)
  342. {
  343. case MIB_IF_TYPE_ETHERNET:
  344. pType = "ETHERNET";
  345. break;
  346. case MIB_IF_TYPE_PPP:
  347. pType = "PPP";
  348. break;
  349. case MIB_IF_TYPE_LOOPBACK:
  350. pType = "LOOPBACK";
  351. break;
  352. case MIB_IF_TYPE_SLIP:
  353. pType = "ATM";
  354. break;
  355. case IF_TYPE_IEEE80211:
  356. pType = "WIFI";
  357. break;
  358. }
  359. printf("\tIfType: %ld (%s)\n", pCurrAddresses->IfType, pType);
  360. printf("\tOperStatus: %ld\n", pCurrAddresses->OperStatus);
  361. printf("\tIpv6IfIndex (IPv6 interface): %u\n",
  362. pCurrAddresses->Ipv6IfIndex);
  363. printf("\tZoneIndices (hex): ");
  364. for (i = 0; i < 16; i++)
  365. printf("%lx ", pCurrAddresses->ZoneIndices[i]);
  366. printf("\n");
  367. printf("\tTransmit link speed: %I64u\n", pCurrAddresses->TransmitLinkSpeed);
  368. printf("\tReceive link speed: %I64u\n", pCurrAddresses->ReceiveLinkSpeed);
  369. pPrefix = pCurrAddresses->FirstPrefix;
  370. if (pPrefix) {
  371. for (i = 0; pPrefix != NULL; i++)
  372. pPrefix = pPrefix->Next;
  373. printf("\tNumber of IP Adapter Prefix entries: %d\n", i);
  374. }
  375. else
  376. printf("\tNumber of IP Adapter Prefix entries: 0\n");
  377. printf("\n");//*/
  378. pCurrAddresses = pCurrAddresses->Next;
  379. }
  380. } else {
  381. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Call to GetAdaptersAddresses failed with error: %d\n", dwRetVal);
  382. if (dwRetVal == ERROR_NO_DATA) {
  383. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "\tNo addresses were found for the requested parameters\n");
  384. } else {
  385. if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  386. NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  387. // Default language
  388. (LPTSTR)&lpMsgBuf, 0, NULL)) {
  389. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "\tError: %s", lpMsgBuf);
  390. LocalFree(lpMsgBuf);
  391. if (pAddresses) {
  392. free(pAddresses);
  393. }
  394. return -2;
  395. }
  396. }
  397. }
  398. if (pAddresses) {
  399. free(pAddresses);
  400. }
  401. #else
  402. struct ifaddrs *ifs = NULL;
  403. struct ifaddrs *ifa = NULL;
  404. char saddr[INET6_ADDRSTRLEN] = "";
  405. if ((getifaddrs(&ifs) == 0) && ifs) {
  406. for (ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) {
  407. if (!(ifa->ifa_flags & IFF_UP)) {
  408. continue;
  409. }
  410. if (!(ifa->ifa_addr)) {
  411. continue;
  412. }
  413. if (ifa->ifa_addr->sa_family == AF_INET) {
  414. if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) {
  415. continue;
  416. }
  417. if (strstr(saddr, "169.254.") == saddr) {
  418. continue;
  419. }
  420. if (!strcmp(saddr, "0.0.0.0")) {
  421. continue;
  422. }
  423. } else if (ifa->ifa_addr->sa_family == AF_INET6) {
  424. if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) {
  425. continue;
  426. }
  427. if (strstr(saddr, "fe80") == saddr) {
  428. continue;
  429. }
  430. if (!strcmp(saddr, "::")) {
  431. continue;
  432. }
  433. } else {
  434. continue;
  435. }
  436. add_listener_addr(saddr);
  437. if (!(ifa->ifa_flags & IFF_LOOPBACK)) {
  438. ret++;
  439. }
  440. }
  441. freeifaddrs(ifs);
  442. }
  443. #endif
  444. return ret;
  445. }
  446. static int make_local_relays_list(int allow_local, int family) {
  447. int counter = 0;
  448. #if defined(WINDOWS)
  449. DWORD dwRetVal = 0;
  450. // Set the flags to pass to GetAdaptersAddresses
  451. ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
  452. // default to unspecified address family (both)
  453. ULONG fm = AF_UNSPEC;
  454. LPVOID lpMsgBuf = NULL;
  455. PIP_ADAPTER_ADDRESSES pAddresses = NULL;
  456. ULONG outBufLen = 0;
  457. ULONG Iterations = 0;
  458. PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
  459. PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
  460. // Allocate a 15 KB buffer to start with.
  461. outBufLen = WORKING_BUFFER_SIZE;
  462. do {
  463. pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
  464. if (pAddresses == NULL) {
  465. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
  466. return -1;
  467. }
  468. dwRetVal = GetAdaptersAddresses(fm, flags, NULL, pAddresses, &outBufLen);
  469. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  470. free(pAddresses);
  471. pAddresses = NULL;
  472. } else {
  473. break;
  474. }
  475. Iterations++;
  476. } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES));
  477. if (dwRetVal == NO_ERROR) {
  478. // If successful, output some information from the data we received
  479. pCurrAddresses = pAddresses;
  480. while (pCurrAddresses) {
  481. /*
  482. printf("\tLength of the IP_ADAPTER_ADDRESS struct: %ld\n",
  483. pCurrAddresses->Length);
  484. printf("\tIfIndex (IPv4 interface): %u\n", pCurrAddresses->IfIndex);
  485. printf("\tAdapter name: %s\n", pCurrAddresses->AdapterName);//*/
  486. pUnicast = pCurrAddresses->FirstUnicastAddress;
  487. if (pUnicast != NULL) {
  488. // printf("\tNumber of Unicast Addresses:\n");
  489. for (; pUnicast != NULL; pUnicast = pUnicast->Next) {
  490. if (!allow_local && (MIB_IF_TYPE_LOOPBACK == pCurrAddresses->IfType)) {
  491. continue;
  492. }
  493. char saddr[INET6_ADDRSTRLEN] = "";
  494. if (AF_INET == pUnicast->Address.lpSockaddr->sa_family) // IPV4
  495. {
  496. if (family != AF_INET) {
  497. continue;
  498. }
  499. if (!inet_ntop(PF_INET, &((struct sockaddr_in *)pUnicast->Address.lpSockaddr)->sin_addr, saddr,
  500. INET6_ADDRSTRLEN)) {
  501. continue;
  502. }
  503. if (strstr(saddr, "169.254.") == saddr) {
  504. continue;
  505. }
  506. if (!strcmp(saddr, "0.0.0.0")) {
  507. continue;
  508. }
  509. } else if (AF_INET6 == pUnicast->Address.lpSockaddr->sa_family) // IPV6
  510. {
  511. if (family != AF_INET6) {
  512. continue;
  513. }
  514. if (!inet_ntop(PF_INET6, &((struct sockaddr_in6 *)pUnicast->Address.lpSockaddr)->sin6_addr, saddr,
  515. INET6_ADDRSTRLEN)) {
  516. continue;
  517. }
  518. if (strstr(saddr, "fe80") == saddr) {
  519. continue;
  520. }
  521. if (!strcmp(saddr, "::")) {
  522. continue;
  523. }
  524. } else {
  525. continue;
  526. }
  527. if (add_relay_addr(saddr) > 0) {
  528. counter += 1;
  529. }
  530. }
  531. }
  532. pCurrAddresses = pCurrAddresses->Next;
  533. }
  534. }
  535. if (pAddresses) {
  536. free(pAddresses);
  537. }
  538. #else
  539. struct ifaddrs *ifs = NULL;
  540. struct ifaddrs *ifa = NULL;
  541. char saddr[INET6_ADDRSTRLEN] = "";
  542. getifaddrs(&ifs);
  543. if (ifs) {
  544. for (ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) {
  545. if (!(ifa->ifa_flags & IFF_UP)) {
  546. continue;
  547. }
  548. if (!(ifa->ifa_name)) {
  549. continue;
  550. }
  551. if (!(ifa->ifa_addr)) {
  552. continue;
  553. }
  554. if (!allow_local && (ifa->ifa_flags & IFF_LOOPBACK)) {
  555. continue;
  556. }
  557. if (ifa->ifa_addr->sa_family == AF_INET) {
  558. if (family != AF_INET) {
  559. continue;
  560. }
  561. if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) {
  562. continue;
  563. }
  564. if (strstr(saddr, "169.254.") == saddr) {
  565. continue;
  566. }
  567. if (!strcmp(saddr, "0.0.0.0")) {
  568. continue;
  569. }
  570. } else if (ifa->ifa_addr->sa_family == AF_INET6) {
  571. if (family != AF_INET6) {
  572. continue;
  573. }
  574. if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) {
  575. continue;
  576. }
  577. if (strstr(saddr, "fe80") == saddr) {
  578. continue;
  579. }
  580. if (!strcmp(saddr, "::")) {
  581. continue;
  582. }
  583. } else {
  584. continue;
  585. }
  586. if (add_relay_addr(saddr) > 0) {
  587. counter += 1;
  588. }
  589. }
  590. freeifaddrs(ifs);
  591. }
  592. #endif
  593. return counter;
  594. }
  595. int get_a_local_relay(int family, ioa_addr *relay_addr) {
  596. int ret = -1;
  597. int allow_local = 0;
  598. #if defined(WINDOWS)
  599. DWORD dwRetVal = 0;
  600. // Set the flags to pass to GetAdaptersAddresses
  601. ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
  602. // default to unspecified address family (both)
  603. ULONG fm = AF_UNSPEC;
  604. LPVOID lpMsgBuf = NULL;
  605. PIP_ADAPTER_ADDRESSES pAddresses = NULL;
  606. ULONG outBufLen = 0;
  607. ULONG Iterations = 0;
  608. PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
  609. PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
  610. outBufLen = WORKING_BUFFER_SIZE;
  611. do {
  612. pAddresses = (IP_ADAPTER_ADDRESSES *)malloc(outBufLen);
  613. if (pAddresses == NULL) {
  614. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Memory allocation failed for IP_ADAPTER_ADDRESSES struct\n");
  615. return -1;
  616. }
  617. dwRetVal = GetAdaptersAddresses(fm, flags, NULL, pAddresses, &outBufLen);
  618. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  619. free(pAddresses);
  620. pAddresses = NULL;
  621. } else {
  622. break;
  623. }
  624. Iterations++;
  625. } while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES));
  626. if (dwRetVal == NO_ERROR) {
  627. galr_start:
  628. // If successful, output some information from the data we received
  629. pCurrAddresses = pAddresses;
  630. while (pCurrAddresses) {
  631. pUnicast = pCurrAddresses->FirstUnicastAddress;
  632. if (pUnicast != NULL) {
  633. // printf("\tNumber of Unicast Addresses:\n");
  634. for (; pUnicast != NULL; pUnicast = pUnicast->Next) {
  635. if (!allow_local && (MIB_IF_TYPE_LOOPBACK == pCurrAddresses->IfType)) {
  636. continue;
  637. }
  638. char saddr[INET6_ADDRSTRLEN] = "";
  639. if (AF_INET == pUnicast->Address.lpSockaddr->sa_family) // IPV4
  640. {
  641. if (family != AF_INET) {
  642. continue;
  643. }
  644. if (!inet_ntop(PF_INET, &((struct sockaddr_in *)pUnicast->Address.lpSockaddr)->sin_addr, saddr,
  645. INET6_ADDRSTRLEN)) {
  646. continue;
  647. }
  648. if (strstr(saddr, "169.254.") == saddr) {
  649. continue;
  650. }
  651. if (!strcmp(saddr, "0.0.0.0")) {
  652. continue;
  653. }
  654. } else if (AF_INET6 == pUnicast->Address.lpSockaddr->sa_family) // IPV6
  655. {
  656. if (family != AF_INET6) {
  657. continue;
  658. }
  659. if (!inet_ntop(PF_INET6, &((struct sockaddr_in6 *)pUnicast->Address.lpSockaddr)->sin6_addr, saddr,
  660. INET6_ADDRSTRLEN)) {
  661. continue;
  662. }
  663. if (strstr(saddr, "fe80") == saddr) {
  664. continue;
  665. }
  666. if (!strcmp(saddr, "::")) {
  667. continue;
  668. }
  669. } else {
  670. continue;
  671. }
  672. if (make_ioa_addr((const uint8_t *)saddr, 0, relay_addr) < 0) {
  673. continue;
  674. } else {
  675. ret = 0;
  676. break;
  677. }
  678. }
  679. }
  680. pCurrAddresses = pCurrAddresses->Next;
  681. }
  682. if (ret < 0 && !allow_local) {
  683. allow_local = 1;
  684. goto galr_start;
  685. }
  686. }
  687. if (pAddresses) {
  688. free(pAddresses);
  689. }
  690. #else
  691. struct ifaddrs *ifs = NULL;
  692. char saddr[INET6_ADDRSTRLEN] = "";
  693. getifaddrs(&ifs);
  694. if (ifs) {
  695. galr_start:
  696. for (struct ifaddrs *ifa = ifs; ifa != NULL; ifa = ifa->ifa_next) {
  697. if (!(ifa->ifa_flags & IFF_UP)) {
  698. continue;
  699. }
  700. if (!(ifa->ifa_name)) {
  701. continue;
  702. }
  703. if (!(ifa->ifa_addr)) {
  704. continue;
  705. }
  706. if (!allow_local && (ifa->ifa_flags & IFF_LOOPBACK)) {
  707. continue;
  708. }
  709. if (ifa->ifa_addr->sa_family == AF_INET) {
  710. if (family != AF_INET) {
  711. continue;
  712. }
  713. if (!inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, saddr, INET_ADDRSTRLEN)) {
  714. continue;
  715. }
  716. if (strstr(saddr, "169.254.") == saddr) {
  717. continue;
  718. }
  719. if (!strcmp(saddr, "0.0.0.0")) {
  720. continue;
  721. }
  722. } else if (ifa->ifa_addr->sa_family == AF_INET6) {
  723. if (family != AF_INET6) {
  724. continue;
  725. }
  726. if (!inet_ntop(AF_INET6, &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, saddr, INET6_ADDRSTRLEN)) {
  727. continue;
  728. }
  729. if (strstr(saddr, "fe80") == saddr) {
  730. continue;
  731. }
  732. if (!strcmp(saddr, "::")) {
  733. continue;
  734. }
  735. } else {
  736. continue;
  737. }
  738. if (make_ioa_addr((const uint8_t *)saddr, 0, relay_addr) < 0) {
  739. continue;
  740. } else {
  741. ret = 0;
  742. break;
  743. }
  744. }
  745. if (ret < 0 && !allow_local) {
  746. allow_local = 1;
  747. goto galr_start;
  748. }
  749. freeifaddrs(ifs);
  750. }
  751. return -1;
  752. #endif
  753. }
  754. //////////////////////////////////////////////////
  755. static char Usage[] =
  756. "Usage: turnserver [options]\n"
  757. "Options:\n"
  758. " -d, --listening-device <device-name> Listener interface device (NOT RECOMMENDED. Optional, Linux "
  759. "only).\n"
  760. " -p, --listening-port <port> TURN listener port (Default: 3478).\n"
  761. " Note: actually, TLS & DTLS sessions can connect to the \"plain\" TCP & "
  762. "UDP port(s), too,\n"
  763. " if allowed by configuration.\n"
  764. " --tls-listening-port <port> TURN listener port for TLS & DTLS listeners\n"
  765. " (Default: 5349).\n"
  766. " Note: actually, \"plain\" TCP & UDP sessions can connect to the TLS & "
  767. "DTLS port(s), too,\n"
  768. " if allowed by configuration. The TURN server\n"
  769. " \"automatically\" recognizes the type of traffic. Actually, two "
  770. "listening\n"
  771. " endpoints (the \"plain\" one and the \"tls\" one) are equivalent in "
  772. "terms of\n"
  773. " functionality; but we keep both endpoints to satisfy the RFC 5766 "
  774. "specs.\n"
  775. " For secure TCP connections, we currently support SSL version 3 and\n"
  776. " TLS versions 1.0, 1.1 and 1.2. For secure UDP connections, we support\n"
  777. " DTLS version 1.\n"
  778. " --alt-listening-port<port> <port> Alternative listening port for STUN CHANGE_REQUEST (in RFC "
  779. "5780 sense, \n"
  780. " or in old RFC 3489 sense, default is \"listening port plus "
  781. "one\").\n"
  782. " --alt-tls-listening-port <port> Alternative listening port for TLS and DTLS,\n"
  783. " the default is \"TLS/DTLS port plus one\".\n"
  784. " --tcp-proxy-port <port> Support connections from TCP loadbalancer on this port. The "
  785. "loadbalancer should\n"
  786. " use the binary proxy protocol "
  787. "(https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt)\n"
  788. " -L, --listening-ip <ip> Listener IP address of relay server. Multiple listeners can be "
  789. "specified.\n"
  790. " --aux-server <ip:port> Auxiliary STUN/TURN server listening endpoint.\n"
  791. " Auxiliary servers do not have alternative ports and\n"
  792. " they do not support RFC 5780 functionality (CHANGE REQUEST).\n"
  793. " Valid formats are 1.2.3.4:5555 for IPv4 and [1:2::3:4]:5555 for IPv6.\n"
  794. " --udp-self-balance (recommended for older Linuxes only) Automatically balance UDP "
  795. "traffic\n"
  796. " over auxiliary servers (if configured).\n"
  797. " The load balancing is using the ALTERNATE-SERVER mechanism.\n"
  798. " The TURN client must support 300 ALTERNATE-SERVER response for this "
  799. "functionality.\n"
  800. " -i, --relay-device <device-name> Relay interface device for relay sockets (NOT RECOMMENDED. "
  801. "Optional, Linux only).\n"
  802. " -E, --relay-ip <ip> Relay address (the local IP address that will be used to relay "
  803. "the\n"
  804. " packets to the peer).\n"
  805. " Multiple relay addresses may be used.\n"
  806. " The same IP(s) can be used as both listening IP(s) and relay IP(s).\n"
  807. " If no relay IP(s) specified, then the turnserver will apply the "
  808. "default\n"
  809. " policy: it will decide itself which relay addresses to be used, and "
  810. "it\n"
  811. " will always be using the client socket IP address as the relay IP "
  812. "address\n"
  813. " of the TURN session (if the requested relay address family is the "
  814. "same\n"
  815. " as the family of the client socket).\n"
  816. " -X, --external-ip <public-ip[/private-ip]> TURN Server public/private address mapping, if the server is "
  817. "behind NAT.\n"
  818. " In that situation, if a -X is used in form \"-X ip\" then that ip will "
  819. "be reported\n"
  820. " as relay IP address of all allocations. This scenario works only in a "
  821. "simple case\n"
  822. " when one single relay address is be used, and no STUN CHANGE_REQUEST\n"
  823. " functionality is required.\n"
  824. " That single relay address must be mapped by NAT to the 'external' IP.\n"
  825. " For that 'external' IP, NAT must forward ports directly (relayed port "
  826. "12345\n"
  827. " must be always mapped to the same 'external' port 12345).\n"
  828. " In more complex case when more than one IP address is involved,\n"
  829. " that option must be used several times in the command line, each entry "
  830. "must\n"
  831. " have form \"-X public-ip/private-ip\", to map all involved addresses.\n"
  832. " --allow-loopback-peers Allow peers on the loopback addresses (127.x.x.x and ::1).\n"
  833. " --no-multicast-peers Disallow peers on well-known broadcast addresses (224.0.0.0 "
  834. "and above, and FFXX:*).\n"
  835. " -m, --relay-threads <number> Number of relay threads to handle the established connections\n"
  836. " (in addition to authentication thread and the listener thread).\n"
  837. " If explicitly set to 0 then application runs in single-threaded mode.\n"
  838. " If not set then a default OS-dependent optimal algorithm will be "
  839. "employed.\n"
  840. " The default thread number is the number of CPUs.\n"
  841. " In older systems (pre-Linux 3.9) the number of UDP relay threads "
  842. "always equals\n"
  843. " the number of listening endpoints (unless -m 0 is set).\n"
  844. " --min-port <port> Lower bound of the UDP port range for relay endpoints "
  845. "allocation.\n"
  846. " Default value is 49152, according to RFC 5766.\n"
  847. " --max-port <port> Upper bound of the UDP port range for relay endpoints "
  848. "allocation.\n"
  849. " Default value is 65535, according to RFC 5766.\n"
  850. " -v, --verbose 'Moderate' verbose mode.\n"
  851. " -V, --Verbose Extra verbose mode, very annoying (for debug purposes only).\n"
  852. " -o, --daemon Start process as daemon (detach from current shell).\n"
  853. " --no-software-attribute Production mode: hide the software version (formerly --prod).\n"
  854. " -f, --fingerprint Use fingerprints in the TURN messages.\n"
  855. " -a, --lt-cred-mech Use the long-term credential mechanism.\n"
  856. " -z, --no-auth Do not use any credential mechanism, allow anonymous access.\n"
  857. " -u, --user <user:pwd> User account, in form 'username:password', for long-term "
  858. "credentials.\n"
  859. " Cannot be used with TURN REST API.\n"
  860. " -r, --realm <realm> The default realm to be used for the users when no explicit\n"
  861. " origin/realm relationship was found in the database.\n"
  862. " Must be used with long-term credentials \n"
  863. " mechanism or with TURN REST API.\n"
  864. " --check-origin-consistency The flag that sets the origin consistency check:\n"
  865. " across the session, all requests must have the same\n"
  866. " main ORIGIN attribute value (if the ORIGIN was\n"
  867. " initially used by the session).\n"
  868. " -q, --user-quota <number> Per-user allocation quota: how many concurrent allocations a user can "
  869. "create.\n"
  870. " This option can also be set through the database, for a particular "
  871. "realm.\n"
  872. " -Q, --total-quota <number> Total allocations quota: global limit on concurrent allocations.\n"
  873. " This option can also be set through the database, for a particular "
  874. "realm.\n"
  875. " -s, --max-bps <number> Default max bytes-per-second bandwidth a TURN session is "
  876. "allowed to handle\n"
  877. " (input and output network streams are treated separately). Anything "
  878. "above\n"
  879. " that limit will be dropped or temporary suppressed\n"
  880. " (within the available buffer limits).\n"
  881. " This option can also be set through the database, for a particular "
  882. "realm.\n"
  883. " -B, --bps-capacity <number> Maximum server capacity.\n"
  884. " Total bytes-per-second bandwidth the TURN server is allowed to "
  885. "allocate\n"
  886. " for the sessions, combined (input and output network streams are "
  887. "treated separately).\n"
  888. " -c <filename> Configuration file name (default - turnserver.conf).\n"
  889. #if !defined(TURN_NO_SQLITE)
  890. " -b, , --db, --userdb <filename> SQLite database file name; default - /var/db/turndb or\n"
  891. " /usr/local/var/db/turndb or /var/lib/turn/turndb.\n"
  892. #endif
  893. #if !defined(TURN_NO_PQ)
  894. " -e, --psql-userdb, --sql-userdb <conn-string> PostgreSQL database connection string, if used (default - "
  895. "empty, no PostgreSQL DB used).\n"
  896. " This database can be used for long-term credentials mechanism users,\n"
  897. " and it can store the secret value(s) for secret-based timed "
  898. "authentication in TURN REST API.\n"
  899. " See http://www.postgresql.org/docs/8.4/static/libpq-connect.html for "
  900. "8.x PostgreSQL\n"
  901. " versions format, see \n"
  902. " "
  903. "http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNSTRING\n"
  904. " for 9.x and newer connection string formats.\n"
  905. #endif
  906. #if !defined(TURN_NO_MYSQL)
  907. " -M, --mysql-userdb <connection-string> MySQL database connection string, if used (default - empty, no "
  908. "MySQL DB used).\n"
  909. " This database can be used for long-term credentials mechanism users,\n"
  910. " and it can store the secret value(s) for secret-based timed "
  911. "authentication in TURN REST API.\n"
  912. " The connection string my be space-separated list of parameters:\n"
  913. " \"host=<ip-addr> dbname=<database-name> user=<database-user> "
  914. "\\\n password=<database-user-password> port=<db-port> "
  915. "connect_timeout=<seconds> read_timeout=<seconds>\".\n\n"
  916. " The connection string parameters for the secure communications (SSL):\n"
  917. " ca, capath, cert, key, cipher\n"
  918. " (see http://dev.mysql.com/doc/refman/5.1/en/ssl-options.html for the\n"
  919. " command options description).\n\n"
  920. " All connection-string parameters are optional.\n\n"
  921. " --secret-key-file <filename> This is the file path which contain secret key of aes encryption while "
  922. "using MySQL password encryption.\n"
  923. " If you want to use in the MySQL connection string the password in "
  924. "encrypted format,\n"
  925. " then set in this option the file path of the secret key. The key which "
  926. "is used to encrypt MySQL password.\n"
  927. " Warning: If this option is set, then MySQL password must be set in "
  928. "\"mysql-userdb\" option in encrypted format!\n"
  929. " If you want to use cleartext password then do not set this option!\n"
  930. #endif
  931. #if !defined(TURN_NO_MONGO)
  932. " -J, --mongo-userdb <connection-string> MongoDB connection string, if used (default - empty, no "
  933. "MongoDB used).\n"
  934. " This database can be used for long-term credentials mechanism users,\n"
  935. " and it can store the secret value(s) for secret-based timed "
  936. "authentication in TURN REST API.\n"
  937. #endif
  938. #if !defined(TURN_NO_HIREDIS)
  939. " -N, --redis-userdb <connection-string> Redis user database connection string, if used (default - "
  940. "empty, no Redis DB used).\n"
  941. " This database can be used for long-term credentials mechanism users,\n"
  942. " and it can store the secret value(s) for secret-based timed "
  943. "authentication in TURN REST API.\n"
  944. " The connection string my be space-separated list of parameters:\n"
  945. " \"host=<ip-addr> dbname=<db-number> \\\n "
  946. " password=<database-user-password> port=<db-port> "
  947. "connect_timeout=<seconds>\".\n\n"
  948. " All connection-string parameters are optional.\n\n"
  949. " -O, --redis-statsdb <connection-string> Redis status and statistics database connection string, if "
  950. "used \n"
  951. " (default - empty, no Redis stats DB used).\n"
  952. " This database keeps allocations status information, and it can be also "
  953. "used for publishing\n"
  954. " and delivering traffic and allocation event notifications.\n"
  955. " The connection string has the same parameters as redis-userdb "
  956. "connection string.\n"
  957. #endif
  958. #if !defined(TURN_NO_PROMETHEUS)
  959. " --prometheus Enable prometheus metrics. It is disabled by default. If it is "
  960. "enabled it will listen on port 9641 under the path /metrics\n"
  961. " also the path / on this port can be used as a health check\n"
  962. " --prometheus-port <port> Prometheus metrics port (Default: 9641).\n"
  963. " --prometheus-username-labels When metrics are enabled, add labels with client usernames.\n"
  964. #endif
  965. " --use-auth-secret TURN REST API flag.\n"
  966. " Flag that sets a special authorization option that is based upon "
  967. "authentication secret\n"
  968. " (TURN Server REST API, see "
  969. "https://github.com/coturn/coturn/blob/master/README.turnserver).\n"
  970. " This option is used with timestamp.\n"
  971. " --static-auth-secret <secret> 'Static' authentication secret value (a string) for TURN REST "
  972. "API only.\n"
  973. " If not set, then the turn server will try to use the 'dynamic' value\n"
  974. " in turn_secret table in user database (if present).\n"
  975. " That database value can be changed on-the-fly\n"
  976. " by a separate program, so this is why it is 'dynamic'.\n"
  977. " Multiple shared secrets can be used (both in the database and in the "
  978. "\"static\" fashion).\n"
  979. " --no-auth-pings Disable periodic health checks to 'dynamic' auth secret tables.\n"
  980. " --no-dynamic-ip-list Do not use dynamic allowed/denied peer ip list.\n"
  981. " --no-dynamic-realms Do not use dynamic realm assignment and options.\n"
  982. " --server-name Server name used for\n"
  983. " the oAuth authentication purposes.\n"
  984. " The default value is the realm name.\n"
  985. " --oauth Support oAuth authentication.\n"
  986. " -n Do not use configuration file, take all parameters from the "
  987. "command line only.\n"
  988. " --cert <filename> Certificate file, PEM format. Same file search rules\n"
  989. " applied as for the configuration file.\n"
  990. " If both --no-tls and --no_dtls options\n"
  991. " are specified, then this parameter is not needed.\n"
  992. " --pkey <filename> Private key file, PEM format. Same file search rules\n"
  993. " applied as for the configuration file.\n"
  994. " If both --no-tls and --no-dtls options\n"
  995. " --pkey-pwd <password> If the private key file is encrypted, then this password to be "
  996. "used.\n"
  997. " --cipher-list <cipher-string> Allowed OpenSSL cipher list for TLS/DTLS connections.\n"
  998. " Default value is \"DEFAULT\" for TLS/DTLS versions up to "
  999. "TLSv1.2/DTLSv1.2,\n"
  1000. " and the library default ciphersuites for TLSv1.3.\n"
  1001. " --CA-file <filename> CA file in OpenSSL format.\n"
  1002. " Forces TURN server to verify the client SSL certificates.\n"
  1003. " By default, no CA is set and no client certificate check is "
  1004. "performed.\n"
  1005. " --ec-curve-name <curve-name> Curve name for EC ciphers, if supported by OpenSSL\n"
  1006. " library (TLS and DTLS). The default value is prime256v1,\n"
  1007. " if pre-OpenSSL 1.0.2 is used. With OpenSSL 1.0.2+,\n"
  1008. " an optimal curve will be automatically calculated, if not defined\n"
  1009. " by this option.\n"
  1010. " --dh566 Use 566 bits predefined DH TLS key. Default size of the predefined key "
  1011. "is 2066.\n"
  1012. " --dh1066 Use 1066 bits predefined DH TLS key. Default size of the predefined "
  1013. "key is 2066.\n"
  1014. " --dh-file <dh-file-name> Use custom DH TLS key, stored in PEM format in the file.\n"
  1015. " Flags --dh566 and --dh1066 are ignored when the DH key is taken from a "
  1016. "file.\n"
  1017. " --no-tlsv1 Set TLSv1.1/DTLSv1.2 as a minimum supported protocol version.\n"
  1018. " With openssl-1.0.2 and below, do not allow "
  1019. "TLSv1/DTLSv1 protocols.\n"
  1020. " --no-tlsv1_1 Set TLSv1.2/DTLSv1.2 as a minimum supported protocol version.\n"
  1021. " With openssl-1.0.2 and below, do not allow TLSv1.1 "
  1022. "protocol.\n"
  1023. " --no-tlsv1_2 Set TLSv1.3/DTLSv1.2 as a minimum supported protocol version.\n"
  1024. " With openssl-1.0.2 and below, do not allow "
  1025. "TLSv1.2/DTLSv1.2 protocols.\n"
  1026. " --no-udp Do not start UDP client listeners.\n"
  1027. " --no-tcp Do not start TCP client listeners.\n"
  1028. " --no-tls Do not start TLS client listeners.\n"
  1029. " --no-dtls Do not start DTLS client listeners.\n"
  1030. " --no-udp-relay Do not allow UDP relay endpoints, use only TCP relay option.\n"
  1031. " --no-tcp-relay Do not allow TCP relay endpoints, use only UDP relay options.\n"
  1032. " -l, --log-file <filename> Option to set the full path name of the log file.\n"
  1033. " By default, the turnserver tries to open a log file in\n"
  1034. " /var/log/turnserver/, /var/log, /var/tmp, /tmp and . (current) "
  1035. "directories\n"
  1036. " (which open operation succeeds first that file will be used).\n"
  1037. " With this option you can set the definite log file name.\n"
  1038. " The special names are \"stdout\" and \"-\" - they will force "
  1039. "everything\n"
  1040. " to the stdout; and \"syslog\" name will force all output to the "
  1041. "syslog.\n"
  1042. " --no-stdout-log Flag to prevent stdout log messages.\n"
  1043. " By default, all log messages are going to both stdout and to\n"
  1044. " a log file. With this option everything will be going to the log file "
  1045. "only\n"
  1046. " (unless the log file itself is stdout).\n"
  1047. " --syslog Output all log information into the system log (syslog), do not use "
  1048. "the file output.\n"
  1049. " --syslog-facility <value> Set syslog facility for syslog messages. Default is ''.\n"
  1050. " --simple-log This flag means that no log file rollover will be used, and "
  1051. "the log file\n"
  1052. " name will be constructed as-is, without PID and date appendage.\n"
  1053. " This option can be used, for example, together with the logrotate "
  1054. "tool.\n"
  1055. " --new-log-timestamp Enable full ISO-8601 timestamp in all logs.\n"
  1056. " --new-log-timestamp-format <format> Set timestamp format (in strftime(1) format). Depends on "
  1057. "--new-log-timestamp to be enabled.\n"
  1058. " --log-binding Log STUN binding request. It is now disabled by default to "
  1059. "avoid DoS attacks.\n"
  1060. " --stale-nonce[=<value>] Use extra security with nonce value having limited lifetime (default "
  1061. "600 secs).\n"
  1062. " --max-allocate-lifetime <value> Set the maximum value for the allocation lifetime. Default to 3600 "
  1063. "secs.\n"
  1064. " --channel-lifetime <value> Set the lifetime for channel binding, default to 600 secs.\n"
  1065. " This value MUST not be changed for production purposes.\n"
  1066. " --permission-lifetime <value> Set the value for the lifetime of the permission. Default to "
  1067. "300 secs.\n"
  1068. " This MUST not be changed for production purposes.\n"
  1069. " -S, --stun-only Option to set standalone STUN operation only, all TURN requests will "
  1070. "be ignored.\n"
  1071. " --no-stun Option to suppress STUN functionality, only TURN requests will "
  1072. "be processed.\n"
  1073. " --alternate-server <ip:port> Set the TURN server to redirect the allocate requests (UDP and "
  1074. "TCP services).\n"
  1075. " Multiple alternate-server options can be set for load balancing "
  1076. "purposes.\n"
  1077. " See the docs for more information.\n"
  1078. " --tls-alternate-server <ip:port> Set the TURN server to redirect the allocate requests (DTLS "
  1079. "and TLS services).\n"
  1080. " Multiple alternate-server options can be set for load balancing "
  1081. "purposes.\n"
  1082. " See the docs for more information.\n"
  1083. " -C, --rest-api-separator <SYMBOL> This is the timestamp/username separator symbol (character) in TURN "
  1084. "REST API.\n"
  1085. " The default value is ':'.\n"
  1086. " --max-allocate-timeout=<seconds> Max time, in seconds, allowed for full allocation establishment. "
  1087. "Default is 60.\n"
  1088. " --allowed-peer-ip=<ip[-ip]> Specifies an ip or range of ips that are explicitly allowed to "
  1089. "connect to the \n"
  1090. " turn server. Multiple allowed-peer-ip can be set.\n"
  1091. " --denied-peer-ip=<ip[-ip]> Specifies an ip or range of ips that are not allowed to "
  1092. "connect to the turn server.\n"
  1093. " Multiple denied-peer-ip can be set.\n"
  1094. " --pidfile <\"pid-file-name\"> File name to store the pid of the process.\n"
  1095. " Default is /var/run/turnserver.pid (if superuser account is used) or\n"
  1096. " /var/tmp/turnserver.pid .\n"
  1097. " --acme-redirect <URL> Redirect ACME, i.e. HTTP GET requests matching "
  1098. "'^/.well-known/acme-challenge/(.*)' to '<URL>$1'.\n"
  1099. " Default is '', i.e. no special handling for such requests.\n"
  1100. " --secure-stun Require authentication of the STUN Binding request.\n"
  1101. " By default, the clients are allowed anonymous access to the STUN "
  1102. "Binding functionality.\n"
  1103. " --proc-user <user-name> User name to run the turnserver process.\n"
  1104. " After the initialization, the turnserver process\n"
  1105. " will make an attempt to change the current user ID to that user.\n"
  1106. " --proc-group <group-name> Group name to run the turnserver process.\n"
  1107. " After the initialization, the turnserver process\n"
  1108. " will make an attempt to change the current group ID to that group.\n"
  1109. " --mobility Mobility with ICE (MICE) specs support.\n"
  1110. " -K, --keep-address-family Deprecated in favor of --allocation-default-address-family!!\n"
  1111. " TURN server allocates address family according TURN\n"
  1112. " Client <=> Server communication address family.\n"
  1113. " !! It breaks RFC6156 section-4.2 (violates default IPv4) !!\n"
  1114. " -A --allocation-default-address-family=<ipv4|ipv6|keep> Default is IPv4\n"
  1115. " TURN server allocates address family according TURN client requested "
  1116. "address family. \n"
  1117. " If address family is not requested explicitly by client, then it falls "
  1118. "back to this default.\n"
  1119. " The standard RFC explicitly define actually that this default must be "
  1120. "IPv4,\n"
  1121. " so use other option values with care!\n"
  1122. " --no-cli Turn OFF the CLI support. By default it is always ON.\n"
  1123. " --cli-ip=<IP> Local system IP address to be used for CLI server endpoint. "
  1124. "Default value\n"
  1125. " is 127.0.0.1.\n"
  1126. " --cli-port=<port> CLI server port. Default is 5766.\n"
  1127. " --cli-password=<password> CLI access password. Default is empty (no password).\n"
  1128. " For the security reasons, it is recommended to use the encrypted\n"
  1129. " for of the password (see the -P command in the turnadmin utility).\n"
  1130. " The dollar signs in the encrypted form must be escaped.\n"
  1131. " --web-admin Enable Turn Web-admin support. By default it is disabled.\n"
  1132. " --web-admin-ip=<IP> Local system IP address to be used for Web-admin server "
  1133. "endpoint. Default value\n"
  1134. " is 127.0.0.1.\n"
  1135. " --web-admin-port=<port> Web-admin server port. Default is 8080.\n"
  1136. " --web-admin-listen-on-workers Enable for web-admin server to listens on STUN/TURN workers "
  1137. "STUN/TURN ports.\n"
  1138. " By default it is disabled for security reasons!\n"
  1139. " (This behavior used to be the default behavior, and was enabled by "
  1140. "default.)\n"
  1141. " --server-relay Server relay. NON-STANDARD AND DANGEROUS OPTION. Only for "
  1142. "those applications\n"
  1143. " when we want to run server applications on the relay endpoints.\n"
  1144. " This option eliminates the IP permissions check on the packets\n"
  1145. " incoming to the relay endpoints.\n"
  1146. " --cli-max-output-sessions Maximum number of output sessions in ps CLI command.\n"
  1147. " This value can be changed on-the-fly in CLI. The default value is "
  1148. "256.\n"
  1149. " --ne=[1|2|3] Set network engine type for the process (for internal "
  1150. "purposes).\n"
  1151. " --no-rfc5780 Disable RFC5780 (NAT behavior discovery).\n"
  1152. " Originally, if there are more than one listener address from the same\n"
  1153. " address family, then by default the NAT behavior discovery feature "
  1154. "enabled.\n"
  1155. " This option disables this original behavior, because the NAT behavior "
  1156. "discovery\n"
  1157. " adds attributes to response, and this increase the possibility of an "
  1158. "amplification attack.\n"
  1159. " Strongly encouraged to use this option to decrease gain factor in STUN "
  1160. "binding responses.\n"
  1161. " --no-stun-backward-compatibility Disable handling old STUN Binding requests and disable MAPPED-ADDRESS "
  1162. "attribute\n"
  1163. " in binding response (use only the XOR-MAPPED-ADDRESS).\n"
  1164. " --response-origin-only-with-rfc5780 Only send RESPONSE-ORIGIN attribute in binding response if "
  1165. "RFC5780 is enabled.\n"
  1166. " --respond-http-unsupported Return an HTTP reponse with a 400 status code to HTTP "
  1167. "connections made to ports not\n"
  1168. " supporting HTTP. The default behaviour is to immediately "
  1169. "close the connection.\n"
  1170. " --version Print version (and exit).\n"
  1171. " -h Help\n"
  1172. "\n";
  1173. static char AdminUsage[] =
  1174. "Usage: turnadmin [command] [options]\n"
  1175. "\nCommands:\n\n"
  1176. " -P, --generate-encrypted-password Generate and print to the standard\n"
  1177. " output an encrypted form of a password\n"
  1178. " (for web admin user or CLI). See wiki, README or man\n"
  1179. " pages for more detailed description.\n"
  1180. " -k, --key generate long-term credential mechanism key for a user\n"
  1181. " -a, --add add/update a long-term mechanism user\n"
  1182. " -A, --add-admin add/update a web admin user\n"
  1183. " -d, --delete delete a long-term mechanism user\n"
  1184. " -D, --delete-admin delete an admin user\n"
  1185. " -l, --list list all long-term mechanism users\n"
  1186. " -L, --list-admin list all admin users\n"
  1187. " -s, --set-secret=<value> Add shared secret for TURN REST API\n"
  1188. " -S, --show-secret Show stored shared secrets for TURN REST API\n"
  1189. " -X, --delete-secret=<value> Delete a shared secret\n"
  1190. " --delete-all-secrets Delete all shared secrets for REST API\n"
  1191. " -O, --add-origin Add origin-to-realm relation.\n"
  1192. " -R, --del-origin Delete origin-to-realm relation.\n"
  1193. " -I, --list-origins List origin-to-realm relations.\n"
  1194. " -g, --set-realm-option Set realm params: max-bps, total-quota, user-quota.\n"
  1195. " -G, --list-realm-options List realm params.\n"
  1196. " -E, --generate-encrypted-password-aes Generate and print to the standard\n"
  1197. " output an encrypted form of password with AES-128\n"
  1198. "\nOptions with mandatory values:\n\n"
  1199. #if !defined(TURN_NO_SQLITE)
  1200. " -b, --db, --userdb SQLite database file, default value is /var/db/turndb or\n"
  1201. " /usr/local/var/db/turndb or /var/lib/turn/turndb.\n"
  1202. #endif
  1203. #if !defined(TURN_NO_PQ)
  1204. " -e, --psql-userdb, --sql-userdb PostgreSQL user database connection string, if PostgreSQL DB is used.\n"
  1205. #endif
  1206. #if !defined(TURN_NO_MYSQL)
  1207. " -M, --mysql-userdb MySQL user database connection string, if MySQL DB is used.\n"
  1208. #endif
  1209. #if !defined(TURN_NO_MONGO)
  1210. " -J, --mongo-userdb MongoDB user database connection string, if MongoDB is used.\n"
  1211. #endif
  1212. #if !defined(TURN_NO_HIREDIS)
  1213. " -N, --redis-userdb Redis user database connection string, if Redis DB is used.\n"
  1214. #endif
  1215. " -u, --user Username\n"
  1216. " -r, --realm Realm\n"
  1217. " -p, --password Password\n"
  1218. " -x, --key-path Generates a 128 bit key into the given path.\n"
  1219. " -f, --file-key-path Contains a 128 bit key in the given path.\n"
  1220. " -v, --verify Verify a given base64 encrypted type password.\n"
  1221. #if !defined(TURN_NO_SQLITE) || !defined(TURN_NO_PQ) || !defined(TURN_NO_MYSQL) || !defined(TURN_NO_MONGO) || \
  1222. !defined(TURN_NO_HIREDIS)
  1223. " -o, --origin Origin\n"
  1224. #endif
  1225. " --max-bps Set value of realm's max-bps parameter.\n"
  1226. " Setting to zero value means removal of the option.\n"
  1227. " --total-quota Set value of realm's total-quota parameter.\n"
  1228. " Setting to zero value means removal of the option.\n"
  1229. " --user-quota Set value of realm's user-quota parameter.\n"
  1230. " Setting to zero value means removal of the option.\n"
  1231. " -h, --help Help\n";
  1232. #define OPTIONS "c:d:p:L:E:X:i:m:l:r:u:b:B:e:M:J:N:O:q:Q:s:C:K:A:vVofhznaS"
  1233. #define ADMIN_OPTIONS "PEgGORIHKYlLkaADSdb:e:M:J:N:u:r:p:s:X:o:h:x:v:f:"
  1234. enum EXTRA_OPTS {
  1235. NO_UDP_OPT = 256,
  1236. NO_TCP_OPT,
  1237. TCP_PROXY_PORT_OPT,
  1238. NO_TLS_OPT,
  1239. NO_DTLS_OPT,
  1240. NO_UDP_RELAY_OPT,
  1241. NO_TCP_RELAY_OPT,
  1242. TLS_PORT_OPT,
  1243. ALT_PORT_OPT,
  1244. ALT_TLS_PORT_OPT,
  1245. CERT_FILE_OPT,
  1246. PKEY_FILE_OPT,
  1247. PKEY_PWD_OPT,
  1248. MIN_PORT_OPT,
  1249. MAX_PORT_OPT,
  1250. STALE_NONCE_OPT,
  1251. MAX_ALLOCATE_LIFETIME_OPT,
  1252. CHANNEL_LIFETIME_OPT,
  1253. PERMISSION_LIFETIME_OPT,
  1254. PROMETHEUS_OPT,
  1255. PROMETHEUS_PORT_OPT,
  1256. PROMETHEUS_ENABLE_USERNAMES_OPT,
  1257. AUTH_SECRET_OPT,
  1258. NO_AUTH_PINGS_OPT,
  1259. NO_DYNAMIC_IP_LIST_OPT,
  1260. NO_DYNAMIC_REALMS_OPT,
  1261. DEL_ALL_AUTH_SECRETS_OPT,
  1262. STATIC_AUTH_SECRET_VAL_OPT,
  1263. AUTH_SECRET_TS_EXP, /* deprecated */
  1264. NO_STDOUT_LOG_OPT,
  1265. SYSLOG_OPT,
  1266. SYSLOG_FACILITY_OPT,
  1267. SIMPLE_LOG_OPT,
  1268. NEW_LOG_TIMESTAMP_OPT,
  1269. NEW_LOG_TIMESTAMP_FORMAT_OPT,
  1270. AUX_SERVER_OPT,
  1271. UDP_SELF_BALANCE_OPT,
  1272. ALTERNATE_SERVER_OPT,
  1273. TLS_ALTERNATE_SERVER_OPT,
  1274. NO_MULTICAST_PEERS_OPT,
  1275. ALLOW_LOOPBACK_PEERS_OPT,
  1276. MAX_ALLOCATE_TIMEOUT_OPT,
  1277. ALLOWED_PEER_IPS,
  1278. DENIED_PEER_IPS,
  1279. CIPHER_LIST_OPT,
  1280. PIDFILE_OPT,
  1281. SECURE_STUN_OPT,
  1282. CA_FILE_OPT,
  1283. DH_FILE_OPT,
  1284. NO_STUN_OPT,
  1285. PROC_USER_OPT,
  1286. PROC_GROUP_OPT,
  1287. MOBILITY_OPT,
  1288. NO_CLI_OPT,
  1289. CLI_IP_OPT,
  1290. CLI_PORT_OPT,
  1291. CLI_PASSWORD_OPT,
  1292. WEB_ADMIN_OPT,
  1293. WEB_ADMIN_IP_OPT,
  1294. WEB_ADMIN_PORT_OPT,
  1295. WEB_ADMIN_LISTEN_ON_WORKERS_OPT,
  1296. SERVER_RELAY_OPT,
  1297. CLI_MAX_SESSIONS_OPT,
  1298. EC_CURVE_NAME_OPT,
  1299. DH566_OPT,
  1300. DH1066_OPT,
  1301. NE_TYPE_OPT,
  1302. NO_SSLV2_OPT, /*deprecated*/
  1303. NO_SSLV3_OPT, /*deprecated*/
  1304. NO_TLSV1_OPT,
  1305. NO_TLSV1_1_OPT,
  1306. NO_TLSV1_2_OPT,
  1307. CHECK_ORIGIN_CONSISTENCY_OPT,
  1308. ADMIN_MAX_BPS_OPT,
  1309. ADMIN_TOTAL_QUOTA_OPT,
  1310. ADMIN_USER_QUOTA_OPT,
  1311. SERVER_NAME_OPT,
  1312. OAUTH_OPT,
  1313. NO_SOFTWARE_ATTRIBUTE_OPT,
  1314. NO_HTTP_OPT,
  1315. SECRET_KEY_OPT,
  1316. ACME_REDIRECT_OPT,
  1317. LOG_BINDING_OPT,
  1318. NO_RFC5780,
  1319. NO_STUN_BACKWARD_COMPATIBILITY_OPT,
  1320. RESPONSE_ORIGIN_ONLY_WITH_RFC5780_OPT,
  1321. RESPOND_HTTP_UNSUPPORTED_OPT,
  1322. VERSION_OPT
  1323. };
  1324. struct myoption {
  1325. const char *name; /* name of long option */
  1326. int has_arg; /* whether option takes an argument */
  1327. int *flag; /* if not NULL, set *flag to val when option found */
  1328. int val; /* if flag is not NULL, value to set *flag to. */
  1329. /* if flag is NULL, return value */
  1330. };
  1331. struct uoptions {
  1332. union {
  1333. const struct myoption *m;
  1334. const struct option *o;
  1335. } u;
  1336. };
  1337. static const struct myoption long_options[] = {
  1338. {"listening-device", required_argument, NULL, 'd'},
  1339. {"listening-port", required_argument, NULL, 'p'},
  1340. {"tls-listening-port", required_argument, NULL, TLS_PORT_OPT},
  1341. {"alt-listening-port", required_argument, NULL, ALT_PORT_OPT},
  1342. {"alt-tls-listening-port", required_argument, NULL, ALT_TLS_PORT_OPT},
  1343. {"tcp-proxy-port", required_argument, NULL, TCP_PROXY_PORT_OPT},
  1344. {"listening-ip", required_argument, NULL, 'L'},
  1345. {"relay-device", required_argument, NULL, 'i'},
  1346. {"relay-ip", required_argument, NULL, 'E'},
  1347. {"external-ip", required_argument, NULL, 'X'},
  1348. {"relay-threads", required_argument, NULL, 'm'},
  1349. {"min-port", required_argument, NULL, MIN_PORT_OPT},
  1350. {"max-port", required_argument, NULL, MAX_PORT_OPT},
  1351. {"lt-cred-mech", optional_argument, NULL, 'a'},
  1352. {"no-auth", optional_argument, NULL, 'z'},
  1353. {"user", required_argument, NULL, 'u'},
  1354. {"userdb", required_argument, NULL, 'b'},
  1355. {"db", required_argument, NULL, 'b'},
  1356. #if !defined(TURN_NO_PQ)
  1357. {"psql-userdb", required_argument, NULL, 'e'},
  1358. {"sql-userdb", required_argument, NULL, 'e'},
  1359. #endif
  1360. #if !defined(TURN_NO_MYSQL)
  1361. {"mysql-userdb", required_argument, NULL, 'M'},
  1362. #endif
  1363. #if !defined(TURN_NO_MONGO)
  1364. {"mongo-userdb", required_argument, NULL, 'J'},
  1365. #endif
  1366. #if !defined(TURN_NO_HIREDIS)
  1367. {"redis-userdb", required_argument, NULL, 'N'},
  1368. {"redis-statsdb", required_argument, NULL, 'O'},
  1369. #endif
  1370. #if !defined(TURN_NO_PROMETHEUS)
  1371. {"prometheus", optional_argument, NULL, PROMETHEUS_OPT},
  1372. {"prometheus-port", optional_argument, NULL, PROMETHEUS_PORT_OPT},
  1373. {"prometheus-username-labels", optional_argument, NULL, PROMETHEUS_ENABLE_USERNAMES_OPT},
  1374. #endif
  1375. {"use-auth-secret", optional_argument, NULL, AUTH_SECRET_OPT},
  1376. {"static-auth-secret", required_argument, NULL, STATIC_AUTH_SECRET_VAL_OPT},
  1377. {"no-auth-pings", optional_argument, NULL, NO_AUTH_PINGS_OPT},
  1378. {"no-dynamic-ip-list", optional_argument, NULL, NO_DYNAMIC_IP_LIST_OPT},
  1379. {"no-dynamic-realms", optional_argument, NULL, NO_DYNAMIC_REALMS_OPT},
  1380. /* deprecated: */ {"secret-ts-exp-time", optional_argument, NULL, AUTH_SECRET_TS_EXP},
  1381. {"realm", required_argument, NULL, 'r'},
  1382. {"server-name", required_argument, NULL, SERVER_NAME_OPT},
  1383. {"oauth", optional_argument, NULL, OAUTH_OPT},
  1384. {"user-quota", required_argument, NULL, 'q'},
  1385. {"total-quota", required_argument, NULL, 'Q'},
  1386. {"max-bps", required_argument, NULL, 's'},
  1387. {"bps-capacity", required_argument, NULL, 'B'},
  1388. {"verbose", optional_argument, NULL, 'v'},
  1389. {"Verbose", optional_argument, NULL, 'V'},
  1390. {"daemon", optional_argument, NULL, 'o'},
  1391. /* deprecated: */ {"prod", optional_argument, NULL, NO_SOFTWARE_ATTRIBUTE_OPT},
  1392. {"no-software-attribute", optional_argument, NULL, NO_SOFTWARE_ATTRIBUTE_OPT},
  1393. {"fingerprint", optional_argument, NULL, 'f'},
  1394. {"check-origin-consistency", optional_argument, NULL, CHECK_ORIGIN_CONSISTENCY_OPT},
  1395. {"no-udp", optional_argument, NULL, NO_UDP_OPT},
  1396. {"no-tcp", optional_argument, NULL, NO_TCP_OPT},
  1397. {"no-tls", optional_argument, NULL, NO_TLS_OPT},
  1398. {"no-dtls", optional_argument, NULL, NO_DTLS_OPT},
  1399. {"no-udp-relay", optional_argument, NULL, NO_UDP_RELAY_OPT},
  1400. {"no-tcp-relay", optional_argument, NULL, NO_TCP_RELAY_OPT},
  1401. {"stale-nonce", optional_argument, NULL, STALE_NONCE_OPT},
  1402. {"max-allocate-lifetime", optional_argument, NULL, MAX_ALLOCATE_LIFETIME_OPT},
  1403. {"channel-lifetime", optional_argument, NULL, CHANNEL_LIFETIME_OPT},
  1404. {"permission-lifetime", optional_argument, NULL, PERMISSION_LIFETIME_OPT},
  1405. {"stun-only", optional_argument, NULL, 'S'},
  1406. {"no-stun", optional_argument, NULL, NO_STUN_OPT},
  1407. {"cert", required_argument, NULL, CERT_FILE_OPT},
  1408. {"pkey", required_argument, NULL, PKEY_FILE_OPT},
  1409. {"pkey-pwd", required_argument, NULL, PKEY_PWD_OPT},
  1410. {"log-file", required_argument, NULL, 'l'},
  1411. {"no-stdout-log", optional_argument, NULL, NO_STDOUT_LOG_OPT},
  1412. {"syslog", optional_argument, NULL, SYSLOG_OPT},
  1413. {"simple-log", optional_argument, NULL, SIMPLE_LOG_OPT},
  1414. {"new-log-timestamp", optional_argument, NULL, NEW_LOG_TIMESTAMP_OPT},
  1415. {"new-log-timestamp-format", required_argument, NULL, NEW_LOG_TIMESTAMP_FORMAT_OPT},
  1416. {"aux-server", required_argument, NULL, AUX_SERVER_OPT},
  1417. {"udp-self-balance", optional_argument, NULL, UDP_SELF_BALANCE_OPT},
  1418. {"alternate-server", required_argument, NULL, ALTERNATE_SERVER_OPT},
  1419. {"tls-alternate-server", required_argument, NULL, TLS_ALTERNATE_SERVER_OPT},
  1420. {"rest-api-separator", required_argument, NULL, 'C'},
  1421. {"max-allocate-timeout", required_argument, NULL, MAX_ALLOCATE_TIMEOUT_OPT},
  1422. {"no-multicast-peers", optional_argument, NULL, NO_MULTICAST_PEERS_OPT},
  1423. {"allow-loopback-peers", optional_argument, NULL, ALLOW_LOOPBACK_PEERS_OPT},
  1424. {"allowed-peer-ip", required_argument, NULL, ALLOWED_PEER_IPS},
  1425. {"denied-peer-ip", required_argument, NULL, DENIED_PEER_IPS},
  1426. {"cipher-list", required_argument, NULL, CIPHER_LIST_OPT},
  1427. {"pidfile", required_argument, NULL, PIDFILE_OPT},
  1428. {"secure-stun", optional_argument, NULL, SECURE_STUN_OPT},
  1429. {"CA-file", required_argument, NULL, CA_FILE_OPT},
  1430. {"dh-file", required_argument, NULL, DH_FILE_OPT},
  1431. {"proc-user", required_argument, NULL, PROC_USER_OPT},
  1432. {"proc-group", required_argument, NULL, PROC_GROUP_OPT},
  1433. {"mobility", optional_argument, NULL, MOBILITY_OPT},
  1434. {"no-cli", optional_argument, NULL, NO_CLI_OPT},
  1435. {"cli-ip", required_argument, NULL, CLI_IP_OPT},
  1436. {"cli-port", required_argument, NULL, CLI_PORT_OPT},
  1437. {"cli-password", required_argument, NULL, CLI_PASSWORD_OPT},
  1438. {"web-admin", optional_argument, NULL, WEB_ADMIN_OPT},
  1439. {"web-admin-ip", required_argument, NULL, WEB_ADMIN_IP_OPT},
  1440. {"web-admin-port", required_argument, NULL, WEB_ADMIN_PORT_OPT},
  1441. {"web-admin-listen-on-workers", optional_argument, NULL, WEB_ADMIN_LISTEN_ON_WORKERS_OPT},
  1442. {"server-relay", optional_argument, NULL, SERVER_RELAY_OPT},
  1443. {"cli-max-output-sessions", required_argument, NULL, CLI_MAX_SESSIONS_OPT},
  1444. {"ec-curve-name", required_argument, NULL, EC_CURVE_NAME_OPT},
  1445. {"dh566", optional_argument, NULL, DH566_OPT},
  1446. {"dh1066", optional_argument, NULL, DH1066_OPT},
  1447. {"ne", required_argument, NULL, NE_TYPE_OPT},
  1448. {"no-sslv2", optional_argument, NULL, NO_SSLV2_OPT}, /* deprecated */
  1449. {"no-sslv3", optional_argument, NULL, NO_SSLV3_OPT}, /* deprecated */
  1450. {"no-tlsv1", optional_argument, NULL, NO_TLSV1_OPT},
  1451. {"no-tlsv1_1", optional_argument, NULL, NO_TLSV1_1_OPT},
  1452. {"no-tlsv1_2", optional_argument, NULL, NO_TLSV1_2_OPT},
  1453. {"secret-key-file", required_argument, NULL, SECRET_KEY_OPT},
  1454. {"keep-address-family", optional_argument, NULL, 'K'},
  1455. {"allocation-default-address-family", required_argument, NULL, 'A'},
  1456. {"acme-redirect", required_argument, NULL, ACME_REDIRECT_OPT},
  1457. {"log-binding", optional_argument, NULL, LOG_BINDING_OPT},
  1458. {"no-rfc5780", optional_argument, NULL, NO_RFC5780},
  1459. {"no-stun-backward-compatibility", optional_argument, NULL, NO_STUN_BACKWARD_COMPATIBILITY_OPT},
  1460. {"response-origin-only-with-rfc5780", optional_argument, NULL, RESPONSE_ORIGIN_ONLY_WITH_RFC5780_OPT},
  1461. {"respond-http-unsupported", optional_argument, NULL, RESPOND_HTTP_UNSUPPORTED_OPT},
  1462. {"version", optional_argument, NULL, VERSION_OPT},
  1463. {"syslog-facility", required_argument, NULL, SYSLOG_FACILITY_OPT},
  1464. {NULL, no_argument, NULL, 0}};
  1465. static const struct myoption admin_long_options[] = {
  1466. {"generate-encrypted-password", no_argument, NULL, 'P'},
  1467. {"generate-encrypted-password-aes", no_argument, NULL, 'E'},
  1468. {"key", no_argument, NULL, 'k'},
  1469. {"add", no_argument, NULL, 'a'},
  1470. {"delete", no_argument, NULL, 'd'},
  1471. {"list", no_argument, NULL, 'l'},
  1472. {"list-admin", no_argument, NULL, 'L'},
  1473. {"set-secret", required_argument, NULL, 's'},
  1474. {"show-secret", no_argument, NULL, 'S'},
  1475. {"delete-secret", required_argument, NULL, 'X'},
  1476. {"delete-all-secrets", no_argument, NULL, DEL_ALL_AUTH_SECRETS_OPT},
  1477. {"add-admin", no_argument, NULL, 'A'},
  1478. {"delete-admin", no_argument, NULL, 'D'},
  1479. #if !defined(TURN_NO_SQLITE)
  1480. {"userdb", required_argument, NULL, 'b'},
  1481. {"db", required_argument, NULL, 'b'},
  1482. #endif
  1483. #if !defined(TURN_NO_PQ)
  1484. {"psql-userdb", required_argument, NULL, 'e'},
  1485. {"sql-userdb", required_argument, NULL, 'e'},
  1486. #endif
  1487. #if !defined(TURN_NO_MYSQL)
  1488. {"mysql-userdb", required_argument, NULL, 'M'},
  1489. #endif
  1490. #if !defined(TURN_NO_MONGO)
  1491. {"mongo-userdb", required_argument, NULL, 'J'},
  1492. #endif
  1493. #if !defined(TURN_NO_HIREDIS)
  1494. {"redis-userdb", required_argument, NULL, 'N'},
  1495. #endif
  1496. {"user", required_argument, NULL, 'u'},
  1497. {"realm", required_argument, NULL, 'r'},
  1498. {"password", required_argument, NULL, 'p'},
  1499. {"file-key-path", required_argument, NULL, 'f'},
  1500. {"verify", required_argument, NULL, 'v'},
  1501. {"key-path", required_argument, NULL, 'x'},
  1502. {"add-origin", no_argument, NULL, 'O'},
  1503. {"del-origin", no_argument, NULL, 'R'},
  1504. {"list-origins", required_argument, NULL, 'I'},
  1505. {"origin", required_argument, NULL, 'o'},
  1506. {"set-realm-option", no_argument, NULL, 'g'},
  1507. {"list-realm-option", no_argument, NULL, 'G'},
  1508. {"user-quota", required_argument, NULL, ADMIN_USER_QUOTA_OPT},
  1509. {"total-quota", required_argument, NULL, ADMIN_TOTAL_QUOTA_OPT},
  1510. {"max-bps", required_argument, NULL, ADMIN_MAX_BPS_OPT},
  1511. {"help", no_argument, NULL, 'h'},
  1512. {NULL, no_argument, NULL, 0}};
  1513. int init_ctr(struct ctr_state *state, const unsigned char iv[8]) {
  1514. state->num = 0;
  1515. memset(state->ecount, 0, 16);
  1516. memset(state->ivec + 8, 0, 8);
  1517. memcpy(state->ivec, iv, 8);
  1518. return 1;
  1519. }
  1520. unsigned char *base64encode(const void *b64_encode_this, int encode_this_many_bytes) {
  1521. BIO *b64_bio, *mem_bio; // Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
  1522. BUF_MEM *mem_bio_mem_ptr; // Pointer to a "memory BIO" structure holding our base64 data.
  1523. b64_bio = BIO_new(BIO_f_base64()); // Initialize our base64 filter BIO.
  1524. mem_bio = BIO_new(BIO_s_mem()); // Initialize our memory sink BIO.
  1525. BIO_push(b64_bio, mem_bio); // Link the BIOs by creating a filter-sink BIO chain.
  1526. BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); // No newlines every 64 characters or less.
  1527. BIO_write(b64_bio, b64_encode_this, encode_this_many_bytes); // Records base64 encoded data.
  1528. (void)BIO_flush(b64_bio); // Flush data. Necessary for b64 encoding, because of pad characters.
  1529. BIO_get_mem_ptr(mem_bio, &mem_bio_mem_ptr); // Store address of mem_bio's memory structure.
  1530. (void)BIO_set_close(mem_bio, BIO_NOCLOSE); // Permit access to mem_ptr after BIOs are destroyed.
  1531. BIO_free_all(b64_bio); // Destroys all BIOs in chain, starting with b64 (i.e. the 1st one).
  1532. BUF_MEM_grow(mem_bio_mem_ptr, (*mem_bio_mem_ptr).length + 1); // Makes space for end null.
  1533. (*mem_bio_mem_ptr).data[(*mem_bio_mem_ptr).length] = '\0'; // Adds null-terminator to tail.
  1534. return (unsigned char *)(*mem_bio_mem_ptr).data; // Returns base-64 encoded data. (See: "buf_mem_st" struct).
  1535. }
  1536. void encrypt_aes_128(unsigned char *in, const unsigned char *mykey) {
  1537. int j = 0, k = 0;
  1538. int totalSize = 0;
  1539. AES_KEY key;
  1540. unsigned char iv[8] = {0}; // changed
  1541. unsigned char out[1024]; // changed
  1542. AES_set_encrypt_key(mykey, 128, &key);
  1543. char total[256];
  1544. int size = 0;
  1545. struct ctr_state state;
  1546. init_ctr(&state, iv);
  1547. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  1548. CRYPTO_ctr128_encrypt(in, out, strlen((char *)in), &key, state.ivec, state.ecount, &state.num,
  1549. (block128_f)AES_encrypt);
  1550. #else
  1551. AES_ctr128_encrypt(in, out, strlen((char *)in), &key, state.ivec, state.ecount, &state.num);
  1552. #endif
  1553. totalSize += strlen((char *)in);
  1554. size = strlen((char *)in);
  1555. for (j = 0; j < size; j++) {
  1556. total[k++] = out[j];
  1557. }
  1558. unsigned char *base64_encoded = base64encode(total, totalSize);
  1559. printf("%s\n", base64_encoded);
  1560. }
  1561. void generate_aes_128_key(char *filePath, unsigned char *returnedKey) {
  1562. int i;
  1563. int part;
  1564. FILE *fptr;
  1565. char key[16];
  1566. turn_srandom();
  1567. for (i = 0; i < 16; i++) {
  1568. part = (rand() % 3);
  1569. if (part == 0) {
  1570. key[i] = (turn_random() % 10) + 48;
  1571. }
  1572. else if (part == 1) {
  1573. key[i] = (turn_random() % 26) + 65;
  1574. }
  1575. else if (part == 2) {
  1576. key[i] = (turn_random() % 26) + 97;
  1577. }
  1578. }
  1579. fptr = fopen(filePath, "w");
  1580. for (i = 0; i < 16; i++) {
  1581. fputc(key[i], fptr);
  1582. }
  1583. STRCPY((char *)returnedKey, key);
  1584. fclose(fptr);
  1585. }
  1586. unsigned char *base64decode(const void *b64_decode_this, int decode_this_many_bytes) {
  1587. BIO *b64_bio, *mem_bio; // Declares two OpenSSL BIOs: a base64 filter and a memory BIO.
  1588. unsigned char *base64_decoded =
  1589. (unsigned char *)calloc((decode_this_many_bytes * 3) / 4 + 1, sizeof(char)); //+1 = null.
  1590. b64_bio = BIO_new(BIO_f_base64()); // Initialize our base64 filter BIO.
  1591. mem_bio = BIO_new(BIO_s_mem()); // Initialize our memory source BIO.
  1592. BIO_write(mem_bio, b64_decode_this, decode_this_many_bytes); // Base64 data saved in source.
  1593. BIO_push(b64_bio, mem_bio); // Link the BIOs by creating a filter-source BIO chain.
  1594. BIO_set_flags(b64_bio, BIO_FLAGS_BASE64_NO_NL); // Don't require trailing newlines.
  1595. int decoded_byte_index = 0; // Index where the next base64_decoded byte should be written.
  1596. while (0 < BIO_read(b64_bio, base64_decoded + decoded_byte_index, 1)) { // Read byte-by-byte.
  1597. decoded_byte_index++; // Increment the index until read of BIO decoded data is complete.
  1598. } // Once we're done reading decoded data, BIO_read returns -1 even though there's no error.
  1599. BIO_free_all(b64_bio); // Destroys all BIOs in chain, starting with b64 (i.e. the 1st one).
  1600. return base64_decoded; // Returns base-64 decoded data with trailing null terminator.
  1601. }
  1602. int decodedTextSize(char *input) {
  1603. int i = 0;
  1604. int result = 0, padding = 0;
  1605. int size = strlen(input);
  1606. for (i = 0; i < size; ++i) {
  1607. if (input[i] == '=') {
  1608. padding++;
  1609. }
  1610. }
  1611. result = (strlen(input) / 4 * 3) - padding;
  1612. return result;
  1613. }
  1614. void decrypt_aes_128(char *in, const unsigned char *mykey) {
  1615. unsigned char iv[8] = {0};
  1616. AES_KEY key;
  1617. unsigned char outdata[256];
  1618. AES_set_encrypt_key(mykey, 128, &key);
  1619. int newTotalSize = decodedTextSize(in);
  1620. int bytes_to_decode = strlen(in);
  1621. unsigned char *encryptedText = base64decode(in, bytes_to_decode);
  1622. char last[1024] = "";
  1623. struct ctr_state state;
  1624. init_ctr(&state, iv);
  1625. memset(outdata, '\0', sizeof(outdata));
  1626. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  1627. CRYPTO_ctr128_encrypt(encryptedText, outdata, newTotalSize, &key, state.ivec, state.ecount, &state.num,
  1628. (block128_f)AES_encrypt);
  1629. #else
  1630. AES_ctr128_encrypt(encryptedText, outdata, newTotalSize, &key, state.ivec, state.ecount, &state.num);
  1631. #endif
  1632. strcat(last, (char *)outdata);
  1633. printf("%s\n", last);
  1634. }
  1635. static int get_int_value(const char *s, int default_value) {
  1636. if (!s || !(s[0])) {
  1637. return default_value;
  1638. }
  1639. return atoi(s);
  1640. }
  1641. static int get_bool_value(const char *s) {
  1642. if (!s || !(s[0])) {
  1643. return 1;
  1644. }
  1645. if (s[0] == '0' || s[0] == 'n' || s[0] == 'N' || s[0] == 'f' || s[0] == 'F') {
  1646. return 0;
  1647. }
  1648. if (s[0] == 'y' || s[0] == 'Y' || s[0] == 't' || s[0] == 'T') {
  1649. return 1;
  1650. }
  1651. if (s[0] > '0' && s[0] <= '9') {
  1652. return 1;
  1653. }
  1654. if (!strcmp(s, "off") || !strcmp(s, "OFF") || !strcmp(s, "Off")) {
  1655. return 0;
  1656. }
  1657. if (!strcmp(s, "on") || !strcmp(s, "ON") || !strcmp(s, "On")) {
  1658. return 1;
  1659. }
  1660. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown boolean value: %s. You can use on/off, yes/no, 1/0, true/false.\n", s);
  1661. exit(-1);
  1662. }
  1663. static void set_option(int c, char *value) {
  1664. if (value && value[0] == '=') {
  1665. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  1666. "WARNING: option -%c is possibly used incorrectly. The short form of the option must be used as "
  1667. "this: -%c <value>, no \'equals\' sign may be used, that sign is used only with long form options "
  1668. "(like --user=<username>).\n",
  1669. (char)c, (char)c);
  1670. }
  1671. switch (c) {
  1672. case 'K':
  1673. if (get_bool_value(value)) {
  1674. turn_params.allocation_default_address_family = ALLOCATION_DEFAULT_ADDRESS_FAMILY_KEEP;
  1675. }
  1676. break;
  1677. case 'A':
  1678. if (value && strlen(value) > 0) {
  1679. if (*value == '=') {
  1680. ++value;
  1681. }
  1682. if (!strcmp(value, "ipv6")) {
  1683. turn_params.allocation_default_address_family = ALLOCATION_DEFAULT_ADDRESS_FAMILY_IPV6;
  1684. } else if (!strcmp(value, "keep")) {
  1685. turn_params.allocation_default_address_family = ALLOCATION_DEFAULT_ADDRESS_FAMILY_KEEP;
  1686. } else if (!strcmp(value, "ipv4")) {
  1687. turn_params.allocation_default_address_family = ALLOCATION_DEFAULT_ADDRESS_FAMILY_IPV4;
  1688. } else {
  1689. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "ERROR: invalid allocation_default_address_family parameter\n");
  1690. }
  1691. } else {
  1692. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "ERROR: invalid allocation_default_address_family parameter\n");
  1693. }
  1694. break;
  1695. case SERVER_NAME_OPT:
  1696. STRCPY(turn_params.oauth_server_name, value);
  1697. break;
  1698. case OAUTH_OPT:
  1699. if (ENC_ALG_NUM == 0) {
  1700. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "WARNING: option --oauth is not supported; ignored.\n");
  1701. } else {
  1702. turn_params.oauth = get_bool_value(value);
  1703. }
  1704. break;
  1705. case NO_SSLV2_OPT:
  1706. // deprecated
  1707. break;
  1708. case NO_SSLV3_OPT:
  1709. // deprecated
  1710. break;
  1711. case NO_TLSV1_OPT:
  1712. turn_params.no_tlsv1 = get_bool_value(value);
  1713. break;
  1714. case NO_TLSV1_1_OPT:
  1715. turn_params.no_tlsv1_1 = get_bool_value(value);
  1716. break;
  1717. case NO_TLSV1_2_OPT:
  1718. turn_params.no_tlsv1_2 = get_bool_value(value);
  1719. break;
  1720. case NE_TYPE_OPT: {
  1721. int ne = atoi(value);
  1722. if ((ne < (int)NEV_MIN) || (ne > (int)NEV_MAX)) {
  1723. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "ERROR: wrong version of the network engine: %d\n", ne);
  1724. }
  1725. turn_params.net_engine_version = (NET_ENG_VERSION)ne;
  1726. } break;
  1727. case DH566_OPT:
  1728. if (get_bool_value(value)) {
  1729. turn_params.dh_key_size = DH_566;
  1730. }
  1731. break;
  1732. case DH1066_OPT:
  1733. if (get_bool_value(value)) {
  1734. turn_params.dh_key_size = DH_1066;
  1735. }
  1736. break;
  1737. case EC_CURVE_NAME_OPT:
  1738. STRCPY(turn_params.ec_curve_name, value);
  1739. break;
  1740. case CLI_MAX_SESSIONS_OPT:
  1741. cli_max_output_sessions = atoi(value);
  1742. break;
  1743. case SERVER_RELAY_OPT:
  1744. turn_params.server_relay = get_bool_value(value);
  1745. break;
  1746. case MOBILITY_OPT:
  1747. turn_params.mobility = get_bool_value(value);
  1748. break;
  1749. case NO_CLI_OPT:
  1750. use_cli = !get_bool_value(value);
  1751. break;
  1752. case CLI_IP_OPT:
  1753. if (make_ioa_addr((const uint8_t *)value, 0, &cli_addr) < 0) {
  1754. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot set cli address: %s\n", value);
  1755. } else {
  1756. cli_addr_set = 1;
  1757. }
  1758. break;
  1759. case CLI_PORT_OPT:
  1760. cli_port = atoi(value);
  1761. break;
  1762. case CLI_PASSWORD_OPT:
  1763. STRCPY(cli_password, value);
  1764. break;
  1765. case WEB_ADMIN_OPT:
  1766. use_web_admin = get_bool_value(value);
  1767. break;
  1768. case WEB_ADMIN_IP_OPT:
  1769. if (make_ioa_addr((const uint8_t *)value, 0, &web_admin_addr) < 0) {
  1770. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot set web-admin address: %s\n", value);
  1771. } else {
  1772. web_admin_addr_set = 1;
  1773. }
  1774. break;
  1775. case WEB_ADMIN_PORT_OPT:
  1776. web_admin_port = atoi(value);
  1777. break;
  1778. case WEB_ADMIN_LISTEN_ON_WORKERS_OPT:
  1779. turn_params.web_admin_listen_on_workers = get_bool_value(value);
  1780. break;
  1781. #if defined(WINDOWS)
  1782. // TODO: implement it!!!
  1783. #else
  1784. case PROC_USER_OPT: {
  1785. struct passwd *pwd = getpwnam(value);
  1786. if (!pwd) {
  1787. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown user name: %s\n", value);
  1788. exit(-1);
  1789. } else {
  1790. procuserid = pwd->pw_uid;
  1791. procuserid_set = 1;
  1792. STRCPY(procusername, value);
  1793. }
  1794. } break;
  1795. case PROC_GROUP_OPT: {
  1796. struct group *gr = getgrnam(value);
  1797. if (!gr) {
  1798. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown group name: %s\n", value);
  1799. exit(-1);
  1800. } else {
  1801. procgroupid = gr->gr_gid;
  1802. procgroupid_set = 1;
  1803. STRCPY(procgroupname, value);
  1804. }
  1805. } break;
  1806. #endif
  1807. case 'i':
  1808. STRCPY(turn_params.relay_ifname, value);
  1809. break;
  1810. case 'm':
  1811. if (atoi(value) > MAX_NUMBER_OF_GENERAL_RELAY_SERVERS) {
  1812. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "WARNING: max number of relay threads is 128.\n");
  1813. turn_params.general_relay_servers_number = MAX_NUMBER_OF_GENERAL_RELAY_SERVERS;
  1814. } else if (atoi(value) <= 0) {
  1815. turn_params.general_relay_servers_number = 0;
  1816. } else {
  1817. turn_params.general_relay_servers_number = atoi(value);
  1818. }
  1819. break;
  1820. case 'd':
  1821. STRCPY(turn_params.listener_ifname, value);
  1822. break;
  1823. case 'p':
  1824. turn_params.listener_port = atoi(value);
  1825. break;
  1826. case TLS_PORT_OPT:
  1827. turn_params.tls_listener_port = atoi(value);
  1828. break;
  1829. case ALT_PORT_OPT:
  1830. turn_params.alt_listener_port = atoi(value);
  1831. break;
  1832. case ALT_TLS_PORT_OPT:
  1833. turn_params.alt_tls_listener_port = atoi(value);
  1834. break;
  1835. case TCP_PROXY_PORT_OPT:
  1836. turn_params.tcp_proxy_port = atoi(value);
  1837. turn_params.tcp_use_proxy = 1;
  1838. break;
  1839. case MIN_PORT_OPT:
  1840. turn_params.min_port = atoi(value);
  1841. break;
  1842. case MAX_PORT_OPT:
  1843. turn_params.max_port = atoi(value);
  1844. break;
  1845. case SECURE_STUN_OPT:
  1846. turn_params.secure_stun = get_bool_value(value);
  1847. break;
  1848. case NO_MULTICAST_PEERS_OPT:
  1849. turn_params.no_multicast_peers = get_bool_value(value);
  1850. break;
  1851. case ALLOW_LOOPBACK_PEERS_OPT:
  1852. turn_params.allow_loopback_peers = get_bool_value(value);
  1853. break;
  1854. case STALE_NONCE_OPT:
  1855. turn_params.stale_nonce = get_int_value(value, STUN_DEFAULT_NONCE_EXPIRATION_TIME);
  1856. break;
  1857. case MAX_ALLOCATE_LIFETIME_OPT:
  1858. turn_params.max_allocate_lifetime = get_int_value(value, STUN_DEFAULT_MAX_ALLOCATE_LIFETIME);
  1859. break;
  1860. case CHANNEL_LIFETIME_OPT:
  1861. turn_params.channel_lifetime = get_int_value(value, STUN_DEFAULT_CHANNEL_LIFETIME);
  1862. break;
  1863. case PERMISSION_LIFETIME_OPT:
  1864. turn_params.permission_lifetime = get_int_value(value, STUN_DEFAULT_PERMISSION_LIFETIME);
  1865. break;
  1866. case MAX_ALLOCATE_TIMEOUT_OPT:
  1867. TURN_MAX_ALLOCATE_TIMEOUT = atoi(value);
  1868. TURN_MAX_ALLOCATE_TIMEOUT_STUN_ONLY = atoi(value);
  1869. break;
  1870. case 'S':
  1871. turn_params.stun_only = get_bool_value(value);
  1872. break;
  1873. case NO_STUN_OPT:
  1874. turn_params.no_stun = get_bool_value(value);
  1875. break;
  1876. case 'L':
  1877. add_listener_addr(value);
  1878. break;
  1879. case 'E':
  1880. add_relay_addr(value);
  1881. break;
  1882. case 'X':
  1883. if (value) {
  1884. char *div = strchr(value, '/');
  1885. if (div) {
  1886. char *nval = strdup(value);
  1887. div = strchr(nval, '/');
  1888. div[0] = 0;
  1889. ++div;
  1890. ioa_addr apub, apriv;
  1891. if (make_ioa_addr((const uint8_t *)nval, 0, &apub) < 0) {
  1892. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "-X : Wrong address format: %s\n", nval);
  1893. } else {
  1894. if (make_ioa_addr((const uint8_t *)div, 0, &apriv) < 0) {
  1895. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "-X : Wrong address format: %s\n", div);
  1896. } else {
  1897. ioa_addr_add_mapping(&apub, &apriv);
  1898. if (add_ip_list_range((const char *)div, NULL, &turn_params.ip_whitelist) == 0) {
  1899. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Whitelisting external-ip private part: %s\n", div);
  1900. }
  1901. }
  1902. }
  1903. free(nval);
  1904. } else {
  1905. if (turn_params.external_ip) {
  1906. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "You cannot define external IP more than once in the configuration\n");
  1907. } else {
  1908. turn_params.external_ip =
  1909. (ioa_addr *)allocate_super_memory_engine(turn_params.listener.ioa_eng, sizeof(ioa_addr));
  1910. if (make_ioa_addr((const uint8_t *)value, 0, turn_params.external_ip) < 0) {
  1911. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "-X : Wrong address format: %s\n", value);
  1912. free(turn_params.external_ip);
  1913. turn_params.external_ip = NULL;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. break;
  1919. case 'v':
  1920. if (turn_params.verbose != TURN_VERBOSE_EXTRA) {
  1921. if (get_bool_value(value)) {
  1922. turn_params.verbose = TURN_VERBOSE_NORMAL;
  1923. } else {
  1924. turn_params.verbose = TURN_VERBOSE_NONE;
  1925. }
  1926. }
  1927. break;
  1928. case 'V':
  1929. if (get_bool_value(value)) {
  1930. turn_params.verbose = TURN_VERBOSE_EXTRA;
  1931. }
  1932. break;
  1933. case 'o':
  1934. turn_params.turn_daemon = get_bool_value(value);
  1935. break;
  1936. case 'a':
  1937. if (get_bool_value(value)) {
  1938. turn_params.ct = TURN_CREDENTIALS_LONG_TERM;
  1939. use_lt_credentials = 1;
  1940. use_ltc = 1;
  1941. } else {
  1942. turn_params.ct = TURN_CREDENTIALS_UNDEFINED;
  1943. use_lt_credentials = 0;
  1944. }
  1945. break;
  1946. case 'z':
  1947. if (!get_bool_value(value)) {
  1948. turn_params.ct = TURN_CREDENTIALS_UNDEFINED;
  1949. anon_credentials = 0;
  1950. } else {
  1951. turn_params.ct = TURN_CREDENTIALS_NONE;
  1952. anon_credentials = 1;
  1953. }
  1954. break;
  1955. case NO_SOFTWARE_ATTRIBUTE_OPT:
  1956. turn_params.no_software_attribute = get_bool_value(value);
  1957. break;
  1958. case 'f':
  1959. turn_params.fingerprint = get_bool_value(value);
  1960. break;
  1961. case 'u':
  1962. add_static_user_account(value);
  1963. break;
  1964. case 'b': {
  1965. #if defined(TURN_NO_SQLITE)
  1966. TURN_LOG_FUNC(
  1967. TURN_LOG_LEVEL_WARNING,
  1968. "WARNING: Options -b, --userdb and --db are not supported because SQLite is not supported in this build.\n");
  1969. #else
  1970. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, value);
  1971. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_SQLITE;
  1972. #endif
  1973. } break;
  1974. #if !defined(TURN_NO_PQ)
  1975. case 'e':
  1976. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, value);
  1977. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_PQ;
  1978. break;
  1979. #endif
  1980. #if !defined(TURN_NO_MYSQL)
  1981. case 'M':
  1982. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, value);
  1983. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_MYSQL;
  1984. break;
  1985. #endif
  1986. #if !defined(TURN_NO_MONGO)
  1987. case 'J':
  1988. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, value);
  1989. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_MONGO;
  1990. break;
  1991. #endif
  1992. #if !defined(TURN_NO_HIREDIS)
  1993. case 'N':
  1994. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, value);
  1995. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_REDIS;
  1996. break;
  1997. case 'O':
  1998. STRCPY(turn_params.redis_statsdb.connection_string, value);
  1999. turn_params.use_redis_statsdb = 1;
  2000. break;
  2001. #endif
  2002. case PROMETHEUS_OPT:
  2003. turn_params.prometheus = 1;
  2004. break;
  2005. case PROMETHEUS_PORT_OPT:
  2006. turn_params.prometheus_port = atoi(value);
  2007. break;
  2008. case PROMETHEUS_ENABLE_USERNAMES_OPT:
  2009. turn_params.prometheus_username_labels = 1;
  2010. break;
  2011. case AUTH_SECRET_OPT:
  2012. turn_params.use_auth_secret_with_timestamp = 1;
  2013. use_tltc = 1;
  2014. turn_params.ct = TURN_CREDENTIALS_LONG_TERM;
  2015. use_lt_credentials = 1;
  2016. break;
  2017. case NO_AUTH_PINGS_OPT:
  2018. turn_params.no_auth_pings = 1;
  2019. break;
  2020. case NO_DYNAMIC_IP_LIST_OPT:
  2021. turn_params.no_dynamic_ip_list = 1;
  2022. break;
  2023. case NO_DYNAMIC_REALMS_OPT:
  2024. turn_params.no_dynamic_realms = 1;
  2025. break;
  2026. case STATIC_AUTH_SECRET_VAL_OPT:
  2027. add_to_secrets_list(&turn_params.default_users_db.ram_db.static_auth_secrets, value);
  2028. turn_params.use_auth_secret_with_timestamp = 1;
  2029. use_tltc = 1;
  2030. turn_params.ct = TURN_CREDENTIALS_LONG_TERM;
  2031. use_lt_credentials = 1;
  2032. break;
  2033. case AUTH_SECRET_TS_EXP:
  2034. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "WARNING: Option --secret-ts-exp-time deprecated and has no effect.\n");
  2035. break;
  2036. case 'r':
  2037. set_default_realm_name(value);
  2038. break;
  2039. case 'q':
  2040. turn_params.user_quota = (vint)atoi(value);
  2041. get_realm(NULL)->options.perf_options.user_quota = atoi(value);
  2042. break;
  2043. case 'Q':
  2044. turn_params.total_quota = (vint)atoi(value);
  2045. get_realm(NULL)->options.perf_options.total_quota = atoi(value);
  2046. break;
  2047. case 's':
  2048. turn_params.max_bps = (band_limit_t)strtoul(value, NULL, 10);
  2049. get_realm(NULL)->options.perf_options.max_bps = (band_limit_t)strtoul(value, NULL, 10);
  2050. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%lu bytes per second allowed per session\n",
  2051. (unsigned long)turn_params.max_bps);
  2052. break;
  2053. case 'B':
  2054. turn_params.bps_capacity = (band_limit_t)strtoul(value, NULL, 10);
  2055. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%lu bytes per second allowed, combined server capacity\n",
  2056. (unsigned long)turn_params.bps_capacity);
  2057. break;
  2058. case CHECK_ORIGIN_CONSISTENCY_OPT:
  2059. turn_params.check_origin = get_bool_value(value);
  2060. break;
  2061. case NO_UDP_OPT:
  2062. turn_params.no_udp = get_bool_value(value);
  2063. break;
  2064. case NO_TCP_OPT:
  2065. turn_params.no_tcp = get_bool_value(value);
  2066. break;
  2067. case NO_UDP_RELAY_OPT:
  2068. turn_params.no_udp_relay = get_bool_value(value);
  2069. break;
  2070. case NO_TCP_RELAY_OPT:
  2071. turn_params.no_tcp_relay = get_bool_value(value);
  2072. break;
  2073. case NO_TLS_OPT:
  2074. #if !TLS_SUPPORTED
  2075. turn_params.no_tls = 1;
  2076. #else
  2077. turn_params.no_tls = get_bool_value(value);
  2078. #endif
  2079. break;
  2080. case NO_DTLS_OPT:
  2081. #if DTLS_SUPPORTED
  2082. turn_params.no_dtls = get_bool_value(value);
  2083. #else
  2084. turn_params.no_dtls = 1;
  2085. #endif
  2086. break;
  2087. case CERT_FILE_OPT:
  2088. STRCPY(turn_params.cert_file, value);
  2089. break;
  2090. case CA_FILE_OPT:
  2091. STRCPY(turn_params.ca_cert_file, value);
  2092. break;
  2093. case DH_FILE_OPT:
  2094. STRCPY(turn_params.dh_file, value);
  2095. break;
  2096. case SECRET_KEY_OPT:
  2097. STRCPY(turn_params.secret_key_file, value);
  2098. break;
  2099. case PKEY_FILE_OPT:
  2100. STRCPY(turn_params.pkey_file, value);
  2101. break;
  2102. case PKEY_PWD_OPT:
  2103. STRCPY(turn_params.tls_password, value);
  2104. break;
  2105. case ALTERNATE_SERVER_OPT:
  2106. add_alternate_server(value);
  2107. break;
  2108. case AUX_SERVER_OPT:
  2109. add_aux_server(value);
  2110. break;
  2111. case UDP_SELF_BALANCE_OPT:
  2112. turn_params.udp_self_balance = get_bool_value(value);
  2113. break;
  2114. case TLS_ALTERNATE_SERVER_OPT:
  2115. add_tls_alternate_server(value);
  2116. break;
  2117. case ALLOWED_PEER_IPS:
  2118. if (add_ip_list_range(value, NULL, &turn_params.ip_whitelist) == 0) {
  2119. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "White listing: %s\n", value);
  2120. }
  2121. break;
  2122. case DENIED_PEER_IPS:
  2123. if (add_ip_list_range(value, NULL, &turn_params.ip_blacklist) == 0) {
  2124. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Black listing: %s\n", value);
  2125. }
  2126. break;
  2127. case CIPHER_LIST_OPT:
  2128. STRCPY(turn_params.cipher_list, value);
  2129. break;
  2130. case PIDFILE_OPT:
  2131. STRCPY(turn_params.pidfile, value);
  2132. break;
  2133. case ACME_REDIRECT_OPT:
  2134. STRCPY(turn_params.acme_redirect, value);
  2135. break;
  2136. case 'C':
  2137. if (value && *value) {
  2138. turn_params.rest_api_separator = *value;
  2139. }
  2140. break;
  2141. case LOG_BINDING_OPT:
  2142. turn_params.log_binding = get_bool_value(value);
  2143. break;
  2144. case NO_RFC5780:
  2145. turn_params.rfc5780 = 0;
  2146. break;
  2147. case NO_STUN_BACKWARD_COMPATIBILITY_OPT:
  2148. turn_params.no_stun_backward_compatibility = get_bool_value(value);
  2149. break;
  2150. case RESPONSE_ORIGIN_ONLY_WITH_RFC5780_OPT:
  2151. turn_params.response_origin_only_with_rfc5780 = get_bool_value(value);
  2152. break;
  2153. case RESPOND_HTTP_UNSUPPORTED_OPT:
  2154. turn_params.respond_http_unsupported = get_bool_value(value);
  2155. break;
  2156. /* these options have been already taken care of before: */
  2157. case 'l':
  2158. case NO_STDOUT_LOG_OPT:
  2159. case SYSLOG_OPT:
  2160. case SIMPLE_LOG_OPT:
  2161. case NEW_LOG_TIMESTAMP_OPT:
  2162. case NEW_LOG_TIMESTAMP_FORMAT_OPT:
  2163. case SYSLOG_FACILITY_OPT:
  2164. case 'c':
  2165. case 'n':
  2166. case 'h':
  2167. break;
  2168. default:
  2169. fprintf(stderr, "\n%s\n", Usage);
  2170. exit(-1);
  2171. }
  2172. if (turn_params.default_users_db.persistent_users_db.userdb[0]) {
  2173. char *userdb_sanitized = sanitize_userdb_string(turn_params.default_users_db.persistent_users_db.userdb);
  2174. STRCPY(turn_params.default_users_db.persistent_users_db.userdb_sanitized, userdb_sanitized);
  2175. free(userdb_sanitized);
  2176. }
  2177. if (turn_params.redis_statsdb.connection_string[0]) {
  2178. char *connection_string = sanitize_userdb_string(turn_params.redis_statsdb.connection_string);
  2179. STRCPY(turn_params.redis_statsdb.connection_string_sanitized, connection_string);
  2180. free(connection_string);
  2181. }
  2182. }
  2183. static int parse_arg_string(char *sarg, int *c, char **value) {
  2184. int i = 0;
  2185. char *name = sarg;
  2186. while (*sarg) {
  2187. if ((*sarg == ' ') || (*sarg == '=') || (*sarg == '\t')) {
  2188. *sarg = 0;
  2189. do {
  2190. ++sarg;
  2191. } while ((*sarg == ' ') || (*sarg == '=') || (*sarg == '\t'));
  2192. *value = sarg;
  2193. break;
  2194. }
  2195. ++sarg;
  2196. *value = sarg;
  2197. }
  2198. if (value && *value && **value == '\"') {
  2199. *value += 1;
  2200. size_t len = strlen(*value);
  2201. while (len > 0 && (((*value)[len - 1] == '\n') || ((*value)[len - 1] == '\r') || ((*value)[len - 1] == ' ') ||
  2202. ((*value)[len - 1] == '\t'))) {
  2203. (*value)[--len] = 0;
  2204. }
  2205. if (len > 0 && (*value)[len - 1] == '\"') {
  2206. (*value)[--len] = 0;
  2207. }
  2208. }
  2209. while (long_options[i].name) {
  2210. if (strcmp(long_options[i].name, name)) {
  2211. ++i;
  2212. continue;
  2213. }
  2214. *c = long_options[i].val;
  2215. return 0;
  2216. }
  2217. return -1;
  2218. }
  2219. static void read_config_file(int argc, char **argv, int pass) {
  2220. static char config_file[1025] = DEFAULT_CONFIG_FILE;
  2221. if (pass == 0) {
  2222. if (argv) {
  2223. int i = 0;
  2224. for (i = 0; i < argc; i++) {
  2225. if (!strcmp(argv[i], "-c")) {
  2226. if (i < argc - 1) {
  2227. STRCPY(config_file, argv[i + 1]);
  2228. } else {
  2229. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "Wrong usage of -c option\n");
  2230. }
  2231. } else if (!strcmp(argv[i], "-n")) {
  2232. turn_params.do_not_use_config_file = 1;
  2233. config_file[0] = 0;
  2234. return;
  2235. } else if (!strcmp(argv[i], "-h")) {
  2236. printf("\n%s\n", Usage);
  2237. exit(0);
  2238. } else if (!strcmp(argv[i], "--version")) {
  2239. printf("%s\n", TURN_SERVER_VERSION);
  2240. exit(0);
  2241. }
  2242. }
  2243. }
  2244. }
  2245. if (!turn_params.do_not_use_config_file && config_file[0]) {
  2246. FILE *f = NULL;
  2247. char *full_path_to_config_file = NULL;
  2248. full_path_to_config_file = find_config_file(config_file, pass);
  2249. if (full_path_to_config_file) {
  2250. f = fopen(full_path_to_config_file, "r");
  2251. }
  2252. if (f) {
  2253. char sbuf[1025];
  2254. char sarg[1035];
  2255. for (;;) {
  2256. char *s = fgets(sbuf, sizeof(sbuf) - 1, f);
  2257. if (!s) {
  2258. break;
  2259. }
  2260. s = skip_blanks(s);
  2261. if (s[0] == '#') {
  2262. continue;
  2263. }
  2264. if (!s[0]) {
  2265. continue;
  2266. }
  2267. size_t slen = strlen(s);
  2268. // strip white-spaces from config file lines end
  2269. while (slen && isspace(s[slen - 1])) {
  2270. s[--slen] = 0;
  2271. }
  2272. if (slen) {
  2273. int c = 0;
  2274. char *value = NULL;
  2275. STRCPY(sarg, s);
  2276. if (parse_arg_string(sarg, &c, &value) < 0) {
  2277. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "Bad configuration format: %s\n", sarg);
  2278. } else if ((pass == 0) && (c == 'l')) {
  2279. set_logfile(value);
  2280. } else if ((pass == 0) && (c == NO_STDOUT_LOG_OPT)) {
  2281. set_no_stdout_log(get_bool_value(value));
  2282. } else if ((pass == 0) && (c == SYSLOG_OPT)) {
  2283. set_log_to_syslog(get_bool_value(value));
  2284. } else if ((pass == 0) && (c == SIMPLE_LOG_OPT)) {
  2285. set_simple_log(get_bool_value(value));
  2286. } else if ((pass == 0) && (c == NEW_LOG_TIMESTAMP_OPT)) {
  2287. use_new_log_timestamp_format = 1;
  2288. } else if ((pass == 0) && (c == NEW_LOG_TIMESTAMP_FORMAT_OPT)) {
  2289. set_turn_log_timestamp_format(value);
  2290. } else if ((pass == 0) && (c == SYSLOG_FACILITY_OPT)) {
  2291. set_syslog_facility(value);
  2292. } else if ((pass == 1) && (c != 'u')) {
  2293. set_option(c, value);
  2294. } else if ((pass == 2) && (c == 'u')) {
  2295. set_option(c, value);
  2296. }
  2297. if (s[slen - 1] == 59) {
  2298. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "Check config! The following line ends with semicolon: \"%s\" \n", s);
  2299. }
  2300. }
  2301. }
  2302. fclose(f);
  2303. } else if (pass == 0) {
  2304. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  2305. "Cannot find config file: %s. Default and command-line settings will be used.\n", config_file);
  2306. }
  2307. if (full_path_to_config_file) {
  2308. free(full_path_to_config_file);
  2309. full_path_to_config_file = NULL;
  2310. }
  2311. }
  2312. }
  2313. static int disconnect_database(void) {
  2314. const turn_dbdriver_t *dbd = get_dbdriver();
  2315. if (dbd && dbd->disconnect) {
  2316. dbd->disconnect();
  2317. }
  2318. return 0;
  2319. }
  2320. static int adminmain(int argc, char **argv) {
  2321. int c = 0;
  2322. int rc = 0;
  2323. TURNADMIN_COMMAND_TYPE ct = TA_COMMAND_UNKNOWN;
  2324. int is_admin = 0;
  2325. FILE *fptr;
  2326. unsigned char generated_key[16]; // changed
  2327. uint8_t user[STUN_MAX_USERNAME_SIZE + 1] = "\0";
  2328. uint8_t realm[STUN_MAX_REALM_SIZE + 1] = "\0";
  2329. uint8_t pwd[STUN_MAX_PWD_SIZE + 1] = "\0";
  2330. uint8_t secret[AUTH_SECRET_SIZE + 1] = "\0";
  2331. uint8_t origin[STUN_MAX_ORIGIN_SIZE + 1] = "\0";
  2332. perf_options_t po = {(band_limit_t)-1, -1, -1};
  2333. struct uoptions uo;
  2334. uo.u.m = admin_long_options;
  2335. int print_enc_password = 0;
  2336. int print_enc_aes_password = 0;
  2337. while (((c = getopt_long(argc, argv, ADMIN_OPTIONS, uo.u.o, NULL)) != -1)) {
  2338. switch (c) {
  2339. case 'P':
  2340. if (pwd[0]) {
  2341. char result[257];
  2342. generate_new_enc_password((char *)pwd, result);
  2343. printf("%s\n", result);
  2344. exit(0);
  2345. }
  2346. print_enc_password = 1;
  2347. break;
  2348. case 'E':
  2349. print_enc_aes_password = 1;
  2350. break;
  2351. case 'g':
  2352. ct = TA_SET_REALM_OPTION;
  2353. break;
  2354. case 'G':
  2355. ct = TA_LIST_REALM_OPTIONS;
  2356. break;
  2357. case ADMIN_USER_QUOTA_OPT:
  2358. po.user_quota = (vint)atoi(optarg);
  2359. break;
  2360. case ADMIN_TOTAL_QUOTA_OPT:
  2361. po.total_quota = (vint)atoi(optarg);
  2362. break;
  2363. case ADMIN_MAX_BPS_OPT:
  2364. po.max_bps = (vint)atoi(optarg);
  2365. break;
  2366. case 'O':
  2367. ct = TA_ADD_ORIGIN;
  2368. break;
  2369. case 'R':
  2370. ct = TA_DEL_ORIGIN;
  2371. break;
  2372. case 'I':
  2373. ct = TA_LIST_ORIGINS;
  2374. break;
  2375. case 'o':
  2376. STRCPY(origin, optarg);
  2377. break;
  2378. case 'k':
  2379. ct = TA_PRINT_KEY;
  2380. break;
  2381. case 'a':
  2382. ct = TA_UPDATE_USER;
  2383. break;
  2384. case 'd':
  2385. ct = TA_DELETE_USER;
  2386. break;
  2387. case 'A':
  2388. ct = TA_UPDATE_USER;
  2389. is_admin = 1;
  2390. break;
  2391. case 'D':
  2392. ct = TA_DELETE_USER;
  2393. is_admin = 1;
  2394. break;
  2395. case 'l':
  2396. ct = TA_LIST_USERS;
  2397. break;
  2398. case 'L':
  2399. ct = TA_LIST_USERS;
  2400. is_admin = 1;
  2401. break;
  2402. case 's':
  2403. ct = TA_SET_SECRET;
  2404. STRCPY(secret, optarg);
  2405. break;
  2406. case 'S':
  2407. ct = TA_SHOW_SECRET;
  2408. break;
  2409. case 'X':
  2410. ct = TA_DEL_SECRET;
  2411. if (optarg) {
  2412. STRCPY(secret, optarg);
  2413. }
  2414. break;
  2415. case DEL_ALL_AUTH_SECRETS_OPT:
  2416. ct = TA_DEL_SECRET;
  2417. break;
  2418. #if !defined(TURN_NO_SQLITE)
  2419. case 'b':
  2420. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, optarg);
  2421. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_SQLITE;
  2422. break;
  2423. #endif
  2424. #if !defined(TURN_NO_PQ)
  2425. case 'e':
  2426. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, optarg);
  2427. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_PQ;
  2428. break;
  2429. #endif
  2430. #if !defined(TURN_NO_MYSQL)
  2431. case 'M':
  2432. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, optarg);
  2433. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_MYSQL;
  2434. break;
  2435. #endif
  2436. #if !defined(TURN_NO_MONGO)
  2437. case 'J':
  2438. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, optarg);
  2439. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_MONGO;
  2440. break;
  2441. #endif
  2442. #if !defined(TURN_NO_HIREDIS)
  2443. case 'N':
  2444. STRCPY(turn_params.default_users_db.persistent_users_db.userdb, optarg);
  2445. turn_params.default_users_db.userdb_type = TURN_USERDB_TYPE_REDIS;
  2446. break;
  2447. #endif
  2448. case 'u':
  2449. STRCPY(user, optarg);
  2450. if (!is_secure_string((uint8_t *)user, 1)) {
  2451. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong user name structure or symbols, choose another name: %s\n", user);
  2452. exit(-1);
  2453. }
  2454. if (SASLprep((uint8_t *)user) < 0) {
  2455. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong user name: %s\n", user);
  2456. exit(-1);
  2457. }
  2458. break;
  2459. case 'r':
  2460. set_default_realm_name(optarg);
  2461. STRCPY(realm, optarg);
  2462. if (SASLprep((uint8_t *)realm) < 0) {
  2463. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong realm: %s\n", realm);
  2464. exit(-1);
  2465. }
  2466. break;
  2467. case 'p':
  2468. STRCPY(pwd, optarg);
  2469. if (SASLprep((uint8_t *)pwd) < 0) {
  2470. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong password: %s\n", pwd);
  2471. exit(-1);
  2472. }
  2473. if (print_enc_password) {
  2474. char result[257];
  2475. generate_new_enc_password((char *)pwd, result);
  2476. printf("%s\n", result);
  2477. exit(0);
  2478. }
  2479. if (print_enc_aes_password) {
  2480. encrypt_aes_128(pwd, generated_key);
  2481. exit(0);
  2482. }
  2483. break;
  2484. case 'x':
  2485. generate_aes_128_key(optarg, generated_key);
  2486. exit(0);
  2487. break;
  2488. case 'f':
  2489. fptr = fopen((char *)optarg, "r");
  2490. if (fptr == NULL) {
  2491. printf("No such file like %s\n", (char *)optarg);
  2492. } else {
  2493. fseek(fptr, 0, SEEK_SET);
  2494. rc = fread(generated_key, sizeof(char), 16, fptr);
  2495. if (rc == 0) {
  2496. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: Secret-Key file is empty\n", __FUNCTION__);
  2497. } else {
  2498. if (rc != 16) {
  2499. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: Secret-Key length is not enough\n", __FUNCTION__);
  2500. }
  2501. }
  2502. fclose(fptr);
  2503. }
  2504. break;
  2505. case 'v':
  2506. decrypt_aes_128((char *)optarg, generated_key);
  2507. exit(0);
  2508. case 'h':
  2509. printf("\n%s\n", AdminUsage);
  2510. exit(0);
  2511. break;
  2512. default:
  2513. fprintf(stderr, "\n%s\n", AdminUsage);
  2514. exit(-1);
  2515. }
  2516. }
  2517. #if !defined(TURN_NO_SQLITE)
  2518. if (!strlen(turn_params.default_users_db.persistent_users_db.userdb) &&
  2519. (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_SQLITE)) {
  2520. strncpy(turn_params.default_users_db.persistent_users_db.userdb, DEFAULT_USERDB_FILE, TURN_LONG_STRING_SIZE);
  2521. }
  2522. #endif
  2523. if (ct == TA_COMMAND_UNKNOWN) {
  2524. fprintf(stderr, "\n%s\n", AdminUsage);
  2525. exit(-1);
  2526. }
  2527. argc -= optind;
  2528. argv += optind;
  2529. if (argc != 0) {
  2530. fprintf(stderr, "\n%s\n", AdminUsage);
  2531. exit(-1);
  2532. }
  2533. int result = adminuser(user, realm, pwd, secret, origin, ct, &po, is_admin);
  2534. disconnect_database();
  2535. return result;
  2536. }
  2537. static void print_features(unsigned long mfn) {
  2538. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Coturn Version %s\n", TURN_SOFTWARE);
  2539. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Max number of open files/sockets allowed for this process: %lu\n", mfn);
  2540. if (turn_params.net_engine_version == NEV_UDP_SOCKET_PER_ENDPOINT) {
  2541. mfn = mfn / 3;
  2542. } else {
  2543. mfn = mfn / 2;
  2544. }
  2545. mfn = ((unsigned long)(mfn / 500)) * 500;
  2546. if (mfn < 500) {
  2547. mfn = 500;
  2548. }
  2549. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,
  2550. "Due to the open files/sockets limitation, max supported number of TURN Sessions possible is: %lu "
  2551. "(approximately)\n",
  2552. mfn);
  2553. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "\n\n==== Show him the instruments, Practical Frost: ====\n\n");
  2554. /*
  2555. Frost stepped forward and opened the polished case with a theatrical
  2556. flourish. It was a masterful piece of craftsmanship. As the lid was
  2557. pulled back, the many trays inside lifted and fanned out, displaying
  2558. Glokta’s tools in all their gruesome glory. There were blades of every
  2559. size and shape, needles curved and straight, bottles of oil and acid,
  2560. nails and screws, clamps and pliers, saws, hammers, chisels. Metal, wood
  2561. and glass glittered in the bright lamplight, all polished to mirror
  2562. brightness and honed to a murderous sharpness.
  2563. */
  2564. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "OpenSSL compile-time version: %s (0x%lx)\n", OPENSSL_VERSION_TEXT,
  2565. OPENSSL_VERSION_NUMBER);
  2566. #if !TLS_SUPPORTED
  2567. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TLS is not supported\n");
  2568. #elif TLSv1_3_SUPPORTED
  2569. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TLS 1.3 supported\n");
  2570. #elif TLSv1_2_SUPPORTED
  2571. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TLS 1.2 supported\n");
  2572. #elif TLSv1_1_SUPPORTED
  2573. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TLS 1.1 supported\n");
  2574. #elif TLSv1_SUPPORTED
  2575. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TLS 1.0 supported\n");
  2576. #endif
  2577. #if !DTLS_SUPPORTED
  2578. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "DTLS is not supported\n");
  2579. #elif DTLSv1_2_SUPPORTED
  2580. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "DTLS 1.2 supported\n");
  2581. #elif DTLS_SUPPORTED
  2582. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "DTLS supported\n");
  2583. #endif
  2584. #if ALPN_SUPPORTED
  2585. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TURN/STUN ALPN supported\n");
  2586. #else
  2587. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TURN/STUN ALPN is not supported\n");
  2588. #endif
  2589. if (ENC_ALG_NUM == 0) {
  2590. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Third-party authorization (oAuth) is not supported\n");
  2591. } else {
  2592. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Third-party authorization (oAuth) supported\n");
  2593. #if defined(TURN_NO_GCM)
  2594. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "GCM (AEAD) is not supported\n");
  2595. #else
  2596. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "GCM (AEAD) supported\n");
  2597. #endif
  2598. }
  2599. #if !defined(TURN_NO_SQLITE)
  2600. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "SQLite supported, default database location is %s\n", DEFAULT_USERDB_FILE);
  2601. #else
  2602. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "SQLite is not supported\n");
  2603. #endif
  2604. #if !defined(TURN_NO_HIREDIS)
  2605. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Redis supported\n");
  2606. #else
  2607. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Redis is not supported\n");
  2608. #endif
  2609. #if !defined(TURN_NO_PQ)
  2610. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "PostgreSQL supported\n");
  2611. #else
  2612. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "PostgreSQL is not supported\n");
  2613. #endif
  2614. #if !defined(TURN_NO_MYSQL)
  2615. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "MySQL supported\n");
  2616. #else
  2617. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "MySQL is not supported\n");
  2618. #endif
  2619. #if !defined(TURN_NO_MONGO)
  2620. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "MongoDB supported\n");
  2621. #else
  2622. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "MongoDB is not supported\n");
  2623. #endif
  2624. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Default Net Engine version: %d (%s)\n", (int)turn_params.net_engine_version,
  2625. turn_params.net_engine_version_txt[(int)turn_params.net_engine_version]);
  2626. }
  2627. #if defined(__linux__) || defined(__LINUX__) || defined(__linux) || defined(linux__) || defined(LINUX) || \
  2628. defined(__LINUX) || defined(LINUX__)
  2629. #include <linux/version.h>
  2630. #endif
  2631. static void set_network_engine(void) {
  2632. if (turn_params.net_engine_version != NEV_UNKNOWN) {
  2633. return;
  2634. }
  2635. turn_params.net_engine_version = NEV_UDP_SOCKET_PER_ENDPOINT;
  2636. #if defined(SO_REUSEPORT)
  2637. #if defined(__linux__) || defined(__LINUX__) || defined(__linux) || defined(linux__) || defined(LINUX) || \
  2638. defined(__LINUX) || defined(LINUX__)
  2639. turn_params.net_engine_version = NEV_UDP_SOCKET_PER_THREAD;
  2640. #else /* BSD ? */
  2641. turn_params.net_engine_version = NEV_UDP_SOCKET_PER_SESSION;
  2642. #endif /* Linux */
  2643. #else /* defined(SO_REUSEPORT) */
  2644. #if defined(__linux__) || defined(__LINUX__) || defined(__linux) || defined(linux__) || defined(LINUX) || \
  2645. defined(__LINUX) || defined(LINUX__)
  2646. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
  2647. // net_engine_version = NEV_UDP_SOCKET_PER_SESSION;
  2648. turn_params.net_engine_version = NEV_UDP_SOCKET_PER_ENDPOINT;
  2649. #else
  2650. turn_params.net_engine_version = NEV_UDP_SOCKET_PER_ENDPOINT;
  2651. #endif /* Linux version */
  2652. #endif /* Linux */
  2653. #endif /* defined(SO_REUSEPORT) */
  2654. }
  2655. static void drop_privileges(void) {
  2656. #if defined(WINDOWS)
  2657. // TODO: implement it!!!
  2658. #else
  2659. setgroups(0, NULL);
  2660. if (procgroupid_set) {
  2661. if (getgid() != procgroupid) {
  2662. if (setgid(procgroupid) != 0) {
  2663. perror("setgid: Unable to change group privileges");
  2664. exit(-1);
  2665. } else {
  2666. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "New GID: %s(%lu)\n", procgroupname, (unsigned long)procgroupid);
  2667. }
  2668. } else {
  2669. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Keep GID: %s(%lu)\n", procgroupname, (unsigned long)procgroupid);
  2670. }
  2671. }
  2672. if (procuserid_set) {
  2673. if (procuserid != getuid()) {
  2674. if (setuid(procuserid) != 0) {
  2675. perror("setuid: Unable to change user privileges");
  2676. exit(-1);
  2677. } else {
  2678. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "New UID: %s(%lu)\n", procusername, (unsigned long)procuserid);
  2679. }
  2680. } else {
  2681. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Keep UID: %s(%lu)\n", procusername, (unsigned long)procuserid);
  2682. }
  2683. }
  2684. #endif
  2685. }
  2686. static void init_domain(void) {
  2687. #if !defined(TURN_NO_GETDOMAINNAME)
  2688. if (getdomainname(turn_params.domain, sizeof(turn_params.domain) - 1) < 0) {
  2689. turn_params.domain[0] = 0;
  2690. } else if (!strcmp(turn_params.domain, "(none)")) {
  2691. turn_params.domain[0] = 0;
  2692. }
  2693. #endif
  2694. }
  2695. int main(int argc, char **argv) {
  2696. int c = 0;
  2697. IS_TURN_SERVER = 1;
  2698. TURN_MUTEX_INIT(&turn_params.tls_mutex);
  2699. set_execdir();
  2700. init_super_memory();
  2701. init_domain();
  2702. create_default_realm();
  2703. init_turn_server_addrs_list(&turn_params.alternate_servers_list);
  2704. init_turn_server_addrs_list(&turn_params.tls_alternate_servers_list);
  2705. init_turn_server_addrs_list(&turn_params.aux_servers_list);
  2706. set_network_engine();
  2707. init_listener();
  2708. init_secrets_list(&turn_params.default_users_db.ram_db.static_auth_secrets);
  2709. init_dynamic_ip_lists();
  2710. if (!strstr(argv[0], "turnadmin")) {
  2711. struct uoptions uo;
  2712. uo.u.m = long_options;
  2713. while (((c = getopt_long(argc, argv, OPTIONS, uo.u.o, NULL)) != -1)) {
  2714. switch (c) {
  2715. case 'l':
  2716. set_logfile(optarg);
  2717. break;
  2718. case NO_STDOUT_LOG_OPT:
  2719. set_no_stdout_log(get_bool_value(optarg));
  2720. break;
  2721. case SYSLOG_OPT:
  2722. set_log_to_syslog(get_bool_value(optarg));
  2723. break;
  2724. case SIMPLE_LOG_OPT:
  2725. set_simple_log(get_bool_value(optarg));
  2726. break;
  2727. case NEW_LOG_TIMESTAMP_OPT:
  2728. use_new_log_timestamp_format = 1;
  2729. break;
  2730. case NEW_LOG_TIMESTAMP_FORMAT_OPT:
  2731. set_turn_log_timestamp_format(optarg);
  2732. break;
  2733. case SYSLOG_FACILITY_OPT:
  2734. set_syslog_facility(optarg);
  2735. break;
  2736. default:;
  2737. }
  2738. }
  2739. }
  2740. optind = 0;
  2741. #if !TLS_SUPPORTED
  2742. turn_params.no_tls = 1;
  2743. #endif
  2744. #if !DTLS_SUPPORTED
  2745. turn_params.no_dtls = 1;
  2746. #endif
  2747. if (strstr(argv[0], "turnadmin")) {
  2748. return adminmain(argc, argv);
  2749. }
  2750. // Zero pass apply the log options.
  2751. read_config_file(argc, argv, 0);
  2752. // First pass read other config options
  2753. read_config_file(argc, argv, 1);
  2754. {
  2755. int cpus = get_system_number_of_cpus();
  2756. if (0 < cpus) {
  2757. turn_params.cpus = get_system_number_of_cpus();
  2758. }
  2759. if (turn_params.cpus < DEFAULT_CPUS_NUMBER) {
  2760. turn_params.cpus = DEFAULT_CPUS_NUMBER;
  2761. } else if (turn_params.cpus > MAX_NUMBER_OF_GENERAL_RELAY_SERVERS) {
  2762. turn_params.cpus = MAX_NUMBER_OF_GENERAL_RELAY_SERVERS;
  2763. }
  2764. turn_params.general_relay_servers_number = (turnserver_id)turn_params.cpus;
  2765. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "System cpu num is %lu\n", turn_params.cpus);
  2766. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "System enable num is %lu\n", get_system_active_number_of_cpus());
  2767. }
  2768. memset(&turn_params.default_users_db, 0, sizeof(default_users_db_t));
  2769. turn_params.default_users_db.ram_db.static_accounts = ur_string_map_create(free);
  2770. struct uoptions uo;
  2771. uo.u.m = long_options;
  2772. while (((c = getopt_long(argc, argv, OPTIONS, uo.u.o, NULL)) != -1)) {
  2773. if (c != 'u') {
  2774. set_option(c, optarg);
  2775. }
  2776. }
  2777. // Second pass read -u options
  2778. read_config_file(argc, argv, 2);
  2779. {
  2780. unsigned long mfn = set_system_parameters(1);
  2781. print_features(mfn);
  2782. }
  2783. if (!get_realm(NULL)->options.name[0]) {
  2784. STRCPY(get_realm(NULL)->options.name, turn_params.domain);
  2785. }
  2786. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Domain name: %s\n", turn_params.domain);
  2787. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Default realm: %s\n", get_realm(NULL)->options.name);
  2788. if (turn_params.acme_redirect[0]) {
  2789. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "ACME redirect URL: %s\n", turn_params.acme_redirect);
  2790. }
  2791. if (turn_params.oauth && turn_params.oauth_server_name[0]) {
  2792. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "oAuth server name: %s\n", turn_params.oauth_server_name);
  2793. }
  2794. optind = 0;
  2795. while (((c = getopt_long(argc, argv, OPTIONS, uo.u.o, NULL)) != -1)) {
  2796. if (c == 'u') {
  2797. set_option(c, optarg);
  2798. }
  2799. }
  2800. if (turn_params.bps_capacity && !(turn_params.max_bps)) {
  2801. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  2802. "\nCONFIG ERROR: If you set the --bps-capacity option, then you must set --max-bps options, too.\n");
  2803. exit(-1);
  2804. }
  2805. if (turn_params.no_udp_relay && turn_params.no_tcp_relay) {
  2806. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  2807. "\nCONFIG ERROR: --no-udp-relay and --no-tcp-relay options cannot be used together.\n");
  2808. exit(-1);
  2809. }
  2810. if (turn_params.no_udp_relay) {
  2811. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "CONFIG: --no-udp-relay: UDP relay endpoints are not allowed.\n");
  2812. }
  2813. if (turn_params.no_tcp_relay) {
  2814. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "CONFIG: --no-tcp-relay: TCP relay endpoints are not allowed.\n");
  2815. }
  2816. if (turn_params.server_relay) {
  2817. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "CONFIG: WARNING: --server-relay: NON-STANDARD AND DANGEROUS OPTION.\n");
  2818. }
  2819. #if !defined(TURN_NO_SQLITE)
  2820. if (!strlen(turn_params.default_users_db.persistent_users_db.userdb) &&
  2821. (turn_params.default_users_db.userdb_type == TURN_USERDB_TYPE_SQLITE)) {
  2822. strncpy(turn_params.default_users_db.persistent_users_db.userdb, DEFAULT_USERDB_FILE, TURN_LONG_STRING_SIZE);
  2823. }
  2824. #endif
  2825. argc -= optind;
  2826. argv += optind;
  2827. if (argc > 0) {
  2828. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "CONFIG: Unknown argument: %s\n", argv[argc - 1]);
  2829. }
  2830. if (use_lt_credentials && anon_credentials) {
  2831. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "CONFIG: -a and -z options cannot be used together.\n");
  2832. exit(-1);
  2833. }
  2834. if (use_ltc && use_tltc) {
  2835. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  2836. "CONFIG: You specified --lt-cred-mech and --use-auth-secret in the same time.\n"
  2837. "Be aware that you could not mix the username/password and the shared secret based auth methods. \n"
  2838. "Shared secret overrides username/password based auth method. Check your configuration!\n");
  2839. }
  2840. if (turn_params.allow_loopback_peers) {
  2841. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  2842. "CONFIG: allow_loopback_peers opens a possible security vulnerability. Do not use in production!!\n");
  2843. if (cli_password[0] == 0 && use_cli) {
  2844. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  2845. "CONFIG: allow_loopback_peers and empty cli password cannot be used together.\n");
  2846. exit(-1);
  2847. }
  2848. }
  2849. if (use_cli && cli_password[0] == 0) {
  2850. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "CONFIG: Empty cli-password, and so telnet cli interface is disabled! "
  2851. "Please set a non empty cli-password!\n");
  2852. use_cli = 0;
  2853. }
  2854. if (!use_lt_credentials && !anon_credentials) {
  2855. if (turn_params.default_users_db.ram_db.users_number) {
  2856. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  2857. "CONFIG: you specified long-term user accounts, (-u option) \n but you did "
  2858. "not specify the long-term credentials option\n (-a or --lt-cred-mech option).\n I am "
  2859. "turning --lt-cred-mech ON for you, but double-check your configuration.\n");
  2860. turn_params.ct = TURN_CREDENTIALS_LONG_TERM;
  2861. use_lt_credentials = 1;
  2862. } else {
  2863. turn_params.ct = TURN_CREDENTIALS_NONE;
  2864. use_lt_credentials = 0;
  2865. }
  2866. }
  2867. if (use_lt_credentials) {
  2868. if (!get_realm(NULL)->options.name[0]) {
  2869. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  2870. "CONFIG: you did specify the long-term credentials usage\n but you did not specify "
  2871. "the default realm option (-r option).\n Check your configuration.\n");
  2872. }
  2873. }
  2874. if (anon_credentials) {
  2875. if (turn_params.default_users_db.ram_db.users_number) {
  2876. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING,
  2877. "CONFIG: you specified user accounts, (-u option) but you also specified the "
  2878. "anonymous user access option (-z or --no-auth option). User accounts will be ignored.\n");
  2879. turn_params.ct = TURN_CREDENTIALS_NONE;
  2880. use_lt_credentials = 0;
  2881. }
  2882. }
  2883. if (use_web_admin && turn_params.no_tls) {
  2884. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "CONFIG: WARNING: web-admin support not compatible with --no-tls option.\n");
  2885. use_web_admin = 0;
  2886. }
  2887. openssl_setup();
  2888. int local_listeners = 0;
  2889. if (!turn_params.listener.addrs_number) {
  2890. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "NO EXPLICIT LISTENER ADDRESS(ES) ARE CONFIGURED\n");
  2891. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "===========Discovering listener addresses: =========\n");
  2892. int maddrs = make_local_listeners_list();
  2893. if ((maddrs < 1) || !turn_params.listener.addrs_number) {
  2894. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: Cannot configure any meaningful IP listener address\n", __FUNCTION__);
  2895. fprintf(stderr, "\n%s\n", Usage);
  2896. exit(-1);
  2897. }
  2898. local_listeners = 1;
  2899. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "=====================================================\n");
  2900. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Total: %d 'real' addresses discovered\n", maddrs);
  2901. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "=====================================================\n");
  2902. }
  2903. if (!turn_params.relays_number) {
  2904. if (!local_listeners && turn_params.listener.addrs_number && turn_params.listener.addrs) {
  2905. size_t la = 0;
  2906. for (la = 0; la < turn_params.listener.addrs_number; la++) {
  2907. if (turn_params.listener.addrs[la]) {
  2908. add_relay_addr(turn_params.listener.addrs[la]);
  2909. }
  2910. }
  2911. }
  2912. if (!turn_params.relays_number) {
  2913. turn_params.default_relays = 1;
  2914. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "NO EXPLICIT RELAY ADDRESS(ES) ARE CONFIGURED\n");
  2915. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "===========Discovering relay addresses: =============\n");
  2916. if (make_local_relays_list(0, AF_INET) < 1) {
  2917. make_local_relays_list(1, AF_INET);
  2918. }
  2919. if (make_local_relays_list(0, AF_INET6) < 1) {
  2920. make_local_relays_list(1, AF_INET6);
  2921. }
  2922. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "=====================================================\n");
  2923. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Total: %d relay addresses discovered\n", (int)turn_params.relays_number);
  2924. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "=====================================================\n");
  2925. }
  2926. if (!turn_params.relays_number) {
  2927. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: You must specify the relay address(es)\n", __FUNCTION__);
  2928. fprintf(stderr, "\n%s\n", Usage);
  2929. exit(-1);
  2930. }
  2931. }
  2932. if (turn_params.external_ip && turn_params.relay_addrs) {
  2933. size_t ir = 0;
  2934. for (ir = 0; ir < turn_params.relays_number; ++ir) {
  2935. if (turn_params.relay_addrs[ir]) {
  2936. const char *sra = (const char *)turn_params.relay_addrs[ir];
  2937. if ((strstr(sra, "127.0.0.1") != sra) && (strstr(sra, "::1") != sra)) {
  2938. ioa_addr ra;
  2939. if (make_ioa_addr((const uint8_t *)sra, 0, &ra) < 0) {
  2940. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "-X : Wrong address format: %s\n", sra);
  2941. } else if (ra.ss.sa_family == turn_params.external_ip->ss.sa_family) {
  2942. ioa_addr_add_mapping(turn_params.external_ip, &ra);
  2943. }
  2944. }
  2945. }
  2946. }
  2947. }
  2948. if (socket_init()) {
  2949. return -1;
  2950. }
  2951. #if defined(WINDOWS)
  2952. // TODO: implement deamon!!! use windows server
  2953. #else
  2954. if (turn_params.turn_daemon) {
  2955. #if !defined(TURN_HAS_DAEMON)
  2956. pid_t pid = fork();
  2957. if (pid > 0) {
  2958. exit(0);
  2959. }
  2960. if (pid < 0) {
  2961. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot start daemon process\n");
  2962. exit(-1);
  2963. }
  2964. #else
  2965. if (daemon(1, 0) < 0) {
  2966. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot start daemon process\n");
  2967. exit(-1);
  2968. }
  2969. reset_rtpprintf();
  2970. #endif
  2971. }
  2972. if (turn_params.pidfile[0]) {
  2973. char s[2049];
  2974. FILE *f = fopen(turn_params.pidfile, "w");
  2975. if (f) {
  2976. STRCPY(s, turn_params.pidfile);
  2977. } else {
  2978. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "Cannot create pid file: %s\n", turn_params.pidfile);
  2979. {
  2980. const char *pfs[] = {"/var/run/turnserver.pid",
  2981. "/var/spool/turnserver.pid",
  2982. "/var/turnserver.pid",
  2983. "/var/tmp/turnserver.pid",
  2984. "/tmp/turnserver.pid",
  2985. "turnserver.pid",
  2986. NULL};
  2987. const char **ppfs = pfs;
  2988. while (*ppfs) {
  2989. f = fopen(*ppfs, "w");
  2990. if (f) {
  2991. STRCPY(s, *ppfs);
  2992. break;
  2993. } else {
  2994. ++ppfs;
  2995. }
  2996. }
  2997. }
  2998. }
  2999. if (f) {
  3000. fprintf(f, "%lu\n", (unsigned long)getpid());
  3001. fclose(f);
  3002. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "pid file created: %s\n", s);
  3003. }
  3004. }
  3005. #endif
  3006. setup_server();
  3007. #if defined(WINDOWS)
  3008. // TODO: implement it!!! add windows server
  3009. #else
  3010. struct event *ev = evsignal_new(turn_params.listener.event_base, SIGUSR2, reload_ssl_certs, NULL);
  3011. event_add(ev, NULL);
  3012. ev = evsignal_new(turn_params.listener.event_base, SIGTERM, shutdown_handler, NULL);
  3013. event_add(ev, NULL);
  3014. ev = evsignal_new(turn_params.listener.event_base, SIGINT, shutdown_handler, NULL);
  3015. event_add(ev, NULL);
  3016. #endif
  3017. drop_privileges();
  3018. start_prometheus_server();
  3019. run_listener_server(&(turn_params.listener));
  3020. disconnect_database();
  3021. return 0;
  3022. }
  3023. ////////// OpenSSL locking ////////////////////////////////////////
  3024. #if defined(OPENSSL_THREADS)
  3025. #if OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0
  3026. // array larger than anything that OpenSSL may need:
  3027. static TURN_MUTEX_DECLARE(mutex_buf[256]);
  3028. static int mutex_buf_initialized = 0;
  3029. void coturn_locking_function(int mode, int n, const char *file, int line);
  3030. void coturn_locking_function(int mode, int n, const char *file, int line) {
  3031. UNUSED_ARG(file);
  3032. UNUSED_ARG(line);
  3033. if (mutex_buf_initialized && (n < CRYPTO_num_locks())) {
  3034. if (mode & CRYPTO_LOCK) {
  3035. TURN_MUTEX_LOCK(&(mutex_buf[n]));
  3036. } else {
  3037. TURN_MUTEX_UNLOCK(&(mutex_buf[n]));
  3038. }
  3039. }
  3040. }
  3041. void coturn_id_function(CRYPTO_THREADID *ctid);
  3042. void coturn_id_function(CRYPTO_THREADID *ctid) {
  3043. UNUSED_ARG(ctid);
  3044. CRYPTO_THREADID_set_numeric(ctid, (unsigned long)pthread_self());
  3045. }
  3046. static int THREAD_setup(void) {
  3047. int i;
  3048. for (i = 0; i < CRYPTO_num_locks(); i++) {
  3049. TURN_MUTEX_INIT(&(mutex_buf[i]));
  3050. }
  3051. mutex_buf_initialized = 1;
  3052. CRYPTO_THREADID_set_callback(coturn_id_function);
  3053. CRYPTO_set_locking_callback(coturn_locking_function);
  3054. return 1;
  3055. }
  3056. int THREAD_cleanup(void) {
  3057. int i;
  3058. if (!mutex_buf_initialized) {
  3059. return 0;
  3060. }
  3061. CRYPTO_THREADID_set_callback(NULL);
  3062. CRYPTO_set_locking_callback(NULL);
  3063. for (i = 0; i < CRYPTO_num_locks(); i++) {
  3064. TURN_MUTEX_DESTROY(&(mutex_buf[i]));
  3065. }
  3066. mutex_buf_initialized = 0;
  3067. return 1;
  3068. }
  3069. #else
  3070. static int THREAD_setup(void) { return 1; }
  3071. int THREAD_cleanup(void);
  3072. int THREAD_cleanup(void) { return 1; }
  3073. #endif /* OPENSSL_VERSION_NUMBER < OPENSSL_VERSION_1_1_0 */
  3074. #endif /* defined(OPENSSL_THREADS) */
  3075. static void adjust_key_file_name(char *fn, const char *file_title, int critical) {
  3076. char *full_path_to_file = NULL;
  3077. if (!fn[0]) {
  3078. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "\nERROR: you must set the %s file parameter\n", file_title);
  3079. goto keyerr;
  3080. } else {
  3081. full_path_to_file = find_config_file(fn, 1);
  3082. {
  3083. FILE *f = full_path_to_file ? fopen(full_path_to_file, "r") : NULL;
  3084. if (!f) {
  3085. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "cannot find %s file: %s (1)\n", file_title, fn);
  3086. goto keyerr;
  3087. } else {
  3088. fclose(f);
  3089. }
  3090. }
  3091. if (!full_path_to_file) {
  3092. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "cannot find %s file: %s (2)\n", file_title, fn);
  3093. goto keyerr;
  3094. }
  3095. strncpy(fn, full_path_to_file, sizeof(turn_params.cert_file) - 1);
  3096. fn[sizeof(turn_params.cert_file) - 1] = 0;
  3097. free(full_path_to_file);
  3098. return;
  3099. }
  3100. keyerr:
  3101. if (critical) {
  3102. turn_params.no_tls = 1;
  3103. turn_params.no_dtls = 1;
  3104. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "cannot start TLS and DTLS listeners because %s file is not set properly\n",
  3105. file_title);
  3106. }
  3107. if (full_path_to_file) {
  3108. free(full_path_to_file);
  3109. }
  3110. return;
  3111. }
  3112. static void adjust_key_file_names(void) {
  3113. if (turn_params.ca_cert_file[0]) {
  3114. adjust_key_file_name(turn_params.ca_cert_file, "CA", 1);
  3115. }
  3116. adjust_key_file_name(turn_params.cert_file, "certificate", 1);
  3117. adjust_key_file_name(turn_params.pkey_file, "private key", 1);
  3118. if (turn_params.dh_file[0]) {
  3119. adjust_key_file_name(turn_params.dh_file, "DH key", 0);
  3120. }
  3121. }
  3122. static DH *get_dh566(void) {
  3123. unsigned char dh566_p[] = {0x36, 0x53, 0xA8, 0x9C, 0x3C, 0xF1, 0xD1, 0x1B, 0x2D, 0xA2, 0x64, 0xDE, 0x59, 0x3B, 0xE3,
  3124. 0x8C, 0x27, 0x74, 0xC2, 0xBE, 0x9B, 0x6D, 0x56, 0xE7, 0xDF, 0xFF, 0x67, 0x6A, 0xD2, 0x0C,
  3125. 0xE8, 0x9E, 0x52, 0x00, 0x05, 0xB3, 0x53, 0xF7, 0x1C, 0x41, 0xB2, 0xAC, 0x38, 0x16, 0x32,
  3126. 0x3A, 0x8E, 0x90, 0x6C, 0x7E, 0xD1, 0x44, 0xCB, 0xF9, 0x2D, 0x1E, 0x4A, 0x9A, 0x32, 0x81,
  3127. 0x58, 0xE1, 0xE1, 0x17, 0xC1, 0x9C, 0xF1, 0x1E, 0x96, 0x2D, 0x5F};
  3128. // -----BEGIN DH PARAMETERS-----
  3129. // MEwCRzZTqJw88dEbLaJk3lk744wndMK+m21W59//Z2rSDOieUgAFs1P3HEGyrDgW
  3130. // MjqOkGx+0UTL+S0eSpoygVjh4RfBnPEeli1fAgEF
  3131. // -----END DH PARAMETERS-----
  3132. unsigned char dh566_g[] = {0x05};
  3133. DH *dh;
  3134. if ((dh = DH_new()) == NULL) {
  3135. return (NULL);
  3136. }
  3137. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3138. dh->p = BN_bin2bn(dh566_p, sizeof(dh566_p), NULL);
  3139. dh->g = BN_bin2bn(dh566_g, sizeof(dh566_g), NULL);
  3140. if ((dh->p == NULL) || (dh->g == NULL)) {
  3141. DH_free(dh);
  3142. return (NULL);
  3143. }
  3144. #else
  3145. DH_set0_pqg(dh, BN_bin2bn(dh566_p, sizeof(dh566_p), NULL), NULL, BN_bin2bn(dh566_g, sizeof(dh566_g), NULL));
  3146. #endif
  3147. return (dh);
  3148. }
  3149. static DH *get_dh1066(void) {
  3150. unsigned char dh1066_p[] = {0x02, 0x0E, 0x26, 0x6F, 0xAA, 0x9F, 0xA8, 0xE5, 0x3F, 0x70, 0x88, 0xF1, 0xA9, 0x29, 0xAE,
  3151. 0x1A, 0x2B, 0xA8, 0x2F, 0xE8, 0xE5, 0x0E, 0x81, 0x78, 0xD7, 0x12, 0x41, 0xDC, 0xE2, 0xD5,
  3152. 0x10, 0x6F, 0x8A, 0x35, 0x23, 0xCE, 0x66, 0x93, 0x67, 0x14, 0xEA, 0x0A, 0x61, 0xD4, 0x43,
  3153. 0x63, 0x5C, 0xDF, 0xDE, 0xF5, 0xB9, 0xC6, 0xB4, 0x8C, 0xBA, 0x1A, 0x25, 0x9F, 0x73, 0x0F,
  3154. 0x1E, 0x1A, 0x97, 0x42, 0x2E, 0x60, 0x9E, 0x4C, 0x3C, 0x70, 0x6A, 0xFB, 0xDD, 0xAA, 0x7A,
  3155. 0x48, 0xA5, 0x1E, 0x87, 0xC8, 0xA3, 0x5E, 0x26, 0x40, 0x1B, 0xDE, 0x08, 0x5E, 0xA2, 0xB8,
  3156. 0xE8, 0x76, 0x43, 0xE8, 0xF1, 0x4B, 0x35, 0x4C, 0x38, 0x92, 0xB9, 0xFF, 0x61, 0xE6, 0x6C,
  3157. 0xBA, 0xF9, 0x16, 0x36, 0x3C, 0x69, 0x2D, 0x57, 0x90, 0x62, 0x8A, 0xD0, 0xD4, 0xFB, 0xB2,
  3158. 0x5A, 0x61, 0x99, 0xA9, 0xE8, 0x93, 0x80, 0xA2, 0xB7, 0xDC, 0xB1, 0x6A, 0xAF, 0xE3};
  3159. // -----BEGIN DH PARAMETERS-----
  3160. // MIGMAoGGAg4mb6qfqOU/cIjxqSmuGiuoL+jlDoF41xJB3OLVEG+KNSPOZpNnFOoK
  3161. // YdRDY1zf3vW5xrSMuholn3MPHhqXQi5gnkw8cGr73ap6SKUeh8ijXiZAG94IXqK4
  3162. // 6HZD6PFLNUw4krn/YeZsuvkWNjxpLVeQYorQ1PuyWmGZqeiTgKK33LFqr+MCAQI=
  3163. // -----END DH PARAMETERS-----
  3164. unsigned char dh1066_g[] = {0x02};
  3165. DH *dh;
  3166. if ((dh = DH_new()) == NULL) {
  3167. return (NULL);
  3168. }
  3169. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3170. dh->p = BN_bin2bn(dh1066_p, sizeof(dh1066_p), NULL);
  3171. dh->g = BN_bin2bn(dh1066_g, sizeof(dh1066_g), NULL);
  3172. if ((dh->p == NULL) || (dh->g == NULL)) {
  3173. DH_free(dh);
  3174. return (NULL);
  3175. }
  3176. #else
  3177. DH_set0_pqg(dh, BN_bin2bn(dh1066_p, sizeof(dh1066_p), NULL), NULL, BN_bin2bn(dh1066_g, sizeof(dh1066_g), NULL));
  3178. #endif
  3179. return (dh);
  3180. }
  3181. static DH *get_dh2066(void) {
  3182. unsigned char dh2066_p[] = {
  3183. 0x03, 0x31, 0x77, 0x20, 0x58, 0xA6, 0x69, 0xA3, 0x9D, 0x2D, 0x5E, 0xE0, 0x5C, 0x46, 0x82, 0x0F, 0x9E, 0x80, 0xF0,
  3184. 0x00, 0x2A, 0xF9, 0x0F, 0x62, 0x1F, 0x89, 0xCE, 0x7D, 0x2A, 0xFD, 0xC5, 0x9A, 0x7C, 0x6A, 0x60, 0x2C, 0xF1, 0xDD,
  3185. 0xD4, 0x4D, 0x6B, 0xCD, 0xE9, 0x95, 0xDB, 0x42, 0x97, 0xBA, 0xE4, 0xAF, 0x41, 0x38, 0x8F, 0x57, 0x31, 0xA4, 0x39,
  3186. 0xDD, 0x31, 0xC3, 0x6F, 0x98, 0x0E, 0xE3, 0xB1, 0x43, 0xD1, 0x36, 0xB0, 0x01, 0x28, 0x42, 0x71, 0xD3, 0xB0, 0x36,
  3187. 0xA0, 0x47, 0x99, 0x25, 0x9B, 0x32, 0xF5, 0x86, 0xB1, 0x13, 0x5C, 0x24, 0x8D, 0x8D, 0x7F, 0xE2, 0x7F, 0x9A, 0xC1,
  3188. 0x52, 0x58, 0xC0, 0x63, 0xAA, 0x00, 0x7C, 0x1F, 0x11, 0xBD, 0xAC, 0x4C, 0x2D, 0xE0, 0xA2, 0x9D, 0x4E, 0x21, 0xE4,
  3189. 0x0B, 0xCD, 0x24, 0x92, 0xD2, 0x37, 0x27, 0x84, 0x59, 0x90, 0x46, 0x2F, 0xD5, 0xB9, 0x27, 0x93, 0x18, 0x88, 0xBD,
  3190. 0x91, 0x5B, 0x87, 0x55, 0x56, 0xD8, 0x1B, 0xE4, 0xCF, 0x1C, 0xAA, 0xBC, 0xCF, 0x80, 0x1E, 0x35, 0x2D, 0xB1, 0xBC,
  3191. 0x35, 0x31, 0x92, 0x62, 0x3C, 0x91, 0x8D, 0x62, 0xDA, 0xCF, 0x83, 0x63, 0x12, 0x4B, 0x30, 0x80, 0xEE, 0x82, 0x3C,
  3192. 0x2C, 0xD2, 0x17, 0x13, 0x1F, 0xF9, 0x62, 0x33, 0x5C, 0x63, 0xD8, 0x75, 0x5B, 0xAA, 0x16, 0x5A, 0x36, 0x49, 0x17,
  3193. 0x77, 0xB7, 0x74, 0xBD, 0x3E, 0x3F, 0x98, 0x20, 0x59, 0x5E, 0xC7, 0x72, 0xE8, 0xA3, 0x89, 0x21, 0xB4, 0x3C, 0x25,
  3194. 0xF4, 0xF4, 0x21, 0x96, 0x5A, 0xA6, 0x77, 0xFF, 0x2C, 0x3A, 0xFC, 0x98, 0x5F, 0xC1, 0xBF, 0x2A, 0xCF, 0xB8, 0x62,
  3195. 0x67, 0x23, 0xE8, 0x2F, 0xCC, 0x7B, 0x32, 0x1B, 0x6B, 0x33, 0x67, 0x0A, 0xCB, 0xD0, 0x1F, 0x65, 0xD7, 0x84, 0x54,
  3196. 0xF6, 0xF1, 0x88, 0xB5, 0xBB, 0x0C, 0x63, 0x65, 0x34, 0xE4, 0x66, 0x4B};
  3197. // -----BEGIN DH PARAMETERS-----
  3198. // MIIBCgKCAQMDMXcgWKZpo50tXuBcRoIPnoDwACr5D2Ific59Kv3FmnxqYCzx3dRN
  3199. // a83pldtCl7rkr0E4j1cxpDndMcNvmA7jsUPRNrABKEJx07A2oEeZJZsy9YaxE1wk
  3200. // jY1/4n+awVJYwGOqAHwfEb2sTC3gop1OIeQLzSSS0jcnhFmQRi/VuSeTGIi9kVuH
  3201. // VVbYG+TPHKq8z4AeNS2xvDUxkmI8kY1i2s+DYxJLMIDugjws0hcTH/liM1xj2HVb
  3202. // qhZaNkkXd7d0vT4/mCBZXsdy6KOJIbQ8JfT0IZZapnf/LDr8mF/BvyrPuGJnI+gv
  3203. // zHsyG2szZwrL0B9l14RU9vGItbsMY2U05GZLAgEF
  3204. // -----END DH PARAMETERS-----
  3205. unsigned char dh2066_g[] = {0x05};
  3206. DH *dh;
  3207. if ((dh = DH_new()) == NULL) {
  3208. return (NULL);
  3209. }
  3210. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3211. dh->p = BN_bin2bn(dh2066_p, sizeof(dh2066_p), NULL);
  3212. dh->g = BN_bin2bn(dh2066_g, sizeof(dh2066_g), NULL);
  3213. if ((dh->p == NULL) || (dh->g == NULL)) {
  3214. DH_free(dh);
  3215. return (NULL);
  3216. }
  3217. #else
  3218. DH_set0_pqg(dh, BN_bin2bn(dh2066_p, sizeof(dh2066_p), NULL), NULL, BN_bin2bn(dh2066_g, sizeof(dh2066_g), NULL));
  3219. #endif
  3220. return (dh);
  3221. }
  3222. static int pem_password_func(char *buf, int size, int rwflag, void *password) {
  3223. UNUSED_ARG(rwflag);
  3224. strncpy(buf, (char *)(password), size);
  3225. buf[size - 1] = 0;
  3226. return (strlen(buf));
  3227. }
  3228. #if ALPN_SUPPORTED
  3229. static int ServerALPNCallback(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in,
  3230. unsigned int inlen, void *arg) {
  3231. UNUSED_ARG(ssl);
  3232. UNUSED_ARG(arg);
  3233. unsigned char sa_len = (unsigned char)strlen(STUN_ALPN);
  3234. unsigned char ta_len = (unsigned char)strlen(TURN_ALPN);
  3235. unsigned char ha_len = (unsigned char)strlen(HTTP_ALPN);
  3236. int found_http = 0;
  3237. const unsigned char *ptr = in;
  3238. while (ptr < (in + inlen)) {
  3239. unsigned char current_len = *ptr;
  3240. if (ptr + 1 + current_len > in + inlen) {
  3241. break;
  3242. }
  3243. if ((!turn_params.no_stun) && (current_len == sa_len) && (memcmp(ptr + 1, STUN_ALPN, sa_len) == 0)) {
  3244. *out = ptr + 1;
  3245. *outlen = sa_len;
  3246. SSL_set_app_data(ssl, STUN_ALPN);
  3247. return SSL_TLSEXT_ERR_OK;
  3248. }
  3249. if ((!turn_params.stun_only) && (current_len == ta_len) && (memcmp(ptr + 1, TURN_ALPN, ta_len) == 0)) {
  3250. *out = ptr + 1;
  3251. *outlen = ta_len;
  3252. SSL_set_app_data(ssl, TURN_ALPN);
  3253. return SSL_TLSEXT_ERR_OK;
  3254. }
  3255. if ((current_len == ha_len) && (memcmp(ptr + 1, HTTP_ALPN, ha_len) == 0)) {
  3256. *out = ptr + 1;
  3257. *outlen = ha_len;
  3258. SSL_set_app_data(ssl, HTTP_ALPN);
  3259. found_http = 1;
  3260. }
  3261. ptr += 1 + current_len;
  3262. }
  3263. if (found_http) {
  3264. return SSL_TLSEXT_ERR_OK;
  3265. }
  3266. return SSL_TLSEXT_ERR_NOACK; //???
  3267. }
  3268. #endif
  3269. static void set_ctx(SSL_CTX **out, const char *protocol, const SSL_METHOD *method) {
  3270. SSL_CTX *ctx = SSL_CTX_new(method);
  3271. int err = 0;
  3272. int rc = 0;
  3273. #if ALPN_SUPPORTED
  3274. SSL_CTX_set_alpn_select_cb(ctx, ServerALPNCallback, NULL);
  3275. #endif
  3276. SSL_CTX_set_default_passwd_cb_userdata(ctx, turn_params.tls_password);
  3277. SSL_CTX_set_default_passwd_cb(ctx, pem_password_func);
  3278. if (!(turn_params.cipher_list[0])) {
  3279. strncpy(turn_params.cipher_list, DEFAULT_CIPHER_LIST, TURN_LONG_STRING_SIZE);
  3280. #if defined(DEFAULT_CIPHERSUITES)
  3281. strncat(turn_params.cipher_list, ":", TURN_LONG_STRING_SIZE - strlen(turn_params.cipher_list));
  3282. strncat(turn_params.cipher_list, DEFAULT_CIPHERSUITES, TURN_LONG_STRING_SIZE - strlen(turn_params.cipher_list));
  3283. #endif
  3284. }
  3285. SSL_CTX_set_cipher_list(ctx, turn_params.cipher_list);
  3286. SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
  3287. #if TLSv1_3_SUPPORTED
  3288. SSL_CTX_set_ciphersuites(ctx, turn_params.cipher_list);
  3289. #endif
  3290. if (!SSL_CTX_use_certificate_chain_file(ctx, turn_params.cert_file)) {
  3291. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: no certificate found\n", protocol);
  3292. err = 1;
  3293. }
  3294. if (!SSL_CTX_use_PrivateKey_file(ctx, turn_params.pkey_file, SSL_FILETYPE_PEM)) {
  3295. if (!SSL_CTX_use_RSAPrivateKey_file(ctx, turn_params.pkey_file, SSL_FILETYPE_PEM)) {
  3296. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
  3297. "%s: ERROR: no valid private key found, or invalid private key password provided\n", protocol);
  3298. err = 1;
  3299. }
  3300. }
  3301. if (!SSL_CTX_check_private_key(ctx)) {
  3302. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: invalid private key\n", protocol);
  3303. err = 1;
  3304. }
  3305. if (turn_params.ca_cert_file[0]) {
  3306. if (!SSL_CTX_load_verify_locations(ctx, turn_params.ca_cert_file, NULL)) {
  3307. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot load CA from file: %s\n", turn_params.ca_cert_file);
  3308. err = 1;
  3309. }
  3310. SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(turn_params.ca_cert_file));
  3311. /* Set to require peer (client) certificate verification */
  3312. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE, NULL);
  3313. /* Set the verification depth to 9 */
  3314. SSL_CTX_set_verify_depth(ctx, 9);
  3315. } else {
  3316. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
  3317. }
  3318. #if !defined(OPENSSL_NO_EC) && defined(OPENSSL_EC_NAMED_CURVE)
  3319. { // Elliptic curve algorithms:
  3320. int nid = 0;
  3321. int set_auto_curve = 0;
  3322. const char *curve_name = turn_params.ec_curve_name;
  3323. if (!(curve_name[0])) {
  3324. #if !SSL_SESSION_ECDH_AUTO_SUPPORTED
  3325. curve_name = DEFAULT_EC_CURVE_NAME;
  3326. #endif
  3327. set_auto_curve = 1;
  3328. }
  3329. if (curve_name[0]) {
  3330. {
  3331. nid = OBJ_sn2nid(curve_name);
  3332. if (nid == 0) {
  3333. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "unknown curve name: %s\n", curve_name);
  3334. curve_name = DEFAULT_EC_CURVE_NAME;
  3335. nid = OBJ_sn2nid(curve_name);
  3336. set_auto_curve = 1;
  3337. }
  3338. }
  3339. {
  3340. EC_KEY *ecdh = EC_KEY_new_by_curve_name(nid);
  3341. if (!ecdh) {
  3342. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: allocate EC suite\n", __FUNCTION__);
  3343. set_auto_curve = 1;
  3344. } else {
  3345. SSL_CTX_set_tmp_ecdh(ctx, ecdh);
  3346. EC_KEY_free(ecdh);
  3347. }
  3348. }
  3349. }
  3350. if (set_auto_curve) {
  3351. #if SSL_SESSION_ECDH_AUTO_SUPPORTED
  3352. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3353. SSL_CTX_set_ecdh_auto(ctx, 1);
  3354. #endif
  3355. #endif
  3356. set_auto_curve = 0;
  3357. }
  3358. }
  3359. #endif
  3360. { // DH algorithms:
  3361. DH *dh = NULL;
  3362. if (turn_params.dh_file[0]) {
  3363. FILE *paramfile = fopen(turn_params.dh_file, "r");
  3364. if (!paramfile) {
  3365. perror("Cannot open DH file");
  3366. } else {
  3367. dh = PEM_read_DHparams(paramfile, NULL, NULL, NULL);
  3368. fclose(paramfile);
  3369. if (dh) {
  3370. turn_params.dh_key_size = DH_CUSTOM;
  3371. }
  3372. }
  3373. }
  3374. if (!dh) {
  3375. if (turn_params.dh_key_size == DH_566) {
  3376. dh = get_dh566();
  3377. } else if (turn_params.dh_key_size == DH_1066) {
  3378. dh = get_dh1066();
  3379. } else {
  3380. dh = get_dh2066();
  3381. }
  3382. }
  3383. if (!dh) {
  3384. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: cannot allocate DH suite\n", __FUNCTION__);
  3385. err = 1;
  3386. } else {
  3387. if (1 != SSL_CTX_set_tmp_dh(ctx, dh)) {
  3388. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: cannot set DH\n", __FUNCTION__);
  3389. err = 1;
  3390. }
  3391. DH_free(dh);
  3392. }
  3393. }
  3394. { // secret key
  3395. if (turn_params.secret_key_file[0]) {
  3396. FILE *f = fopen(turn_params.secret_key_file, "r");
  3397. if (!f) {
  3398. perror("Cannot open Secret-Key file");
  3399. } else {
  3400. fseek(f, 0, SEEK_SET);
  3401. rc = fread(turn_params.secret_key, sizeof(char), 16, f);
  3402. if (rc == 0) {
  3403. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: Secret-Key file is empty\n", __FUNCTION__);
  3404. } else {
  3405. if (rc != 16) {
  3406. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: ERROR: Secret-Key length is not enough\n", __FUNCTION__);
  3407. }
  3408. }
  3409. fclose(f);
  3410. }
  3411. }
  3412. }
  3413. {
  3414. int op = 0;
  3415. #if defined(SSL_OP_NO_SSLv2)
  3416. op |= SSL_OP_NO_SSLv2;
  3417. #endif
  3418. #if defined(SSL_OP_NO_SSLv3)
  3419. op |= SSL_OP_NO_SSLv3;
  3420. #endif
  3421. #if defined(SSL_OP_CIPHER_SERVER_PREFERENCE)
  3422. op |= SSL_OP_CIPHER_SERVER_PREFERENCE;
  3423. #endif
  3424. #if defined(SSL_OP_SINGLE_DH_USE)
  3425. op |= SSL_OP_SINGLE_DH_USE;
  3426. #endif
  3427. #if defined(SSL_OP_SINGLE_ECDH_USE)
  3428. op |= SSL_OP_SINGLE_ECDH_USE;
  3429. #endif
  3430. SSL_CTX_set_options(ctx, op);
  3431. }
  3432. if (*out == NULL) {
  3433. // Always initialize, even if issues were encountered
  3434. *out = ctx;
  3435. } else if (!err) {
  3436. SSL_CTX_free(*out);
  3437. *out = ctx;
  3438. }
  3439. }
  3440. static void openssl_load_certificates(void);
  3441. static void openssl_setup(void) {
  3442. THREAD_setup();
  3443. SSL_load_error_strings();
  3444. OpenSSL_add_ssl_algorithms();
  3445. #if !TLS_SUPPORTED
  3446. if (!turn_params.no_tls) {
  3447. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "WARNING: TLS is not supported\n");
  3448. turn_params.no_tls = 1;
  3449. }
  3450. #endif
  3451. if (!(turn_params.no_tls && turn_params.no_dtls) && !turn_params.cert_file[0]) {
  3452. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "\nWARNING: certificate file is not specified, I cannot start TLS/DTLS "
  3453. "services.\nOnly 'plain' UDP/TCP listeners can be started.\n");
  3454. turn_params.no_tls = 1;
  3455. turn_params.no_dtls = 1;
  3456. }
  3457. if (!(turn_params.no_tls && turn_params.no_dtls) && !turn_params.pkey_file[0]) {
  3458. TURN_LOG_FUNC(TURN_LOG_LEVEL_WARNING, "\nWARNING: private key file is not specified, I cannot start TLS/DTLS "
  3459. "services.\nOnly 'plain' UDP/TCP listeners can be started.\n");
  3460. turn_params.no_tls = 1;
  3461. turn_params.no_dtls = 1;
  3462. }
  3463. if (!(turn_params.no_tls && turn_params.no_dtls)) {
  3464. adjust_key_file_names();
  3465. }
  3466. openssl_load_certificates();
  3467. }
  3468. static void openssl_load_certificates(void) {
  3469. print_abs_file_name("", "Certificate", turn_params.cert_file);
  3470. print_abs_file_name("", "Private key", turn_params.pkey_file);
  3471. TURN_MUTEX_LOCK(&turn_params.tls_mutex);
  3472. if (!turn_params.no_tls) {
  3473. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  3474. set_ctx(&turn_params.tls_ctx, "TLS", TLSv1_2_server_method()); /*openssl-1.0.2 version specific API */
  3475. if (turn_params.no_tlsv1) {
  3476. SSL_CTX_set_options(turn_params.tls_ctx, SSL_OP_NO_TLSv1);
  3477. }
  3478. #if TLSv1_1_SUPPORTED
  3479. if (turn_params.no_tlsv1_1) {
  3480. SSL_CTX_set_options(turn_params.tls_ctx, SSL_OP_NO_TLSv1_1);
  3481. }
  3482. #if TLSv1_2_SUPPORTED
  3483. if (turn_params.no_tlsv1_2) {
  3484. SSL_CTX_set_options(turn_params.tls_ctx, SSL_OP_NO_TLSv1_2);
  3485. }
  3486. #endif
  3487. #endif
  3488. #else // OPENSSL_VERSION_NUMBER < 0x10100000L
  3489. set_ctx(&turn_params.tls_ctx, "TLS", TLS_server_method());
  3490. if (turn_params.no_tlsv1) {
  3491. SSL_CTX_set_min_proto_version(turn_params.tls_ctx, TLS1_1_VERSION);
  3492. }
  3493. if (turn_params.no_tlsv1_1) {
  3494. SSL_CTX_set_min_proto_version(turn_params.tls_ctx, TLS1_2_VERSION);
  3495. }
  3496. #if TLSv1_3_SUPPORTED
  3497. if (turn_params.no_tlsv1_2) {
  3498. SSL_CTX_set_min_proto_version(turn_params.tls_ctx, TLS1_3_VERSION);
  3499. }
  3500. #endif
  3501. #endif // OPENSSL_VERSION_NUMBER < 0x10100000L
  3502. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "TLS cipher suite: %s\n", turn_params.cipher_list);
  3503. }
  3504. if (!turn_params.no_dtls) {
  3505. #if !DTLS_SUPPORTED
  3506. TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "ERROR: DTLS is not supported.\n");
  3507. #elif OPENSSL_VERSION_NUMBER < 0x10000000L
  3508. TURN_LOG_FUNC(
  3509. TURN_LOG_LEVEL_WARNING,
  3510. "WARNING: TURN Server was compiled with rather old OpenSSL version, DTLS may not be working correctly.\n");
  3511. #else
  3512. #if OPENSSL_VERSION_NUMBER < 0x10100000L // before openssl-1.1.0 no version independent API
  3513. #if DTLSv1_2_SUPPORTED
  3514. set_ctx(&turn_params.dtls_ctx, "DTLS", DTLSv1_2_server_method()); // openssl-1.0.2
  3515. if (turn_params.no_tlsv1_2) {
  3516. SSL_CTX_set_options(turn_params.dtls_ctx, SSL_OP_NO_DTLSv1_2);
  3517. }
  3518. #else
  3519. set_ctx(&turn_params.dtls_ctx, "DTLS", DTLSv1_server_method()); // < openssl-1.0.2
  3520. #endif
  3521. if (turn_params.no_tlsv1 || turn_params.no_tlsv1_1) {
  3522. SSL_CTX_set_options(turn_params.dtls_ctx, SSL_OP_NO_DTLSv1);
  3523. }
  3524. #else // OPENSSL_VERSION_NUMBER < 0x10100000L
  3525. set_ctx(&turn_params.dtls_ctx, "DTLS", DTLS_server_method());
  3526. if (turn_params.no_tlsv1 || turn_params.no_tlsv1_1) {
  3527. SSL_CTX_set_min_proto_version(turn_params.dtls_ctx, DTLS1_2_VERSION);
  3528. }
  3529. if (turn_params.no_tlsv1_2) {
  3530. SSL_CTX_set_max_proto_version(turn_params.dtls_ctx, DTLS1_VERSION);
  3531. }
  3532. #endif // OPENSSL_VERSION_NUMBER < 0x10100000L
  3533. setup_dtls_callbacks(turn_params.dtls_ctx);
  3534. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "DTLS cipher suite: %s\n", turn_params.cipher_list);
  3535. #endif
  3536. }
  3537. TURN_MUTEX_UNLOCK(&turn_params.tls_mutex);
  3538. }
  3539. static void reload_ssl_certs(evutil_socket_t sock, short events, void *args) {
  3540. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Reloading TLS certificates and keys\n");
  3541. openssl_load_certificates();
  3542. if (turn_params.tls_ctx_update_ev != NULL) {
  3543. event_active(turn_params.tls_ctx_update_ev, EV_READ, 0);
  3544. }
  3545. UNUSED_ARG(sock);
  3546. UNUSED_ARG(events);
  3547. UNUSED_ARG(args);
  3548. }
  3549. static void shutdown_handler(evutil_socket_t sock, short events, void *args) {
  3550. TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "Terminating on signal %d\n", sock);
  3551. turn_params.stop_turn_server = 1;
  3552. UNUSED_ARG(events);
  3553. UNUSED_ARG(args);
  3554. }
  3555. ///////////////////////////////