cgiedit.c 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478
  1. /*********************************************************************
  2. *
  3. * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
  4. *
  5. * Purpose : CGI-based actionsfile editor.
  6. *
  7. * NOTE: The CGIs in this file use parameter names
  8. * such as "f" and "s" which are really *BAD* choices.
  9. * However, I'm trying to save bytes in the
  10. * edit-actions-list HTML page - the standard actions
  11. * file generated a 550kbyte page, which is ridiculous.
  12. *
  13. * Stick to the short names in this file for consistency.
  14. *
  15. * Copyright : Written by and Copyright (C) 2001-2014 the
  16. * Privoxy team. https://www.privoxy.org/
  17. *
  18. * Based on the Internet Junkbuster originally written
  19. * by and Copyright (C) 1997 Anonymous Coders and
  20. * Junkbusters Corporation. http://www.junkbusters.com
  21. *
  22. * This program is free software; you can redistribute it
  23. * and/or modify it under the terms of the GNU General
  24. * Public License as published by the Free Software
  25. * Foundation; either version 2 of the License, or (at
  26. * your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will
  29. * be useful, but WITHOUT ANY WARRANTY; without even the
  30. * implied warranty of MERCHANTABILITY or FITNESS FOR A
  31. * PARTICULAR PURPOSE. See the GNU General Public
  32. * License for more details.
  33. *
  34. * The GNU General Public License should be included with
  35. * this file. If not, you can view it at
  36. * http://www.gnu.org/copyleft/gpl.html
  37. * or write to the Free Software Foundation, Inc., 59
  38. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  39. *
  40. **********************************************************************/
  41. #include "config.h"
  42. /*
  43. * FIXME: Following includes copied from cgi.c - which are actually needed?
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <sys/types.h>
  48. #include <ctype.h>
  49. #include <string.h>
  50. #include <assert.h>
  51. #include <sys/stat.h>
  52. #include "project.h"
  53. #include "cgi.h"
  54. #include "cgiedit.h"
  55. #include "cgisimple.h"
  56. #include "list.h"
  57. #include "encode.h"
  58. #include "actions.h"
  59. #include "miscutil.h"
  60. #include "errlog.h"
  61. #include "loaders.h"
  62. #ifdef FEATURE_TOGGLE
  63. /* loadcfg.h is for global_toggle_state only */
  64. #include "loadcfg.h"
  65. #endif /* def FEATURE_TOGGLE */
  66. #include "urlmatch.h"
  67. #ifdef FEATURE_CGI_EDIT_ACTIONS
  68. /**
  69. * A line in an editable_file.
  70. */
  71. struct file_line
  72. {
  73. /** Next entry in the linked list */
  74. struct file_line * next;
  75. /** The raw data, to write out if this line is unmodified. */
  76. char * raw;
  77. /** Comments and/or whitespace to put before this line if it's modified
  78. and then written out. */
  79. char * prefix;
  80. /** The actual data, as a string. Line continuation and comment removal
  81. are performed on the data read from file before it's stored here, so
  82. it will be a single line of data. */
  83. char * unprocessed;
  84. /** The type of data on this line. One of the FILE_LINE_xxx constants. */
  85. int type;
  86. /** The actual data, processed into some sensible data type. */
  87. union
  88. {
  89. /** An action specification. */
  90. struct action_spec action[1];
  91. /** A name=value pair. */
  92. struct
  93. {
  94. /** The name in the name=value pair. */
  95. char * name;
  96. /** The value in the name=value pair, as a string. */
  97. char * svalue;
  98. /** The value in the name=value pair, as an integer. */
  99. int ivalue;
  100. } setting;
  101. } data;
  102. };
  103. /** This file_line has not been processed yet. */
  104. #define FILE_LINE_UNPROCESSED 1
  105. /** This file_line is blank. Can only appear at the end of a file, due to
  106. the way the parser works. */
  107. #define FILE_LINE_BLANK 2
  108. /** This file_line says {{alias}}. */
  109. #define FILE_LINE_ALIAS_HEADER 3
  110. /** This file_line defines an alias. */
  111. #define FILE_LINE_ALIAS_ENTRY 4
  112. /** This file_line defines an {action}. */
  113. #define FILE_LINE_ACTION 5
  114. /** This file_line specifies a URL pattern. */
  115. #define FILE_LINE_URL 6
  116. /** This file_line says {{settings}}. */
  117. #define FILE_LINE_SETTINGS_HEADER 7
  118. /** This file_line is in a {{settings}} block. */
  119. #define FILE_LINE_SETTINGS_ENTRY 8
  120. /** This file_line says {{description}}. */
  121. #define FILE_LINE_DESCRIPTION_HEADER 9
  122. /** This file_line is in a {{description}} block. */
  123. #define FILE_LINE_DESCRIPTION_ENTRY 10
  124. /*
  125. * Number of file modification time mismatches
  126. * before the CGI editor gets turned off.
  127. */
  128. #define ACCEPTABLE_TIMESTAMP_MISMATCHES 3
  129. /**
  130. * A configuration file, in a format that can be edited and written back to
  131. * disk.
  132. */
  133. struct editable_file
  134. {
  135. struct file_line * lines; /**< The contents of the file. A linked list of lines. */
  136. const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */
  137. unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */
  138. const char * version_str; /**< Last modification time, as a string. For CGI param. */
  139. /**< Can be used in URL without using url_param(). */
  140. unsigned version; /**< Last modification time - prevents chaos with
  141. the browser's "back" button. Note that this is a
  142. time_t cast to an unsigned. When comparing, always
  143. cast the time_t to an unsigned, and *NOT* vice-versa.
  144. This may lose the top few bits, but they're not
  145. significant anyway. */
  146. int newline; /**< Newline convention - one of the NEWLINE_xxx constants.
  147. Note that changing this after the file has been
  148. read in will cause a mess. */
  149. struct file_line * parse_error; /**< On parse error, this is the offending line. */
  150. const char * parse_error_text; /**< On parse error, this is the problem.
  151. (Statically allocated) */
  152. };
  153. /**
  154. * Information about the filter types.
  155. * Used for macro replacement in cgi_edit_actions_for_url.
  156. */
  157. struct filter_type_info
  158. {
  159. const int multi_action_index; /**< The multi action index as defined in project.h */
  160. const char *macro_name; /**< Name of the macro that has to be replaced
  161. with the prepared templates.
  162. For example "content-filter-params" */
  163. const char *type; /**< Name of the filter type,
  164. for example "server-header-filter". */
  165. /* XXX: check if these two can be combined. */
  166. const char *disable_all_option; /**< Name of the catch-all radio option that has
  167. to be checked or unchecked for this filter type. */
  168. const char *disable_all_param; /**< Name of the parameter that causes all filters of
  169. this type to be disabled. */
  170. const char *abbr_type; /**< Abbreviation of the filter type, usually the
  171. first or second character capitalized */
  172. const char *anchor; /**< Anchor for the User Manual link,
  173. for example "SERVER-HEADER-FILTER" */
  174. };
  175. /* Accessed by index, keep the order in the way the FT_ macros are defined. */
  176. static const struct filter_type_info filter_type_info[] =
  177. {
  178. {
  179. ACTION_MULTI_FILTER,
  180. "content-filter-params", "filter",
  181. "filter-all", "filter_all",
  182. "F", "FILTER"
  183. },
  184. {
  185. ACTION_MULTI_CLIENT_HEADER_FILTER,
  186. "client-header-filter-params", "client-header-filter",
  187. "client-header-filter-all", "client_header_filter_all",
  188. "C", "CLIENT-HEADER-FILTER"
  189. },
  190. {
  191. ACTION_MULTI_SERVER_HEADER_FILTER,
  192. "server-header-filter-params", "server-header-filter",
  193. "server-header-filter-all", "server_header_filter_all",
  194. "S", "SERVER-HEADER-FILTER"
  195. },
  196. {
  197. ACTION_MULTI_CLIENT_HEADER_TAGGER,
  198. "client-header-tagger-params", "client-header-tagger",
  199. "client-header-tagger-all", "client_header_tagger_all",
  200. "L", "CLIENT-HEADER-TAGGER"
  201. },
  202. {
  203. ACTION_MULTI_SERVER_HEADER_TAGGER,
  204. "server-header-tagger-params", "server-header-tagger",
  205. "server-header-tagger-all", "server_header_tagger_all",
  206. "E", "SERVER-HEADER-TAGGER"
  207. },
  208. #ifdef FEATURE_EXTERNAL_FILTERS
  209. {
  210. ACTION_MULTI_EXTERNAL_FILTER,
  211. "external-content-filter-params", "external-filter",
  212. "external-content-filter-all", "external_content_filter_all",
  213. "E", "EXTERNAL-CONTENT-FILTER"
  214. },
  215. #endif
  216. };
  217. /* FIXME: Following non-static functions should be prototyped in .h or made static */
  218. /* Functions to read and write arbitrary config files */
  219. jb_err edit_read_file(struct client_state *csp,
  220. const struct map *parameters,
  221. int require_version,
  222. struct editable_file **pfile);
  223. jb_err edit_write_file(struct editable_file * file);
  224. void edit_free_file(struct editable_file * file);
  225. /* Functions to read and write actions files */
  226. jb_err edit_parse_actions_file(struct editable_file * file);
  227. jb_err edit_read_actions_file(struct client_state *csp,
  228. struct http_response *rsp,
  229. const struct map *parameters,
  230. int require_version,
  231. struct editable_file **pfile);
  232. /* Error handlers */
  233. jb_err cgi_error_modified(struct client_state *csp,
  234. struct http_response *rsp,
  235. const char *filename);
  236. jb_err cgi_error_parse(struct client_state *csp,
  237. struct http_response *rsp,
  238. struct editable_file *file);
  239. jb_err cgi_error_file(struct client_state *csp,
  240. struct http_response *rsp,
  241. const char *filename);
  242. jb_err cgi_error_file_read_only(struct client_state *csp,
  243. struct http_response *rsp,
  244. const char *filename);
  245. /* Internal arbitrary config file support functions */
  246. static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
  247. static void edit_free_file_lines(struct file_line * first_line);
  248. /* Internal actions file support functions */
  249. static int match_actions_file_header_line(const char * line, const char * name);
  250. static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
  251. /* Internal parameter parsing functions */
  252. static jb_err get_url_spec_param(struct client_state *csp,
  253. const struct map *parameters,
  254. const char *name,
  255. char **pvalue);
  256. /* Internal actionsfile <==> HTML conversion functions */
  257. static jb_err map_radio(struct map * exports,
  258. const char * optionname,
  259. const char * values,
  260. int value);
  261. static jb_err actions_to_radio(struct map * exports,
  262. const struct action_spec *action);
  263. static jb_err actions_from_radio(const struct map * parameters,
  264. struct action_spec *action);
  265. static jb_err map_copy_parameter_html(struct map *out,
  266. const struct map *in,
  267. const char *name);
  268. static jb_err get_file_name_param(struct client_state *csp,
  269. const struct map *parameters,
  270. const char *param_name,
  271. const char **pfilename);
  272. /* Internal convenience functions */
  273. static char *section_target(const unsigned sectionid);
  274. /*********************************************************************
  275. *
  276. * Function : section_target
  277. *
  278. * Description : Given an unsigned (section id) n, produce a dynamically
  279. * allocated string of the form #l<n>, for use in link
  280. * targets.
  281. *
  282. * XXX: The hash should be moved into the templates
  283. * to make this function more generic and render
  284. * stringify() obsolete.
  285. *
  286. * Parameters :
  287. * 1 : sectionid = start line number of section
  288. *
  289. * Returns : String with link target, or NULL if out of
  290. * memory
  291. *
  292. *********************************************************************/
  293. static char *section_target(const unsigned sectionid)
  294. {
  295. char buf[30];
  296. snprintf(buf, sizeof(buf), "#l%u", sectionid);
  297. return(strdup(buf));
  298. }
  299. /*********************************************************************
  300. *
  301. * Function : stringify
  302. *
  303. * Description : Convert a number into a dynamically allocated string.
  304. *
  305. * Parameters :
  306. * 1 : number = The number to convert.
  307. *
  308. * Returns : String with link target, or NULL if out of memory
  309. *
  310. *********************************************************************/
  311. static char *stringify(const unsigned number)
  312. {
  313. char buf[6];
  314. snprintf(buf, sizeof(buf), "%u", number);
  315. return strdup(buf);
  316. }
  317. /*********************************************************************
  318. *
  319. * Function : map_copy_parameter_html
  320. *
  321. * Description : Copy a CGI parameter from one map to another, HTML
  322. * encoding it.
  323. *
  324. * Parameters :
  325. * 1 : out = target map
  326. * 2 : in = source map
  327. * 3 : name = name of cgi parameter to copy
  328. *
  329. * Returns : JB_ERR_OK on success
  330. * JB_ERR_MEMORY on out-of-memory
  331. * JB_ERR_CGI_PARAMS if the parameter doesn't exist
  332. * in the source map
  333. *
  334. *********************************************************************/
  335. static jb_err map_copy_parameter_html(struct map *out,
  336. const struct map *in,
  337. const char *name)
  338. {
  339. const char * value;
  340. jb_err err;
  341. assert(out);
  342. assert(in);
  343. assert(name);
  344. value = lookup(in, name);
  345. err = map(out, name, 1, html_encode(value), 0);
  346. if (err)
  347. {
  348. /* Out of memory */
  349. return err;
  350. }
  351. else if (*value == '\0')
  352. {
  353. return JB_ERR_CGI_PARAMS;
  354. }
  355. else
  356. {
  357. return JB_ERR_OK;
  358. }
  359. }
  360. /*********************************************************************
  361. *
  362. * Function : cgi_edit_actions_url_form
  363. *
  364. * Description : CGI function that displays a form for
  365. * edit-actions-url
  366. *
  367. * Parameters :
  368. * 1 : csp = Current client state (buffers, headers, etc...)
  369. * 2 : rsp = http_response data structure for output
  370. * 3 : parameters = map of cgi parameters
  371. *
  372. * CGI Parameters
  373. * i : (action index) Identifies the file to edit
  374. * v : (version) File's last-modified time
  375. * p : (pattern) Line number of pattern to edit
  376. *
  377. * Returns : JB_ERR_OK on success
  378. * JB_ERR_MEMORY on out-of-memory
  379. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  380. * specified or not valid.
  381. *
  382. *********************************************************************/
  383. jb_err cgi_edit_actions_url_form(struct client_state *csp,
  384. struct http_response *rsp,
  385. const struct map *parameters)
  386. {
  387. struct map * exports;
  388. unsigned patternid;
  389. struct editable_file * file;
  390. struct file_line * cur_line;
  391. unsigned line_number;
  392. unsigned section_start_line_number = 0;
  393. jb_err err;
  394. assert(csp);
  395. assert(rsp);
  396. assert(parameters);
  397. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  398. {
  399. return cgi_error_disabled(csp, rsp);
  400. }
  401. err = get_number_param(csp, parameters, "p", &patternid);
  402. if (err)
  403. {
  404. return err;
  405. }
  406. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  407. if (err)
  408. {
  409. /* No filename specified, can't read file, modified, or out of memory. */
  410. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  411. }
  412. cur_line = file->lines;
  413. for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
  414. {
  415. if (cur_line->type == FILE_LINE_ACTION)
  416. {
  417. section_start_line_number = line_number;
  418. }
  419. cur_line = cur_line->next;
  420. }
  421. if ( (cur_line == NULL)
  422. || (line_number != patternid)
  423. || (patternid < 1U)
  424. || (cur_line->type != FILE_LINE_URL))
  425. {
  426. /* Invalid "patternid" parameter */
  427. edit_free_file(file);
  428. return JB_ERR_CGI_PARAMS;
  429. }
  430. if (NULL == (exports = default_exports(csp, NULL)))
  431. {
  432. edit_free_file(file);
  433. return JB_ERR_MEMORY;
  434. }
  435. err = map(exports, "f", 1, stringify(file->identifier), 0);
  436. if (!err) err = map(exports, "v", 1, file->version_str, 1);
  437. if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
  438. if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
  439. if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
  440. edit_free_file(file);
  441. if (err)
  442. {
  443. free_map(exports);
  444. return err;
  445. }
  446. return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
  447. }
  448. /*********************************************************************
  449. *
  450. * Function : cgi_edit_actions_add_url_form
  451. *
  452. * Description : CGI function that displays a form for
  453. * edit-actions-url
  454. *
  455. * Parameters :
  456. * 1 : csp = Current client state (buffers, headers, etc...)
  457. * 2 : rsp = http_response data structure for output
  458. * 3 : parameters = map of cgi parameters
  459. *
  460. * CGI Parameters :
  461. * f : (filename) Identifies the file to edit
  462. * v : (version) File's last-modified time
  463. * s : (section) Line number of section to edit
  464. *
  465. * Returns : JB_ERR_OK on success
  466. * JB_ERR_MEMORY on out-of-memory
  467. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  468. * specified or not valid.
  469. *
  470. *********************************************************************/
  471. jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
  472. struct http_response *rsp,
  473. const struct map *parameters)
  474. {
  475. struct map *exports;
  476. jb_err err;
  477. assert(csp);
  478. assert(rsp);
  479. assert(parameters);
  480. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  481. {
  482. return cgi_error_disabled(csp, rsp);
  483. }
  484. if (NULL == (exports = default_exports(csp, NULL)))
  485. {
  486. return JB_ERR_MEMORY;
  487. }
  488. err = map_copy_parameter_html(exports, parameters, "f");
  489. if (!err) err = map_copy_parameter_html(exports, parameters, "v");
  490. if (!err) err = map_copy_parameter_html(exports, parameters, "s");
  491. if (err)
  492. {
  493. free_map(exports);
  494. return err;
  495. }
  496. return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
  497. }
  498. /*********************************************************************
  499. *
  500. * Function : cgi_edit_actions_remove_url_form
  501. *
  502. * Description : CGI function that displays a form for
  503. * edit-actions-url
  504. *
  505. * Parameters :
  506. * 1 : csp = Current client state (buffers, headers, etc...)
  507. * 2 : rsp = http_response data structure for output
  508. * 3 : parameters = map of cgi parameters
  509. *
  510. * CGI Parameters :
  511. * f : (number) The action file identifier.
  512. * v : (version) File's last-modified time
  513. * p : (pattern) Line number of pattern to edit
  514. *
  515. * Returns : JB_ERR_OK on success
  516. * JB_ERR_MEMORY on out-of-memory
  517. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  518. * specified or not valid.
  519. *
  520. *********************************************************************/
  521. jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
  522. struct http_response *rsp,
  523. const struct map *parameters)
  524. {
  525. struct map * exports;
  526. unsigned patternid;
  527. struct editable_file * file;
  528. struct file_line * cur_line;
  529. unsigned line_number;
  530. unsigned section_start_line_number = 0;
  531. jb_err err;
  532. assert(csp);
  533. assert(rsp);
  534. assert(parameters);
  535. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  536. {
  537. return cgi_error_disabled(csp, rsp);
  538. }
  539. err = get_number_param(csp, parameters, "p", &patternid);
  540. if (err)
  541. {
  542. return err;
  543. }
  544. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  545. if (err)
  546. {
  547. /* No filename specified, can't read file, modified, or out of memory. */
  548. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  549. }
  550. cur_line = file->lines;
  551. for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
  552. {
  553. if (cur_line->type == FILE_LINE_ACTION)
  554. {
  555. section_start_line_number = line_number;
  556. }
  557. cur_line = cur_line->next;
  558. }
  559. if ( (cur_line == NULL)
  560. || (line_number != patternid)
  561. || (patternid < 1U)
  562. || (cur_line->type != FILE_LINE_URL))
  563. {
  564. /* Invalid "patternid" parameter */
  565. edit_free_file(file);
  566. return JB_ERR_CGI_PARAMS;
  567. }
  568. if (NULL == (exports = default_exports(csp, NULL)))
  569. {
  570. edit_free_file(file);
  571. return JB_ERR_MEMORY;
  572. }
  573. err = map(exports, "f", 1, stringify(file->identifier), 0);
  574. if (!err) err = map(exports, "v", 1, file->version_str, 1);
  575. if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
  576. if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
  577. if (!err) err = map(exports, "jumptarget", 1, section_target(section_start_line_number), 0);
  578. if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
  579. edit_free_file(file);
  580. if (err)
  581. {
  582. free_map(exports);
  583. return err;
  584. }
  585. return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
  586. }
  587. /*********************************************************************
  588. *
  589. * Function : edit_write_file
  590. *
  591. * Description : Write a complete file to disk.
  592. *
  593. * Parameters :
  594. * 1 : file = File to write.
  595. *
  596. * Returns : JB_ERR_OK on success
  597. * JB_ERR_FILE on error writing to file.
  598. * JB_ERR_MEMORY on out of memory
  599. *
  600. *********************************************************************/
  601. jb_err edit_write_file(struct editable_file * file)
  602. {
  603. FILE * fp;
  604. struct file_line * cur_line;
  605. struct stat statbuf[1];
  606. char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
  607. digits in time_t, assuming this is a 64-bit
  608. machine, plus null terminator, plus one
  609. for paranoia */
  610. assert(file);
  611. assert(file->filename);
  612. if (NULL == (fp = fopen(file->filename, "wb")))
  613. {
  614. return JB_ERR_FILE;
  615. }
  616. cur_line = file->lines;
  617. while (cur_line != NULL)
  618. {
  619. if (cur_line->raw)
  620. {
  621. if (fputs(cur_line->raw, fp) < 0)
  622. {
  623. fclose(fp);
  624. return JB_ERR_FILE;
  625. }
  626. }
  627. else
  628. {
  629. if (cur_line->prefix)
  630. {
  631. if (fputs(cur_line->prefix, fp) < 0)
  632. {
  633. fclose(fp);
  634. return JB_ERR_FILE;
  635. }
  636. }
  637. if (cur_line->unprocessed)
  638. {
  639. if (NULL != strchr(cur_line->unprocessed, '#'))
  640. {
  641. /* Must quote '#' characters */
  642. int numhash = 0;
  643. size_t len;
  644. char * src;
  645. char * dest;
  646. char * str;
  647. /* Count number of # characters, so we know length of output string */
  648. src = cur_line->unprocessed;
  649. while (NULL != (src = strchr(src, '#')))
  650. {
  651. numhash++;
  652. src++;
  653. }
  654. assert(numhash > 0);
  655. /* Allocate new memory for string */
  656. len = strlen(cur_line->unprocessed) + (size_t)numhash;
  657. str = malloc_or_die(len + 1);
  658. /* Copy string but quote hashes */
  659. src = cur_line->unprocessed;
  660. dest = str;
  661. while (*src)
  662. {
  663. if (*src == '#')
  664. {
  665. *dest++ = '\\';
  666. numhash--;
  667. assert(numhash >= 0);
  668. }
  669. *dest++ = *src++;
  670. }
  671. *dest = '\0';
  672. assert(numhash == 0);
  673. assert(strlen(str) == len);
  674. assert(str == dest - len);
  675. assert(src - len <= cur_line->unprocessed);
  676. if ((strlen(str) != len) || (numhash != 0))
  677. {
  678. /*
  679. * Escaping didn't work as expected, go spread the news.
  680. * Only reached in non-debugging builds.
  681. */
  682. log_error(LOG_LEVEL_ERROR,
  683. "Looks like hash escaping failed. %s might be corrupted now.",
  684. file->filename);
  685. }
  686. if (fputs(str, fp) < 0)
  687. {
  688. free(str);
  689. fclose(fp);
  690. return JB_ERR_FILE;
  691. }
  692. free(str);
  693. }
  694. else
  695. {
  696. /* Can write without quoting '#' characters. */
  697. if (fputs(cur_line->unprocessed, fp) < 0)
  698. {
  699. fclose(fp);
  700. return JB_ERR_FILE;
  701. }
  702. }
  703. if (fputs(NEWLINE(file->newline), fp) < 0)
  704. {
  705. fclose(fp);
  706. return JB_ERR_FILE;
  707. }
  708. }
  709. else
  710. {
  711. /* FIXME: Write data from file->data->whatever */
  712. assert(0);
  713. }
  714. }
  715. cur_line = cur_line->next;
  716. }
  717. fclose(fp);
  718. /* Update the version stamp in the file structure, since we just
  719. * wrote to the file & changed it's date.
  720. */
  721. if (stat(file->filename, statbuf) < 0)
  722. {
  723. /* Error, probably file not found. */
  724. return JB_ERR_FILE;
  725. }
  726. file->version = (unsigned)statbuf->st_mtime;
  727. /* Correct file->version_str */
  728. freez(file->version_str);
  729. snprintf(version_buf, sizeof(version_buf), "%u", file->version);
  730. version_buf[sizeof(version_buf)-1] = '\0';
  731. file->version_str = strdup_or_die(version_buf);
  732. return JB_ERR_OK;
  733. }
  734. /*********************************************************************
  735. *
  736. * Function : edit_free_file
  737. *
  738. * Description : Free a complete file in memory.
  739. *
  740. * Parameters :
  741. * 1 : file = Data structure to free.
  742. *
  743. * Returns : N/A
  744. *
  745. *********************************************************************/
  746. void edit_free_file(struct editable_file * file)
  747. {
  748. if (!file)
  749. {
  750. /* Silently ignore NULL pointer */
  751. return;
  752. }
  753. edit_free_file_lines(file->lines);
  754. freez(file->version_str);
  755. file->version = 0;
  756. file->parse_error_text = NULL; /* Statically allocated */
  757. file->parse_error = NULL;
  758. free(file);
  759. }
  760. /*********************************************************************
  761. *
  762. * Function : edit_free_file_lines
  763. *
  764. * Description : Free an entire linked list of file lines.
  765. *
  766. * Parameters :
  767. * 1 : first_line = Data structure to free.
  768. *
  769. * Returns : N/A
  770. *
  771. *********************************************************************/
  772. static void edit_free_file_lines(struct file_line * first_line)
  773. {
  774. struct file_line * next_line;
  775. while (first_line != NULL)
  776. {
  777. next_line = first_line->next;
  778. first_line->next = NULL;
  779. freez(first_line->raw);
  780. freez(first_line->prefix);
  781. freez(first_line->unprocessed);
  782. switch(first_line->type)
  783. {
  784. case 0: /* special case if memory zeroed */
  785. case FILE_LINE_UNPROCESSED:
  786. case FILE_LINE_BLANK:
  787. case FILE_LINE_ALIAS_HEADER:
  788. case FILE_LINE_SETTINGS_HEADER:
  789. case FILE_LINE_DESCRIPTION_HEADER:
  790. case FILE_LINE_DESCRIPTION_ENTRY:
  791. case FILE_LINE_ALIAS_ENTRY:
  792. case FILE_LINE_URL:
  793. /* No data is stored for these */
  794. break;
  795. case FILE_LINE_ACTION:
  796. free_action(first_line->data.action);
  797. break;
  798. case FILE_LINE_SETTINGS_ENTRY:
  799. freez(first_line->data.setting.name);
  800. freez(first_line->data.setting.svalue);
  801. break;
  802. default:
  803. /* Should never happen */
  804. assert(0);
  805. break;
  806. }
  807. first_line->type = 0; /* paranoia */
  808. free(first_line);
  809. first_line = next_line;
  810. }
  811. }
  812. /*********************************************************************
  813. *
  814. * Function : match_actions_file_header_line
  815. *
  816. * Description : Match an actions file {{header}} line
  817. *
  818. * Parameters :
  819. * 1 : line = String from file
  820. * 2 : name = Header to match against
  821. *
  822. * Returns : 0 iff they match.
  823. *
  824. *********************************************************************/
  825. static int match_actions_file_header_line(const char * line, const char * name)
  826. {
  827. size_t len;
  828. assert(line);
  829. assert(name);
  830. /* Look for "{{" */
  831. if ((line[0] != '{') || (line[1] != '{'))
  832. {
  833. return 1;
  834. }
  835. line += 2;
  836. /* Look for optional whitespace */
  837. while ((*line == ' ') || (*line == '\t'))
  838. {
  839. line++;
  840. }
  841. /* Look for the specified name (case-insensitive) */
  842. len = strlen(name);
  843. if (0 != strncmpic(line, name, len))
  844. {
  845. return 1;
  846. }
  847. line += len;
  848. /* Look for optional whitespace */
  849. while ((*line == ' ') || (*line == '\t'))
  850. {
  851. line++;
  852. }
  853. /* Look for "}}" and end of string*/
  854. if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
  855. {
  856. return 1;
  857. }
  858. /* It matched!! */
  859. return 0;
  860. }
  861. /*********************************************************************
  862. *
  863. * Function : match_actions_file_header_line
  864. *
  865. * Description : Match an actions file {{header}} line
  866. *
  867. * Parameters :
  868. * 1 : line = String from file. Must not start with
  869. * whitespace (else infinite loop!)
  870. * 2 : pname = Destination for name
  871. * 2 : pvalue = Destination for value
  872. *
  873. * Returns : JB_ERR_OK on success
  874. * JB_ERR_MEMORY on out-of-memory
  875. * JB_ERR_PARSE if there's no "=" sign, or if there's
  876. * nothing before the "=" sign (but empty
  877. * values *after* the "=" sign are legal).
  878. *
  879. *********************************************************************/
  880. static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
  881. {
  882. const char * name_end;
  883. const char * value_start;
  884. size_t name_len;
  885. assert(line);
  886. assert(pname);
  887. assert(pvalue);
  888. assert(*line != ' ');
  889. assert(*line != '\t');
  890. *pname = NULL;
  891. *pvalue = NULL;
  892. value_start = strchr(line, '=');
  893. if ((value_start == NULL) || (value_start == line))
  894. {
  895. return JB_ERR_PARSE;
  896. }
  897. name_end = value_start - 1;
  898. /* Eat any whitespace before the '=' */
  899. while ((*name_end == ' ') || (*name_end == '\t'))
  900. {
  901. /*
  902. * we already know we must have at least 1 non-ws char
  903. * at start of buf - no need to check
  904. */
  905. name_end--;
  906. }
  907. name_len = (size_t)(name_end - line) + 1; /* Length excluding \0 */
  908. *pname = malloc_or_die(name_len + 1);
  909. strncpy(*pname, line, name_len);
  910. (*pname)[name_len] = '\0';
  911. /* Eat any the whitespace after the '=' */
  912. value_start++;
  913. while ((*value_start == ' ') || (*value_start == '\t'))
  914. {
  915. value_start++;
  916. }
  917. if (NULL == (*pvalue = strdup(value_start)))
  918. {
  919. free(*pname);
  920. *pname = NULL;
  921. return JB_ERR_MEMORY;
  922. }
  923. return JB_ERR_OK;
  924. }
  925. /*********************************************************************
  926. *
  927. * Function : edit_parse_actions_file
  928. *
  929. * Description : Parse an actions file in memory.
  930. *
  931. * Passed linked list must have the "data" member
  932. * zeroed, and must contain valid "next" and
  933. * "unprocessed" fields. The "raw" and "prefix"
  934. * fields are ignored, and "type" is just overwritten.
  935. *
  936. * Note that on error the file may have been
  937. * partially parsed.
  938. *
  939. * Parameters :
  940. * 1 : file = Actions file to be parsed in-place.
  941. *
  942. * Returns : JB_ERR_OK on success
  943. * JB_ERR_MEMORY on out-of-memory
  944. * JB_ERR_PARSE on error
  945. *
  946. *********************************************************************/
  947. jb_err edit_parse_actions_file(struct editable_file * file)
  948. {
  949. struct file_line * cur_line;
  950. size_t len;
  951. const char * text; /* Text from a line */
  952. char * name; /* For lines of the form name=value */
  953. char * value; /* For lines of the form name=value */
  954. struct action_alias * alias_list = NULL;
  955. jb_err err = JB_ERR_OK;
  956. /* alias_list contains the aliases defined in this file.
  957. * It might be better to use the "file_line.data" fields
  958. * in the relevant places instead.
  959. */
  960. cur_line = file->lines;
  961. /* A note about blank line support: Blank lines should only
  962. * ever occur as the last line in the file. This function
  963. * is more forgiving than that - FILE_LINE_BLANK can occur
  964. * anywhere.
  965. */
  966. /* Skip leading blanks. Should only happen if file is
  967. * empty (which is valid, but pointless).
  968. */
  969. while ((cur_line != NULL)
  970. && (cur_line->unprocessed[0] == '\0'))
  971. {
  972. /* Blank line */
  973. cur_line->type = FILE_LINE_BLANK;
  974. cur_line = cur_line->next;
  975. }
  976. if ((cur_line != NULL)
  977. && (cur_line->unprocessed[0] != '{'))
  978. {
  979. /* File doesn't start with a header */
  980. file->parse_error = cur_line;
  981. file->parse_error_text = "First (non-comment) line of the file must contain a header.";
  982. return JB_ERR_PARSE;
  983. }
  984. if ((cur_line != NULL) && (0 ==
  985. match_actions_file_header_line(cur_line->unprocessed, "settings")))
  986. {
  987. cur_line->type = FILE_LINE_SETTINGS_HEADER;
  988. cur_line = cur_line->next;
  989. while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
  990. {
  991. if (cur_line->unprocessed[0])
  992. {
  993. cur_line->type = FILE_LINE_SETTINGS_ENTRY;
  994. err = split_line_on_equals(cur_line->unprocessed,
  995. &cur_line->data.setting.name,
  996. &cur_line->data.setting.svalue);
  997. if (err == JB_ERR_MEMORY)
  998. {
  999. return err;
  1000. }
  1001. else if (err != JB_ERR_OK)
  1002. {
  1003. /* Line does not contain a name=value pair */
  1004. file->parse_error = cur_line;
  1005. file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
  1006. return JB_ERR_PARSE;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. cur_line->type = FILE_LINE_BLANK;
  1012. }
  1013. cur_line = cur_line->next;
  1014. }
  1015. }
  1016. if ((cur_line != NULL) && (0 ==
  1017. match_actions_file_header_line(cur_line->unprocessed, "description")))
  1018. {
  1019. cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
  1020. cur_line = cur_line->next;
  1021. while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
  1022. {
  1023. if (cur_line->unprocessed[0])
  1024. {
  1025. cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
  1026. }
  1027. else
  1028. {
  1029. cur_line->type = FILE_LINE_BLANK;
  1030. }
  1031. cur_line = cur_line->next;
  1032. }
  1033. }
  1034. if ((cur_line != NULL) && (0 ==
  1035. match_actions_file_header_line(cur_line->unprocessed, "alias")))
  1036. {
  1037. cur_line->type = FILE_LINE_ALIAS_HEADER;
  1038. cur_line = cur_line->next;
  1039. while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
  1040. {
  1041. if (cur_line->unprocessed[0])
  1042. {
  1043. /* define an alias */
  1044. struct action_alias * new_alias;
  1045. cur_line->type = FILE_LINE_ALIAS_ENTRY;
  1046. err = split_line_on_equals(cur_line->unprocessed, &name, &value);
  1047. if (err == JB_ERR_MEMORY)
  1048. {
  1049. free_alias_list(alias_list);
  1050. return err;
  1051. }
  1052. else if (err != JB_ERR_OK)
  1053. {
  1054. /* Line does not contain a name=value pair */
  1055. file->parse_error = cur_line;
  1056. file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
  1057. free_alias_list(alias_list);
  1058. return JB_ERR_PARSE;
  1059. }
  1060. new_alias = zalloc_or_die(sizeof(*new_alias));
  1061. err = get_actions(value, alias_list, new_alias->action);
  1062. if (err)
  1063. {
  1064. /* Invalid action or out of memory */
  1065. free(name);
  1066. free(value);
  1067. free(new_alias);
  1068. free_alias_list(alias_list);
  1069. if (err == JB_ERR_MEMORY)
  1070. {
  1071. return err;
  1072. }
  1073. else
  1074. {
  1075. /* Line does not contain a name=value pair */
  1076. file->parse_error = cur_line;
  1077. file->parse_error_text = "This alias does not specify a valid set of actions.";
  1078. return JB_ERR_PARSE;
  1079. }
  1080. }
  1081. free(value);
  1082. new_alias->name = name;
  1083. /* add to list */
  1084. new_alias->next = alias_list;
  1085. alias_list = new_alias;
  1086. }
  1087. else
  1088. {
  1089. cur_line->type = FILE_LINE_BLANK;
  1090. }
  1091. cur_line = cur_line->next;
  1092. }
  1093. }
  1094. /* Header done, process the main part of the file */
  1095. while (cur_line != NULL)
  1096. {
  1097. /* At this point, (cur_line->unprocessed[0] == '{') */
  1098. assert(cur_line->unprocessed[0] == '{');
  1099. text = cur_line->unprocessed + 1;
  1100. len = strlen(text) - 1;
  1101. if (text[len] != '}')
  1102. {
  1103. /* No closing } on header */
  1104. free_alias_list(alias_list);
  1105. file->parse_error = cur_line;
  1106. file->parse_error_text = "Headers starting with '{' must have a "
  1107. "closing bracket ('}'). Headers starting with two brackets ('{{') "
  1108. "must close with two brackets ('}}').";
  1109. return JB_ERR_PARSE;
  1110. }
  1111. if (text[0] == '{')
  1112. {
  1113. /* An invalid {{ header. */
  1114. free_alias_list(alias_list);
  1115. file->parse_error = cur_line;
  1116. file->parse_error_text = "Unknown or unexpected two-bracket header. "
  1117. "Please remember that the system (two-bracket) headers must "
  1118. "appear in the order {{settings}}, {{description}}, {{alias}}, "
  1119. "and must appear before any actions (one-bracket) headers. "
  1120. "Also note that system headers may not be repeated.";
  1121. return JB_ERR_PARSE;
  1122. }
  1123. while ((*text == ' ') || (*text == '\t'))
  1124. {
  1125. text++;
  1126. len--;
  1127. }
  1128. while ((len > (size_t)0)
  1129. && ((text[len - 1] == ' ')
  1130. || (text[len - 1] == '\t')))
  1131. {
  1132. len--;
  1133. }
  1134. cur_line->type = FILE_LINE_ACTION;
  1135. /* Remove {} and make copy */
  1136. value = malloc_or_die(len + 1);
  1137. strncpy(value, text, len);
  1138. value[len] = '\0';
  1139. /* Get actions */
  1140. err = get_actions(value, alias_list, cur_line->data.action);
  1141. if (err)
  1142. {
  1143. /* Invalid action or out of memory */
  1144. free(value);
  1145. free_alias_list(alias_list);
  1146. if (err == JB_ERR_MEMORY)
  1147. {
  1148. return err;
  1149. }
  1150. else
  1151. {
  1152. /* Line does not contain a name=value pair */
  1153. file->parse_error = cur_line;
  1154. file->parse_error_text = "This header does not specify a valid set of actions.";
  1155. return JB_ERR_PARSE;
  1156. }
  1157. }
  1158. /* Done with string - it was clobbered anyway */
  1159. free(value);
  1160. /* Process next line */
  1161. cur_line = cur_line->next;
  1162. /* Loop processing URL patterns */
  1163. while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
  1164. {
  1165. if (cur_line->unprocessed[0])
  1166. {
  1167. /* Could parse URL here, but this isn't currently needed */
  1168. cur_line->type = FILE_LINE_URL;
  1169. }
  1170. else
  1171. {
  1172. cur_line->type = FILE_LINE_BLANK;
  1173. }
  1174. cur_line = cur_line->next;
  1175. }
  1176. } /* End main while(cur_line != NULL) loop */
  1177. free_alias_list(alias_list);
  1178. return JB_ERR_OK;
  1179. }
  1180. /*********************************************************************
  1181. *
  1182. * Function : edit_read_file_lines
  1183. *
  1184. * Description : Read all the lines of a file into memory.
  1185. * Handles whitespace, comments and line continuation.
  1186. *
  1187. * Parameters :
  1188. * 1 : fp = File to read from. On return, this will be
  1189. * at EOF but it will not have been closed.
  1190. * 2 : pfile = Destination for a linked list of file_lines.
  1191. * Will be set to NULL on error.
  1192. * 3 : newline = How to handle newlines.
  1193. *
  1194. * Returns : JB_ERR_OK on success
  1195. * JB_ERR_MEMORY on out-of-memory
  1196. *
  1197. *********************************************************************/
  1198. jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
  1199. {
  1200. struct file_line * first_line; /* Keep for return value or to free */
  1201. struct file_line * cur_line; /* Current line */
  1202. struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
  1203. jb_err rval;
  1204. assert(fp);
  1205. assert(pfile);
  1206. *pfile = NULL;
  1207. cur_line = first_line = zalloc_or_die(sizeof(struct file_line));
  1208. cur_line->type = FILE_LINE_UNPROCESSED;
  1209. rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
  1210. if (rval)
  1211. {
  1212. /* Out of memory or empty file. */
  1213. /* Note that empty file is not an error we propagate up */
  1214. free(cur_line);
  1215. return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
  1216. }
  1217. do
  1218. {
  1219. prev_line = cur_line;
  1220. cur_line = prev_line->next = zalloc_or_die(sizeof(struct file_line));
  1221. cur_line->type = FILE_LINE_UNPROCESSED;
  1222. rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
  1223. if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
  1224. {
  1225. /* Out of memory */
  1226. edit_free_file_lines(first_line);
  1227. return JB_ERR_MEMORY;
  1228. }
  1229. }
  1230. while (rval != JB_ERR_FILE);
  1231. /* EOF */
  1232. /* We allocated one too many - free it */
  1233. prev_line->next = NULL;
  1234. free(cur_line);
  1235. *pfile = first_line;
  1236. return JB_ERR_OK;
  1237. }
  1238. /*********************************************************************
  1239. *
  1240. * Function : edit_read_file
  1241. *
  1242. * Description : Read a complete file into memory.
  1243. * Handles CGI parameter parsing. If requested, also
  1244. * checks the file's modification timestamp.
  1245. *
  1246. * Parameters :
  1247. * 1 : csp = Current client state (buffers, headers, etc...)
  1248. * 2 : parameters = map of cgi parameters.
  1249. * 3 : require_version = true to check "ver" parameter.
  1250. * 4 : pfile = Destination for the file. Will be set
  1251. * to NULL on error.
  1252. *
  1253. * CGI Parameters :
  1254. * f : The action file identifier.
  1255. * ver : (Only if require_version is nonzero)
  1256. * Timestamp of the actions file. If wrong, this
  1257. * function fails with JB_ERR_MODIFIED.
  1258. *
  1259. * Returns : JB_ERR_OK on success
  1260. * JB_ERR_MEMORY on out-of-memory
  1261. * JB_ERR_CGI_PARAMS if "filename" was not specified
  1262. * or is not valid.
  1263. * JB_ERR_FILE if the file cannot be opened or
  1264. * contains no data
  1265. * JB_ERR_MODIFIED if version checking was requested and
  1266. * failed - the file was modified outside
  1267. * of this CGI editor instance.
  1268. *
  1269. *********************************************************************/
  1270. jb_err edit_read_file(struct client_state *csp,
  1271. const struct map *parameters,
  1272. int require_version,
  1273. struct editable_file **pfile)
  1274. {
  1275. struct file_line * lines;
  1276. FILE * fp;
  1277. jb_err err;
  1278. const char *filename = NULL;
  1279. struct editable_file * file;
  1280. unsigned version = 0;
  1281. struct stat statbuf[1];
  1282. char version_buf[22];
  1283. int newline = NEWLINE_UNKNOWN;
  1284. unsigned i;
  1285. assert(csp);
  1286. assert(parameters);
  1287. assert(pfile);
  1288. *pfile = NULL;
  1289. err = get_number_param(csp, parameters, "f", &i);
  1290. if ((JB_ERR_OK == err) && (i < MAX_AF_FILES) && (NULL != csp->config->actions_file[i]))
  1291. {
  1292. filename = csp->config->actions_file[i];
  1293. }
  1294. else if (JB_ERR_CGI_PARAMS == err)
  1295. {
  1296. /*
  1297. * Probably an old-school URL like
  1298. * http://config.privoxy.org/edit-actions-list?f=default
  1299. */
  1300. get_file_name_param(csp, parameters, "f", &filename);
  1301. }
  1302. if (NULL == filename || stat(filename, statbuf) < 0)
  1303. {
  1304. /* Error, probably file not found. */
  1305. return JB_ERR_FILE;
  1306. }
  1307. version = (unsigned) statbuf->st_mtime;
  1308. if (require_version)
  1309. {
  1310. unsigned specified_version;
  1311. err = get_number_param(csp, parameters, "v", &specified_version);
  1312. if (err)
  1313. {
  1314. return err;
  1315. }
  1316. if (version != specified_version)
  1317. {
  1318. return JB_ERR_MODIFIED;
  1319. }
  1320. }
  1321. if (NULL == (fp = fopen(filename,"rb")))
  1322. {
  1323. return JB_ERR_FILE;
  1324. }
  1325. err = edit_read_file_lines(fp, &lines, &newline);
  1326. fclose(fp);
  1327. if (err)
  1328. {
  1329. return err;
  1330. }
  1331. file = zalloc_or_die(sizeof(*file));
  1332. file->lines = lines;
  1333. file->newline = newline;
  1334. file->filename = filename;
  1335. file->version = version;
  1336. file->identifier = i;
  1337. /* Correct file->version_str */
  1338. freez(file->version_str);
  1339. snprintf(version_buf, sizeof(version_buf), "%u", file->version);
  1340. version_buf[sizeof(version_buf)-1] = '\0';
  1341. file->version_str = strdup_or_die(version_buf);
  1342. *pfile = file;
  1343. return JB_ERR_OK;
  1344. }
  1345. /*********************************************************************
  1346. *
  1347. * Function : edit_read_actions_file
  1348. *
  1349. * Description : Read a complete actions file into memory.
  1350. * Handles CGI parameter parsing. If requested, also
  1351. * checks the file's modification timestamp.
  1352. *
  1353. * If this function detects an error in the categories
  1354. * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
  1355. * then it handles it by filling in the specified
  1356. * response structure and returning JB_ERR_FILE.
  1357. *
  1358. * Parameters :
  1359. * 1 : csp = Current client state (buffers, headers, etc...)
  1360. * 2 : rsp = HTTP response. Only filled in on error.
  1361. * 2 : parameters = map of cgi parameters.
  1362. * 3 : require_version = true to check "ver" parameter.
  1363. * 4 : pfile = Destination for the file. Will be set
  1364. * to NULL on error.
  1365. *
  1366. * CGI Parameters :
  1367. * f : The actions file identifier.
  1368. * ver : (Only if require_version is nonzero)
  1369. * Timestamp of the actions file. If wrong, this
  1370. * function fails with JB_ERR_MODIFIED.
  1371. *
  1372. * Returns : JB_ERR_OK on success
  1373. * JB_ERR_MEMORY on out-of-memory
  1374. * JB_ERR_CGI_PARAMS if "filename" was not specified
  1375. * or is not valid.
  1376. * JB_ERR_FILE if the file does not contain valid data,
  1377. * or if file cannot be opened or
  1378. * contains no data, or if version
  1379. * checking was requested and failed.
  1380. *
  1381. *********************************************************************/
  1382. jb_err edit_read_actions_file(struct client_state *csp,
  1383. struct http_response *rsp,
  1384. const struct map *parameters,
  1385. int require_version,
  1386. struct editable_file **pfile)
  1387. {
  1388. jb_err err;
  1389. struct editable_file *file;
  1390. static int acceptable_failures = ACCEPTABLE_TIMESTAMP_MISMATCHES - 1;
  1391. assert(csp);
  1392. assert(parameters);
  1393. assert(pfile);
  1394. *pfile = NULL;
  1395. err = edit_read_file(csp, parameters, require_version, &file);
  1396. if (err)
  1397. {
  1398. /* Try to handle if possible */
  1399. if (err == JB_ERR_FILE)
  1400. {
  1401. err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
  1402. }
  1403. else if (err == JB_ERR_MODIFIED)
  1404. {
  1405. assert(require_version);
  1406. err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
  1407. log_error(LOG_LEVEL_ERROR,
  1408. "Blocking CGI edit request due to modification time mismatch.");
  1409. if (acceptable_failures > 0)
  1410. {
  1411. log_error(LOG_LEVEL_INFO,
  1412. "The CGI editor will be turned off after another %d mismatche(s).",
  1413. acceptable_failures);
  1414. acceptable_failures--;
  1415. }
  1416. else
  1417. {
  1418. log_error(LOG_LEVEL_INFO,
  1419. "Timestamp mismatch limit reached, turning CGI editor off. "
  1420. "Reload the configuration file to re-enable it.");
  1421. csp->config->feature_flags &= ~RUNTIME_FEATURE_CGI_EDIT_ACTIONS;
  1422. }
  1423. }
  1424. if (err == JB_ERR_OK)
  1425. {
  1426. /*
  1427. * Signal to higher-level CGI code that there was a problem but we
  1428. * handled it, they should just return JB_ERR_OK.
  1429. */
  1430. err = JB_ERR_FILE;
  1431. }
  1432. return err;
  1433. }
  1434. err = edit_parse_actions_file(file);
  1435. if (err)
  1436. {
  1437. if (err == JB_ERR_PARSE)
  1438. {
  1439. err = cgi_error_parse(csp, rsp, file);
  1440. if (err == JB_ERR_OK)
  1441. {
  1442. /*
  1443. * Signal to higher-level CGI code that there was a problem but we
  1444. * handled it, they should just return JB_ERR_OK.
  1445. */
  1446. err = JB_ERR_FILE;
  1447. }
  1448. }
  1449. edit_free_file(file);
  1450. return err;
  1451. }
  1452. *pfile = file;
  1453. return JB_ERR_OK;
  1454. }
  1455. /*********************************************************************
  1456. *
  1457. * Function : get_file_name_param
  1458. *
  1459. * Description : Get the name of the file to edit from the parameters
  1460. * passed to a CGI function using the old syntax.
  1461. * This function handles security checks and only
  1462. * accepts files that Privoxy already knows.
  1463. *
  1464. * Parameters :
  1465. * 1 : csp = Current client state (buffers, headers, etc...)
  1466. * 2 : parameters = map of cgi parameters
  1467. * 3 : param_name = The name of the parameter to read
  1468. * 4 : pfilename = pointer to the filename in
  1469. * csp->config->actions_file[] if found. Set to NULL on error.
  1470. *
  1471. * Returns : JB_ERR_OK on success
  1472. * JB_ERR_MEMORY on out-of-memory
  1473. * JB_ERR_CGI_PARAMS if "filename" was not specified
  1474. * or is not valid.
  1475. *
  1476. *********************************************************************/
  1477. static jb_err get_file_name_param(struct client_state *csp,
  1478. const struct map *parameters,
  1479. const char *param_name,
  1480. const char **pfilename)
  1481. {
  1482. const char *param;
  1483. const char suffix[] = ".action";
  1484. const char *s;
  1485. char *name;
  1486. char *fullpath;
  1487. char ch;
  1488. size_t len;
  1489. size_t name_size;
  1490. int i;
  1491. assert(csp);
  1492. assert(parameters);
  1493. assert(pfilename);
  1494. *pfilename = NULL;
  1495. param = lookup(parameters, param_name);
  1496. if (!*param)
  1497. {
  1498. return JB_ERR_CGI_PARAMS;
  1499. }
  1500. len = strlen(param);
  1501. if (len >= FILENAME_MAX)
  1502. {
  1503. /* Too long. */
  1504. return JB_ERR_CGI_PARAMS;
  1505. }
  1506. /*
  1507. * Check every character to see if it's legal.
  1508. * Totally unnecessary but we do it anyway.
  1509. */
  1510. s = param;
  1511. while ((ch = *s++) != '\0')
  1512. {
  1513. if ( ((ch < 'A') || (ch > 'Z'))
  1514. && ((ch < 'a') || (ch > 'z'))
  1515. && ((ch < '0') || (ch > '9'))
  1516. && (ch != '-')
  1517. && (ch != '_'))
  1518. {
  1519. /* Probable hack attempt. */
  1520. return JB_ERR_CGI_PARAMS;
  1521. }
  1522. }
  1523. /* Append extension */
  1524. name_size = len + strlen(suffix) + 1;
  1525. name = malloc_or_die(name_size);
  1526. strlcpy(name, param, name_size);
  1527. strlcat(name, suffix, name_size);
  1528. /* Prepend path */
  1529. fullpath = make_path(csp->config->confdir, name);
  1530. free(name);
  1531. if (fullpath == NULL)
  1532. {
  1533. return JB_ERR_MEMORY;
  1534. }
  1535. /* Check if the file is known */
  1536. for (i = 0; i < MAX_AF_FILES; i++)
  1537. {
  1538. if (NULL != csp->config->actions_file[i] &&
  1539. !strcmp(fullpath, csp->config->actions_file[i]))
  1540. {
  1541. /* Success */
  1542. *pfilename = csp->config->actions_file[i];
  1543. freez(fullpath);
  1544. return JB_ERR_OK;
  1545. }
  1546. }
  1547. freez(fullpath);
  1548. return JB_ERR_CGI_PARAMS;
  1549. }
  1550. /*********************************************************************
  1551. *
  1552. * Function : get_url_spec_param
  1553. *
  1554. * Description : Get a URL pattern from the parameters
  1555. * passed to a CGI function. Removes leading/trailing
  1556. * spaces and validates it.
  1557. *
  1558. * Parameters :
  1559. * 1 : csp = Current client state (buffers, headers, etc...)
  1560. * 2 : parameters = map of cgi parameters
  1561. * 3 : name = Name of CGI parameter to read
  1562. * 4 : pvalue = destination for value. Will be malloc()'d.
  1563. * Set to NULL on error.
  1564. *
  1565. * Returns : JB_ERR_OK on success
  1566. * JB_ERR_MEMORY on out-of-memory
  1567. * JB_ERR_CGI_PARAMS if the parameter was not specified
  1568. * or is not valid.
  1569. *
  1570. *********************************************************************/
  1571. static jb_err get_url_spec_param(struct client_state *csp,
  1572. const struct map *parameters,
  1573. const char *name,
  1574. char **pvalue)
  1575. {
  1576. const char *orig_param;
  1577. char *param;
  1578. char *s;
  1579. struct pattern_spec compiled[1];
  1580. jb_err err;
  1581. assert(csp);
  1582. assert(parameters);
  1583. assert(name);
  1584. assert(pvalue);
  1585. *pvalue = NULL;
  1586. orig_param = lookup(parameters, name);
  1587. if (!*orig_param)
  1588. {
  1589. return JB_ERR_CGI_PARAMS;
  1590. }
  1591. /* Copy and trim whitespace */
  1592. param = strdup(orig_param);
  1593. if (param == NULL)
  1594. {
  1595. return JB_ERR_MEMORY;
  1596. }
  1597. chomp(param);
  1598. /* Must be non-empty, and can't allow 1st character to be '{' */
  1599. if (param[0] == '\0' || param[0] == '{')
  1600. {
  1601. free(param);
  1602. return JB_ERR_CGI_PARAMS;
  1603. }
  1604. /* Check for embedded newlines */
  1605. for (s = param; *s != '\0'; s++)
  1606. {
  1607. if ((*s == '\r') || (*s == '\n'))
  1608. {
  1609. free(param);
  1610. return JB_ERR_CGI_PARAMS;
  1611. }
  1612. }
  1613. /* Check that regex is valid */
  1614. s = strdup(param);
  1615. if (s == NULL)
  1616. {
  1617. free(param);
  1618. return JB_ERR_MEMORY;
  1619. }
  1620. err = create_pattern_spec(compiled, s);
  1621. free(s);
  1622. if (err)
  1623. {
  1624. free(param);
  1625. return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
  1626. }
  1627. free_pattern_spec(compiled);
  1628. if (param[strlen(param) - 1] == '\\')
  1629. {
  1630. /*
  1631. * Must protect trailing '\\' from becoming line continuation character.
  1632. * Two methods: 1) If it's a domain only, add a trailing '/'.
  1633. * 2) For path, add the do-nothing PCRE expression (?:) to the end
  1634. */
  1635. if (strchr(param, '/') == NULL)
  1636. {
  1637. err = string_append(&param, "/");
  1638. }
  1639. else
  1640. {
  1641. err = string_append(&param, "(?:)");
  1642. }
  1643. if (err)
  1644. {
  1645. return err;
  1646. }
  1647. /* Check that the modified regex is valid */
  1648. s = strdup(param);
  1649. if (s == NULL)
  1650. {
  1651. free(param);
  1652. return JB_ERR_MEMORY;
  1653. }
  1654. err = create_pattern_spec(compiled, s);
  1655. free(s);
  1656. if (err)
  1657. {
  1658. free(param);
  1659. return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
  1660. }
  1661. free_pattern_spec(compiled);
  1662. }
  1663. *pvalue = param;
  1664. return JB_ERR_OK;
  1665. }
  1666. /*********************************************************************
  1667. *
  1668. * Function : map_radio
  1669. *
  1670. * Description : Map a set of radio button values. E.g. if you have
  1671. * 3 radio buttons, declare them as:
  1672. * <option type="radio" name="xyz" @xyz-a@>
  1673. * <option type="radio" name="xyz" @xyz-b@>
  1674. * <option type="radio" name="xyz" @xyz-c@>
  1675. * Then map one of the @xyz-?@ variables to "checked"
  1676. * and all the others to empty by calling:
  1677. * map_radio(exports, "xyz", "abc", sel)
  1678. * Where 'sel' is 'a', 'b', or 'c'.
  1679. *
  1680. * Parameters :
  1681. * 1 : exports = Exports map to modify.
  1682. * 2 : optionname = name for map
  1683. * 3 : values = null-terminated list of values;
  1684. * 4 : value = Selected value.
  1685. *
  1686. * CGI Parameters : None
  1687. *
  1688. * Returns : JB_ERR_OK on success
  1689. * JB_ERR_MEMORY on out-of-memory
  1690. *
  1691. *********************************************************************/
  1692. static jb_err map_radio(struct map * exports,
  1693. const char * optionname,
  1694. const char * values,
  1695. int value)
  1696. {
  1697. char * buf;
  1698. char * p;
  1699. char c;
  1700. const size_t len = strlen(optionname);
  1701. const size_t buf_size = len + 3;
  1702. assert(exports);
  1703. assert(optionname);
  1704. assert(values);
  1705. buf = malloc_or_die(buf_size);
  1706. strlcpy(buf, optionname, buf_size);
  1707. /* XXX: this looks ... interesting */
  1708. p = buf + len;
  1709. *p++ = '-';
  1710. p[1] = '\0';
  1711. while ((c = *values++) != '\0')
  1712. {
  1713. if (c != value)
  1714. {
  1715. *p = c;
  1716. if (map(exports, buf, 1, "", 1))
  1717. {
  1718. return JB_ERR_MEMORY;
  1719. }
  1720. }
  1721. }
  1722. *p = (char)value;
  1723. return map(exports, buf, 0, "checked", 1);
  1724. }
  1725. /*********************************************************************
  1726. *
  1727. * Function : cgi_error_modified
  1728. *
  1729. * Description : CGI function that is called when a file is modified
  1730. * outside the CGI editor.
  1731. *
  1732. * Parameters :
  1733. * 1 : csp = Current client state (buffers, headers, etc...)
  1734. * 2 : rsp = http_response data structure for output
  1735. * 3 : filename = The file that was modified.
  1736. *
  1737. * CGI Parameters : none
  1738. *
  1739. * Returns : JB_ERR_OK on success
  1740. * JB_ERR_MEMORY on out-of-memory error.
  1741. *
  1742. *********************************************************************/
  1743. jb_err cgi_error_modified(struct client_state *csp,
  1744. struct http_response *rsp,
  1745. const char *filename)
  1746. {
  1747. struct map *exports;
  1748. jb_err err;
  1749. assert(csp);
  1750. assert(rsp);
  1751. assert(filename);
  1752. if (NULL == (exports = default_exports(csp, NULL)))
  1753. {
  1754. return JB_ERR_MEMORY;
  1755. }
  1756. err = map(exports, "f", 1, html_encode(filename), 0);
  1757. if (err)
  1758. {
  1759. free_map(exports);
  1760. return err;
  1761. }
  1762. return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
  1763. }
  1764. /*********************************************************************
  1765. *
  1766. * Function : cgi_error_parse
  1767. *
  1768. * Description : CGI function that is called when a file cannot
  1769. * be parsed by the CGI editor.
  1770. *
  1771. * Parameters :
  1772. * 1 : csp = Current client state (buffers, headers, etc...)
  1773. * 2 : rsp = http_response data structure for output
  1774. * 3 : file = The file that was modified.
  1775. *
  1776. * CGI Parameters : none
  1777. *
  1778. * Returns : JB_ERR_OK on success
  1779. * JB_ERR_MEMORY on out-of-memory error.
  1780. *
  1781. *********************************************************************/
  1782. jb_err cgi_error_parse(struct client_state *csp,
  1783. struct http_response *rsp,
  1784. struct editable_file *file)
  1785. {
  1786. struct map *exports;
  1787. jb_err err;
  1788. struct file_line *cur_line;
  1789. assert(csp);
  1790. assert(rsp);
  1791. assert(file);
  1792. if (NULL == (exports = default_exports(csp, NULL)))
  1793. {
  1794. return JB_ERR_MEMORY;
  1795. }
  1796. err = map(exports, "f", 1, stringify(file->identifier), 0);
  1797. if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
  1798. cur_line = file->parse_error;
  1799. assert(cur_line);
  1800. if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
  1801. if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
  1802. if (err)
  1803. {
  1804. free_map(exports);
  1805. return err;
  1806. }
  1807. return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
  1808. }
  1809. /*********************************************************************
  1810. *
  1811. * Function : cgi_error_file
  1812. *
  1813. * Description : CGI function that is called when a file cannot be
  1814. * opened by the CGI editor.
  1815. *
  1816. * Parameters :
  1817. * 1 : csp = Current client state (buffers, headers, etc...)
  1818. * 2 : rsp = http_response data structure for output
  1819. * 3 : filename = The file that was modified.
  1820. *
  1821. * CGI Parameters : none
  1822. *
  1823. * Returns : JB_ERR_OK on success
  1824. * JB_ERR_MEMORY on out-of-memory error.
  1825. *
  1826. *********************************************************************/
  1827. jb_err cgi_error_file(struct client_state *csp,
  1828. struct http_response *rsp,
  1829. const char *filename)
  1830. {
  1831. struct map *exports;
  1832. jb_err err;
  1833. assert(csp);
  1834. assert(rsp);
  1835. assert(filename);
  1836. if (NULL == (exports = default_exports(csp, NULL)))
  1837. {
  1838. return JB_ERR_MEMORY;
  1839. }
  1840. err = map(exports, "f", 1, html_encode(filename), 0);
  1841. if (err)
  1842. {
  1843. free_map(exports);
  1844. return err;
  1845. }
  1846. return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
  1847. }
  1848. /*********************************************************************
  1849. *
  1850. * Function : cgi_error_file_read_only
  1851. *
  1852. * Description : CGI function that is called when a file cannot be
  1853. * opened for writing by the CGI editor.
  1854. *
  1855. * Parameters :
  1856. * 1 : csp = Current client state (buffers, headers, etc...)
  1857. * 2 : rsp = http_response data structure for output
  1858. * 3 : filename = The file that we can't write to
  1859. *
  1860. * CGI Parameters : none
  1861. *
  1862. * Returns : JB_ERR_OK on success
  1863. * JB_ERR_MEMORY on out-of-memory error.
  1864. *
  1865. *********************************************************************/
  1866. jb_err cgi_error_file_read_only(struct client_state *csp,
  1867. struct http_response *rsp,
  1868. const char *filename)
  1869. {
  1870. struct map *exports;
  1871. jb_err err;
  1872. assert(csp);
  1873. assert(rsp);
  1874. assert(filename);
  1875. if (NULL == (exports = default_exports(csp, NULL)))
  1876. {
  1877. return JB_ERR_MEMORY;
  1878. }
  1879. err = map(exports, "f", 1, html_encode(filename), 0);
  1880. if (err)
  1881. {
  1882. free_map(exports);
  1883. return err;
  1884. }
  1885. return template_fill_for_cgi(csp, "cgi-error-file-read-only", exports, rsp);
  1886. }
  1887. /*********************************************************************
  1888. *
  1889. * Function : cgi_edit_actions
  1890. *
  1891. * Description : CGI function that allows the user to choose which
  1892. * actions file to edit.
  1893. *
  1894. * Parameters :
  1895. * 1 : csp = Current client state (buffers, headers, etc...)
  1896. * 2 : rsp = http_response data structure for output
  1897. * 3 : parameters = map of cgi parameters
  1898. *
  1899. * CGI Parameters : None
  1900. *
  1901. * Returns : JB_ERR_OK on success
  1902. * JB_ERR_MEMORY on out-of-memory error
  1903. *
  1904. *********************************************************************/
  1905. jb_err cgi_edit_actions(struct client_state *csp,
  1906. struct http_response *rsp,
  1907. const struct map *parameters)
  1908. {
  1909. (void)parameters;
  1910. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  1911. {
  1912. return cgi_error_disabled(csp, rsp);
  1913. }
  1914. /* FIXME: Incomplete */
  1915. return cgi_redirect(rsp, CGI_PREFIX "edit-actions-list?f=default");
  1916. }
  1917. /*********************************************************************
  1918. *
  1919. * Function : cgi_edit_actions_list
  1920. *
  1921. * Description : CGI function that edits the actions list.
  1922. * FIXME: This function shouldn't FATAL ever.
  1923. * FIXME: This function doesn't check the retval of map()
  1924. * Parameters :
  1925. * 1 : csp = Current client state (buffers, headers, etc...)
  1926. * 2 : rsp = http_response data structure for output
  1927. * 3 : parameters = map of cgi parameters
  1928. *
  1929. * CGI Parameters : filename
  1930. *
  1931. * Returns : JB_ERR_OK on success
  1932. * JB_ERR_MEMORY on out-of-memory
  1933. * JB_ERR_FILE if the file cannot be opened or
  1934. * contains no data
  1935. * JB_ERR_CGI_PARAMS if "filename" was not specified
  1936. * or is not valid.
  1937. *
  1938. *********************************************************************/
  1939. jb_err cgi_edit_actions_list(struct client_state *csp,
  1940. struct http_response *rsp,
  1941. const struct map *parameters)
  1942. {
  1943. char * section_template;
  1944. char * url_template;
  1945. char * sections;
  1946. char * urls;
  1947. char buf[150];
  1948. char * s;
  1949. struct map * exports;
  1950. struct map * section_exports;
  1951. struct map * url_exports;
  1952. struct editable_file * file;
  1953. struct file_line * cur_line;
  1954. unsigned line_number = 0;
  1955. unsigned prev_section_line_number = ((unsigned) (-1));
  1956. int i, url_1_2;
  1957. struct file_list * fl;
  1958. struct url_actions * b;
  1959. char * buttons = NULL;
  1960. jb_err err;
  1961. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  1962. {
  1963. return cgi_error_disabled(csp, rsp);
  1964. }
  1965. if (NULL == (exports = default_exports(csp, NULL)))
  1966. {
  1967. return JB_ERR_MEMORY;
  1968. }
  1969. /* Load actions file */
  1970. err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
  1971. if (err)
  1972. {
  1973. /* No filename specified, can't read file, or out of memory. */
  1974. free_map(exports);
  1975. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  1976. }
  1977. /* Find start of actions in file */
  1978. cur_line = file->lines;
  1979. line_number = 1;
  1980. while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
  1981. {
  1982. cur_line = cur_line->next;
  1983. line_number++;
  1984. }
  1985. /*
  1986. * Conventional actions files should have a match all block
  1987. * at the start:
  1988. * cur_line = {...global actions...}
  1989. * cur_line->next = /
  1990. * cur_line->next->next = {...actions...} or EOF
  1991. */
  1992. if ( (cur_line != NULL)
  1993. && (cur_line->type == FILE_LINE_ACTION)
  1994. && (cur_line->next != NULL)
  1995. && (cur_line->next->type == FILE_LINE_URL)
  1996. && (0 == strcmp(cur_line->next->unprocessed, "/"))
  1997. && ( (cur_line->next->next == NULL)
  1998. || (cur_line->next->next->type != FILE_LINE_URL)
  1999. ) )
  2000. {
  2001. /*
  2002. * Generate string with buttons to set actions for "/" to
  2003. * any predefined set of actions (named standard.*, probably
  2004. * residing in standard.action).
  2005. */
  2006. err = template_load(csp, &section_template, "edit-actions-list-button", 0);
  2007. if (err)
  2008. {
  2009. edit_free_file(file);
  2010. free_map(exports);
  2011. if (err == JB_ERR_FILE)
  2012. {
  2013. return cgi_error_no_template(csp, rsp, "edit-actions-list-button");
  2014. }
  2015. return err;
  2016. }
  2017. err = template_fill(&section_template, exports);
  2018. if (err)
  2019. {
  2020. edit_free_file(file);
  2021. free_map(exports);
  2022. return err;
  2023. }
  2024. buttons = strdup("");
  2025. for (i = 0; i < MAX_AF_FILES; i++)
  2026. {
  2027. if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
  2028. {
  2029. for (b = b->next; NULL != b; b = b->next)
  2030. {
  2031. if (!strncmp(b->url->spec, "standard.", 9) && *(b->url->spec + 9) != '\0')
  2032. {
  2033. if (err)
  2034. {
  2035. freez(buttons);
  2036. free(section_template);
  2037. edit_free_file(file);
  2038. free_map(exports);
  2039. return JB_ERR_MEMORY;
  2040. }
  2041. section_exports = new_map();
  2042. err = map(section_exports, "button-name", 1, b->url->spec + 9, 1);
  2043. if (err || (NULL == (s = strdup(section_template))))
  2044. {
  2045. free_map(section_exports);
  2046. freez(buttons);
  2047. free(section_template);
  2048. edit_free_file(file);
  2049. free_map(exports);
  2050. return JB_ERR_MEMORY;
  2051. }
  2052. if (!err) err = template_fill(&s, section_exports);
  2053. free_map(section_exports);
  2054. if (!err) err = string_join(&buttons, s);
  2055. }
  2056. }
  2057. }
  2058. }
  2059. freez(section_template);
  2060. if (!err) err = map(exports, "all-urls-buttons", 1, buttons, 0);
  2061. /*
  2062. * Conventional actions file, supply extra editing help.
  2063. * (e.g. don't allow them to make it an unconventional one).
  2064. */
  2065. if (!err) err = map_conditional(exports, "all-urls-present", 1);
  2066. snprintf(buf, sizeof(buf), "%u", line_number);
  2067. if (!err) err = map(exports, "all-urls-s", 1, buf, 1);
  2068. snprintf(buf, sizeof(buf), "%u", line_number + 2);
  2069. if (!err) err = map(exports, "all-urls-s-next", 1, buf, 1);
  2070. if (!err) err = map(exports, "all-urls-actions", 1,
  2071. actions_to_html(csp, cur_line->data.action), 0);
  2072. /* Skip the 2 lines */
  2073. cur_line = cur_line->next->next;
  2074. line_number += 2;
  2075. /*
  2076. * Note that prev_section_line_number is NOT set here.
  2077. * This is deliberate and not a bug. It stops a "Move up"
  2078. * option appearing on the next section. Clicking "Move
  2079. * up" would make the actions file unconventional, which
  2080. * we don't want, so we hide this option.
  2081. */
  2082. }
  2083. else
  2084. {
  2085. /*
  2086. * Non-standard actions file - does not begin with
  2087. * the "All URLs" section.
  2088. */
  2089. if (!err) err = map_conditional(exports, "all-urls-present", 0);
  2090. }
  2091. /* Set up global exports */
  2092. if (!err) err = map(exports, "actions-file", 1, html_encode(file->filename), 0);
  2093. if (!err) err = map(exports, "f", 1, stringify(file->identifier), 0);
  2094. if (!err) err = map(exports, "v", 1, file->version_str, 1);
  2095. /* Discourage private additions to default.action */
  2096. if (!err) err = map_conditional(exports, "default-action",
  2097. (strstr("default.action", file->filename) != NULL));
  2098. if (err)
  2099. {
  2100. edit_free_file(file);
  2101. free_map(exports);
  2102. return err;
  2103. }
  2104. /* Should do all global exports above this point */
  2105. /* Load templates */
  2106. err = template_load(csp, &section_template, "edit-actions-list-section", 0);
  2107. if (err)
  2108. {
  2109. edit_free_file(file);
  2110. free_map(exports);
  2111. if (err == JB_ERR_FILE)
  2112. {
  2113. return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
  2114. }
  2115. return err;
  2116. }
  2117. err = template_load(csp, &url_template, "edit-actions-list-url", 0);
  2118. if (err)
  2119. {
  2120. free(section_template);
  2121. edit_free_file(file);
  2122. free_map(exports);
  2123. if (err == JB_ERR_FILE)
  2124. {
  2125. return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
  2126. }
  2127. return err;
  2128. }
  2129. err = template_fill(&section_template, exports);
  2130. if (err)
  2131. {
  2132. free(url_template);
  2133. edit_free_file(file);
  2134. free_map(exports);
  2135. return err;
  2136. }
  2137. err = template_fill(&url_template, exports);
  2138. if (err)
  2139. {
  2140. free(section_template);
  2141. edit_free_file(file);
  2142. free_map(exports);
  2143. return err;
  2144. }
  2145. if (NULL == (sections = strdup("")))
  2146. {
  2147. free(section_template);
  2148. free(url_template);
  2149. edit_free_file(file);
  2150. free_map(exports);
  2151. return JB_ERR_MEMORY;
  2152. }
  2153. while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
  2154. {
  2155. section_exports = new_map();
  2156. snprintf(buf, sizeof(buf), "%u", line_number);
  2157. err = map(section_exports, "s", 1, buf, 1);
  2158. if (!err) err = map(section_exports, "actions", 1,
  2159. actions_to_html(csp, cur_line->data.action), 0);
  2160. if ((!err)
  2161. && (cur_line->next != NULL)
  2162. && (cur_line->next->type == FILE_LINE_URL))
  2163. {
  2164. /* This section contains at least one URL, don't allow delete */
  2165. err = map_block_killer(section_exports, "empty-section");
  2166. }
  2167. else
  2168. {
  2169. if (!err) err = map_block_keep(section_exports, "empty-section");
  2170. }
  2171. if (prev_section_line_number != ((unsigned)(-1)))
  2172. {
  2173. /* Not last section */
  2174. snprintf(buf, sizeof(buf), "%u", prev_section_line_number);
  2175. if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
  2176. if (!err) err = map_block_keep(section_exports, "s-prev-exists");
  2177. }
  2178. else
  2179. {
  2180. /* Last section */
  2181. if (!err) err = map_block_killer(section_exports, "s-prev-exists");
  2182. }
  2183. prev_section_line_number = line_number;
  2184. if (err)
  2185. {
  2186. free(sections);
  2187. free(section_template);
  2188. free(url_template);
  2189. edit_free_file(file);
  2190. free_map(exports);
  2191. free_map(section_exports);
  2192. return err;
  2193. }
  2194. /* Should do all section-specific exports above this point */
  2195. if (NULL == (urls = strdup("")))
  2196. {
  2197. free(sections);
  2198. free(section_template);
  2199. free(url_template);
  2200. edit_free_file(file);
  2201. free_map(exports);
  2202. free_map(section_exports);
  2203. return JB_ERR_MEMORY;
  2204. }
  2205. url_1_2 = 2;
  2206. cur_line = cur_line->next;
  2207. line_number++;
  2208. while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
  2209. {
  2210. url_exports = new_map();
  2211. snprintf(buf, sizeof(buf), "%u", line_number);
  2212. err = map(url_exports, "p", 1, buf, 1);
  2213. snprintf(buf, sizeof(buf), "%d", url_1_2);
  2214. if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
  2215. if (!err) err = map(url_exports, "url-html", 1,
  2216. html_encode(cur_line->unprocessed), 0);
  2217. if (!err) err = map(url_exports, "url", 1,
  2218. url_encode(cur_line->unprocessed), 0);
  2219. if (err)
  2220. {
  2221. free(urls);
  2222. free(sections);
  2223. free(section_template);
  2224. free(url_template);
  2225. edit_free_file(file);
  2226. free_map(exports);
  2227. free_map(section_exports);
  2228. free_map(url_exports);
  2229. return err;
  2230. }
  2231. if (NULL == (s = strdup(url_template)))
  2232. {
  2233. free(urls);
  2234. free(sections);
  2235. free(section_template);
  2236. free(url_template);
  2237. edit_free_file(file);
  2238. free_map(exports);
  2239. free_map(section_exports);
  2240. free_map(url_exports);
  2241. return JB_ERR_MEMORY;
  2242. }
  2243. err = template_fill(&s, section_exports);
  2244. if (!err) err = template_fill(&s, url_exports);
  2245. if (!err) err = string_append(&urls, s);
  2246. free_map(url_exports);
  2247. freez(s);
  2248. if (err)
  2249. {
  2250. freez(urls);
  2251. free(sections);
  2252. free(section_template);
  2253. free(url_template);
  2254. edit_free_file(file);
  2255. free_map(exports);
  2256. free_map(section_exports);
  2257. return err;
  2258. }
  2259. url_1_2 = 3 - url_1_2;
  2260. cur_line = cur_line->next;
  2261. line_number++;
  2262. }
  2263. err = map(section_exports, "urls", 1, urls, 0);
  2264. /* Could also do section-specific exports here, but it wouldn't be as fast */
  2265. snprintf(buf, sizeof(buf), "%u", line_number);
  2266. if (!err) err = map(section_exports, "s-next", 1, buf, 1);
  2267. if ((cur_line != NULL)
  2268. && (cur_line->type == FILE_LINE_ACTION))
  2269. {
  2270. /* Not last section */
  2271. if (!err) err = map_block_keep(section_exports, "s-next-exists");
  2272. }
  2273. else
  2274. {
  2275. /* Last section */
  2276. if (!err) err = map_block_killer(section_exports, "s-next-exists");
  2277. }
  2278. if (err)
  2279. {
  2280. free(sections);
  2281. free(section_template);
  2282. free(url_template);
  2283. edit_free_file(file);
  2284. free_map(exports);
  2285. free_map(section_exports);
  2286. return err;
  2287. }
  2288. if (NULL == (s = strdup(section_template)))
  2289. {
  2290. free(sections);
  2291. free(section_template);
  2292. free(url_template);
  2293. edit_free_file(file);
  2294. free_map(exports);
  2295. free_map(section_exports);
  2296. return JB_ERR_MEMORY;
  2297. }
  2298. err = template_fill(&s, section_exports);
  2299. if (!err) err = string_append(&sections, s);
  2300. freez(s);
  2301. free_map(section_exports);
  2302. if (err)
  2303. {
  2304. freez(sections);
  2305. free(section_template);
  2306. free(url_template);
  2307. edit_free_file(file);
  2308. free_map(exports);
  2309. return err;
  2310. }
  2311. }
  2312. edit_free_file(file);
  2313. free(section_template);
  2314. free(url_template);
  2315. err = map(exports, "sections", 1, sections, 0);
  2316. if (err)
  2317. {
  2318. free_map(exports);
  2319. return err;
  2320. }
  2321. /* Could also do global exports here, but it wouldn't be as fast */
  2322. return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
  2323. }
  2324. /*********************************************************************
  2325. *
  2326. * Function : cgi_edit_actions_for_url
  2327. *
  2328. * Description : CGI function that edits the Actions list.
  2329. *
  2330. * Parameters :
  2331. * 1 : csp = Current client state (buffers, headers, etc...)
  2332. * 2 : rsp = http_response data structure for output
  2333. * 3 : parameters = map of cgi parameters
  2334. *
  2335. * CGI Parameters : None
  2336. *
  2337. * Returns : JB_ERR_OK on success
  2338. * JB_ERR_MEMORY on out-of-memory
  2339. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  2340. * specified or not valid.
  2341. *
  2342. *********************************************************************/
  2343. jb_err cgi_edit_actions_for_url(struct client_state *csp,
  2344. struct http_response *rsp,
  2345. const struct map *parameters)
  2346. {
  2347. struct map * exports;
  2348. unsigned sectionid;
  2349. struct editable_file * file;
  2350. struct file_line * cur_line;
  2351. unsigned line_number;
  2352. jb_err err;
  2353. struct re_filterfile_spec *filter_group;
  2354. int i, have_filters = 0;
  2355. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  2356. {
  2357. return cgi_error_disabled(csp, rsp);
  2358. }
  2359. err = get_number_param(csp, parameters, "s", &sectionid);
  2360. if (err)
  2361. {
  2362. return err;
  2363. }
  2364. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  2365. if (err)
  2366. {
  2367. /* No filename specified, can't read file, modified, or out of memory. */
  2368. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  2369. }
  2370. cur_line = file->lines;
  2371. for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
  2372. {
  2373. cur_line = cur_line->next;
  2374. }
  2375. if ( (cur_line == NULL)
  2376. || (line_number != sectionid)
  2377. || (sectionid < 1)
  2378. || (cur_line->type != FILE_LINE_ACTION))
  2379. {
  2380. /* Invalid "sectionid" parameter */
  2381. edit_free_file(file);
  2382. return JB_ERR_CGI_PARAMS;
  2383. }
  2384. if (NULL == (exports = default_exports(csp, NULL)))
  2385. {
  2386. edit_free_file(file);
  2387. return JB_ERR_MEMORY;
  2388. }
  2389. err = map(exports, "f", 1, stringify(file->identifier), 0);
  2390. if (!err) err = map(exports, "v", 1, file->version_str, 1);
  2391. if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
  2392. if (!err) err = actions_to_radio(exports, cur_line->data.action);
  2393. /*
  2394. * XXX: Some browsers (at least IE6 and IE7) have an artificial URL
  2395. * length limitation and ignore clicks on the Submit buttons if
  2396. * the resulting GET URL would be longer than their limit.
  2397. *
  2398. * In Privoxy 3.0.5 beta the standard edit-actions-for-url template
  2399. * reached this limit and action editing stopped working in these
  2400. * browsers (BR #1570678).
  2401. *
  2402. * The config option split-large-forms works around this browser
  2403. * bug (HTTP has no URL length limitation) by dividing the action
  2404. * list form into multiple smaller ones. It means the URLs are shorter
  2405. * and work in broken browsers as well, but the user can no longer change
  2406. * all actions with one submit.
  2407. *
  2408. * A better solution would be to switch to POST requests,
  2409. * but this will do for now.
  2410. */
  2411. if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_SPLIT_LARGE_FORMS))
  2412. {
  2413. /* Generate multiple smaller form by killing the big one. */
  2414. err = map_block_killer(exports, "one-form-only");
  2415. }
  2416. else
  2417. {
  2418. /* Default: Generate one large form by killing the smaller ones. */
  2419. err = map_block_killer(exports, "multiple-forms");
  2420. }
  2421. for (i = 0; i < MAX_AF_FILES; i++)
  2422. {
  2423. if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
  2424. {
  2425. if (!err) err = map_conditional(exports, "any-filters-defined", 1);
  2426. have_filters = 1;
  2427. break;
  2428. }
  2429. }
  2430. #ifndef FEATURE_EXTERNAL_FILTERS
  2431. if (!err) err = map_block_killer(exports, "external-content-filters");
  2432. #endif
  2433. #ifndef FEATURE_HTTPS_INSPECTION
  2434. if (!err) err = map_block_killer(exports, "https-inspection");
  2435. #endif
  2436. if (err)
  2437. {
  2438. edit_free_file(file);
  2439. free_map(exports);
  2440. return err;
  2441. }
  2442. if (0 == have_filters)
  2443. {
  2444. err = map(exports, "filter-params", 1, "", 1);
  2445. }
  2446. else
  2447. {
  2448. /*
  2449. * List available filters and their settings.
  2450. */
  2451. char *filter_template;
  2452. int filter_identifier = 0;
  2453. char *prepared_templates[MAX_FILTER_TYPES];
  2454. for (i = 0; i < MAX_FILTER_TYPES; i++)
  2455. {
  2456. prepared_templates[i] = strdup("");
  2457. }
  2458. err = template_load(csp, &filter_template, "edit-actions-for-url-filter", 0);
  2459. if (err)
  2460. {
  2461. edit_free_file(file);
  2462. free_map(exports);
  2463. if (err == JB_ERR_FILE)
  2464. {
  2465. return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
  2466. }
  2467. return err;
  2468. }
  2469. err = template_fill(&filter_template, exports);
  2470. for (i = 0; i < MAX_AF_FILES; i++)
  2471. {
  2472. if ((csp->rlist[i] != NULL) && (csp->rlist[i]->f != NULL))
  2473. {
  2474. filter_group = csp->rlist[i]->f;
  2475. for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
  2476. {
  2477. char current_mode = 'x';
  2478. char number[20];
  2479. struct list_entry *filter_name;
  2480. struct map *line_exports;
  2481. const enum filter_type type = filter_group->type;
  2482. const int multi_action_index = filter_type_info[type].multi_action_index;
  2483. assert(type < MAX_FILTER_TYPES);
  2484. filter_name = cur_line->data.action->multi_add[multi_action_index]->first;
  2485. while ((filter_name != NULL)
  2486. && (0 != strcmp(filter_group->name, filter_name->str)))
  2487. {
  2488. filter_name = filter_name->next;
  2489. }
  2490. if (filter_name != NULL)
  2491. {
  2492. current_mode = 'y';
  2493. }
  2494. else
  2495. {
  2496. filter_name = cur_line->data.action->multi_remove[multi_action_index]->first;
  2497. while ((filter_name != NULL)
  2498. && (0 != strcmp(filter_group->name, filter_name->str)))
  2499. {
  2500. filter_name = filter_name->next;
  2501. }
  2502. if (filter_name != NULL)
  2503. {
  2504. current_mode = 'n';
  2505. }
  2506. }
  2507. /* Generate a unique serial number */
  2508. snprintf(number, sizeof(number), "%x", filter_identifier++);
  2509. number[sizeof(number) - 1] = '\0';
  2510. line_exports = new_map();
  2511. if (line_exports == NULL)
  2512. {
  2513. err = JB_ERR_MEMORY;
  2514. }
  2515. else
  2516. {
  2517. char *filter_line;
  2518. if (!err) err = map(line_exports, "index", 1, number, 1);
  2519. if (!err) err = map(line_exports, "name", 1, filter_group->name, 1);
  2520. if (!err) err = map(line_exports, "description", 1, filter_group->description, 1);
  2521. if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
  2522. if (!err) err = map(line_exports, "filter-type", 1, filter_type_info[type].type, 1);
  2523. if (!err) err = map(line_exports, "abbr-filter-type", 1, filter_type_info[type].abbr_type, 1);
  2524. if (!err) err = map(line_exports, "anchor", 1, filter_type_info[type].anchor, 1);
  2525. if (!err)
  2526. {
  2527. filter_line = strdup(filter_template);
  2528. if (filter_line == NULL) err = JB_ERR_MEMORY;
  2529. }
  2530. if (!err) err = template_fill(&filter_line, line_exports);
  2531. string_join(&prepared_templates[type], filter_line);
  2532. free_map(line_exports);
  2533. }
  2534. }
  2535. }
  2536. }
  2537. freez(filter_template);
  2538. /* Replace all filter macros with the aggregated templates */
  2539. for (i = 0; i < MAX_FILTER_TYPES; i++)
  2540. {
  2541. if (err) break;
  2542. err = map(exports, filter_type_info[i].macro_name, 1, prepared_templates[i], 0);
  2543. }
  2544. if (err)
  2545. {
  2546. /* Free aggregated templates */
  2547. for (i = 0; i < MAX_FILTER_TYPES; i++)
  2548. {
  2549. freez(prepared_templates[i]);
  2550. }
  2551. }
  2552. }
  2553. /* Check or uncheck the "disable all of this type" radio buttons. */
  2554. for (i = 0; i < MAX_FILTER_TYPES; i++)
  2555. {
  2556. const int a = filter_type_info[i].multi_action_index;
  2557. const int disable_all = cur_line->data.action->multi_remove_all[a];
  2558. if (err) break;
  2559. err = map_radio(exports, filter_type_info[i].disable_all_option, "nx", (disable_all ? 'n' : 'x'));
  2560. }
  2561. edit_free_file(file);
  2562. if (err)
  2563. {
  2564. free_map(exports);
  2565. return err;
  2566. }
  2567. return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
  2568. }
  2569. /*********************************************************************
  2570. *
  2571. * Function : get_number_of_filters
  2572. *
  2573. * Description : Counts the number of filter available.
  2574. *
  2575. * Parameters :
  2576. * 1 : csp = Current client state (buffers, headers, etc...)
  2577. *
  2578. * Returns : Number of filters available.
  2579. *
  2580. *********************************************************************/
  2581. static int get_number_of_filters(const struct client_state *csp)
  2582. {
  2583. int i;
  2584. struct re_filterfile_spec *b;
  2585. struct file_list *fl;
  2586. int number_of_filters = 0;
  2587. for (i = 0; i < MAX_AF_FILES; i++)
  2588. {
  2589. fl = csp->rlist[i];
  2590. if ((NULL == fl) || (NULL == fl->f))
  2591. {
  2592. /*
  2593. * Either there are no filter files left or this
  2594. * filter file just contains no valid filters.
  2595. *
  2596. * Continue to be sure we don't miss valid filter
  2597. * files that are chained after empty or invalid ones.
  2598. */
  2599. continue;
  2600. }
  2601. for (b = fl->f; b != NULL; b = b->next)
  2602. {
  2603. number_of_filters++;
  2604. }
  2605. }
  2606. return number_of_filters;
  2607. }
  2608. /*********************************************************************
  2609. *
  2610. * Function : cgi_edit_actions_submit
  2611. *
  2612. * Description : CGI function that actually edits the Actions list.
  2613. *
  2614. * Parameters :
  2615. * 1 : csp = Current client state (buffers, headers, etc...)
  2616. * 2 : rsp = http_response data structure for output
  2617. * 3 : parameters = map of cgi parameters
  2618. *
  2619. * CGI Parameters : None
  2620. *
  2621. * Returns : JB_ERR_OK on success
  2622. * JB_ERR_MEMORY on out-of-memory
  2623. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  2624. * specified or not valid.
  2625. *
  2626. *********************************************************************/
  2627. jb_err cgi_edit_actions_submit(struct client_state *csp,
  2628. struct http_response *rsp,
  2629. const struct map *parameters)
  2630. {
  2631. unsigned sectionid;
  2632. char * actiontext;
  2633. char * newtext;
  2634. size_t newtext_size;
  2635. size_t len;
  2636. struct editable_file * file;
  2637. struct file_line * cur_line;
  2638. unsigned line_number;
  2639. char target[1024];
  2640. jb_err err;
  2641. int filter_identifier;
  2642. int i;
  2643. const char * action_set_name;
  2644. struct file_list * fl;
  2645. struct url_actions * b;
  2646. int number_of_filters;
  2647. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  2648. {
  2649. return cgi_error_disabled(csp, rsp);
  2650. }
  2651. err = get_number_param(csp, parameters, "s", &sectionid);
  2652. if (err)
  2653. {
  2654. return err;
  2655. }
  2656. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  2657. if (err)
  2658. {
  2659. /* No filename specified, can't read file, modified, or out of memory. */
  2660. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  2661. }
  2662. cur_line = file->lines;
  2663. for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
  2664. {
  2665. cur_line = cur_line->next;
  2666. }
  2667. if ( (cur_line == NULL)
  2668. || (line_number != sectionid)
  2669. || (sectionid < 1)
  2670. || (cur_line->type != FILE_LINE_ACTION))
  2671. {
  2672. /* Invalid "sectionid" parameter */
  2673. edit_free_file(file);
  2674. return JB_ERR_CGI_PARAMS;
  2675. }
  2676. get_string_param(parameters, "p", &action_set_name);
  2677. if (action_set_name != NULL)
  2678. {
  2679. for (filter_identifier = 0; filter_identifier < MAX_AF_FILES; filter_identifier++)
  2680. {
  2681. if (((fl = csp->actions_list[filter_identifier]) != NULL) && ((b = fl->f) != NULL))
  2682. {
  2683. for (b = b->next; NULL != b; b = b->next)
  2684. {
  2685. if (!strncmp(b->url->spec, "standard.", 9) && !strcmp(b->url->spec + 9, action_set_name))
  2686. {
  2687. copy_action(cur_line->data.action, b->action);
  2688. goto found;
  2689. }
  2690. }
  2691. }
  2692. }
  2693. edit_free_file(file);
  2694. return JB_ERR_CGI_PARAMS;
  2695. found: ;
  2696. }
  2697. else
  2698. {
  2699. err = actions_from_radio(parameters, cur_line->data.action);
  2700. }
  2701. if (err)
  2702. {
  2703. /* Out of memory */
  2704. edit_free_file(file);
  2705. return err;
  2706. }
  2707. /* Check the "disable all of this type" parameters. */
  2708. for (i = 0; i < MAX_FILTER_TYPES; i++)
  2709. {
  2710. const int multi_action_index = filter_type_info[i].multi_action_index;
  2711. const char ch = get_char_param(parameters, filter_type_info[i].disable_all_param);
  2712. if (ch == 'N')
  2713. {
  2714. list_remove_all(cur_line->data.action->multi_add[multi_action_index]);
  2715. list_remove_all(cur_line->data.action->multi_remove[multi_action_index]);
  2716. cur_line->data.action->multi_remove_all[multi_action_index] = 1;
  2717. }
  2718. else if (ch == 'X')
  2719. {
  2720. cur_line->data.action->multi_remove_all[multi_action_index] = 0;
  2721. }
  2722. }
  2723. number_of_filters = get_number_of_filters(csp);
  2724. for (filter_identifier = 0; filter_identifier < number_of_filters && !err; filter_identifier++)
  2725. {
  2726. char key_value[30];
  2727. char key_name[30];
  2728. char key_type[30];
  2729. const char *name;
  2730. char value; /*
  2731. * Filter state. Valid states are: 'Y' (active),
  2732. * 'N' (inactive) and 'X' (no change).
  2733. * XXX: bad name.
  2734. */
  2735. char type; /*
  2736. * Abbreviated filter type. Valid types are: 'F' (content filter),
  2737. * 'S' (server-header filter) and 'C' (client-header filter).
  2738. */
  2739. int multi_action_index = 0;
  2740. /* Generate the keys */
  2741. snprintf(key_value, sizeof(key_value), "filter_r%x", filter_identifier);
  2742. key_value[sizeof(key_value) - 1] = '\0'; /* XXX: Why? */
  2743. snprintf(key_name, sizeof(key_name), "filter_n%x", filter_identifier);
  2744. key_name[sizeof(key_name) - 1] = '\0'; /* XXX: Why? */
  2745. snprintf(key_type, sizeof(key_type), "filter_t%x", filter_identifier);
  2746. err = get_string_param(parameters, key_name, &name);
  2747. if (err) break;
  2748. if (name == NULL)
  2749. {
  2750. /* The filter identifier isn't present. Try the next one. */
  2751. continue;
  2752. }
  2753. type = get_char_param(parameters, key_type);
  2754. switch (type)
  2755. {
  2756. case 'F':
  2757. multi_action_index = ACTION_MULTI_FILTER;
  2758. break;
  2759. case 'S':
  2760. multi_action_index = ACTION_MULTI_SERVER_HEADER_FILTER;
  2761. break;
  2762. case 'C':
  2763. multi_action_index = ACTION_MULTI_CLIENT_HEADER_FILTER;
  2764. break;
  2765. case 'L':
  2766. multi_action_index = ACTION_MULTI_CLIENT_HEADER_TAGGER;
  2767. break;
  2768. case 'E':
  2769. multi_action_index = ACTION_MULTI_SERVER_HEADER_TAGGER;
  2770. break;
  2771. default:
  2772. log_error(LOG_LEVEL_ERROR,
  2773. "Unknown filter type: %c for filter %s. Filter ignored.", type, name);
  2774. continue;
  2775. }
  2776. assert(multi_action_index);
  2777. value = get_char_param(parameters, key_value);
  2778. if (value == 'Y')
  2779. {
  2780. list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
  2781. if (!err) err = enlist(cur_line->data.action->multi_add[multi_action_index], name);
  2782. list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
  2783. }
  2784. else if (value == 'N')
  2785. {
  2786. list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
  2787. if (!cur_line->data.action->multi_remove_all[multi_action_index])
  2788. {
  2789. list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
  2790. if (!err) err = enlist(cur_line->data.action->multi_remove[multi_action_index], name);
  2791. }
  2792. }
  2793. else if (value == 'X')
  2794. {
  2795. list_remove_item(cur_line->data.action->multi_add[multi_action_index], name);
  2796. list_remove_item(cur_line->data.action->multi_remove[multi_action_index], name);
  2797. }
  2798. }
  2799. if (err)
  2800. {
  2801. /* Out of memory */
  2802. edit_free_file(file);
  2803. return err;
  2804. }
  2805. if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
  2806. {
  2807. /* Out of memory */
  2808. edit_free_file(file);
  2809. return JB_ERR_MEMORY;
  2810. }
  2811. len = strlen(actiontext);
  2812. if (len == 0)
  2813. {
  2814. /*
  2815. * Empty action - must special-case this.
  2816. * Simply setting len to 1 is sufficient...
  2817. */
  2818. len = 1;
  2819. }
  2820. newtext_size = len + 2;
  2821. newtext = malloc_or_die(newtext_size);
  2822. strlcpy(newtext, actiontext, newtext_size);
  2823. free(actiontext);
  2824. newtext[0] = '{';
  2825. newtext[len] = '}';
  2826. newtext[len + 1] = '\0';
  2827. freez(cur_line->raw);
  2828. freez(cur_line->unprocessed);
  2829. cur_line->unprocessed = newtext;
  2830. err = edit_write_file(file);
  2831. if (err)
  2832. {
  2833. /* Error writing file */
  2834. if (err == JB_ERR_FILE)
  2835. {
  2836. /* Read-only file. */
  2837. err = cgi_error_file_read_only(csp, rsp, file->filename);
  2838. }
  2839. edit_free_file(file);
  2840. return err;
  2841. }
  2842. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
  2843. (unsigned long) time(NULL), file->identifier, sectionid);
  2844. edit_free_file(file);
  2845. return cgi_redirect(rsp, target);
  2846. }
  2847. /*********************************************************************
  2848. *
  2849. * Function : cgi_edit_actions_url
  2850. *
  2851. * Description : CGI function that actually edits a URL pattern in
  2852. * an actions file.
  2853. *
  2854. * Parameters :
  2855. * 1 : csp = Current client state (buffers, headers, etc...)
  2856. * 2 : rsp = http_response data structure for output
  2857. * 3 : parameters = map of cgi parameters
  2858. *
  2859. * CGI Parameters :
  2860. * filename : Identifies the file to edit
  2861. * ver : File's last-modified time
  2862. * section : Line number of section to edit
  2863. * pattern : Line number of pattern to edit
  2864. * newval : New value for pattern
  2865. *
  2866. * Returns : JB_ERR_OK on success
  2867. * JB_ERR_MEMORY on out-of-memory
  2868. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  2869. * specified or not valid.
  2870. *
  2871. *********************************************************************/
  2872. jb_err cgi_edit_actions_url(struct client_state *csp,
  2873. struct http_response *rsp,
  2874. const struct map *parameters)
  2875. {
  2876. unsigned patternid;
  2877. char * new_pattern;
  2878. struct editable_file * file;
  2879. struct file_line * cur_line;
  2880. unsigned line_number;
  2881. unsigned section_start_line_number = 0;
  2882. char target[1024];
  2883. jb_err err;
  2884. assert(csp);
  2885. assert(rsp);
  2886. assert(parameters);
  2887. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  2888. {
  2889. return cgi_error_disabled(csp, rsp);
  2890. }
  2891. err = get_number_param(csp, parameters, "p", &patternid);
  2892. if (err)
  2893. {
  2894. return err;
  2895. }
  2896. if (patternid < 1U)
  2897. {
  2898. return JB_ERR_CGI_PARAMS;
  2899. }
  2900. err = get_url_spec_param(csp, parameters, "u", &new_pattern);
  2901. if (err)
  2902. {
  2903. return err;
  2904. }
  2905. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  2906. if (err)
  2907. {
  2908. /* No filename specified, can't read file, modified, or out of memory. */
  2909. free(new_pattern);
  2910. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  2911. }
  2912. line_number = 1;
  2913. cur_line = file->lines;
  2914. while ((cur_line != NULL) && (line_number < patternid))
  2915. {
  2916. if (cur_line->type == FILE_LINE_ACTION)
  2917. {
  2918. section_start_line_number = line_number;
  2919. }
  2920. cur_line = cur_line->next;
  2921. line_number++;
  2922. }
  2923. if ((cur_line == NULL)
  2924. || (cur_line->type != FILE_LINE_URL))
  2925. {
  2926. /* Invalid "patternid" parameter */
  2927. free(new_pattern);
  2928. edit_free_file(file);
  2929. return JB_ERR_CGI_PARAMS;
  2930. }
  2931. /* At this point, the line to edit is in cur_line */
  2932. freez(cur_line->raw);
  2933. freez(cur_line->unprocessed);
  2934. cur_line->unprocessed = new_pattern;
  2935. err = edit_write_file(file);
  2936. if (err)
  2937. {
  2938. /* Error writing file */
  2939. if (err == JB_ERR_FILE)
  2940. {
  2941. /* Read-only file. */
  2942. err = cgi_error_file_read_only(csp, rsp, file->filename);
  2943. }
  2944. edit_free_file(file);
  2945. return err;
  2946. }
  2947. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
  2948. (unsigned long) time(NULL), file->identifier, section_start_line_number);
  2949. edit_free_file(file);
  2950. return cgi_redirect(rsp, target);
  2951. }
  2952. /*********************************************************************
  2953. *
  2954. * Function : cgi_edit_actions_add_url
  2955. *
  2956. * Description : CGI function that actually adds a URL pattern to
  2957. * an actions file.
  2958. *
  2959. * Parameters :
  2960. * 1 : csp = Current client state (buffers, headers, etc...)
  2961. * 2 : rsp = http_response data structure for output
  2962. * 3 : parameters = map of cgi parameters
  2963. *
  2964. * CGI Parameters :
  2965. * filename : Identifies the file to edit
  2966. * ver : File's last-modified time
  2967. * section : Line number of section to edit
  2968. * newval : New pattern
  2969. *
  2970. * Returns : JB_ERR_OK on success
  2971. * JB_ERR_MEMORY on out-of-memory
  2972. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  2973. * specified or not valid.
  2974. *
  2975. *********************************************************************/
  2976. jb_err cgi_edit_actions_add_url(struct client_state *csp,
  2977. struct http_response *rsp,
  2978. const struct map *parameters)
  2979. {
  2980. unsigned sectionid;
  2981. char * new_pattern;
  2982. struct file_line * new_line;
  2983. struct editable_file * file;
  2984. struct file_line * cur_line;
  2985. unsigned line_number;
  2986. char target[1024];
  2987. jb_err err;
  2988. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  2989. {
  2990. return cgi_error_disabled(csp, rsp);
  2991. }
  2992. err = get_number_param(csp, parameters, "s", &sectionid);
  2993. if (err)
  2994. {
  2995. return err;
  2996. }
  2997. if (sectionid < 1U)
  2998. {
  2999. return JB_ERR_CGI_PARAMS;
  3000. }
  3001. err = get_url_spec_param(csp, parameters, "u", &new_pattern);
  3002. if (err)
  3003. {
  3004. return err;
  3005. }
  3006. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  3007. if (err)
  3008. {
  3009. /* No filename specified, can't read file, modified, or out of memory. */
  3010. free(new_pattern);
  3011. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  3012. }
  3013. line_number = 1;
  3014. cur_line = file->lines;
  3015. while ((cur_line != NULL) && (line_number < sectionid))
  3016. {
  3017. cur_line = cur_line->next;
  3018. line_number++;
  3019. }
  3020. if ((cur_line == NULL)
  3021. || (cur_line->type != FILE_LINE_ACTION))
  3022. {
  3023. /* Invalid "sectionid" parameter */
  3024. free(new_pattern);
  3025. edit_free_file(file);
  3026. return JB_ERR_CGI_PARAMS;
  3027. }
  3028. /* At this point, the section header is in cur_line - add after this. */
  3029. /* Allocate the new line */
  3030. new_line = zalloc_or_die(sizeof(*new_line));
  3031. /* Fill in the data members of the new line */
  3032. new_line->raw = NULL;
  3033. new_line->prefix = NULL;
  3034. new_line->unprocessed = new_pattern;
  3035. new_line->type = FILE_LINE_URL;
  3036. /* Link new_line into the list, after cur_line */
  3037. new_line->next = cur_line->next;
  3038. cur_line->next = new_line;
  3039. /* Done making changes, now commit */
  3040. err = edit_write_file(file);
  3041. if (err)
  3042. {
  3043. /* Error writing file */
  3044. if (err == JB_ERR_FILE)
  3045. {
  3046. /* Read-only file. */
  3047. err = cgi_error_file_read_only(csp, rsp, file->filename);
  3048. }
  3049. edit_free_file(file);
  3050. return err;
  3051. }
  3052. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
  3053. (unsigned long) time(NULL), file->identifier, sectionid);
  3054. edit_free_file(file);
  3055. return cgi_redirect(rsp, target);
  3056. }
  3057. /*********************************************************************
  3058. *
  3059. * Function : cgi_edit_actions_remove_url
  3060. *
  3061. * Description : CGI function that actually removes a URL pattern from
  3062. * the actions file.
  3063. *
  3064. * Parameters :
  3065. * 1 : csp = Current client state (buffers, headers, etc...)
  3066. * 2 : rsp = http_response data structure for output
  3067. * 3 : parameters = map of cgi parameters
  3068. *
  3069. * CGI Parameters :
  3070. * f : (filename) Identifies the file to edit
  3071. * v : (version) File's last-modified time
  3072. * p : (pattern) Line number of pattern to remove
  3073. *
  3074. * Returns : JB_ERR_OK on success
  3075. * JB_ERR_MEMORY on out-of-memory
  3076. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  3077. * specified or not valid.
  3078. *
  3079. *********************************************************************/
  3080. jb_err cgi_edit_actions_remove_url(struct client_state *csp,
  3081. struct http_response *rsp,
  3082. const struct map *parameters)
  3083. {
  3084. unsigned patternid;
  3085. struct editable_file * file;
  3086. struct file_line * cur_line;
  3087. struct file_line * prev_line;
  3088. unsigned line_number;
  3089. unsigned section_start_line_number = 0;
  3090. char target[1024];
  3091. jb_err err;
  3092. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  3093. {
  3094. return cgi_error_disabled(csp, rsp);
  3095. }
  3096. err = get_number_param(csp, parameters, "p", &patternid);
  3097. if (err)
  3098. {
  3099. return err;
  3100. }
  3101. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  3102. if (err)
  3103. {
  3104. /* No filename specified, can't read file, modified, or out of memory. */
  3105. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  3106. }
  3107. line_number = 1;
  3108. prev_line = NULL;
  3109. cur_line = file->lines;
  3110. while ((cur_line != NULL) && (line_number < patternid))
  3111. {
  3112. if (cur_line->type == FILE_LINE_ACTION)
  3113. {
  3114. section_start_line_number = line_number;
  3115. }
  3116. prev_line = cur_line;
  3117. cur_line = cur_line->next;
  3118. line_number++;
  3119. }
  3120. if ( (cur_line == NULL)
  3121. || (prev_line == NULL)
  3122. || (cur_line->type != FILE_LINE_URL))
  3123. {
  3124. /* Invalid "patternid" parameter */
  3125. edit_free_file(file);
  3126. return JB_ERR_CGI_PARAMS;
  3127. }
  3128. /* At this point, the line to remove is in cur_line, and the previous
  3129. * one is in prev_line
  3130. */
  3131. /* Unlink cur_line */
  3132. prev_line->next = cur_line->next;
  3133. cur_line->next = NULL;
  3134. /* Free cur_line */
  3135. edit_free_file_lines(cur_line);
  3136. err = edit_write_file(file);
  3137. if (err)
  3138. {
  3139. /* Error writing file */
  3140. if (err == JB_ERR_FILE)
  3141. {
  3142. /* Read-only file. */
  3143. err = cgi_error_file_read_only(csp, rsp, file->filename);
  3144. }
  3145. edit_free_file(file);
  3146. return err;
  3147. }
  3148. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u#l%u",
  3149. (unsigned long) time(NULL), file->identifier, section_start_line_number);
  3150. edit_free_file(file);
  3151. return cgi_redirect(rsp, target);
  3152. }
  3153. /*********************************************************************
  3154. *
  3155. * Function : cgi_edit_actions_section_remove
  3156. *
  3157. * Description : CGI function that actually removes a whole section from
  3158. * the actions file. The section must be empty first
  3159. * (else JB_ERR_CGI_PARAMS).
  3160. *
  3161. * Parameters :
  3162. * 1 : csp = Current client state (buffers, headers, etc...)
  3163. * 2 : rsp = http_response data structure for output
  3164. * 3 : parameters = map of cgi parameters
  3165. *
  3166. * CGI Parameters :
  3167. * f : (filename) Identifies the file to edit
  3168. * v : (version) File's last-modified time
  3169. * s : (section) Line number of section to edit
  3170. *
  3171. * Returns : JB_ERR_OK on success
  3172. * JB_ERR_MEMORY on out-of-memory
  3173. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  3174. * specified or not valid.
  3175. *
  3176. *********************************************************************/
  3177. jb_err cgi_edit_actions_section_remove(struct client_state *csp,
  3178. struct http_response *rsp,
  3179. const struct map *parameters)
  3180. {
  3181. unsigned sectionid;
  3182. struct editable_file * file;
  3183. struct file_line * cur_line;
  3184. struct file_line * prev_line;
  3185. unsigned line_number;
  3186. char target[1024];
  3187. jb_err err;
  3188. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  3189. {
  3190. return cgi_error_disabled(csp, rsp);
  3191. }
  3192. err = get_number_param(csp, parameters, "s", &sectionid);
  3193. if (err)
  3194. {
  3195. return err;
  3196. }
  3197. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  3198. if (err)
  3199. {
  3200. /* No filename specified, can't read file, modified, or out of memory. */
  3201. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  3202. }
  3203. line_number = 1;
  3204. cur_line = file->lines;
  3205. prev_line = NULL;
  3206. while ((cur_line != NULL) && (line_number < sectionid))
  3207. {
  3208. prev_line = cur_line;
  3209. cur_line = cur_line->next;
  3210. line_number++;
  3211. }
  3212. if ((cur_line == NULL)
  3213. || (cur_line->type != FILE_LINE_ACTION))
  3214. {
  3215. /* Invalid "sectionid" parameter */
  3216. edit_free_file(file);
  3217. return JB_ERR_CGI_PARAMS;
  3218. }
  3219. if ((cur_line->next != NULL)
  3220. && (cur_line->next->type == FILE_LINE_URL))
  3221. {
  3222. /* Section not empty. */
  3223. edit_free_file(file);
  3224. return JB_ERR_CGI_PARAMS;
  3225. }
  3226. /* At this point, the line to remove is in cur_line, and the previous
  3227. * one is in prev_line
  3228. */
  3229. /* Unlink cur_line */
  3230. if (prev_line == NULL)
  3231. {
  3232. /* Removing the first line from the file */
  3233. file->lines = cur_line->next;
  3234. }
  3235. else
  3236. {
  3237. prev_line->next = cur_line->next;
  3238. }
  3239. cur_line->next = NULL;
  3240. /* Free cur_line */
  3241. edit_free_file_lines(cur_line);
  3242. err = edit_write_file(file);
  3243. if (err)
  3244. {
  3245. /* Error writing file */
  3246. if (err == JB_ERR_FILE)
  3247. {
  3248. /* Read-only file. */
  3249. err = cgi_error_file_read_only(csp, rsp, file->filename);
  3250. }
  3251. edit_free_file(file);
  3252. return err;
  3253. }
  3254. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
  3255. (unsigned long) time(NULL), file->identifier);
  3256. edit_free_file(file);
  3257. return cgi_redirect(rsp, target);
  3258. }
  3259. /*********************************************************************
  3260. *
  3261. * Function : cgi_edit_actions_section_add
  3262. *
  3263. * Description : CGI function that adds a new empty section to
  3264. * an actions file.
  3265. *
  3266. * Parameters :
  3267. * 1 : csp = Current client state (buffers, headers, etc...)
  3268. * 2 : rsp = http_response data structure for output
  3269. * 3 : parameters = map of cgi parameters
  3270. *
  3271. * CGI Parameters :
  3272. * f : (filename) Identifies the file to edit
  3273. * v : (version) File's last-modified time
  3274. * s : (section) Line number of section to add after, 0 for
  3275. * start of file.
  3276. *
  3277. * Returns : JB_ERR_OK on success
  3278. * JB_ERR_MEMORY on out-of-memory
  3279. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  3280. * specified or not valid.
  3281. *
  3282. *********************************************************************/
  3283. jb_err cgi_edit_actions_section_add(struct client_state *csp,
  3284. struct http_response *rsp,
  3285. const struct map *parameters)
  3286. {
  3287. unsigned sectionid;
  3288. struct file_line * new_line;
  3289. char * new_text;
  3290. struct editable_file * file;
  3291. struct file_line * cur_line;
  3292. unsigned line_number;
  3293. char target[1024];
  3294. jb_err err;
  3295. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  3296. {
  3297. return cgi_error_disabled(csp, rsp);
  3298. }
  3299. err = get_number_param(csp, parameters, "s", &sectionid);
  3300. if (err)
  3301. {
  3302. return err;
  3303. }
  3304. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  3305. if (err)
  3306. {
  3307. /* No filename specified, can't read file, modified, or out of memory. */
  3308. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  3309. }
  3310. line_number = 1;
  3311. cur_line = file->lines;
  3312. if (sectionid <= 1U)
  3313. {
  3314. /* Add to start of file */
  3315. if (cur_line != NULL && cur_line->type != FILE_LINE_ACTION)
  3316. {
  3317. /* There's something in the file, find the line before the first
  3318. * action.
  3319. */
  3320. while ((cur_line->next != NULL)
  3321. && (cur_line->next->type != FILE_LINE_ACTION))
  3322. {
  3323. cur_line = cur_line->next;
  3324. line_number++;
  3325. }
  3326. }
  3327. else
  3328. {
  3329. /* File starts with action line, so insert at top */
  3330. cur_line = NULL;
  3331. }
  3332. }
  3333. else
  3334. {
  3335. /* Add after stated section. */
  3336. while ((cur_line != NULL) && (line_number < sectionid))
  3337. {
  3338. cur_line = cur_line->next;
  3339. line_number++;
  3340. }
  3341. if ((cur_line == NULL)
  3342. || (cur_line->type != FILE_LINE_ACTION))
  3343. {
  3344. /* Invalid "sectionid" parameter */
  3345. edit_free_file(file);
  3346. return JB_ERR_CGI_PARAMS;
  3347. }
  3348. /* Skip through the section to find the last line in it. */
  3349. while ((cur_line->next != NULL)
  3350. && (cur_line->next->type != FILE_LINE_ACTION))
  3351. {
  3352. cur_line = cur_line->next;
  3353. line_number++;
  3354. }
  3355. }
  3356. /* At this point, the last line in the previous section is in cur_line
  3357. * - add after this. (Or if we need to add as the first line, cur_line
  3358. * will be NULL).
  3359. */
  3360. new_text = strdup("{}");
  3361. if (NULL == new_text)
  3362. {
  3363. edit_free_file(file);
  3364. return JB_ERR_MEMORY;
  3365. }
  3366. /* Allocate the new line */
  3367. new_line = zalloc_or_die(sizeof(*new_line));
  3368. /* Fill in the data members of the new line */
  3369. new_line->raw = NULL;
  3370. new_line->prefix = NULL;
  3371. new_line->unprocessed = new_text;
  3372. new_line->type = FILE_LINE_ACTION;
  3373. if (cur_line != NULL)
  3374. {
  3375. /* Link new_line into the list, after cur_line */
  3376. new_line->next = cur_line->next;
  3377. cur_line->next = new_line;
  3378. }
  3379. else
  3380. {
  3381. /* Link new_line into the list, as first line */
  3382. new_line->next = file->lines;
  3383. file->lines = new_line;
  3384. }
  3385. /* Done making changes, now commit */
  3386. err = edit_write_file(file);
  3387. if (err)
  3388. {
  3389. /* Error writing file */
  3390. if (err == JB_ERR_FILE)
  3391. {
  3392. /* Read-only file. */
  3393. err = cgi_error_file_read_only(csp, rsp, file->filename);
  3394. }
  3395. edit_free_file(file);
  3396. return err;
  3397. }
  3398. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
  3399. (unsigned long) time(NULL), file->identifier);
  3400. edit_free_file(file);
  3401. return cgi_redirect(rsp, target);
  3402. }
  3403. /*********************************************************************
  3404. *
  3405. * Function : cgi_edit_actions_section_swap
  3406. *
  3407. * Description : CGI function that swaps the order of two sections
  3408. * in the actions file. Note that this CGI can actually
  3409. * swap any two arbitrary sections, but the GUI interface
  3410. * currently only allows consecutive sections to be
  3411. * specified.
  3412. *
  3413. * Parameters :
  3414. * 1 : csp = Current client state (buffers, headers, etc...)
  3415. * 2 : rsp = http_response data structure for output
  3416. * 3 : parameters = map of cgi parameters
  3417. *
  3418. * CGI Parameters :
  3419. * f : (filename) Identifies the file to edit
  3420. * v : (version) File's last-modified time
  3421. * s1 : (section1) Line number of first section to swap
  3422. * s2 : (section2) Line number of second section to swap
  3423. *
  3424. * Returns : JB_ERR_OK on success
  3425. * JB_ERR_MEMORY on out-of-memory
  3426. * JB_ERR_CGI_PARAMS if the CGI parameters are not
  3427. * specified or not valid.
  3428. *
  3429. *********************************************************************/
  3430. jb_err cgi_edit_actions_section_swap(struct client_state *csp,
  3431. struct http_response *rsp,
  3432. const struct map *parameters)
  3433. {
  3434. unsigned section1;
  3435. unsigned section2;
  3436. struct editable_file * file;
  3437. struct file_line * cur_line;
  3438. struct file_line * prev_line;
  3439. struct file_line * line_before_section1;
  3440. struct file_line * line_start_section1;
  3441. struct file_line * line_end_section1;
  3442. struct file_line * line_after_section1;
  3443. struct file_line * line_before_section2;
  3444. struct file_line * line_start_section2;
  3445. struct file_line * line_end_section2;
  3446. struct file_line * line_after_section2;
  3447. unsigned line_number;
  3448. char target[1024];
  3449. jb_err err;
  3450. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
  3451. {
  3452. return cgi_error_disabled(csp, rsp);
  3453. }
  3454. err = get_number_param(csp, parameters, "s1", &section1);
  3455. if (!err) err = get_number_param(csp, parameters, "s2", &section2);
  3456. if (err)
  3457. {
  3458. return err;
  3459. }
  3460. if (section1 > section2)
  3461. {
  3462. unsigned temp = section2;
  3463. section2 = section1;
  3464. section1 = temp;
  3465. }
  3466. err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
  3467. if (err)
  3468. {
  3469. /* No filename specified, can't read file, modified, or out of memory. */
  3470. return (err == JB_ERR_FILE ? JB_ERR_OK : err);
  3471. }
  3472. /* Start at the beginning... */
  3473. line_number = 1;
  3474. cur_line = file->lines;
  3475. prev_line = NULL;
  3476. /* ... find section1 ... */
  3477. while ((cur_line != NULL) && (line_number < section1))
  3478. {
  3479. prev_line = cur_line;
  3480. cur_line = cur_line->next;
  3481. line_number++;
  3482. }
  3483. if ((cur_line == NULL)
  3484. || (cur_line->type != FILE_LINE_ACTION))
  3485. {
  3486. /* Invalid "section1" parameter */
  3487. edit_free_file(file);
  3488. return JB_ERR_CGI_PARAMS;
  3489. }
  3490. /* If no-op, we've validated params and can skip the rest. */
  3491. if (section1 != section2)
  3492. {
  3493. /* ... find the end of section1 ... */
  3494. line_before_section1 = prev_line;
  3495. line_start_section1 = cur_line;
  3496. do
  3497. {
  3498. prev_line = cur_line;
  3499. cur_line = cur_line->next;
  3500. line_number++;
  3501. }
  3502. while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
  3503. line_end_section1 = prev_line;
  3504. line_after_section1 = cur_line;
  3505. /* ... find section2 ... */
  3506. while ((cur_line != NULL) && (line_number < section2))
  3507. {
  3508. prev_line = cur_line;
  3509. cur_line = cur_line->next;
  3510. line_number++;
  3511. }
  3512. if ((cur_line == NULL)
  3513. || (cur_line->type != FILE_LINE_ACTION))
  3514. {
  3515. /* Invalid "section2" parameter */
  3516. edit_free_file(file);
  3517. return JB_ERR_CGI_PARAMS;
  3518. }
  3519. /* ... find the end of section2 ... */
  3520. line_before_section2 = prev_line;
  3521. line_start_section2 = cur_line;
  3522. do
  3523. {
  3524. prev_line = cur_line;
  3525. cur_line = cur_line->next;
  3526. line_number++;
  3527. }
  3528. while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
  3529. line_end_section2 = prev_line;
  3530. line_after_section2 = cur_line;
  3531. /* Now have all the pointers we need. Do the swap. */
  3532. /* Change the pointer to section1 to point to section2 instead */
  3533. if (line_before_section1 == NULL)
  3534. {
  3535. file->lines = line_start_section2;
  3536. }
  3537. else
  3538. {
  3539. line_before_section1->next = line_start_section2;
  3540. }
  3541. if (line_before_section2 == line_end_section1)
  3542. {
  3543. /* Consecutive sections */
  3544. line_end_section2->next = line_start_section1;
  3545. }
  3546. else
  3547. {
  3548. line_end_section2->next = line_after_section1;
  3549. line_before_section2->next = line_start_section1;
  3550. }
  3551. /* Set the pointer from the end of section1 to the rest of the file */
  3552. line_end_section1->next = line_after_section2;
  3553. err = edit_write_file(file);
  3554. if (err)
  3555. {
  3556. /* Error writing file */
  3557. if (err == JB_ERR_FILE)
  3558. {
  3559. /* Read-only file. */
  3560. err = cgi_error_file_read_only(csp, rsp, file->filename);
  3561. }
  3562. edit_free_file(file);
  3563. return err;
  3564. }
  3565. } /* END if (section1 != section2) */
  3566. snprintf(target, sizeof(target), CGI_PREFIX "edit-actions-list?foo=%lu&f=%u",
  3567. (unsigned long) time(NULL), file->identifier);
  3568. edit_free_file(file);
  3569. return cgi_redirect(rsp, target);
  3570. }
  3571. /*********************************************************************
  3572. *
  3573. * Function : javascriptify
  3574. *
  3575. * Description : Converts a string into a form JavaScript will like.
  3576. *
  3577. * Netscape 4's JavaScript sucks - it doesn't use
  3578. * "id" parameters, so you have to set the "name"
  3579. * used to submit a form element to something JavaScript
  3580. * will like. (Or access the elements by index in an
  3581. * array. That array contains >60 elements and will
  3582. * be changed whenever we add a new action to the
  3583. * editor, so I'm NOT going to use indexes that have
  3584. * to be figured out by hand.)
  3585. *
  3586. * Currently the only thing we have to worry about
  3587. * is "-" ==> "_" conversion.
  3588. *
  3589. * This is a length-preserving operation so it is
  3590. * carried out in-place, no memory is allocated
  3591. * or freed.
  3592. *
  3593. * Parameters :
  3594. * 1 : identifier = String to make JavaScript-friendly.
  3595. *
  3596. * Returns : N/A
  3597. *
  3598. *********************************************************************/
  3599. static void javascriptify(char * identifier)
  3600. {
  3601. char * p = identifier;
  3602. while (NULL != (p = strchr(p, '-')))
  3603. {
  3604. *p++ = '_';
  3605. }
  3606. }
  3607. /*********************************************************************
  3608. *
  3609. * Function : actions_to_radio
  3610. *
  3611. * Description : Converts a actionsfile entry into settings for
  3612. * radio buttons and edit boxes on a HTML form.
  3613. *
  3614. * Parameters :
  3615. * 1 : exports = List of substitutions to add to.
  3616. * 2 : action = Action to read
  3617. *
  3618. * Returns : JB_ERR_OK on success
  3619. * JB_ERR_MEMORY on out-of-memory
  3620. *
  3621. *********************************************************************/
  3622. static jb_err actions_to_radio(struct map * exports,
  3623. const struct action_spec *action)
  3624. {
  3625. unsigned long mask;
  3626. unsigned long add;
  3627. int mapped_param;
  3628. int checked;
  3629. char current_mode;
  3630. assert(exports);
  3631. assert(action);
  3632. mask = action->mask;
  3633. add = action->add;
  3634. /* sanity - prevents "-feature +feature" */
  3635. mask |= add;
  3636. #define DEFINE_ACTION_BOOL(name, bit) \
  3637. if (!(mask & bit)) \
  3638. { \
  3639. current_mode = 'n'; \
  3640. } \
  3641. else if (add & bit) \
  3642. { \
  3643. current_mode = 'y'; \
  3644. } \
  3645. else \
  3646. { \
  3647. current_mode = 'x'; \
  3648. } \
  3649. if (map_radio(exports, name, "ynx", current_mode)) \
  3650. { \
  3651. return JB_ERR_MEMORY; \
  3652. }
  3653. #define DEFINE_ACTION_STRING(name, bit, index) \
  3654. DEFINE_ACTION_BOOL(name, bit); \
  3655. mapped_param = 0;
  3656. #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
  3657. if (add & bit) \
  3658. { \
  3659. checked = !strcmp(action->string[index], value); \
  3660. } \
  3661. else \
  3662. { \
  3663. checked = is_default; \
  3664. } \
  3665. mapped_param |= checked; \
  3666. if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
  3667. { \
  3668. return JB_ERR_MEMORY; \
  3669. }
  3670. #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
  3671. if (map(exports, name "-param-custom", 1, \
  3672. ((!mapped_param) ? "checked" : ""), 1)) \
  3673. { \
  3674. return JB_ERR_MEMORY; \
  3675. } \
  3676. if (map(exports, name "-param", 1, \
  3677. (((add & bit) && !mapped_param) ? \
  3678. action->string[index] : default_val), 1)) \
  3679. { \
  3680. return JB_ERR_MEMORY; \
  3681. }
  3682. #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
  3683. if (map(exports, name "-param", 1, \
  3684. ((add & bit) ? action->string[index] : default_val), 1)) \
  3685. { \
  3686. return JB_ERR_MEMORY; \
  3687. }
  3688. #define DEFINE_ACTION_MULTI(name, index) \
  3689. if (action->multi_add[index]->first) \
  3690. { \
  3691. current_mode = 'y'; \
  3692. } \
  3693. else if (action->multi_remove_all[index]) \
  3694. { \
  3695. current_mode = 'n'; \
  3696. } \
  3697. else if (action->multi_remove[index]->first) \
  3698. { \
  3699. current_mode = 'y'; \
  3700. } \
  3701. else \
  3702. { \
  3703. current_mode = 'x'; \
  3704. } \
  3705. if (map_radio(exports, name, "ynx", current_mode)) \
  3706. { \
  3707. return JB_ERR_MEMORY; \
  3708. }
  3709. #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
  3710. #include "actionlist.h"
  3711. #undef DEFINE_ACTION_MULTI
  3712. #undef DEFINE_ACTION_STRING
  3713. #undef DEFINE_ACTION_BOOL
  3714. #undef DEFINE_ACTION_ALIAS
  3715. #undef DEFINE_CGI_PARAM_CUSTOM
  3716. #undef DEFINE_CGI_PARAM_RADIO
  3717. #undef DEFINE_CGI_PARAM_NO_RADIO
  3718. return JB_ERR_OK;
  3719. }
  3720. /*********************************************************************
  3721. *
  3722. * Function : actions_from_radio
  3723. *
  3724. * Description : Converts a map of parameters passed to a CGI function
  3725. * into an actionsfile entry.
  3726. *
  3727. * Parameters :
  3728. * 1 : parameters = parameters to the CGI call
  3729. * 2 : action = Action to change. Must be valid before
  3730. * the call, actions not specified will be
  3731. * left unchanged.
  3732. *
  3733. * Returns : JB_ERR_OK on success
  3734. * JB_ERR_MEMORY on out-of-memory
  3735. *
  3736. *********************************************************************/
  3737. static jb_err actions_from_radio(const struct map * parameters,
  3738. struct action_spec *action)
  3739. {
  3740. const char * param;
  3741. char * param_dup;
  3742. char ch;
  3743. const char * js_name;
  3744. jb_err err = JB_ERR_OK;
  3745. assert(parameters);
  3746. assert(action);
  3747. /* Statics are generally a potential race condition,
  3748. * but in this case we're safe and don't need semaphores.
  3749. * Be careful if you modify this function.
  3750. * - Jon
  3751. * The js_name_arr's are never free()d, but this is no
  3752. * problem, since they will only be created once and
  3753. * used by all threads thereafter. -oes
  3754. */
  3755. #define JAVASCRIPTIFY(dest_var, string) \
  3756. { \
  3757. static int first_time = 1; \
  3758. static char *js_name_arr; \
  3759. if (first_time) \
  3760. { \
  3761. js_name_arr = strdup(string); \
  3762. javascriptify(js_name_arr); \
  3763. } \
  3764. dest_var = js_name_arr; \
  3765. first_time = 0; \
  3766. } \
  3767. #define DEFINE_ACTION_BOOL(name, bit) \
  3768. JAVASCRIPTIFY(js_name, name); \
  3769. ch = get_char_param(parameters, js_name); \
  3770. if (ch == 'Y') \
  3771. { \
  3772. action->add |= bit; \
  3773. action->mask |= bit; \
  3774. } \
  3775. else if (ch == 'N') \
  3776. { \
  3777. action->add &= ~bit; \
  3778. action->mask &= ~bit; \
  3779. } \
  3780. else if (ch == 'X') \
  3781. { \
  3782. action->add &= ~bit; \
  3783. action->mask |= bit; \
  3784. } \
  3785. #define DEFINE_ACTION_STRING(name, bit, index) \
  3786. JAVASCRIPTIFY(js_name, name); \
  3787. ch = get_char_param(parameters, js_name); \
  3788. if (ch == 'Y') \
  3789. { \
  3790. param = NULL; \
  3791. JAVASCRIPTIFY(js_name, name "-mode"); \
  3792. if (!err) err = get_string_param(parameters, js_name, &param); \
  3793. if ((param == NULL) || (0 == strcmp(param, "CUSTOM"))) \
  3794. { \
  3795. JAVASCRIPTIFY(js_name, name "-param"); \
  3796. if (!err) err = get_string_param(parameters, js_name, &param); \
  3797. } \
  3798. if (param != NULL) \
  3799. { \
  3800. if (NULL == (param_dup = strdup(param))) \
  3801. { \
  3802. return JB_ERR_MEMORY; \
  3803. } \
  3804. freez(action->string[index]); \
  3805. action->add |= bit; \
  3806. action->mask |= bit; \
  3807. action->string[index] = param_dup; \
  3808. } \
  3809. } \
  3810. else if (ch == 'N') \
  3811. { \
  3812. if (action->add & bit) \
  3813. { \
  3814. freez(action->string[index]); \
  3815. } \
  3816. action->add &= ~bit; \
  3817. action->mask &= ~bit; \
  3818. } \
  3819. else if (ch == 'X') \
  3820. { \
  3821. if (action->add & bit) \
  3822. { \
  3823. freez(action->string[index]); \
  3824. } \
  3825. action->add &= ~bit; \
  3826. action->mask |= bit; \
  3827. } \
  3828. #define DEFINE_ACTION_MULTI(name, index) \
  3829. JAVASCRIPTIFY(js_name, name); \
  3830. ch = get_char_param(parameters, js_name); \
  3831. if (ch == 'Y') \
  3832. { \
  3833. /* FIXME */ \
  3834. } \
  3835. else if (ch == 'N') \
  3836. { \
  3837. list_remove_all(action->multi_add[index]); \
  3838. list_remove_all(action->multi_remove[index]); \
  3839. action->multi_remove_all[index] = 1; \
  3840. } \
  3841. else if (ch == 'X') \
  3842. { \
  3843. list_remove_all(action->multi_add[index]); \
  3844. list_remove_all(action->multi_remove[index]); \
  3845. action->multi_remove_all[index] = 0; \
  3846. } \
  3847. #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
  3848. #include "actionlist.h"
  3849. #undef DEFINE_ACTION_MULTI
  3850. #undef DEFINE_ACTION_STRING
  3851. #undef DEFINE_ACTION_BOOL
  3852. #undef DEFINE_ACTION_ALIAS
  3853. #undef JAVASCRIPTIFY
  3854. return err;
  3855. }
  3856. #endif /* def FEATURE_CGI_EDIT_ACTIONS */
  3857. #ifdef FEATURE_TOGGLE
  3858. /*********************************************************************
  3859. *
  3860. * Function : cgi_toggle
  3861. *
  3862. * Description : CGI function that adds a new empty section to
  3863. * an actions file.
  3864. *
  3865. * Parameters :
  3866. * 1 : csp = Current client state (buffers, headers, etc...)
  3867. * 2 : rsp = http_response data structure for output
  3868. * 3 : parameters = map of cgi parameters
  3869. *
  3870. * CGI Parameters :
  3871. * set : If present, how to change toggle setting:
  3872. * "enable", "disable", "toggle", or none (default).
  3873. * mini : If present, use mini reply template.
  3874. *
  3875. * Returns : JB_ERR_OK on success
  3876. * JB_ERR_MEMORY on out-of-memory
  3877. *
  3878. *********************************************************************/
  3879. jb_err cgi_toggle(struct client_state *csp,
  3880. struct http_response *rsp,
  3881. const struct map *parameters)
  3882. {
  3883. struct map *exports;
  3884. char mode;
  3885. const char *template_name;
  3886. assert(csp);
  3887. assert(rsp);
  3888. assert(parameters);
  3889. if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
  3890. {
  3891. return cgi_error_disabled(csp, rsp);
  3892. }
  3893. mode = get_char_param(parameters, "set");
  3894. if (mode == 'E')
  3895. {
  3896. /* Enable */
  3897. global_toggle_state = 1;
  3898. }
  3899. else if (mode == 'D')
  3900. {
  3901. /* Disable */
  3902. global_toggle_state = 0;
  3903. }
  3904. else if (mode == 'T')
  3905. {
  3906. /* Toggle */
  3907. global_toggle_state = !global_toggle_state;
  3908. }
  3909. log_error(LOG_LEVEL_INFO, "Now toggled %s.", global_toggle_state ? "ON" : "OFF");
  3910. if (NULL == (exports = default_exports(csp, "toggle")))
  3911. {
  3912. return JB_ERR_MEMORY;
  3913. }
  3914. template_name = (get_char_param(parameters, "mini")
  3915. ? "toggle-mini"
  3916. : "toggle");
  3917. return template_fill_for_cgi(csp, template_name, exports, rsp);
  3918. }
  3919. #endif /* def FEATURE_TOGGLE */
  3920. /*
  3921. Local Variables:
  3922. tab-width: 3
  3923. end:
  3924. */