log.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. **
  43. ** log.c
  44. **
  45. ** Routines for writing access and error/debug logs
  46. **
  47. **
  48. ** History:
  49. ** As of DS 4.0, we support log rotation for the ACCESS/ERROR/AUDIT log.
  50. */
  51. #include "log.h"
  52. #include "fe.h"
  53. #ifndef _WIN32
  54. #include <pwd.h> /* getpwnam */
  55. #endif
  56. #if defined( XP_WIN32 )
  57. #include <fcntl.h>
  58. #include "ntslapdmessages.h"
  59. #include "proto-ntutil.h"
  60. extern HANDLE hSlapdEventSource;
  61. extern LPTSTR pszServerName;
  62. #define _PSEP '\\'
  63. #else
  64. #define _PSEP '/'
  65. #endif
  66. /**************************************************************************
  67. * GLOBALS, defines, and ...
  68. *************************************************************************/
  69. /* main struct which contains all the information about logging */
  70. PRUintn logbuf_tsdindex;
  71. struct logbufinfo *logbuf_accum;
  72. static struct logging_opts loginfo;
  73. static int detached=0;
  74. /* used to lock the timestamp info used by vslapd_log_access */
  75. static PRLock *ts_time_lock = NULL;
  76. /*
  77. * Note: the order of the values in the slapi_log_map array must exactly
  78. * match that of the SLAPI_LOG_XXX #defines found in slapi-plugin.h (this is
  79. * so we can use the SLAPI_LOG_XXX values to index directly into the array).
  80. */
  81. static int slapi_log_map[] = {
  82. LDAP_DEBUG_ANY, /* SLAPI_LOG_FATAL */
  83. LDAP_DEBUG_TRACE, /* SLAPI_LOG_TRACE */
  84. LDAP_DEBUG_PACKETS, /* SLAPI_LOG_PACKETS */
  85. LDAP_DEBUG_ARGS, /* SLAPI_LOG_ARGS */
  86. LDAP_DEBUG_CONNS, /* SLAPI_LOG_CONNS */
  87. LDAP_DEBUG_BER, /* SLAPI_LOG_BER */
  88. LDAP_DEBUG_FILTER, /* SLAPI_LOG_FILTER */
  89. LDAP_DEBUG_CONFIG, /* SLAPI_LOG_CONFIG */
  90. LDAP_DEBUG_ACL, /* SLAPI_LOG_ACL */
  91. LDAP_DEBUG_SHELL, /* SLAPI_LOG_SHELL */
  92. LDAP_DEBUG_PARSE, /* SLAPI_LOG_PARSE */
  93. LDAP_DEBUG_HOUSE, /* SLAPI_LOG_HOUSE */
  94. LDAP_DEBUG_REPL, /* SLAPI_LOG_REPL */
  95. LDAP_DEBUG_CACHE, /* SLAPI_LOG_CACHE */
  96. LDAP_DEBUG_PLUGIN, /* SLAPI_LOG_PLUGIN */
  97. LDAP_DEBUG_TIMING, /* SLAPI_LOG_TIMING */
  98. LDAP_DEBUG_ACLSUMMARY, /* SLAPI_LOG_ACLSUMMARY */
  99. };
  100. #define SLAPI_LOG_MIN SLAPI_LOG_FATAL /* from slapi-plugin.h */
  101. #define SLAPI_LOG_MAX SLAPI_LOG_ACLSUMMARY /* from slapi-plugin.h */
  102. #define TBUFSIZE 50 /* size for time buffers */
  103. #define SLAPI_LOG_BUFSIZ 2048 /* size for data buffers */
  104. /**************************************************************************
  105. * PROTOTYPES
  106. *************************************************************************/
  107. static int log__open_accesslogfile(int logfile_type, int locked);
  108. static int log__open_errorlogfile(int logfile_type, int locked);
  109. static int log__open_auditlogfile(int logfile_type, int locked);
  110. static int log__needrotation(LOGFD fp, int logtype);
  111. static int log__delete_access_logfile();
  112. static int log__delete_error_logfile(int locked);
  113. static int log__delete_audit_logfile();
  114. static int log__access_rotationinfof(char *pathname);
  115. static int log__error_rotationinfof(char *pathname);
  116. static int log__audit_rotationinfof(char *pathname);
  117. static int log__extract_logheader (FILE *fp, long *f_ctime, PRInt64 *f_size);
  118. static int log__check_prevlogs (FILE *fp, char *filename);
  119. static PRInt64 log__getfilesize(LOGFD fp);
  120. static int log__enough_freespace(char *path);
  121. static int vslapd_log_error(LOGFD fp, char *subsystem, char *fmt, va_list ap, int locked );
  122. static int vslapd_log_access(char *fmt, va_list ap );
  123. static void log_convert_time (time_t ctime, char *tbuf, int type);
  124. static time_t log_reverse_convert_time (char *tbuf);
  125. static LogBufferInfo *log_create_buffer(size_t sz);
  126. static void log_append_buffer2(time_t tnl, LogBufferInfo *lbi, char *msg1, size_t size1, char *msg2, size_t size2);
  127. static void log_flush_buffer(LogBufferInfo *lbi, int type, int sync_now);
  128. static void log_write_title(LOGFD fp);
  129. static void log__error_emergency(const char *errstr, int reopen, int locked);
  130. static int
  131. slapd_log_error_proc_internal(
  132. char *subsystem, /* omitted if NULL */
  133. char *fmt,
  134. va_list ap_err,
  135. va_list ap_file);
  136. /*
  137. * these macros are used for opening a log file, closing a log file, and
  138. * writing out to a log file. we have to do this because currently NSPR
  139. * is extremely under-performant on NT, while fopen/fwrite fail on several
  140. * unix platforms if there are more than 128 files open.
  141. *
  142. * LOG_OPEN_APPEND(fd, filename, mode) returns true if successful. 'fd' should
  143. * be of type LOGFD (check log.h). the file is open for appending to.
  144. * LOG_OPEN_WRITE(fd, filename, mode) is the same but truncates the file and
  145. * starts writing at the beginning of the file.
  146. * LOG_WRITE(fd, buffer, size, headersize) writes into a LOGFD
  147. * LOG_WRITE_NOW(fd, buffer, size, headersize, err) writes into a LOGFD and
  148. * flushes the buffer if necessary
  149. * LOG_CLOSE(fd) closes the logfile
  150. */
  151. #ifdef XP_WIN32
  152. #define LOG_OPEN_APPEND(fd, filename, mode) \
  153. (((fd) = fopen((filename), "a")) != NULL)
  154. #define LOG_OPEN_WRITE(fd, filename, mode) \
  155. (((fd) = fopen((filename), "w")) != NULL)
  156. #define LOG_WRITE(fd, buffer, size, headersize) \
  157. if ( fwrite((buffer), (size), 1, (fd)) != 1 ) \
  158. {\
  159. ReportSlapdEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVER_FAILED_TO_WRITE_LOG, 1, (buffer)); \
  160. }
  161. #define LOG_WRITE_NOW(fd, buffer, size, headersize, err) do {\
  162. (err) = 0; \
  163. if ( fwrite((buffer), (size), 1, (fd)) != 1 ) \
  164. { \
  165. ReportSlapdEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVER_FAILED_TO_WRITE_LOG, 1, (buffer)); \
  166. (err) = 1; \
  167. }; \
  168. fflush((fd)); \
  169. } while (0)
  170. #define LOG_CLOSE(fd) \
  171. fclose((fd))
  172. #else /* xp_win32 */
  173. #define LOG_OPEN_APPEND(fd, filename, mode) \
  174. (((fd) = PR_Open((filename), PR_WRONLY | PR_APPEND | PR_CREATE_FILE , \
  175. mode)) != NULL)
  176. #define LOG_OPEN_WRITE(fd, filename, mode) \
  177. (((fd) = PR_Open((filename), PR_WRONLY | PR_TRUNCATE | \
  178. PR_CREATE_FILE, mode)) != NULL)
  179. #define LOG_WRITE(fd, buffer, size, headersize) \
  180. if ( slapi_write_buffer((fd), (buffer), (size)) != (size) ) \
  181. { \
  182. PRErrorCode prerr = PR_GetError(); \
  183. syslog(LOG_ERR, "Failed to write log, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s): %s\n", prerr, slapd_pr_strerror(prerr), (buffer)+(headersize) ); \
  184. }
  185. #define LOG_WRITE_NOW(fd, buffer, size, headersize, err) do {\
  186. (err) = 0; \
  187. if ( slapi_write_buffer((fd), (buffer), (size)) != (size) ) \
  188. { \
  189. PRErrorCode prerr = PR_GetError(); \
  190. syslog(LOG_ERR, "Failed to write log, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s): %s\n", prerr, slapd_pr_strerror(prerr), (buffer)+(headersize) ); \
  191. (err) = prerr; \
  192. } \
  193. /* Should be a flush in here ?? Yes because PR_SYNC doesn't work ! */ \
  194. PR_Sync(fd); \
  195. } while (0)
  196. #define LOG_CLOSE(fd) \
  197. PR_Close((fd))
  198. #endif
  199. /******************************************************************************
  200. * Set the access level
  201. ******************************************************************************/
  202. void g_set_accesslog_level(int val)
  203. {
  204. LOG_ACCESS_LOCK_WRITE( );
  205. loginfo.log_access_level = val;
  206. LOG_ACCESS_UNLOCK_WRITE();
  207. }
  208. /******************************************************************************
  209. * Set whether the process is alive or dead
  210. * If it is detached, then we write the error in 'stderr'
  211. ******************************************************************************/
  212. void g_set_detached(int val)
  213. {
  214. detached = val;
  215. }
  216. /******************************************************************************
  217. * Tell me whether logging begins or not
  218. ******************************************************************************/
  219. void g_log_init(int log_enabled)
  220. {
  221. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  222. #if defined( XP_WIN32 )
  223. /* char * instancedir = NULL; obsolete. */
  224. /* To port to Windows, need to support FHS. */
  225. #endif
  226. ts_time_lock = PR_NewLock();
  227. if (! ts_time_lock)
  228. exit(-1);
  229. #if defined( XP_WIN32 )
  230. pszServerName = slapi_ch_malloc( MAX_SERVICE_NAME );
  231. /* instancedir = config_get_instancedir(); eliminated */
  232. unixtodospath(instancedir);
  233. if( !SlapdGetServerNameFromCmdline(pszServerName, instancedir, 1) )
  234. {
  235. MessageBox(GetDesktopWindow(), "Failed to get the Directory"
  236. " Server name from the command-line argument.",
  237. " ", MB_ICONEXCLAMATION | MB_OK);
  238. exit( 1 );
  239. }
  240. slapi_ch_free((void **)&instancedir);
  241. /* Register with the NT EventLog */
  242. hSlapdEventSource = RegisterEventSource(NULL, pszServerName );
  243. if( !hSlapdEventSource )
  244. {
  245. char szMessage[256];
  246. PR_snprintf( szMessage, sizeof(szMessage), "Directory Server %s is terminating. Failed "
  247. "to set the EventLog source.", pszServerName);
  248. MessageBox(GetDesktopWindow(), szMessage, " ",
  249. MB_ICONEXCLAMATION | MB_OK);
  250. exit( 1 );
  251. }
  252. #endif
  253. /* ACCESS LOG */
  254. loginfo.log_access_state = 0;
  255. loginfo.log_access_mode = SLAPD_DEFAULT_FILE_MODE;
  256. loginfo.log_access_maxnumlogs = 1;
  257. loginfo.log_access_maxlogsize = -1;
  258. loginfo.log_access_rotationsync_enabled = 0;
  259. loginfo.log_access_rotationsynchour = -1;
  260. loginfo.log_access_rotationsyncmin = -1;
  261. loginfo.log_access_rotationsyncclock = -1;
  262. loginfo.log_access_rotationtime = 1; /* default: 1 */
  263. loginfo.log_access_rotationunit = LOG_UNIT_DAYS; /* default: day */
  264. loginfo.log_access_rotationtime_secs = 86400; /* default: 1 day */
  265. loginfo.log_access_maxdiskspace = -1;
  266. loginfo.log_access_minfreespace = -1;
  267. loginfo.log_access_exptime = -1; /* default: -1 */
  268. loginfo.log_access_exptimeunit = LOG_UNIT_MONTHS; /* default: month */
  269. loginfo.log_access_exptime_secs = -1; /* default: -1 */
  270. loginfo.log_access_level = LDAP_DEBUG_STATS;
  271. loginfo.log_access_ctime = 0L;
  272. loginfo.log_access_fdes = NULL;
  273. loginfo.log_access_file = NULL;
  274. loginfo.log_numof_access_logs = 1;
  275. loginfo.log_access_logchain = NULL;
  276. loginfo.log_access_buffer = log_create_buffer(LOG_BUFFER_MAXSIZE);
  277. if (loginfo.log_access_buffer == NULL)
  278. exit(-1);
  279. if ((loginfo.log_access_buffer->lock = PR_NewLock())== NULL )
  280. exit (-1);
  281. slapdFrontendConfig->accessloglevel = LDAP_DEBUG_STATS;
  282. /* ERROR LOG */
  283. loginfo.log_error_state = 0;
  284. loginfo.log_error_mode = SLAPD_DEFAULT_FILE_MODE;
  285. loginfo.log_error_maxnumlogs = 1;
  286. loginfo.log_error_maxlogsize = -1;
  287. loginfo.log_error_rotationsync_enabled = 0;
  288. loginfo.log_error_rotationsynchour = -1;
  289. loginfo.log_error_rotationsyncmin = -1;
  290. loginfo.log_error_rotationsyncclock = -1;
  291. loginfo.log_error_rotationtime = 1; /* default: 1 */
  292. loginfo.log_error_rotationunit = LOG_UNIT_WEEKS; /* default: week */
  293. loginfo.log_error_rotationtime_secs = 604800; /* default: 1 week */
  294. loginfo.log_error_maxdiskspace = -1;
  295. loginfo.log_error_minfreespace = -1;
  296. loginfo.log_error_exptime = -1; /* default: -1 */
  297. loginfo.log_error_exptimeunit = LOG_UNIT_MONTHS; /* default: month */
  298. loginfo.log_error_exptime_secs = -1; /* default: -1 */
  299. loginfo.log_error_ctime = 0L;
  300. loginfo.log_error_file = NULL;
  301. loginfo.log_error_fdes = NULL;
  302. loginfo.log_numof_error_logs = 1;
  303. loginfo.log_error_logchain = NULL;
  304. if ((loginfo.log_error_rwlock =rwl_new())== NULL ) {
  305. exit (-1);
  306. }
  307. /* AUDIT LOG */
  308. loginfo.log_audit_state = 0;
  309. loginfo.log_audit_mode = SLAPD_DEFAULT_FILE_MODE;
  310. loginfo.log_audit_maxnumlogs = 1;
  311. loginfo.log_audit_maxlogsize = -1;
  312. loginfo.log_audit_rotationsync_enabled = 0;
  313. loginfo.log_audit_rotationsynchour = -1;
  314. loginfo.log_audit_rotationsyncmin = -1;
  315. loginfo.log_audit_rotationsyncclock = -1;
  316. loginfo.log_audit_rotationtime = 1; /* default: 1 */
  317. loginfo.log_audit_rotationunit = LOG_UNIT_WEEKS; /* default: week */
  318. loginfo.log_audit_rotationtime_secs = 604800; /* default: 1 week */
  319. loginfo.log_audit_maxdiskspace = -1;
  320. loginfo.log_audit_minfreespace = -1;
  321. loginfo.log_audit_exptime = -1; /* default: -1 */
  322. loginfo.log_audit_exptimeunit = LOG_UNIT_WEEKS; /* default: week */
  323. loginfo.log_audit_exptime_secs = -1; /* default: -1 */
  324. loginfo.log_audit_ctime = 0L;
  325. loginfo.log_audit_file = NULL;
  326. loginfo.log_numof_audit_logs = 1;
  327. loginfo.log_audit_fdes = NULL;
  328. loginfo.log_audit_logchain = NULL;
  329. if ((loginfo.log_audit_rwlock =rwl_new())== NULL ) {
  330. exit (-1);
  331. }
  332. }
  333. /******************************************************************************
  334. * Tell me if log is enabled or disabled
  335. ******************************************************************************/
  336. int
  337. log_set_logging(const char *attrname, char *value, int logtype, char *errorbuf, int apply)
  338. {
  339. int v;
  340. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  341. if ( NULL == value ) {
  342. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  343. "%s: NULL value; valid values "
  344. "are \"on\" or \"off\"", attrname );
  345. return LDAP_OPERATIONS_ERROR;
  346. }
  347. if (strcasecmp(value, "on") == 0) {
  348. v = LOGGING_ENABLED;
  349. }
  350. else if (strcasecmp(value, "off") == 0 ) {
  351. v = 0;
  352. }
  353. else {
  354. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  355. "%s: invalid value \"%s\", valid values "
  356. "are \"on\" or \"off\"", attrname, value );
  357. return LDAP_OPERATIONS_ERROR;
  358. }
  359. if ( !apply ){
  360. return LDAP_SUCCESS;
  361. }
  362. switch (logtype) {
  363. case SLAPD_ACCESS_LOG:
  364. LOG_ACCESS_LOCK_WRITE( );
  365. fe_cfg->accesslog_logging_enabled = v;
  366. if(v) {
  367. loginfo.log_access_state |= LOGGING_ENABLED;
  368. }
  369. else {
  370. loginfo.log_access_state &= ~LOGGING_ENABLED;
  371. }
  372. LOG_ACCESS_UNLOCK_WRITE();
  373. break;
  374. case SLAPD_ERROR_LOG:
  375. LOG_ERROR_LOCK_WRITE( );
  376. fe_cfg->errorlog_logging_enabled = v;
  377. if (v) {
  378. loginfo.log_error_state |= LOGGING_ENABLED;
  379. }
  380. else {
  381. loginfo.log_error_state &= ~LOGGING_ENABLED;
  382. }
  383. LOG_ERROR_UNLOCK_WRITE();
  384. break;
  385. case SLAPD_AUDIT_LOG:
  386. LOG_AUDIT_LOCK_WRITE( );
  387. fe_cfg->auditlog_logging_enabled = v;
  388. if (v) {
  389. loginfo.log_audit_state |= LOGGING_ENABLED;
  390. }
  391. else {
  392. loginfo.log_audit_state &= ~LOGGING_ENABLED;
  393. }
  394. LOG_AUDIT_UNLOCK_WRITE();
  395. break;
  396. }
  397. return LDAP_SUCCESS;
  398. }
  399. /******************************************************************************
  400. * Tell me the access log file name inc path
  401. ******************************************************************************/
  402. char *
  403. g_get_access_log () {
  404. char *logfile = NULL;
  405. LOG_ACCESS_LOCK_READ();
  406. if ( loginfo.log_access_file)
  407. logfile = slapi_ch_strdup (loginfo.log_access_file);
  408. LOG_ACCESS_UNLOCK_READ();
  409. return logfile;
  410. }
  411. /******************************************************************************
  412. * Point to a new access logdir
  413. *
  414. * Returns:
  415. * LDAP_SUCCESS -- success
  416. * LDAP_UNWILLING_TO_PERFORM -- when trying to open a invalid log file
  417. * LDAP_LOCAL_ERRO -- some error
  418. ******************************************************************************/
  419. int
  420. log_update_accesslogdir(char *pathname, int apply)
  421. {
  422. int rv = LDAP_SUCCESS;
  423. LOGFD fp;
  424. /* try to open the file, we may have a incorrect path */
  425. if (! LOG_OPEN_APPEND(fp, pathname, loginfo.log_access_mode)) {
  426. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't open file %s. "
  427. "errno %d (%s)\n",
  428. pathname, errno, slapd_system_strerror(errno));
  429. /* stay with the current log file */
  430. return LDAP_UNWILLING_TO_PERFORM;
  431. }
  432. LOG_CLOSE(fp);
  433. /* skip the rest if we aren't doing this for real */
  434. if ( !apply ) {
  435. return LDAP_SUCCESS;
  436. }
  437. /*
  438. ** The user has changed the access log directory. That means we
  439. ** need to start fresh.
  440. */
  441. LOG_ACCESS_LOCK_WRITE ();
  442. if (loginfo.log_access_fdes) {
  443. LogFileInfo *logp, *d_logp;
  444. LDAPDebug(LDAP_DEBUG_TRACE,
  445. "LOGINFO:Closing the access log file. "
  446. "Moving to a new access log file (%s)\n", pathname,0,0);
  447. LOG_CLOSE(loginfo.log_access_fdes);
  448. loginfo.log_access_fdes = 0;
  449. loginfo.log_access_ctime = 0;
  450. logp = loginfo.log_access_logchain;
  451. while (logp) {
  452. d_logp = logp;
  453. logp = logp->l_next;
  454. slapi_ch_free((void**)&d_logp);
  455. }
  456. loginfo.log_access_logchain = NULL;
  457. slapi_ch_free((void**)&loginfo.log_access_file);
  458. loginfo.log_access_file = NULL;
  459. loginfo.log_numof_access_logs = 1;
  460. }
  461. /* Now open the new access log file */
  462. if ( access_log_openf (pathname, 1 /* locked */)) {
  463. rv = LDAP_LOCAL_ERROR; /* error Unable to use the new dir */
  464. }
  465. LOG_ACCESS_UNLOCK_WRITE();
  466. return rv;
  467. }
  468. /******************************************************************************
  469. * Tell me the error log file name inc path
  470. ******************************************************************************/
  471. char *
  472. g_get_error_log() {
  473. char *logfile = NULL;
  474. LOG_ERROR_LOCK_READ();
  475. if ( loginfo.log_error_file)
  476. logfile = slapi_ch_strdup (loginfo.log_error_file);
  477. LOG_ERROR_UNLOCK_READ();
  478. return logfile;
  479. }
  480. /******************************************************************************
  481. * Point to a new error logdir
  482. *
  483. * Returns:
  484. * LDAP_SUCCESS -- success
  485. * LDAP_UNWILLING_TO_PERFORM -- when trying to open a invalid log file
  486. * LDAP_LOCAL_ERRO -- some error
  487. ******************************************************************************/
  488. int
  489. log_update_errorlogdir(char *pathname, int apply)
  490. {
  491. int rv = LDAP_SUCCESS;
  492. LOGFD fp;
  493. /* try to open the file, we may have a incorrect path */
  494. if (! LOG_OPEN_APPEND(fp, pathname, loginfo.log_error_mode)) {
  495. char buffer[SLAPI_LOG_BUFSIZ];
  496. PRErrorCode prerr = PR_GetError();
  497. /* stay with the current log file */
  498. PR_snprintf(buffer, sizeof(buffer),
  499. "Failed to open file %s. error %d (%s). Exiting...",
  500. pathname, prerr, slapd_pr_strerror(prerr));
  501. log__error_emergency(buffer, 0, 0);
  502. return LDAP_UNWILLING_TO_PERFORM;
  503. }
  504. /* skip the rest if we aren't doing this for real */
  505. if ( !apply ) {
  506. return LDAP_SUCCESS;
  507. }
  508. LOG_CLOSE(fp);
  509. /*
  510. ** The user has changed the error log directory. That means we
  511. ** need to start fresh.
  512. */
  513. LOG_ERROR_LOCK_WRITE ();
  514. if (loginfo.log_error_fdes) {
  515. LogFileInfo *logp, *d_logp;
  516. LOG_CLOSE(loginfo.log_error_fdes);
  517. loginfo.log_error_fdes = 0;
  518. loginfo.log_error_ctime = 0;
  519. logp = loginfo.log_error_logchain;
  520. while (logp) {
  521. d_logp = logp;
  522. logp = logp->l_next;
  523. slapi_ch_free((void**)&d_logp);
  524. }
  525. loginfo.log_error_logchain = NULL;
  526. slapi_ch_free((void**)&loginfo.log_error_file);
  527. loginfo.log_error_file = NULL;
  528. loginfo.log_numof_error_logs = 1;
  529. }
  530. /* Now open the new errorlog */
  531. if ( error_log_openf (pathname, 1 /* obtained lock */)) {
  532. rv = LDAP_LOCAL_ERROR; /* error: Unable to use the new dir */
  533. }
  534. LOG_ERROR_UNLOCK_WRITE();
  535. return rv;
  536. }
  537. /******************************************************************************
  538. * Tell me the audit log file name inc path
  539. ******************************************************************************/
  540. char *
  541. g_get_audit_log() {
  542. char *logfile = NULL;
  543. LOG_AUDIT_LOCK_READ();
  544. if ( loginfo.log_audit_file)
  545. logfile = slapi_ch_strdup (loginfo.log_audit_file);
  546. LOG_AUDIT_UNLOCK_READ();
  547. return logfile;
  548. }
  549. /******************************************************************************
  550. * Point to a new audit logdir
  551. *
  552. * Returns:
  553. * LDAP_SUCCESS -- success
  554. * LDAP_UNWILLING_TO_PERFORM -- when trying to open a invalid log file
  555. * LDAP_LOCAL_ERRO -- some error
  556. ******************************************************************************/
  557. int
  558. log_update_auditlogdir(char *pathname, int apply)
  559. {
  560. int rv = LDAP_SUCCESS;
  561. LOGFD fp;
  562. /* try to open the file, we may have a incorrect path */
  563. if (! LOG_OPEN_APPEND(fp, pathname, loginfo.log_audit_mode)) {
  564. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't open file %s. "
  565. "errno %d (%s)\n",
  566. pathname, errno, slapd_system_strerror(errno));
  567. /* stay with the current log file */
  568. return LDAP_UNWILLING_TO_PERFORM;
  569. }
  570. /* skip the rest if we aren't doing this for real */
  571. if ( !apply ) {
  572. return LDAP_SUCCESS;
  573. }
  574. LOG_CLOSE(fp);
  575. /*
  576. ** The user has changed the audit log directory. That means we
  577. ** need to start fresh.
  578. */
  579. LOG_AUDIT_LOCK_WRITE ();
  580. if (loginfo.log_audit_fdes) {
  581. LogFileInfo *logp, *d_logp;
  582. LDAPDebug(LDAP_DEBUG_TRACE,
  583. "LOGINFO:Closing the audit log file. "
  584. "Moving to a new audit file (%s)\n", pathname,0,0);
  585. LOG_CLOSE(loginfo.log_audit_fdes);
  586. loginfo.log_audit_fdes = 0;
  587. loginfo.log_audit_ctime = 0;
  588. logp = loginfo.log_audit_logchain;
  589. while (logp) {
  590. d_logp = logp;
  591. logp = logp->l_next;
  592. slapi_ch_free((void**)&d_logp);
  593. }
  594. loginfo.log_audit_logchain = NULL;
  595. slapi_ch_free((void**)&loginfo.log_audit_file);
  596. loginfo.log_audit_file = NULL;
  597. loginfo.log_numof_audit_logs = 1;
  598. }
  599. /* Now open the new errorlog */
  600. if ( audit_log_openf (pathname, 1 /* locked */)) {
  601. rv = LDAP_LOCAL_ERROR; /* error: Unable to use the new dir */
  602. }
  603. LOG_AUDIT_UNLOCK_WRITE();
  604. return rv;
  605. }
  606. int
  607. log_set_mode (const char *attrname, char *value, int logtype, char *errorbuf, int apply)
  608. {
  609. int v = 0;
  610. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  611. if ( NULL == value ) {
  612. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  613. "%s: null value; valid values "
  614. "are are of the format \"yz-yz-yz-\" where y could be 'r' or '-',"
  615. " and z could be 'w' or '-'", attrname );
  616. return LDAP_OPERATIONS_ERROR;
  617. }
  618. if ( !apply ){
  619. return LDAP_SUCCESS;
  620. }
  621. v = strtol (value, NULL, 8);
  622. switch (logtype) {
  623. case SLAPD_ACCESS_LOG:
  624. LOG_ACCESS_LOCK_WRITE( );
  625. slapi_ch_free ( (void **) &fe_cfg->accesslog_mode );
  626. fe_cfg->accesslog_mode = slapi_ch_strdup (value);
  627. loginfo.log_access_mode = v;
  628. LOG_ACCESS_UNLOCK_WRITE();
  629. break;
  630. case SLAPD_ERROR_LOG:
  631. LOG_ERROR_LOCK_WRITE( );
  632. slapi_ch_free ( (void **) &fe_cfg->errorlog_mode );
  633. fe_cfg->errorlog_mode = slapi_ch_strdup (value);
  634. loginfo.log_error_mode = v;
  635. LOG_ERROR_UNLOCK_WRITE();
  636. break;
  637. case SLAPD_AUDIT_LOG:
  638. LOG_AUDIT_LOCK_WRITE( );
  639. slapi_ch_free ( (void **) &fe_cfg->auditlog_mode );
  640. fe_cfg->auditlog_mode = slapi_ch_strdup (value);
  641. loginfo.log_audit_mode = v;
  642. LOG_AUDIT_UNLOCK_WRITE();
  643. break;
  644. }
  645. return LDAP_SUCCESS;
  646. }
  647. /******************************************************************************
  648. * MAX NUMBER OF LOGS
  649. ******************************************************************************/
  650. int
  651. log_set_numlogsperdir(const char *attrname, char *numlogs_str, int logtype, char *returntext, int apply)
  652. {
  653. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  654. int rv = LDAP_SUCCESS;
  655. int numlogs;
  656. if ( logtype != SLAPD_ACCESS_LOG &&
  657. logtype != SLAPD_ERROR_LOG &&
  658. logtype != SLAPD_AUDIT_LOG ) {
  659. rv = LDAP_OPERATIONS_ERROR;
  660. PR_snprintf( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  661. "%s: invalid log type %d", attrname, logtype );
  662. }
  663. if ( !apply || !numlogs_str || !*numlogs_str) {
  664. return rv;
  665. }
  666. numlogs = atoi(numlogs_str);
  667. if (numlogs >= 1) {
  668. switch (logtype) {
  669. case SLAPD_ACCESS_LOG:
  670. LOG_ACCESS_LOCK_WRITE( );
  671. loginfo.log_access_maxnumlogs = numlogs;
  672. fe_cfg->accesslog_maxnumlogs = numlogs;
  673. LOG_ACCESS_UNLOCK_WRITE();
  674. break;
  675. case SLAPD_ERROR_LOG:
  676. LOG_ERROR_LOCK_WRITE( );
  677. loginfo.log_error_maxnumlogs = numlogs;
  678. fe_cfg->errorlog_maxnumlogs = numlogs;
  679. LOG_ERROR_UNLOCK_WRITE();
  680. break;
  681. case SLAPD_AUDIT_LOG:
  682. LOG_AUDIT_LOCK_WRITE( );
  683. loginfo.log_audit_maxnumlogs = numlogs;
  684. fe_cfg->auditlog_maxnumlogs = numlogs;
  685. LOG_AUDIT_UNLOCK_WRITE();
  686. break;
  687. default:
  688. rv = LDAP_OPERATIONS_ERROR;
  689. LDAPDebug( LDAP_DEBUG_ANY,
  690. "log_set_numlogsperdir: invalid log type %d", logtype,0,0 );
  691. }
  692. }
  693. return rv;
  694. }
  695. /******************************************************************************
  696. * LOG SIZE
  697. * Return Values:
  698. * LDAP_OPERATIONS_ERROR -- fail
  699. * LDAP_SUCCESS -- success
  700. *
  701. * NOTE: The config struct should contain the maxlogsize in MB and not in bytes.
  702. ******************************************************************************/
  703. int
  704. log_set_logsize(const char *attrname, char *logsize_str, int logtype, char *returntext, int apply)
  705. {
  706. int rv = LDAP_SUCCESS;
  707. PRInt64 mdiskspace= 0; /* in bytes */
  708. PRInt64 max_logsize; /* in bytes */
  709. int logsize; /* in megabytes */
  710. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  711. if (!apply || !logsize_str || !*logsize_str)
  712. return rv;
  713. logsize = atoi(logsize_str);
  714. /* convert it to bytes */
  715. max_logsize = (PRInt64)logsize * LOG_MB_IN_BYTES;
  716. if (max_logsize <= 0) {
  717. max_logsize = -1;
  718. }
  719. switch (logtype) {
  720. case SLAPD_ACCESS_LOG:
  721. LOG_ACCESS_LOCK_WRITE( );
  722. mdiskspace = loginfo.log_access_maxdiskspace;
  723. break;
  724. case SLAPD_ERROR_LOG:
  725. LOG_ERROR_LOCK_WRITE( );
  726. mdiskspace = loginfo.log_error_maxdiskspace;
  727. break;
  728. case SLAPD_AUDIT_LOG:
  729. LOG_AUDIT_LOCK_WRITE( );
  730. mdiskspace = loginfo.log_audit_maxdiskspace;
  731. break;
  732. default:
  733. PR_snprintf( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  734. "%s: invalid logtype %d", attrname, logtype );
  735. rv = LDAP_OPERATIONS_ERROR;
  736. }
  737. if ((max_logsize > mdiskspace) && (mdiskspace != -1))
  738. rv = 2;
  739. switch (logtype) {
  740. case SLAPD_ACCESS_LOG:
  741. if (!rv && apply) {
  742. loginfo.log_access_maxlogsize = max_logsize;
  743. fe_cfg->accesslog_maxlogsize = logsize;
  744. }
  745. LOG_ACCESS_UNLOCK_WRITE();
  746. break;
  747. case SLAPD_ERROR_LOG:
  748. if (!rv && apply) {
  749. loginfo.log_error_maxlogsize = max_logsize;
  750. fe_cfg->errorlog_maxlogsize = logsize;
  751. }
  752. LOG_ERROR_UNLOCK_WRITE();
  753. break;
  754. case SLAPD_AUDIT_LOG:
  755. if (!rv && apply) {
  756. loginfo.log_audit_maxlogsize = max_logsize;
  757. fe_cfg->auditlog_maxlogsize = logsize;
  758. }
  759. LOG_AUDIT_UNLOCK_WRITE();
  760. break;
  761. default:
  762. rv = 1;
  763. }
  764. /* logsize is in MB */
  765. if (rv == 2) {
  766. LDAPDebug (LDAP_DEBUG_ANY,
  767. "Invalid value for Maximum log size:"
  768. "Maxlogsize:%d (MB) exceeds Maxdisksize:%d (MB)\n",
  769. logsize, mdiskspace/LOG_MB_IN_BYTES,0);
  770. rv = LDAP_OPERATIONS_ERROR;
  771. }
  772. return rv;
  773. }
  774. time_t
  775. log_get_rotationsyncclock(int synchour, int syncmin)
  776. {
  777. struct tm *currtm;
  778. time_t currclock;
  779. time_t syncclock;
  780. int hours, minutes;
  781. time( &currclock);
  782. currtm = localtime( &currclock );
  783. if ( syncmin < currtm->tm_min ) {
  784. minutes = syncmin + 60 - currtm->tm_min;
  785. hours = synchour - 1 - currtm->tm_hour;
  786. } else {
  787. minutes = syncmin - currtm->tm_min;
  788. hours = synchour - currtm->tm_hour;
  789. }
  790. if ( hours < 0 ) hours += 24;
  791. syncclock = currclock + hours * 3600 + minutes * 60;
  792. return syncclock;
  793. }
  794. int
  795. log_set_rotationsync_enabled(const char *attrname, char *value, int logtype, char *errorbuf, int apply)
  796. {
  797. int v;
  798. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  799. if ( NULL == value ) {
  800. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  801. "%s: NULL value; valid values "
  802. "are \"on\" or \"off\"", attrname );
  803. return LDAP_OPERATIONS_ERROR;
  804. }
  805. if (strcasecmp(value, "on") == 0) {
  806. v = LDAP_ON;
  807. }
  808. else if (strcasecmp(value, "off") == 0 ) {
  809. v = LDAP_OFF;
  810. }
  811. else {
  812. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  813. "%s: invalid value \"%s\", valid values "
  814. "are \"on\" or \"off\"", attrname, value );
  815. return LDAP_OPERATIONS_ERROR;
  816. }
  817. if ( !apply ){
  818. return LDAP_SUCCESS;
  819. }
  820. switch (logtype) {
  821. case SLAPD_ACCESS_LOG:
  822. LOG_ACCESS_LOCK_WRITE( );
  823. fe_cfg->accesslog_rotationsync_enabled = v;
  824. loginfo.log_access_rotationsync_enabled = v;
  825. LOG_ACCESS_UNLOCK_WRITE();
  826. break;
  827. case SLAPD_ERROR_LOG:
  828. LOG_ERROR_LOCK_WRITE( );
  829. fe_cfg->errorlog_rotationsync_enabled = v;
  830. loginfo.log_error_rotationsync_enabled = v;
  831. LOG_ERROR_UNLOCK_WRITE();
  832. break;
  833. case SLAPD_AUDIT_LOG:
  834. LOG_AUDIT_LOCK_WRITE( );
  835. fe_cfg->auditlog_rotationsync_enabled = v;
  836. loginfo.log_audit_rotationsync_enabled = v;
  837. LOG_AUDIT_UNLOCK_WRITE();
  838. break;
  839. }
  840. return LDAP_SUCCESS;
  841. }
  842. int
  843. log_set_rotationsynchour(const char *attrname, char *rhour_str, int logtype, char *returntext, int apply)
  844. {
  845. int rhour = -1;
  846. int rv = LDAP_SUCCESS;
  847. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  848. if ( logtype != SLAPD_ACCESS_LOG &&
  849. logtype != SLAPD_ERROR_LOG &&
  850. logtype != SLAPD_AUDIT_LOG ) {
  851. PR_snprintf( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  852. "%s: invalid log type: %d", attrname, logtype );
  853. return LDAP_OPERATIONS_ERROR;
  854. }
  855. /* return if we aren't doing this for real */
  856. if ( !apply ) {
  857. return rv;
  858. }
  859. if ( rhour_str && *rhour_str != '\0' )
  860. rhour = atol( rhour_str );
  861. if ( rhour > 23 )
  862. rhour = rhour % 24;
  863. switch (logtype) {
  864. case SLAPD_ACCESS_LOG:
  865. LOG_ACCESS_LOCK_WRITE( );
  866. loginfo.log_access_rotationsynchour = rhour;
  867. loginfo.log_access_rotationsyncclock = log_get_rotationsyncclock( rhour, loginfo.log_access_rotationsyncmin );
  868. fe_cfg->accesslog_rotationsynchour = rhour;
  869. LOG_ACCESS_UNLOCK_WRITE();
  870. break;
  871. case SLAPD_ERROR_LOG:
  872. LOG_ERROR_LOCK_WRITE( );
  873. loginfo.log_error_rotationsynchour = rhour;
  874. loginfo.log_error_rotationsyncclock = log_get_rotationsyncclock( rhour, loginfo.log_error_rotationsyncmin );
  875. fe_cfg->errorlog_rotationsynchour = rhour;
  876. LOG_ERROR_UNLOCK_WRITE();
  877. break;
  878. case SLAPD_AUDIT_LOG:
  879. LOG_AUDIT_LOCK_WRITE( );
  880. loginfo.log_audit_rotationsynchour = rhour;
  881. loginfo.log_audit_rotationsyncclock = log_get_rotationsyncclock( rhour, loginfo.log_audit_rotationsyncmin );
  882. fe_cfg->auditlog_rotationsynchour = rhour;
  883. LOG_AUDIT_UNLOCK_WRITE();
  884. break;
  885. default:
  886. rv = 1;
  887. }
  888. return rv;
  889. }
  890. int
  891. log_set_rotationsyncmin(const char *attrname, char *rmin_str, int logtype, char *returntext, int apply)
  892. {
  893. int rmin = -1;
  894. int rv = LDAP_SUCCESS;
  895. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  896. if ( logtype != SLAPD_ACCESS_LOG &&
  897. logtype != SLAPD_ERROR_LOG &&
  898. logtype != SLAPD_AUDIT_LOG ) {
  899. PR_snprintf( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  900. "%s: invalid log type: %d", attrname, logtype );
  901. return LDAP_OPERATIONS_ERROR;
  902. }
  903. /* return if we aren't doing this for real */
  904. if ( !apply ) {
  905. return rv;
  906. }
  907. if ( rmin_str && *rmin_str != '\0' )
  908. rmin = atol( rmin_str );
  909. if ( rmin > 59 )
  910. rmin = rmin % 60;
  911. switch (logtype) {
  912. case SLAPD_ACCESS_LOG:
  913. LOG_ACCESS_LOCK_WRITE( );
  914. loginfo.log_access_rotationsyncmin = rmin;
  915. fe_cfg->accesslog_rotationsyncmin = rmin;
  916. loginfo.log_access_rotationsyncclock = log_get_rotationsyncclock( loginfo.log_access_rotationsynchour, rmin );
  917. LOG_ACCESS_UNLOCK_WRITE();
  918. break;
  919. case SLAPD_ERROR_LOG:
  920. LOG_ERROR_LOCK_WRITE( );
  921. loginfo.log_error_rotationsyncmin = rmin;
  922. loginfo.log_error_rotationsyncclock = log_get_rotationsyncclock( loginfo.log_error_rotationsynchour, rmin );
  923. fe_cfg->errorlog_rotationsyncmin = rmin;
  924. LOG_ERROR_UNLOCK_WRITE();
  925. break;
  926. case SLAPD_AUDIT_LOG:
  927. LOG_AUDIT_LOCK_WRITE( );
  928. loginfo.log_audit_rotationsyncmin = rmin;
  929. fe_cfg->auditlog_rotationsyncmin = rmin;
  930. loginfo.log_audit_rotationsyncclock = log_get_rotationsyncclock( loginfo.log_audit_rotationsynchour, rmin );
  931. LOG_AUDIT_UNLOCK_WRITE();
  932. break;
  933. default:
  934. rv = 1;
  935. }
  936. return rv;
  937. }
  938. /******************************************************************************
  939. * ROTATION TIME
  940. * Return Values:
  941. * 1 -- fail
  942. * 0 -- success
  943. ******************************************************************************/
  944. int
  945. log_set_rotationtime(const char *attrname, char *rtime_str, int logtype, char *returntext, int apply)
  946. {
  947. int runit= 0;
  948. int value, rtime;
  949. int rv = LDAP_SUCCESS;
  950. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  951. if ( logtype != SLAPD_ACCESS_LOG &&
  952. logtype != SLAPD_ERROR_LOG &&
  953. logtype != SLAPD_AUDIT_LOG ) {
  954. PR_snprintf( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  955. "%s: invalid log type: %d", attrname, logtype );
  956. return LDAP_OPERATIONS_ERROR;
  957. }
  958. /* return if we aren't doing this for real */
  959. if ( !apply || !rtime_str || !*rtime_str) {
  960. return rv;
  961. }
  962. rtime = atoi(rtime_str);
  963. if (0 == rtime) {
  964. rtime = -1; /* Value Range: -1 | 1 to PR_INT32_MAX */
  965. }
  966. switch (logtype) {
  967. case SLAPD_ACCESS_LOG:
  968. LOG_ACCESS_LOCK_WRITE( );
  969. loginfo.log_access_rotationtime = rtime;
  970. runit = loginfo.log_access_rotationunit;
  971. break;
  972. case SLAPD_ERROR_LOG:
  973. LOG_ERROR_LOCK_WRITE( );
  974. loginfo.log_error_rotationtime = rtime;
  975. runit = loginfo.log_error_rotationunit;
  976. break;
  977. case SLAPD_AUDIT_LOG:
  978. LOG_AUDIT_LOCK_WRITE( );
  979. loginfo.log_audit_rotationtime = rtime;
  980. runit = loginfo.log_audit_rotationunit;
  981. break;
  982. default:
  983. rv = 1;
  984. }
  985. /* find out the rotation unit we have se right now */
  986. if (runit == LOG_UNIT_MONTHS) {
  987. value = 31 * 24 * 60 * 60 * rtime;
  988. } else if (runit == LOG_UNIT_WEEKS) {
  989. value = 7 * 24 * 60 * 60 * rtime;
  990. } else if (runit == LOG_UNIT_DAYS ) {
  991. value = 24 * 60 * 60 * rtime;
  992. } else if (runit == LOG_UNIT_HOURS) {
  993. value = 3600 * rtime;
  994. } else if (runit == LOG_UNIT_MINS) {
  995. value = 60 * rtime;
  996. } else {
  997. /* In this case we don't rotate */
  998. value = -1;
  999. }
  1000. if (rtime > 0 && value < 0) {
  1001. value = PR_INT32_MAX; /* overflown */
  1002. }
  1003. switch (logtype) {
  1004. case SLAPD_ACCESS_LOG:
  1005. fe_cfg->accesslog_rotationtime = rtime;
  1006. loginfo.log_access_rotationtime_secs = value;
  1007. LOG_ACCESS_UNLOCK_WRITE();
  1008. break;
  1009. case SLAPD_ERROR_LOG:
  1010. fe_cfg->errorlog_rotationtime = rtime;
  1011. loginfo.log_error_rotationtime_secs = value;
  1012. LOG_ERROR_UNLOCK_WRITE();
  1013. break;
  1014. case SLAPD_AUDIT_LOG:
  1015. fe_cfg->auditlog_rotationtime = rtime;
  1016. loginfo.log_audit_rotationtime_secs = value;
  1017. LOG_AUDIT_UNLOCK_WRITE();
  1018. break;
  1019. default:
  1020. rv = 1;
  1021. }
  1022. return rv;
  1023. }
  1024. /******************************************************************************
  1025. * ROTATION TIME UNIT
  1026. * Return Values:
  1027. * 1 -- fail
  1028. * 0 -- success
  1029. ******************************************************************************/
  1030. int log_set_rotationtimeunit(const char *attrname, char *runit, int logtype, char *errorbuf, int apply)
  1031. {
  1032. int origvalue = 0, value = 0;
  1033. int runitType;
  1034. int rv = 0;
  1035. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  1036. if ( logtype != SLAPD_ACCESS_LOG &&
  1037. logtype != SLAPD_ERROR_LOG &&
  1038. logtype != SLAPD_AUDIT_LOG ) {
  1039. PR_snprintf ( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1040. "%s: invalid log type: %d", attrname, logtype );
  1041. return LDAP_OPERATIONS_ERROR;
  1042. }
  1043. if ( (strcasecmp(runit, "month") == 0) ||
  1044. (strcasecmp(runit, "week") == 0) ||
  1045. (strcasecmp(runit, "day") == 0) ||
  1046. (strcasecmp(runit, "hour") == 0) ||
  1047. (strcasecmp(runit, "minute") == 0)) {
  1048. /* all good values */
  1049. } else {
  1050. PR_snprintf ( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1051. "%s: unknown unit \"%s\"", attrname, runit );
  1052. rv = LDAP_OPERATIONS_ERROR;
  1053. }
  1054. /* return if we aren't doing this for real */
  1055. if ( !apply ) {
  1056. return rv;
  1057. }
  1058. switch (logtype) {
  1059. case SLAPD_ACCESS_LOG:
  1060. LOG_ACCESS_LOCK_WRITE( );
  1061. origvalue = loginfo.log_access_rotationtime;
  1062. break;
  1063. case SLAPD_ERROR_LOG:
  1064. LOG_ERROR_LOCK_WRITE( );
  1065. origvalue = loginfo.log_error_rotationtime;
  1066. break;
  1067. case SLAPD_AUDIT_LOG:
  1068. LOG_AUDIT_LOCK_WRITE( );
  1069. origvalue = loginfo.log_audit_rotationtime;
  1070. break;
  1071. default:
  1072. rv = 1;
  1073. }
  1074. if (strcasecmp(runit, "month") == 0) {
  1075. runitType = LOG_UNIT_MONTHS;
  1076. value = origvalue * 31 * 24 * 60 * 60;
  1077. } else if (strcasecmp(runit, "week") == 0) {
  1078. runitType = LOG_UNIT_WEEKS;
  1079. value = origvalue * 7 * 24 * 60 * 60;
  1080. } else if (strcasecmp(runit, "day") == 0) {
  1081. runitType = LOG_UNIT_DAYS;
  1082. value = origvalue * 24 * 60 * 60;
  1083. } else if (strcasecmp(runit, "hour") == 0) {
  1084. runitType = LOG_UNIT_HOURS;
  1085. value = origvalue * 3600;
  1086. } else if (strcasecmp(runit, "minute") == 0) {
  1087. runitType = LOG_UNIT_MINS;
  1088. value = origvalue * 60;
  1089. } else {
  1090. /* In this case we don't rotate */
  1091. runitType = LOG_UNIT_UNKNOWN;
  1092. value = -1;
  1093. }
  1094. if (origvalue > 0 && value < 0) {
  1095. value = PR_INT32_MAX; /* overflown */
  1096. }
  1097. switch (logtype) {
  1098. case SLAPD_ACCESS_LOG:
  1099. loginfo.log_access_rotationtime_secs = value;
  1100. loginfo.log_access_rotationunit = runitType;
  1101. slapi_ch_free ( (void **) &fe_cfg->accesslog_rotationunit);
  1102. fe_cfg->accesslog_rotationunit = slapi_ch_strdup ( runit );
  1103. LOG_ACCESS_UNLOCK_WRITE();
  1104. break;
  1105. case SLAPD_ERROR_LOG:
  1106. loginfo.log_error_rotationtime_secs = value;
  1107. loginfo.log_error_rotationunit = runitType;
  1108. slapi_ch_free ( (void **) &fe_cfg->errorlog_rotationunit) ;
  1109. fe_cfg->errorlog_rotationunit = slapi_ch_strdup ( runit );
  1110. LOG_ERROR_UNLOCK_WRITE();
  1111. break;
  1112. case SLAPD_AUDIT_LOG:
  1113. loginfo.log_audit_rotationtime_secs = value;
  1114. loginfo.log_audit_rotationunit = runitType;
  1115. slapi_ch_free ( (void **) &fe_cfg->auditlog_rotationunit);
  1116. fe_cfg->auditlog_rotationunit = slapi_ch_strdup ( runit );
  1117. LOG_AUDIT_UNLOCK_WRITE();
  1118. break;
  1119. default:
  1120. rv = 1;
  1121. }
  1122. return rv;
  1123. }
  1124. /******************************************************************************
  1125. * MAXIMUM DISK SPACE
  1126. * Return Values:
  1127. * 1 -- fail
  1128. * 0 -- success
  1129. *
  1130. * NOTE:
  1131. * The config struct should contain the value in MB and not in bytes.
  1132. ******************************************************************************/
  1133. int
  1134. log_set_maxdiskspace(const char *attrname, char *maxdiskspace_str, int logtype, char *errorbuf, int apply)
  1135. {
  1136. int rv = 0;
  1137. PRInt64 mlogsize; /* in bytes */
  1138. PRInt64 maxdiskspace; /* in bytes */
  1139. int s_maxdiskspace; /* in megabytes */
  1140. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  1141. if ( logtype != SLAPD_ACCESS_LOG &&
  1142. logtype != SLAPD_ERROR_LOG &&
  1143. logtype != SLAPD_AUDIT_LOG ) {
  1144. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1145. "%s: invalid log type: %d", attrname, logtype );
  1146. return LDAP_OPERATIONS_ERROR;
  1147. }
  1148. if (!apply || !maxdiskspace_str || !*maxdiskspace_str)
  1149. return rv;
  1150. s_maxdiskspace = atoi(maxdiskspace_str);
  1151. /* Disk space are in MB but store in bytes */
  1152. switch (logtype) {
  1153. case SLAPD_ACCESS_LOG:
  1154. LOG_ACCESS_LOCK_WRITE( );
  1155. mlogsize = loginfo.log_access_maxlogsize;
  1156. break;
  1157. case SLAPD_ERROR_LOG:
  1158. LOG_ERROR_LOCK_WRITE( );
  1159. mlogsize = loginfo.log_error_maxlogsize;
  1160. break;
  1161. case SLAPD_AUDIT_LOG:
  1162. LOG_AUDIT_LOCK_WRITE( );
  1163. mlogsize = loginfo.log_audit_maxlogsize;
  1164. break;
  1165. default:
  1166. rv = 1;
  1167. mlogsize = -1;
  1168. }
  1169. maxdiskspace = (PRInt64)s_maxdiskspace * LOG_MB_IN_BYTES;
  1170. if (maxdiskspace < 0) {
  1171. maxdiskspace = -1;
  1172. } else if (maxdiskspace < mlogsize) {
  1173. rv = LDAP_OPERATIONS_ERROR;
  1174. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1175. "%s: \"%d (MB)\" is less than max log size \"%d (MB)\"",
  1176. attrname, s_maxdiskspace, (int)(mlogsize/LOG_MB_IN_BYTES) );
  1177. }
  1178. switch (logtype) {
  1179. case SLAPD_ACCESS_LOG:
  1180. if (rv== 0 && apply) {
  1181. loginfo.log_access_maxdiskspace = maxdiskspace; /* in bytes */
  1182. fe_cfg->accesslog_maxdiskspace = s_maxdiskspace; /* in megabytes */
  1183. }
  1184. LOG_ACCESS_UNLOCK_WRITE();
  1185. break;
  1186. case SLAPD_ERROR_LOG:
  1187. if (rv== 0 && apply) {
  1188. loginfo.log_error_maxdiskspace = maxdiskspace; /* in bytes */
  1189. fe_cfg->errorlog_maxdiskspace = s_maxdiskspace; /* in megabytes */
  1190. }
  1191. LOG_ERROR_UNLOCK_WRITE();
  1192. break;
  1193. case SLAPD_AUDIT_LOG:
  1194. if (rv== 0 && apply) {
  1195. loginfo.log_audit_maxdiskspace = maxdiskspace; /* in bytes */
  1196. fe_cfg->auditlog_maxdiskspace = s_maxdiskspace; /* in megabytes */
  1197. }
  1198. LOG_AUDIT_UNLOCK_WRITE();
  1199. break;
  1200. default:
  1201. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1202. "%s: invalid log type (%d) for setting maximum disk space: %d MB\n",
  1203. attrname, logtype, s_maxdiskspace);
  1204. rv = LDAP_OPERATIONS_ERROR;
  1205. }
  1206. return rv;
  1207. }
  1208. /******************************************************************************
  1209. * MINIMUM FREE SPACE
  1210. * Return Values:
  1211. * 1 -- fail
  1212. * 0 -- success
  1213. ******************************************************************************/
  1214. int
  1215. log_set_mindiskspace(const char *attrname, char *minfreespace_str, int logtype, char *errorbuf, int apply)
  1216. {
  1217. int rv=LDAP_SUCCESS;
  1218. int minfreespace; /* in megabytes */
  1219. PRInt64 minfreespaceB; /* in bytes */
  1220. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  1221. if ( logtype != SLAPD_ACCESS_LOG &&
  1222. logtype != SLAPD_ERROR_LOG &&
  1223. logtype != SLAPD_AUDIT_LOG ) {
  1224. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1225. "%s: invalid log type: %d", attrname, logtype );
  1226. rv = LDAP_OPERATIONS_ERROR;
  1227. }
  1228. /* return if we aren't doing this for real */
  1229. if ( !apply || !minfreespace_str || !*minfreespace_str) {
  1230. return rv;
  1231. }
  1232. minfreespace = atoi(minfreespace_str);
  1233. /* Disk space are in MB but store in bytes */
  1234. if (minfreespace >= 1 ) {
  1235. minfreespaceB = (PRInt64)minfreespace * LOG_MB_IN_BYTES;
  1236. switch (logtype) {
  1237. case SLAPD_ACCESS_LOG:
  1238. LOG_ACCESS_LOCK_WRITE( );
  1239. loginfo.log_access_minfreespace = minfreespaceB;
  1240. fe_cfg->accesslog_minfreespace = minfreespace;
  1241. LOG_ACCESS_UNLOCK_WRITE();
  1242. break;
  1243. case SLAPD_ERROR_LOG:
  1244. LOG_ERROR_LOCK_WRITE( );
  1245. loginfo.log_error_minfreespace = minfreespaceB;
  1246. fe_cfg->errorlog_minfreespace = minfreespace;
  1247. LOG_ERROR_UNLOCK_WRITE();
  1248. break;
  1249. case SLAPD_AUDIT_LOG:
  1250. LOG_AUDIT_LOCK_WRITE( );
  1251. loginfo.log_audit_minfreespace = minfreespaceB;
  1252. fe_cfg->auditlog_minfreespace = minfreespace;
  1253. LOG_AUDIT_UNLOCK_WRITE();
  1254. break;
  1255. default:
  1256. rv = 1;
  1257. }
  1258. }
  1259. return rv;
  1260. }
  1261. /******************************************************************************
  1262. * LOG EXPIRATION TIME
  1263. * Return Values:
  1264. * 1 -- fail
  1265. * 0 -- success
  1266. ******************************************************************************/
  1267. int
  1268. log_set_expirationtime(const char *attrname, char *exptime_str, int logtype, char *errorbuf, int apply)
  1269. {
  1270. int eunit, value, exptime;
  1271. int rsec=0;
  1272. int rv = 0;
  1273. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  1274. if ( logtype != SLAPD_ACCESS_LOG &&
  1275. logtype != SLAPD_ERROR_LOG &&
  1276. logtype != SLAPD_AUDIT_LOG ) {
  1277. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1278. "%s: invalid log type: %d", attrname, logtype );
  1279. rv = LDAP_OPERATIONS_ERROR;
  1280. }
  1281. /* return if we aren't doing this for real */
  1282. if ( !apply || !exptime_str || !*exptime_str) {
  1283. return rv;
  1284. }
  1285. exptime = atoi(exptime_str); /* <= 0: no exptime */
  1286. switch (logtype) {
  1287. case SLAPD_ACCESS_LOG:
  1288. LOG_ACCESS_LOCK_WRITE( );
  1289. loginfo.log_access_exptime = exptime;
  1290. eunit = loginfo.log_access_exptimeunit;
  1291. rsec = loginfo.log_access_rotationtime_secs;
  1292. break;
  1293. case SLAPD_ERROR_LOG:
  1294. LOG_ERROR_LOCK_WRITE( );
  1295. loginfo.log_error_exptime = exptime;
  1296. eunit = loginfo.log_error_exptimeunit;
  1297. rsec = loginfo.log_error_rotationtime_secs;
  1298. break;
  1299. case SLAPD_AUDIT_LOG:
  1300. LOG_AUDIT_LOCK_WRITE( );
  1301. loginfo.log_audit_exptime = exptime;
  1302. eunit = loginfo.log_audit_exptimeunit;
  1303. rsec = loginfo.log_audit_rotationtime_secs;
  1304. break;
  1305. default:
  1306. rv = 1;
  1307. eunit = -1;
  1308. }
  1309. value = -1; /* never expires, by default */
  1310. if (exptime > 0) {
  1311. if (eunit == LOG_UNIT_MONTHS) {
  1312. value = 31 * 24 * 60 * 60 * exptime;
  1313. } else if (eunit == LOG_UNIT_WEEKS) {
  1314. value = 7 * 24 * 60 * 60 * exptime;
  1315. } else if (eunit == LOG_UNIT_DAYS) {
  1316. value = 24 * 60 * 60 * exptime;
  1317. }
  1318. }
  1319. if (value > 0 && value < rsec) {
  1320. value = rsec;
  1321. } else if (exptime > 0 && value < -1) {
  1322. /* value is overflown */
  1323. value = PR_INT32_MAX;
  1324. }
  1325. switch (logtype) {
  1326. case SLAPD_ACCESS_LOG:
  1327. loginfo.log_access_exptime_secs = value;
  1328. fe_cfg->accesslog_exptime = exptime;
  1329. LOG_ACCESS_UNLOCK_WRITE();
  1330. break;
  1331. case SLAPD_ERROR_LOG:
  1332. loginfo.log_error_exptime_secs = value;
  1333. fe_cfg->errorlog_exptime = exptime;
  1334. LOG_ERROR_UNLOCK_WRITE();
  1335. break;
  1336. case SLAPD_AUDIT_LOG:
  1337. loginfo.log_audit_exptime_secs = value;
  1338. fe_cfg->auditlog_exptime = exptime;
  1339. LOG_AUDIT_UNLOCK_WRITE();
  1340. break;
  1341. default:
  1342. rv = 1;
  1343. }
  1344. return rv;
  1345. }
  1346. /******************************************************************************
  1347. * LOG EXPIRATION TIME UNIT
  1348. * Return Values:
  1349. * 1 -- fail
  1350. * 0 -- success
  1351. ******************************************************************************/
  1352. int
  1353. log_set_expirationtimeunit(const char *attrname, char *expunit, int logtype, char *errorbuf, int apply)
  1354. {
  1355. int value = 0;
  1356. int rv = 0;
  1357. int exptime, rsecs;
  1358. int *exptimeunitp = NULL;
  1359. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  1360. if ( logtype != SLAPD_ACCESS_LOG &&
  1361. logtype != SLAPD_ERROR_LOG &&
  1362. logtype != SLAPD_AUDIT_LOG ) {
  1363. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1364. "%s: invalid log type: %d", attrname, logtype );
  1365. return LDAP_OPERATIONS_ERROR;
  1366. }
  1367. if ( NULL == expunit ) {
  1368. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1369. "%s: NULL value", attrname );
  1370. return LDAP_OPERATIONS_ERROR;
  1371. }
  1372. if ( (strcasecmp(expunit, "month") == 0) ||
  1373. (strcasecmp(expunit, "week") == 0) ||
  1374. (strcasecmp(expunit, "day") == 0)) {
  1375. /* we have good values */
  1376. } else {
  1377. PR_snprintf( errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
  1378. "%s: invalid time unit \"%s\"", attrname, expunit );
  1379. rv = LDAP_OPERATIONS_ERROR;;
  1380. }
  1381. /* return if we aren't doing this for real */
  1382. if ( !apply ) {
  1383. return rv;
  1384. }
  1385. switch (logtype) {
  1386. case SLAPD_ACCESS_LOG:
  1387. LOG_ACCESS_LOCK_WRITE( );
  1388. exptime = loginfo.log_access_exptime;
  1389. rsecs = loginfo.log_access_rotationtime_secs;
  1390. exptimeunitp = &(loginfo.log_access_exptimeunit);
  1391. break;
  1392. case SLAPD_ERROR_LOG:
  1393. LOG_ERROR_LOCK_WRITE( );
  1394. exptime = loginfo.log_error_exptime;
  1395. rsecs = loginfo.log_error_rotationtime_secs;
  1396. exptimeunitp = &(loginfo.log_error_exptimeunit);
  1397. break;
  1398. case SLAPD_AUDIT_LOG:
  1399. LOG_AUDIT_LOCK_WRITE( );
  1400. exptime = loginfo.log_audit_exptime;
  1401. rsecs = loginfo.log_audit_rotationtime_secs;
  1402. exptimeunitp = &(loginfo.log_audit_exptimeunit);
  1403. break;
  1404. default:
  1405. rv = 1;
  1406. exptime = -1;
  1407. rsecs = -1;
  1408. }
  1409. value = -1;
  1410. if (strcasecmp(expunit, "month") == 0) {
  1411. if (exptime > 0) {
  1412. value = 31 * 24 * 60 * 60 * exptime;
  1413. }
  1414. if (exptimeunitp) {
  1415. *exptimeunitp = LOG_UNIT_MONTHS;
  1416. }
  1417. } else if (strcasecmp(expunit, "week") == 0) {
  1418. if (exptime > 0) {
  1419. value = 7 * 24 * 60 * 60 * exptime;
  1420. }
  1421. if (exptimeunitp) {
  1422. *exptimeunitp = LOG_UNIT_WEEKS;
  1423. }
  1424. } else if (strcasecmp(expunit, "day") == 0) {
  1425. if (exptime > 0) {
  1426. value = 24 * 60 * 60 * exptime;
  1427. }
  1428. if (exptimeunitp) {
  1429. *exptimeunitp = LOG_UNIT_DAYS;
  1430. }
  1431. }
  1432. if ((value > 0) && value < rsecs) {
  1433. value = rsecs;
  1434. }
  1435. switch (logtype) {
  1436. case SLAPD_ACCESS_LOG:
  1437. loginfo.log_access_exptime_secs = value;
  1438. slapi_ch_free ( (void **) &(fe_cfg->accesslog_exptimeunit) );
  1439. fe_cfg->accesslog_exptimeunit = slapi_ch_strdup ( expunit );
  1440. LOG_ACCESS_UNLOCK_WRITE();
  1441. break;
  1442. case SLAPD_ERROR_LOG:
  1443. loginfo.log_error_exptime_secs = value;
  1444. slapi_ch_free ( (void **) &(fe_cfg->errorlog_exptimeunit) );
  1445. fe_cfg->errorlog_exptimeunit = slapi_ch_strdup ( expunit );
  1446. LOG_ERROR_UNLOCK_WRITE();
  1447. break;
  1448. case SLAPD_AUDIT_LOG:
  1449. loginfo.log_audit_exptime_secs = value;
  1450. slapi_ch_free ( (void **) &(fe_cfg->auditlog_exptimeunit) );
  1451. fe_cfg->auditlog_exptimeunit = slapi_ch_strdup ( expunit );
  1452. LOG_AUDIT_UNLOCK_WRITE();
  1453. break;
  1454. default:
  1455. rv = 1;
  1456. }
  1457. return rv;
  1458. }
  1459. /******************************************************************************
  1460. * Write title line in log file
  1461. *****************************************************************************/
  1462. static void
  1463. log_write_title (LOGFD fp)
  1464. {
  1465. slapdFrontendConfig_t *fe_cfg = getFrontendConfig();
  1466. char *buildnum = config_get_buildnum();
  1467. char buff[512];
  1468. int bufflen = sizeof(buff);
  1469. int err = 0;
  1470. PR_snprintf(buff, bufflen, "\t%s B%s\n",
  1471. fe_cfg->versionstring ? fe_cfg->versionstring : "Fedora-Directory",
  1472. buildnum ? buildnum : "");
  1473. LOG_WRITE_NOW(fp, buff, strlen(buff), 0, err);
  1474. if (fe_cfg->localhost) {
  1475. PR_snprintf(buff, bufflen, "\t%s:%d (%s)\n\n",
  1476. fe_cfg->localhost,
  1477. fe_cfg->security ? fe_cfg->secureport : fe_cfg->port,
  1478. fe_cfg->configdir ? fe_cfg->configdir : "");
  1479. }
  1480. else {
  1481. /* If fe_cfg->localhost is not set, ignore fe_cfg->port since
  1482. * it is the default and might be misleading.
  1483. */
  1484. PR_snprintf(buff, bufflen, "\t<host>:<port> (%s)\n\n",
  1485. fe_cfg->configdir ? fe_cfg->configdir : "");
  1486. }
  1487. LOG_WRITE_NOW(fp, buff, strlen(buff), 0, err);
  1488. slapi_ch_free((void **)&buildnum);
  1489. }
  1490. /******************************************************************************
  1491. * init function for the error log
  1492. * Returns:
  1493. * 0 - success
  1494. * 1 - fail
  1495. ******************************************************************************/
  1496. int error_log_openf( char *pathname, int locked)
  1497. {
  1498. int rv = 0;
  1499. int logfile_type =0;
  1500. char buf[BUFSIZ];
  1501. if (!locked) LOG_ERROR_LOCK_WRITE ();
  1502. /* save the file name */
  1503. slapi_ch_free ((void**)&loginfo.log_error_file);
  1504. loginfo.log_error_file = slapi_ch_strdup(pathname);
  1505. /* store the rotation info fiel path name */
  1506. PR_snprintf (buf, sizeof(buf), "%s.rotationinfo",pathname);
  1507. slapi_ch_free ((void**)&loginfo.log_errorinfo_file);
  1508. loginfo.log_errorinfo_file = slapi_ch_strdup ( buf );
  1509. /*
  1510. ** Check if we have a log file already. If we have it then
  1511. ** we need to parse the header info and update the loginfo
  1512. ** struct.
  1513. */
  1514. logfile_type = log__error_rotationinfof(loginfo.log_errorinfo_file);
  1515. if (log__open_errorlogfile(logfile_type, 1/* got lock*/) != LOG_SUCCESS) {
  1516. rv = 1;
  1517. }
  1518. if (!locked) LOG_ERROR_UNLOCK_WRITE();
  1519. return rv;
  1520. }
  1521. /******************************************************************************
  1522. * init function for the audit log
  1523. * Returns:
  1524. * 0 - success
  1525. * 1 - fail
  1526. ******************************************************************************/
  1527. int
  1528. audit_log_openf( char *pathname, int locked)
  1529. {
  1530. int rv=0;
  1531. int logfile_type = 0;
  1532. char buf[BUFSIZ];
  1533. if (!locked) LOG_AUDIT_LOCK_WRITE( );
  1534. /* store the path name */
  1535. loginfo.log_audit_file = slapi_ch_strdup ( pathname );
  1536. /* store the rotation info file path name */
  1537. PR_snprintf (buf, sizeof(buf), "%s.rotationinfo",pathname);
  1538. loginfo.log_auditinfo_file = slapi_ch_strdup ( buf );
  1539. /*
  1540. ** Check if we have a log file already. If we have it then
  1541. ** we need to parse the header info and update the loginfo
  1542. ** struct.
  1543. */
  1544. logfile_type = log__audit_rotationinfof(loginfo.log_auditinfo_file);
  1545. if (log__open_auditlogfile(logfile_type, 1/* got lock*/) != LOG_SUCCESS) {
  1546. rv = 1;
  1547. }
  1548. if (!locked) LOG_AUDIT_UNLOCK_WRITE();
  1549. return rv;
  1550. }
  1551. /******************************************************************************
  1552. * write in the audit log
  1553. ******************************************************************************/
  1554. int
  1555. slapd_log_audit_proc (
  1556. char *buffer,
  1557. int buf_len)
  1558. {
  1559. int err;
  1560. if ( (loginfo.log_audit_state & LOGGING_ENABLED) && (loginfo.log_audit_file != NULL) ){
  1561. LOG_AUDIT_LOCK_WRITE( );
  1562. if (log__needrotation(loginfo.log_audit_fdes,
  1563. SLAPD_AUDIT_LOG) == LOG_ROTATE) {
  1564. if (log__open_auditlogfile(LOGFILE_NEW, 1) != LOG_SUCCESS) {
  1565. LDAPDebug(LDAP_DEBUG_ANY,
  1566. "LOGINFO: Unable to open audit file:%s\n",
  1567. loginfo.log_audit_file,0,0);
  1568. LOG_AUDIT_UNLOCK_WRITE();
  1569. return 0;
  1570. }
  1571. while (loginfo.log_audit_rotationsyncclock <= loginfo.log_audit_ctime) {
  1572. loginfo.log_audit_rotationsyncclock += PR_ABS(loginfo.log_audit_rotationtime_secs);
  1573. }
  1574. }
  1575. if (loginfo.log_audit_state & LOGGING_NEED_TITLE) {
  1576. log_write_title( loginfo.log_audit_fdes);
  1577. loginfo.log_audit_state &= ~LOGGING_NEED_TITLE;
  1578. }
  1579. LOG_WRITE_NOW(loginfo.log_audit_fdes, buffer, buf_len, 0, err);
  1580. LOG_AUDIT_UNLOCK_WRITE();
  1581. return 0;
  1582. }
  1583. return 0;
  1584. }
  1585. /******************************************************************************
  1586. * write in the error log
  1587. ******************************************************************************/
  1588. int
  1589. slapd_log_error_proc(
  1590. char *subsystem, /* omitted if NULL */
  1591. char *fmt,
  1592. ... )
  1593. {
  1594. va_list ap_err;
  1595. va_list ap_file;
  1596. va_start( ap_err, fmt );
  1597. va_start( ap_file, fmt );
  1598. slapd_log_error_proc_internal(subsystem, fmt, ap_err, ap_file);
  1599. va_end(ap_err);
  1600. va_end(ap_file);
  1601. return 0;
  1602. }
  1603. static int
  1604. slapd_log_error_proc_internal(
  1605. char *subsystem, /* omitted if NULL */
  1606. char *fmt,
  1607. va_list ap_err,
  1608. va_list ap_file)
  1609. {
  1610. int rc = 0;
  1611. if ( (loginfo.log_error_state & LOGGING_ENABLED) && (loginfo.log_error_file != NULL) ) {
  1612. LOG_ERROR_LOCK_WRITE( );
  1613. if (log__needrotation(loginfo.log_error_fdes,
  1614. SLAPD_ERROR_LOG) == LOG_ROTATE) {
  1615. if (log__open_errorlogfile(LOGFILE_NEW, 1) != LOG_SUCCESS) {
  1616. LOG_ERROR_UNLOCK_WRITE();
  1617. /* shouldn't continue. error is syslog'ed in open_errorlogfile */
  1618. g_set_shutdown( SLAPI_SHUTDOWN_EXIT );
  1619. return 0;
  1620. }
  1621. while (loginfo.log_error_rotationsyncclock <= loginfo.log_error_ctime) {
  1622. loginfo.log_error_rotationsyncclock += PR_ABS(loginfo.log_error_rotationtime_secs);
  1623. }
  1624. }
  1625. if (!(detached)) {
  1626. rc = vslapd_log_error( NULL, subsystem, fmt, ap_err, 1 );
  1627. }
  1628. if ( loginfo.log_error_fdes != NULL ) {
  1629. if (loginfo.log_error_state & LOGGING_NEED_TITLE) {
  1630. log_write_title(loginfo.log_error_fdes);
  1631. loginfo.log_error_state &= ~LOGGING_NEED_TITLE;
  1632. }
  1633. rc = vslapd_log_error( loginfo.log_error_fdes, subsystem, fmt, ap_file, 1 );
  1634. }
  1635. LOG_ERROR_UNLOCK_WRITE();
  1636. } else {
  1637. /* log the problem in the stderr */
  1638. rc = vslapd_log_error( NULL, subsystem, fmt, ap_err, 0 );
  1639. }
  1640. return( rc );
  1641. }
  1642. static int
  1643. vslapd_log_error(
  1644. LOGFD fp,
  1645. char *subsystem, /* omitted if NULL */
  1646. char *fmt,
  1647. va_list ap,
  1648. int locked )
  1649. {
  1650. time_t tnl;
  1651. long tz;
  1652. struct tm *tmsp, tms;
  1653. char tbuf[ TBUFSIZE ];
  1654. char sign;
  1655. char buffer[SLAPI_LOG_BUFSIZ];
  1656. int blen;
  1657. char *vbuf;
  1658. int header_len = 0;
  1659. int err = 0;
  1660. tnl = current_time();
  1661. #ifdef _WIN32
  1662. {
  1663. struct tm *pt = localtime( &tnl );
  1664. tmsp = &tms;
  1665. memcpy(&tms, pt, sizeof(struct tm) );
  1666. }
  1667. #else
  1668. (void)localtime_r( &tnl, &tms );
  1669. tmsp = &tms;
  1670. #endif
  1671. #ifdef BSD_TIME
  1672. tz = tmsp->tm_gmtoff;
  1673. #else /* BSD_TIME */
  1674. tz = - timezone;
  1675. if ( tmsp->tm_isdst ) {
  1676. tz += 3600;
  1677. }
  1678. #endif /* BSD_TIME */
  1679. sign = ( tz >= 0 ? '+' : '-' );
  1680. if ( tz < 0 ) {
  1681. tz = -tz;
  1682. }
  1683. (void)strftime( tbuf, (size_t)TBUFSIZE, "%d/%b/%Y:%H:%M:%S", tmsp);
  1684. sprintf( buffer, "[%s %c%02d%02d]%s%s - ", tbuf, sign,
  1685. (int)( tz / 3600 ), (int)( tz % 3600 ),
  1686. subsystem ? " " : "",
  1687. subsystem ? subsystem : "");
  1688. /* Bug 561525: to be able to remove timestamp to not over pollute syslog, we may need
  1689. to skip the timestamp part of the message.
  1690. The size of the header is:
  1691. the size of the time string
  1692. + size of space
  1693. + size of one char (sign)
  1694. + size of 2 char
  1695. + size of 2 char
  1696. + size of [
  1697. + size of ]
  1698. */
  1699. header_len = strlen(tbuf) + 8;
  1700. if ((vbuf = PR_vsmprintf(fmt, ap)) == NULL) {
  1701. return -1;
  1702. }
  1703. blen = strlen(buffer);
  1704. PR_snprintf (buffer+blen, sizeof(buffer)-blen, "%s", vbuf);
  1705. buffer[sizeof(buffer)-1] = '\0';
  1706. if (fp)
  1707. #if 0
  1708. LOG_WRITE_NOW(fp, buffer, strlen(buffer), header_len, err);
  1709. #else
  1710. do {
  1711. int size = strlen(buffer);
  1712. (err) = 0;
  1713. if ( slapi_write_buffer((fp), (buffer), (size)) != (size) )
  1714. {
  1715. PRErrorCode prerr = PR_GetError();
  1716. syslog(LOG_ERR, "Failed to write log, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s): %s\n", prerr, slapd_pr_strerror(prerr), (buffer)+(header_len) );
  1717. (err) = prerr;
  1718. }
  1719. /* Should be a flush in here ?? Yes because PR_SYNC doesn't work ! */
  1720. PR_Sync(fp);
  1721. } while (0);
  1722. #endif
  1723. else /* stderr is always unbuffered */
  1724. fprintf(stderr, "%s", buffer);
  1725. if (err) {
  1726. PR_snprintf(buffer, sizeof(buffer),
  1727. "Writing to the errors log failed. Exiting...");
  1728. log__error_emergency(buffer, 1, locked);
  1729. /* failed to write to the errors log. should not continue. */
  1730. g_set_shutdown( SLAPI_SHUTDOWN_EXIT );
  1731. }
  1732. PR_smprintf_free (vbuf);
  1733. return( 0 );
  1734. }
  1735. int
  1736. slapi_log_error( int severity, char *subsystem, char *fmt, ... )
  1737. {
  1738. va_list ap1;
  1739. va_list ap2;
  1740. int rc;
  1741. if ( severity < SLAPI_LOG_MIN || severity > SLAPI_LOG_MAX ) {
  1742. (void)slapd_log_error_proc( subsystem,
  1743. "slapi_log_error: invalid severity %d (message %s)\n",
  1744. severity, fmt );
  1745. return( -1 );
  1746. }
  1747. if (
  1748. #ifdef _WIN32
  1749. *module_ldap_debug
  1750. #else
  1751. slapd_ldap_debug
  1752. #endif
  1753. & slapi_log_map[ severity ] ) {
  1754. va_start( ap1, fmt );
  1755. va_start( ap2, fmt );
  1756. rc = slapd_log_error_proc_internal( subsystem, fmt, ap1, ap2 );
  1757. va_end( ap1 );
  1758. va_end( ap2 );
  1759. } else {
  1760. rc = 0; /* nothing to be logged --> always return success */
  1761. }
  1762. return( rc );
  1763. }
  1764. int
  1765. slapi_is_loglevel_set ( const int loglevel )
  1766. {
  1767. return (
  1768. #ifdef _WIN32
  1769. *module_ldap_debug
  1770. #else
  1771. slapd_ldap_debug
  1772. #endif
  1773. & slapi_log_map[ loglevel ] ? 1 : 0);
  1774. }
  1775. /******************************************************************************
  1776. * write in the access log
  1777. ******************************************************************************/
  1778. static int vslapd_log_access(char *fmt, va_list ap)
  1779. {
  1780. time_t tnl;
  1781. long tz;
  1782. struct tm *tmsp, tms;
  1783. char tbuf[ TBUFSIZE ];
  1784. char sign;
  1785. char buffer[SLAPI_LOG_BUFSIZ];
  1786. char vbuf[SLAPI_LOG_BUFSIZ];
  1787. int blen, vlen;
  1788. /* info needed to keep us from calling localtime/strftime so often: */
  1789. static time_t old_time = 0;
  1790. static char old_tbuf[TBUFSIZE];
  1791. static int old_blen = 0;
  1792. tnl = current_time();
  1793. /* check if we can use the old strftime buffer */
  1794. PR_Lock(ts_time_lock);
  1795. if (tnl == old_time) {
  1796. strcpy(buffer, old_tbuf);
  1797. blen = old_blen;
  1798. PR_Unlock(ts_time_lock);
  1799. } else {
  1800. /* nope... painstakingly create the new strftime buffer */
  1801. #ifdef _WIN32
  1802. {
  1803. struct tm *pt = localtime( &tnl );
  1804. tmsp = &tms;
  1805. memcpy(&tms, pt, sizeof(struct tm) );
  1806. }
  1807. #else
  1808. (void)localtime_r( &tnl, &tms );
  1809. tmsp = &tms;
  1810. #endif
  1811. #ifdef BSD_TIME
  1812. tz = tmsp->tm_gmtoff;
  1813. #else /* BSD_TIME */
  1814. tz = - timezone;
  1815. if ( tmsp->tm_isdst ) {
  1816. tz += 3600;
  1817. }
  1818. #endif /* BSD_TIME */
  1819. sign = ( tz >= 0 ? '+' : '-' );
  1820. if ( tz < 0 ) {
  1821. tz = -tz;
  1822. }
  1823. (void)strftime( tbuf, (size_t)TBUFSIZE, "%d/%b/%Y:%H:%M:%S", tmsp);
  1824. sprintf( buffer, "[%s %c%02d%02d] ", tbuf, sign,
  1825. (int)( tz / 3600 ), (int)( tz % 3600));
  1826. old_time = tnl;
  1827. strcpy(old_tbuf, buffer);
  1828. blen = strlen(buffer);
  1829. old_blen = blen;
  1830. PR_Unlock(ts_time_lock);
  1831. }
  1832. vlen = PR_vsnprintf(vbuf, SLAPI_LOG_BUFSIZ, fmt, ap);
  1833. if (! vlen) {
  1834. return -1;
  1835. }
  1836. if (SLAPI_LOG_BUFSIZ - blen < vlen) {
  1837. return -1;
  1838. }
  1839. log_append_buffer2(tnl, loginfo.log_access_buffer, buffer, blen, vbuf, vlen);
  1840. return( 0 );
  1841. }
  1842. int
  1843. slapi_log_access( int level,
  1844. char *fmt,
  1845. ... )
  1846. {
  1847. va_list ap;
  1848. int rc=0;
  1849. if (!(loginfo.log_access_state & LOGGING_ENABLED)) {
  1850. return 0;
  1851. }
  1852. va_start( ap, fmt );
  1853. if (( level & loginfo.log_access_level ) &&
  1854. ( loginfo.log_access_fdes != NULL ) && (loginfo.log_access_file != NULL) ) {
  1855. rc = vslapd_log_access(fmt, ap);
  1856. }
  1857. va_end( ap );
  1858. return( rc );
  1859. }
  1860. /******************************************************************************
  1861. * access_log_openf
  1862. *
  1863. * Open the access log file
  1864. *
  1865. * Returns:
  1866. * 0 -- success
  1867. * 1 -- fail
  1868. ******************************************************************************/
  1869. int access_log_openf(char *pathname, int locked)
  1870. {
  1871. int rv=0;
  1872. int logfile_type = 0;
  1873. char buf[BUFSIZ];
  1874. if (!locked) LOG_ACCESS_LOCK_WRITE( );
  1875. /* store the path name */
  1876. loginfo.log_access_file = slapi_ch_strdup ( pathname );
  1877. /* store the rotation info fiel path name */
  1878. PR_snprintf (buf, sizeof(buf), "%s.rotationinfo",pathname);
  1879. loginfo.log_accessinfo_file = slapi_ch_strdup ( buf );
  1880. /*
  1881. ** Check if we have a log file already. If we have it then
  1882. ** we need to parse the header info and update the loginfo
  1883. ** struct.
  1884. */
  1885. logfile_type = log__access_rotationinfof(loginfo.log_accessinfo_file);
  1886. if (log__open_accesslogfile(logfile_type, 1/* got lock*/) != LOG_SUCCESS) {
  1887. rv = 1;
  1888. }
  1889. if (!locked) LOG_ACCESS_UNLOCK_WRITE();
  1890. return rv;
  1891. }
  1892. /******************************************************************************
  1893. * log__open_accesslogfile
  1894. *
  1895. * Open a new log file. If we have run out of the max logs we can have
  1896. * then delete the oldest file.
  1897. ******************************************************************************/
  1898. static int
  1899. log__open_accesslogfile(int logfile_state, int locked)
  1900. {
  1901. time_t now;
  1902. LOGFD fp;
  1903. LOGFD fpinfo = NULL;
  1904. char tbuf[TBUFSIZE];
  1905. struct logfileinfo *logp;
  1906. char buffer[BUFSIZ];
  1907. if (!locked) LOG_ACCESS_LOCK_WRITE( );
  1908. /*
  1909. ** Here we are trying to create a new log file.
  1910. ** If we alredy have one, then we need to rename it as
  1911. ** "filename.time", close it and update it's information
  1912. ** in the array stack.
  1913. */
  1914. if (loginfo.log_access_fdes != NULL) {
  1915. struct logfileinfo *log;
  1916. char newfile[BUFSIZ];
  1917. PRInt64 f_size;
  1918. /* get rid of the old one */
  1919. if ((f_size = log__getfilesize(loginfo.log_access_fdes)) == -1) {
  1920. /* Then assume that we have the max size (in bytes) */
  1921. f_size = loginfo.log_access_maxlogsize;
  1922. }
  1923. /* Check if I have to delete any old file, delete it if it is required.
  1924. ** If there is just one file, then access and access.rotation files
  1925. ** are deleted. After that we start fresh
  1926. */
  1927. while (log__delete_access_logfile());
  1928. /* close the file */
  1929. LOG_CLOSE(loginfo.log_access_fdes);
  1930. /*
  1931. * loginfo.log_access_fdes is not set to NULL here, otherwise
  1932. * slapi_log_access() will not send a message to the access log
  1933. * if it is called between this point and where this field is
  1934. * set again after calling LOG_OPEN_APPEND.
  1935. */
  1936. if ( loginfo.log_access_maxnumlogs > 1 ) {
  1937. log = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  1938. log->l_ctime = loginfo.log_access_ctime;
  1939. log->l_size = f_size;
  1940. log_convert_time (log->l_ctime, tbuf, 1 /*short */);
  1941. PR_snprintf(newfile, sizeof(newfile), "%s.%s", loginfo.log_access_file, tbuf);
  1942. if (PR_Rename (loginfo.log_access_file, newfile) != PR_SUCCESS) {
  1943. loginfo.log_access_fdes = NULL;
  1944. if (!locked) LOG_ACCESS_UNLOCK_WRITE();
  1945. return LOG_UNABLE_TO_OPENFILE;
  1946. }
  1947. /* add the log to the chain */
  1948. log->l_next = loginfo.log_access_logchain;
  1949. loginfo.log_access_logchain = log;
  1950. loginfo.log_numof_access_logs++;
  1951. }
  1952. }
  1953. /* open a new log file */
  1954. if (! LOG_OPEN_APPEND(fp, loginfo.log_access_file, loginfo.log_access_mode)) {
  1955. int oserr = errno;
  1956. loginfo.log_access_fdes = NULL;
  1957. if (!locked) LOG_ACCESS_UNLOCK_WRITE();
  1958. LDAPDebug(LDAP_DEBUG_ANY, "access file open %s failed errno %d (%s)\n",
  1959. loginfo.log_access_file, oserr, slapd_system_strerror(oserr));
  1960. return LOG_UNABLE_TO_OPENFILE;
  1961. }
  1962. loginfo.log_access_fdes = fp;
  1963. if (logfile_state == LOGFILE_REOPENED) {
  1964. /* we have all the information */
  1965. if (!locked) LOG_ACCESS_UNLOCK_WRITE( );
  1966. return LOG_SUCCESS;
  1967. }
  1968. loginfo.log_access_state |= LOGGING_NEED_TITLE;
  1969. if (! LOG_OPEN_WRITE(fpinfo, loginfo.log_accessinfo_file, loginfo.log_access_mode)) {
  1970. int oserr = errno;
  1971. if (!locked) LOG_ACCESS_UNLOCK_WRITE();
  1972. LDAPDebug( LDAP_DEBUG_ANY, "accessinfo file open %s failed errno %d (%s)\n",
  1973. loginfo.log_accessinfo_file,
  1974. oserr, slapd_system_strerror(oserr));
  1975. return LOG_UNABLE_TO_OPENFILE;
  1976. }
  1977. /* write the header in the log */
  1978. now = current_time();
  1979. log_convert_time (now, tbuf, 2 /* long */);
  1980. PR_snprintf (buffer,sizeof(buffer),"LOGINFO:Log file created at: %s (%lu)\n", tbuf, now);
  1981. LOG_WRITE(fpinfo, buffer, strlen(buffer), 0);
  1982. logp = loginfo.log_access_logchain;
  1983. while ( logp) {
  1984. log_convert_time (logp->l_ctime, tbuf, 1 /*short*/);
  1985. PR_snprintf(buffer, sizeof(buffer), "LOGINFO:%s%s.%s (%lu) (%"
  1986. NSPRI64 "d)\n", PREVLOGFILE, loginfo.log_access_file, tbuf,
  1987. logp->l_ctime, logp->l_size);
  1988. LOG_WRITE(fpinfo, buffer, strlen(buffer), 0);
  1989. logp = logp->l_next;
  1990. }
  1991. /* Close the info file. We need only when we need to rotate to the
  1992. ** next log file.
  1993. */
  1994. if (fpinfo) LOG_CLOSE(fpinfo);
  1995. /* This is now the current access log */
  1996. loginfo.log_access_ctime = now;
  1997. if (!locked) LOG_ACCESS_UNLOCK_WRITE( );
  1998. return LOG_SUCCESS;
  1999. }
  2000. /******************************************************************************
  2001. * log__needrotation
  2002. *
  2003. * Do we need to rotate the log file ?
  2004. * Find out based on rotation time and the max log size;
  2005. *
  2006. * Return:
  2007. * LOG_CONTINUE -- Use the same one
  2008. * LOG_ROTATE -- log need to be rotated
  2009. *
  2010. * Note:
  2011. * A READ LOCK is obtained.
  2012. ********************************************************************************/
  2013. #define LOG_SIZE_EXCEEDED 1
  2014. #define LOG_EXPIRED 2
  2015. static int
  2016. log__needrotation(LOGFD fp, int logtype)
  2017. {
  2018. time_t curr_time;
  2019. time_t log_createtime= 0;
  2020. time_t syncclock = 0;
  2021. int type = LOG_CONTINUE;
  2022. PRInt64 f_size = 0;
  2023. PRInt64 maxlogsize;
  2024. int nlogs;
  2025. int rotationtime_secs = -1;
  2026. int sync_enabled = 0, timeunit = 0;
  2027. if (fp == NULL) {
  2028. return LOG_ROTATE;
  2029. }
  2030. switch (logtype) {
  2031. case SLAPD_ACCESS_LOG:
  2032. nlogs = loginfo.log_access_maxnumlogs;
  2033. maxlogsize = loginfo.log_access_maxlogsize;
  2034. sync_enabled = loginfo.log_access_rotationsync_enabled;
  2035. syncclock = loginfo.log_access_rotationsyncclock;
  2036. timeunit = loginfo.log_access_rotationunit;
  2037. rotationtime_secs = loginfo.log_access_rotationtime_secs;
  2038. log_createtime = loginfo.log_access_ctime;
  2039. break;
  2040. case SLAPD_ERROR_LOG:
  2041. nlogs = loginfo.log_error_maxnumlogs;
  2042. maxlogsize = loginfo.log_error_maxlogsize;
  2043. sync_enabled = loginfo.log_error_rotationsync_enabled;
  2044. syncclock = loginfo.log_error_rotationsyncclock;
  2045. timeunit = loginfo.log_error_rotationunit;
  2046. rotationtime_secs = loginfo.log_error_rotationtime_secs;
  2047. log_createtime = loginfo.log_error_ctime;
  2048. break;
  2049. case SLAPD_AUDIT_LOG:
  2050. nlogs = loginfo.log_audit_maxnumlogs;
  2051. maxlogsize = loginfo.log_audit_maxlogsize;
  2052. sync_enabled = loginfo.log_audit_rotationsync_enabled;
  2053. syncclock = loginfo.log_audit_rotationsyncclock;
  2054. timeunit = loginfo.log_audit_rotationunit;
  2055. rotationtime_secs = loginfo.log_audit_rotationtime_secs;
  2056. log_createtime = loginfo.log_audit_ctime;
  2057. break;
  2058. default: /* error */
  2059. maxlogsize = -1;
  2060. nlogs = 1;
  2061. }
  2062. /* If we have one log then can't rotate at all */
  2063. if (nlogs == 1)
  2064. return LOG_CONTINUE;
  2065. if ((f_size = log__getfilesize(fp)) == -1) {
  2066. /* The next option is to rotate based on the rotation time */
  2067. f_size = 0;
  2068. }
  2069. /* If the log size is more than the limit, then it's time to rotate. */
  2070. if ((maxlogsize > 0) && (f_size >= maxlogsize)) {
  2071. type = LOG_SIZE_EXCEEDED;
  2072. goto log_rotate;
  2073. }
  2074. /* If rotation interval <= 0 then can't rotate by time */
  2075. if (rotationtime_secs <= 0)
  2076. return LOG_CONTINUE;
  2077. /*
  2078. ** If the log is older than the time allowed to be active,
  2079. ** then it's time to move on (i.e., rotate).
  2080. */
  2081. time (&curr_time);
  2082. if ( !sync_enabled || timeunit == LOG_UNIT_HOURS || timeunit == LOG_UNIT_MINS ) {
  2083. if (curr_time - log_createtime > rotationtime_secs) {
  2084. type = LOG_EXPIRED;
  2085. goto log_rotate;
  2086. }
  2087. } else if (curr_time > syncclock) {
  2088. type = LOG_EXPIRED;
  2089. goto log_rotate;
  2090. }
  2091. log_rotate:
  2092. /*
  2093. ** Don't send messages to the error log whilst we're rotating it.
  2094. ** This'll lead to a recursive call to the logging function, and
  2095. ** an assertion trying to relock the write lock.
  2096. */
  2097. if (logtype!=SLAPD_ERROR_LOG)
  2098. {
  2099. if (type == LOG_SIZE_EXCEEDED) {
  2100. LDAPDebug (LDAP_DEBUG_TRACE,
  2101. "LOGINFO:End of Log because size exceeded(Max:%"
  2102. NSPRI64 "d bytes) (Is:%" NSPRI64 "d bytes)\n",
  2103. maxlogsize, f_size, 0);
  2104. } else if ( type == LOG_EXPIRED) {
  2105. LDAPDebug(LDAP_DEBUG_TRACE,
  2106. "LOGINFO:End of Log because time exceeded(Max:%d secs) (Is:%ld secs)\n",
  2107. rotationtime_secs, curr_time - log_createtime,0);
  2108. }
  2109. }
  2110. return (type == LOG_CONTINUE) ? LOG_CONTINUE : LOG_ROTATE;
  2111. }
  2112. /******************************************************************************
  2113. * log__delete_access_logfile
  2114. *
  2115. * Do we need to delete a logfile. Find out if we need to delete the log
  2116. * file based on expiration time, max diskspace, and minfreespace.
  2117. * Delete the file if we need to.
  2118. *
  2119. * Assumption: A WRITE lock has been acquired for the ACCESS
  2120. ******************************************************************************/
  2121. static int
  2122. log__delete_access_logfile()
  2123. {
  2124. struct logfileinfo *logp = NULL;
  2125. struct logfileinfo *delete_logp = NULL;
  2126. struct logfileinfo *p_delete_logp = NULL;
  2127. struct logfileinfo *prev_logp = NULL;
  2128. PRInt64 total_size=0;
  2129. time_t cur_time;
  2130. PRInt64 f_size;
  2131. int numoflogs=loginfo.log_numof_access_logs;
  2132. int rv = 0;
  2133. char *logstr;
  2134. char buffer[BUFSIZ];
  2135. char tbuf[TBUFSIZE];
  2136. /* If we have only one log, then will delete this one */
  2137. if (loginfo.log_access_maxnumlogs == 1) {
  2138. LOG_CLOSE(loginfo.log_access_fdes);
  2139. loginfo.log_access_fdes = NULL;
  2140. PR_snprintf (buffer, sizeof(buffer), "%s", loginfo.log_access_file);
  2141. if (PR_Delete(buffer) != PR_SUCCESS) {
  2142. LDAPDebug(LDAP_DEBUG_TRACE,
  2143. "LOGINFO:Unable to remove file:%s\n", loginfo.log_access_file,0,0);
  2144. }
  2145. /* Delete the rotation file also. */
  2146. PR_snprintf (buffer, sizeof(buffer), "%s.rotationinfo", loginfo.log_access_file);
  2147. if (PR_Delete(buffer) != PR_SUCCESS) {
  2148. LDAPDebug(LDAP_DEBUG_TRACE,
  2149. "LOGINFO:Unable to remove file:%s.rotationinfo\n", loginfo.log_access_file,0,0);
  2150. }
  2151. return 0;
  2152. }
  2153. /* If we have already the maximum number of log files, we
  2154. ** have to delete one any how.
  2155. */
  2156. if (++numoflogs > loginfo.log_access_maxnumlogs) {
  2157. logstr = "Exceeded max number of logs allowed";
  2158. goto delete_logfile;
  2159. }
  2160. /* Now check based on the maxdiskspace */
  2161. if (loginfo.log_access_maxdiskspace > 0) {
  2162. logp = loginfo.log_access_logchain;
  2163. while (logp) {
  2164. total_size += logp->l_size;
  2165. logp = logp->l_next;
  2166. }
  2167. if ((f_size = log__getfilesize(loginfo.log_access_fdes)) == -1) {
  2168. /* then just assume the max size */
  2169. total_size += loginfo.log_access_maxlogsize;
  2170. } else {
  2171. total_size += f_size;
  2172. }
  2173. /* If we have exceeded the max disk space or we have less than the
  2174. ** minimum, then we have to delete a file.
  2175. */
  2176. if (total_size >= loginfo.log_access_maxdiskspace) {
  2177. logstr = "exceeded maximum log disk space";
  2178. goto delete_logfile;
  2179. }
  2180. }
  2181. /* Now check based on the free space */
  2182. if ( loginfo.log_access_minfreespace > 0) {
  2183. rv = log__enough_freespace(loginfo.log_access_file);
  2184. if ( rv == 0) {
  2185. /* Not enough free space */
  2186. logstr = "Not enough free disk space";
  2187. goto delete_logfile;
  2188. }
  2189. }
  2190. /* Now check based on the expiration time */
  2191. if ( loginfo.log_access_exptime_secs > 0 ) {
  2192. /* is the file old enough */
  2193. time (&cur_time);
  2194. prev_logp = logp = loginfo.log_access_logchain;
  2195. while (logp) {
  2196. if ((cur_time - logp->l_ctime) > loginfo.log_access_exptime_secs) {
  2197. delete_logp = logp;
  2198. p_delete_logp = prev_logp;
  2199. logstr = "The file is older than the log expiration time";
  2200. goto delete_logfile;
  2201. }
  2202. prev_logp = logp;
  2203. logp = logp->l_next;
  2204. }
  2205. }
  2206. /* No log files to delete */
  2207. return 0;
  2208. delete_logfile:
  2209. if (delete_logp == NULL) {
  2210. time_t oldest;
  2211. time(&oldest);
  2212. prev_logp = logp = loginfo.log_access_logchain;
  2213. while (logp) {
  2214. if (logp->l_ctime <= oldest) {
  2215. oldest = logp->l_ctime;
  2216. delete_logp = logp;
  2217. p_delete_logp = prev_logp;
  2218. }
  2219. prev_logp = logp;
  2220. logp = logp->l_next;
  2221. }
  2222. /* We might face this case if we have only one log file and
  2223. ** trying to delete it because of deletion requirement.
  2224. */
  2225. if (!delete_logp) {
  2226. return 0;
  2227. }
  2228. }
  2229. if (p_delete_logp == delete_logp) {
  2230. /* then we are deleteing the first one */
  2231. loginfo.log_access_logchain = delete_logp->l_next;
  2232. } else {
  2233. p_delete_logp->l_next = delete_logp->l_next;
  2234. }
  2235. /* Delete the access file */
  2236. log_convert_time (delete_logp->l_ctime, tbuf, 1 /*short */);
  2237. PR_snprintf (buffer, sizeof(buffer), "%s.%s", loginfo.log_access_file, tbuf);
  2238. if (PR_Delete(buffer) != PR_SUCCESS) {
  2239. LDAPDebug(LDAP_DEBUG_TRACE,
  2240. "LOGINFO:Unable to remove file:%s.%s\n",
  2241. loginfo.log_access_file,tbuf,0);
  2242. } else {
  2243. LDAPDebug(LDAP_DEBUG_TRACE,
  2244. "LOGINFO:Removed file:%s.%s because of (%s)\n",
  2245. loginfo.log_access_file, tbuf,
  2246. logstr);
  2247. }
  2248. slapi_ch_free((void**)&delete_logp);
  2249. loginfo.log_numof_access_logs--;
  2250. return 1;
  2251. }
  2252. #define ERRORSLOG 1
  2253. #define ACCESSLOG 2
  2254. #define AUDITLOG 3
  2255. static int
  2256. log__fix_rotationinfof(char *pathname)
  2257. {
  2258. char *logsdir = NULL;
  2259. time_t now;
  2260. PRDir *dirptr = NULL;
  2261. PRDirEntry *dirent = NULL;
  2262. PRDirFlags dirflags = PR_SKIP_BOTH & PR_SKIP_HIDDEN;
  2263. char *log_type = NULL;
  2264. int log_type_id;
  2265. int rval = LOG_ERROR;
  2266. char *p;
  2267. /* rotation info file is broken; can't trust the contents */
  2268. time (&now);
  2269. loginfo.log_error_ctime = now;
  2270. logsdir = slapi_ch_strdup(pathname);
  2271. p = strrchr(logsdir, _PSEP);
  2272. if (NULL == p) /* pathname is not path/filename.rotationinfo; do nothing */
  2273. goto done;
  2274. *p = '\0';
  2275. log_type = ++p;
  2276. p = strchr(log_type, '.');
  2277. if (NULL == p) /* file is not rotationinfo; do nothing */
  2278. goto done;
  2279. *p = '\0';
  2280. if (0 == strcmp(log_type, "errors"))
  2281. log_type_id = ERRORSLOG;
  2282. else if (0 == strcmp(log_type, "access"))
  2283. log_type_id = ACCESSLOG;
  2284. else if (0 == strcmp(log_type, "audit"))
  2285. log_type_id = AUDITLOG;
  2286. else
  2287. goto done; /* file is not errors nor access nor audit; do nothing */
  2288. if (!(dirptr = PR_OpenDir(logsdir)))
  2289. goto done;
  2290. switch (log_type_id) {
  2291. case ERRORSLOG:
  2292. loginfo.log_numof_error_logs = 0;
  2293. loginfo.log_error_logchain = NULL;
  2294. break;
  2295. case ACCESSLOG:
  2296. loginfo.log_numof_access_logs = 0;
  2297. loginfo.log_access_logchain = NULL;
  2298. break;
  2299. case AUDITLOG:
  2300. loginfo.log_numof_audit_logs = 0;
  2301. loginfo.log_audit_logchain = NULL;
  2302. break;
  2303. }
  2304. /* read the directory entries into a linked list */
  2305. for (dirent = PR_ReadDir(dirptr, dirflags); dirent ;
  2306. dirent = PR_ReadDir(dirptr, dirflags)) {
  2307. if (0 == strcmp(log_type, dirent->name)) {
  2308. switch (log_type_id) {
  2309. case ERRORSLOG:
  2310. loginfo.log_numof_error_logs++;
  2311. break;
  2312. case ACCESSLOG:
  2313. loginfo.log_numof_access_logs++;
  2314. break;
  2315. case AUDITLOG:
  2316. loginfo.log_numof_audit_logs++;
  2317. break;
  2318. }
  2319. } else if (0 == strncmp(log_type, dirent->name, strlen(log_type)) &&
  2320. (p = strrchr(dirent->name, '.')) != NULL &&
  2321. 15 == strlen(++p) &&
  2322. NULL != strchr(p, '-')) { /* e.g., errors.20051123-165135 */
  2323. struct logfileinfo *logp;
  2324. char *q;
  2325. int ignoreit = 0;
  2326. for (q = p; q && *q; q++) {
  2327. if (*q != '-' && !isdigit(*q))
  2328. ignoreit = 1;
  2329. }
  2330. if (ignoreit)
  2331. continue;
  2332. logp = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  2333. logp->l_ctime = log_reverse_convert_time(p);
  2334. switch (log_type_id) {
  2335. case ERRORSLOG:
  2336. logp->l_size = loginfo.log_error_maxlogsize; /* dummy */
  2337. logp->l_next = loginfo.log_error_logchain;
  2338. loginfo.log_error_logchain = logp;
  2339. loginfo.log_numof_error_logs++;
  2340. break;
  2341. case ACCESSLOG:
  2342. logp->l_size = loginfo.log_access_maxlogsize;
  2343. logp->l_next = loginfo.log_access_logchain;
  2344. loginfo.log_access_logchain = logp;
  2345. loginfo.log_numof_access_logs++;
  2346. break;
  2347. case AUDITLOG:
  2348. logp->l_size =loginfo.log_audit_maxlogsize;
  2349. logp->l_next = loginfo.log_audit_logchain;
  2350. loginfo.log_audit_logchain = logp;
  2351. loginfo.log_numof_audit_logs++;
  2352. break;
  2353. }
  2354. }
  2355. }
  2356. rval = LOG_SUCCESS;
  2357. done:
  2358. if (NULL != dirptr)
  2359. PR_CloseDir(dirptr);
  2360. slapi_ch_free_string(&logsdir);
  2361. return rval;
  2362. }
  2363. #undef ERRORSLOG
  2364. #undef ACCESSLOG
  2365. #undef AUDITLOG
  2366. /******************************************************************************
  2367. * log__access_rotationinfof
  2368. *
  2369. * Try to open the log file. If we have one already, then try to read the
  2370. * header and update the information.
  2371. *
  2372. * Assumption: Lock has been acquired already
  2373. ******************************************************************************/
  2374. static int
  2375. log__access_rotationinfof(char *pathname)
  2376. {
  2377. long f_ctime;
  2378. PRInt64 f_size;
  2379. int main_log = 1;
  2380. time_t now;
  2381. FILE *fp;
  2382. int rval, logfile_type = LOGFILE_REOPENED;
  2383. /*
  2384. ** Okay -- I confess, we want to use NSPR calls but I want to
  2385. ** use fgets and not use PR_Read() and implement a complicated
  2386. ** parsing module. Since this will be called only during the startup
  2387. ** and never aftre that, we can live by it.
  2388. */
  2389. if ((fp = fopen (pathname, "r")) == NULL) {
  2390. return LOGFILE_NEW;
  2391. }
  2392. loginfo.log_numof_access_logs = 0;
  2393. /*
  2394. ** We have reopened the log access file. Now we need to read the
  2395. ** log file info and update the values.
  2396. */
  2397. while ((rval = log__extract_logheader(fp, &f_ctime, &f_size)) == LOG_CONTINUE) {
  2398. /* first we would get the main log info */
  2399. if (f_ctime == 0 && f_size == 0)
  2400. continue;
  2401. time (&now);
  2402. if (main_log) {
  2403. if (f_ctime > 0L)
  2404. loginfo.log_access_ctime = f_ctime;
  2405. else {
  2406. loginfo.log_access_ctime = now;
  2407. }
  2408. main_log = 0;
  2409. } else {
  2410. struct logfileinfo *logp;
  2411. logp = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  2412. if (f_ctime > 0L)
  2413. logp->l_ctime = f_ctime;
  2414. else
  2415. logp->l_ctime = now;
  2416. if (f_size > 0)
  2417. logp->l_size = f_size;
  2418. else {
  2419. /* make it the max log size */
  2420. logp->l_size = loginfo.log_access_maxlogsize;
  2421. }
  2422. logp->l_next = loginfo.log_access_logchain;
  2423. loginfo.log_access_logchain = logp;
  2424. }
  2425. loginfo.log_numof_access_logs++;
  2426. }
  2427. if (LOG_DONE == rval)
  2428. rval = log__check_prevlogs(fp, pathname);
  2429. fclose (fp);
  2430. if (LOG_ERROR == rval)
  2431. if (LOG_SUCCESS == log__fix_rotationinfof(pathname))
  2432. logfile_type = LOGFILE_NEW;
  2433. /* Check if there is a rotation overdue */
  2434. if (loginfo.log_access_rotationsync_enabled &&
  2435. loginfo.log_access_rotationunit != LOG_UNIT_HOURS &&
  2436. loginfo.log_access_rotationunit != LOG_UNIT_MINS &&
  2437. loginfo.log_access_ctime < loginfo.log_access_rotationsyncclock - PR_ABS(loginfo.log_access_rotationtime_secs)) {
  2438. loginfo.log_access_rotationsyncclock -= PR_ABS(loginfo.log_access_rotationtime_secs);
  2439. }
  2440. return logfile_type;
  2441. }
  2442. /*
  2443. * log__check_prevlogs
  2444. *
  2445. * check if a given prev log file (e.g., /opt/fedora-ds/slapd-fe/logs/errors.20051201-101347)
  2446. * is found in the rotationinfo file.
  2447. */
  2448. static int
  2449. log__check_prevlogs (FILE *fp, char *pathname)
  2450. {
  2451. char buf[BUFSIZ], *p;
  2452. char *logsdir = NULL;
  2453. int rval = LOG_CONTINUE;
  2454. char *log_type = NULL;
  2455. PRDir *dirptr = NULL;
  2456. PRDirEntry *dirent = NULL;
  2457. PRDirFlags dirflags = PR_SKIP_BOTH & PR_SKIP_HIDDEN;
  2458. logsdir = slapi_ch_strdup(pathname);
  2459. p = strrchr(logsdir, _PSEP);
  2460. if (NULL == p) /* pathname is not path/filename.rotationinfo; do nothing */
  2461. goto done;
  2462. *p = '\0';
  2463. log_type = ++p;
  2464. p = strchr(log_type, '.');
  2465. if (NULL == p) /* file is not rotationinfo; do nothing */
  2466. goto done;
  2467. *p = '\0';
  2468. if (0 != strcmp(log_type, "errors") &&
  2469. 0 != strcmp(log_type, "access") &&
  2470. 0 != strcmp(log_type, "audit"))
  2471. goto done; /* file is not errors nor access nor audit; do nothing */
  2472. if (!(dirptr = PR_OpenDir(logsdir)))
  2473. goto done;
  2474. for (dirent = PR_ReadDir(dirptr, dirflags); dirent ;
  2475. dirent = PR_ReadDir(dirptr, dirflags)) {
  2476. if (0 == strncmp(log_type, dirent->name, strlen(log_type)) &&
  2477. (p = strrchr(dirent->name, '.')) != NULL &&
  2478. 15 == strlen(++p) &&
  2479. NULL != strchr(p, '-')) { /* e.g., errors.20051123-165135 */
  2480. char *q;
  2481. int ignoreit = 0;
  2482. for (q = p; q && *q; q++) {
  2483. if (*q != '-' && !isdigit(*q))
  2484. ignoreit = 1;
  2485. }
  2486. if (ignoreit)
  2487. continue;
  2488. fseek(fp, 0 ,SEEK_SET);
  2489. buf[BUFSIZ-1] = '\0';
  2490. while (fgets(buf, BUFSIZ - 1, fp)) {
  2491. if (strstr(buf, dirent->name)) {
  2492. rval = LOG_CONTINUE; /* found in .rotationinfo */
  2493. continue;
  2494. }
  2495. }
  2496. rval = LOG_ERROR; /* not found in .rotationinfo */
  2497. break;
  2498. }
  2499. }
  2500. done:
  2501. if (NULL != dirptr)
  2502. PR_CloseDir(dirptr);
  2503. slapi_ch_free_string(&logsdir);
  2504. return rval;
  2505. }
  2506. /******************************************************************************
  2507. * log__extract_logheader
  2508. *
  2509. * Extract each LOGINFO heder line. From there extract the time and
  2510. * size info of all the old log files.
  2511. ******************************************************************************/
  2512. static int
  2513. log__extract_logheader (FILE *fp, long *f_ctime, PRInt64 *f_size)
  2514. {
  2515. char buf[BUFSIZ];
  2516. char *p, *s, *next;
  2517. if (NULL == f_ctime || NULL == f_size) {
  2518. return LOG_ERROR;
  2519. }
  2520. *f_ctime = 0L;
  2521. *f_size = 0L;
  2522. if ( fp == NULL)
  2523. return LOG_ERROR;
  2524. buf[BUFSIZ-1] = '\0'; /* for safety */
  2525. if (fgets(buf, BUFSIZ - 1, fp) == NULL) {
  2526. return LOG_DONE;
  2527. }
  2528. if ((p=strstr(buf, "LOGINFO")) == NULL) {
  2529. return LOG_ERROR;
  2530. }
  2531. s = p;
  2532. if ((p = strchr(p, '(')) == NULL) {
  2533. return LOG_CONTINUE;
  2534. }
  2535. if ((next= strchr(p, ')')) == NULL) {
  2536. return LOG_CONTINUE;
  2537. }
  2538. p++;
  2539. s = next;
  2540. next++;
  2541. *s = '\0';
  2542. /* Now p must hold the ctime value */
  2543. *f_ctime = strtol(p, (char **)NULL, 0);
  2544. if ((p = strchr(next, '(')) == NULL) {
  2545. /* that's fine -- it means we have no size info */
  2546. *f_size = 0L;
  2547. return LOG_CONTINUE;
  2548. }
  2549. if ((next= strchr(p, ')')) == NULL) {
  2550. return LOG_CONTINUE;
  2551. }
  2552. p++;
  2553. *next = '\0';
  2554. /* Now p must hold the size value */
  2555. *f_size = strtoll(p, (char **)NULL, 0);
  2556. /* check if the Previous Log file really exists */
  2557. if ((p = strstr(buf, PREVLOGFILE)) != NULL) {
  2558. p += strlen(PREVLOGFILE);
  2559. s = strchr(p, ' ');
  2560. if (NULL == s) {
  2561. s = strchr(p, '(');
  2562. if (NULL != s) {
  2563. *s = '\0';
  2564. }
  2565. } else {
  2566. *s = '\0';
  2567. }
  2568. if (PR_SUCCESS != PR_Access(p, PR_ACCESS_EXISTS)) {
  2569. return LOG_ERROR;
  2570. }
  2571. }
  2572. return LOG_CONTINUE;
  2573. }
  2574. /******************************************************************************
  2575. * log__getfilesize
  2576. * Get the file size
  2577. *
  2578. * Assumption: Lock has been acquired already.
  2579. ******************************************************************************/
  2580. /* this kinda has to be diff't on each platform :( */
  2581. /* using an int implies that all logfiles will be under 2G. this is
  2582. * probably a safe assumption for now.
  2583. */
  2584. #ifdef XP_WIN32
  2585. static PRInt64
  2586. log__getfilesize(LOGFD fp)
  2587. {
  2588. struct stat info;
  2589. int rv;
  2590. if ((rv = fstat(fileno(fp), &info)) != 0) {
  2591. return -1;
  2592. }
  2593. return (PRInt64)info.st_size;
  2594. }
  2595. #else
  2596. static PRInt64
  2597. log__getfilesize(LOGFD fp)
  2598. {
  2599. PRFileInfo info;
  2600. if (PR_GetOpenFileInfo (fp, &info) == PR_FAILURE) {
  2601. return -1;
  2602. }
  2603. return (PRInt64)info.size; /* type of size is off_t */
  2604. }
  2605. #endif
  2606. /******************************************************************************
  2607. * log__enough_freespace
  2608. *
  2609. * Returns:
  2610. * 1 - we have enough space
  2611. * 0 - No the avialable space is less than recomended
  2612. * Assumption: Lock has been acquired already.
  2613. ******************************************************************************/
  2614. static int
  2615. log__enough_freespace(char *path)
  2616. {
  2617. #ifdef _WIN32
  2618. DWORD sectorsPerCluster, bytesPerSector, freeClusters, totalClusters;
  2619. char rootpath[4];
  2620. #else
  2621. #ifdef LINUX
  2622. struct statfs buf;
  2623. #else
  2624. struct statvfs buf;
  2625. #endif /* LINUX */
  2626. #endif
  2627. PRInt64 freeBytes;
  2628. PRInt64 tmpval;
  2629. #ifdef _WIN32
  2630. strncpy(rootpath, path, 3);
  2631. rootpath[3] = '\0';
  2632. /* we should consider using GetDiskFreeSpaceEx here someday */
  2633. if ( !GetDiskFreeSpace(rootpath, &sectorsPerCluster, &bytesPerSector,
  2634. &freeClusters, &totalClusters)) {
  2635. LDAPDebug(LDAP_DEBUG_ANY,
  2636. "log__enough_freespace: Unable to get the free space\n",0,0,0);
  2637. return 1;
  2638. } else {
  2639. LL_UI2L(freeBytes, freeClusters);
  2640. LL_UI2L(tmpval, sectorsPerCluster);
  2641. LL_MUL(freeBytes, freeBytes, tmpval);
  2642. LL_UI2L(tmpval, bytesPerSector);
  2643. LL_MUL(freeBytes, freeBytes, tmpval);
  2644. /* freeBytes = freeClusters * sectorsPerCluster * bytesPerSector; */
  2645. }
  2646. #else
  2647. #ifdef LINUX
  2648. if (statfs(path, &buf) == -1)
  2649. #else
  2650. if (statvfs(path, &buf) == -1)
  2651. #endif
  2652. {
  2653. char buffer[BUFSIZ];
  2654. PR_snprintf(buffer, sizeof(buffer),
  2655. "log__enough_freespace: Unable to get the free space (errno:%d)\n",
  2656. errno);
  2657. /* This function could be called in the ERROR WRITE LOCK,
  2658. * which causes the self deadlock if you call LDAPDebug for logging.
  2659. * Thus, instead of LDAPDebug, call log__error_emergency with locked == 1. */
  2660. log__error_emergency(buffer, 0, 1);
  2661. return 1;
  2662. } else {
  2663. LL_UI2L(freeBytes, buf.f_bavail);
  2664. LL_UI2L(tmpval, buf.f_bsize);
  2665. LL_MUL(freeBytes, freeBytes, tmpval);
  2666. /* freeBytes = buf.f_bavail * buf.f_bsize; */
  2667. }
  2668. #endif
  2669. LL_UI2L(tmpval, loginfo.log_access_minfreespace);
  2670. if (LL_UCMP(freeBytes, <, tmpval)) {
  2671. /* if (freeBytes < loginfo.log_access_minfreespace) { */
  2672. return 0;
  2673. }
  2674. return 1;
  2675. }
  2676. /******************************************************************************
  2677. * log__getaccesslist
  2678. * Update the previous access files in the slapdFrontendConfig_t.
  2679. * Returns:
  2680. * num > 1 -- how many are there
  2681. * 0 -- otherwise
  2682. ******************************************************************************/
  2683. char **
  2684. log_get_loglist(int logtype)
  2685. {
  2686. char **list=NULL;
  2687. int num, i;
  2688. LogFileInfo *logp = NULL;
  2689. char buf[BUFSIZ];
  2690. char tbuf[TBUFSIZE];
  2691. char *file;
  2692. switch (logtype) {
  2693. case SLAPD_ACCESS_LOG:
  2694. LOG_ACCESS_LOCK_READ( );
  2695. num = loginfo.log_numof_access_logs;
  2696. logp = loginfo.log_access_logchain;
  2697. file = loginfo.log_access_file;
  2698. break;
  2699. case SLAPD_ERROR_LOG:
  2700. LOG_ERROR_LOCK_READ( );
  2701. num = loginfo.log_numof_error_logs;
  2702. logp = loginfo.log_error_logchain;
  2703. file = loginfo.log_error_file;
  2704. break;
  2705. case SLAPD_AUDIT_LOG:
  2706. LOG_AUDIT_LOCK_READ( );
  2707. num = loginfo.log_numof_audit_logs;
  2708. logp = loginfo.log_audit_logchain;
  2709. file = loginfo.log_audit_file;
  2710. break;
  2711. default:
  2712. return NULL;
  2713. }
  2714. list = (char **) slapi_ch_calloc(1, (num + 1) * sizeof(char *));
  2715. i = 0;
  2716. while (logp) {
  2717. log_convert_time (logp->l_ctime, tbuf, 1 /*short */);
  2718. PR_snprintf(buf, sizeof(buf), "%s.%s", file, tbuf);
  2719. list[i] = slapi_ch_strdup(buf);
  2720. i++;
  2721. if (i == num) { /* mismatch b/w num and logchain;
  2722. cut the chain and save the process */
  2723. break;
  2724. }
  2725. logp = logp->l_next;
  2726. }
  2727. list[i] = NULL;
  2728. switch (logtype) {
  2729. case SLAPD_ACCESS_LOG:
  2730. LOG_ACCESS_UNLOCK_READ();
  2731. break;
  2732. case SLAPD_ERROR_LOG:
  2733. LOG_ERROR_UNLOCK_READ();
  2734. break;
  2735. case SLAPD_AUDIT_LOG:
  2736. LOG_AUDIT_UNLOCK_READ();
  2737. break;
  2738. }
  2739. return list;
  2740. }
  2741. /******************************************************************************
  2742. * log__delete_error_logfile
  2743. *
  2744. * Do we need to delete a logfile. Find out if we need to delete the log
  2745. * file based on expiration time, max diskspace, and minfreespace.
  2746. * Delete the file if we need to.
  2747. *
  2748. * Assumption: A WRITE lock has been acquired for the error log.
  2749. ******************************************************************************/
  2750. static int
  2751. log__delete_error_logfile(int locked)
  2752. {
  2753. struct logfileinfo *logp = NULL;
  2754. struct logfileinfo *delete_logp = NULL;
  2755. struct logfileinfo *p_delete_logp = NULL;
  2756. struct logfileinfo *prev_logp = NULL;
  2757. PRInt64 total_size=0;
  2758. time_t cur_time;
  2759. PRInt64 f_size;
  2760. int numoflogs=loginfo.log_numof_error_logs;
  2761. int rv = 0;
  2762. char *logstr;
  2763. char buffer[BUFSIZ];
  2764. char tbuf[TBUFSIZE];
  2765. /* If we have only one log, then will delete this one */
  2766. if (loginfo.log_error_maxnumlogs == 1) {
  2767. LOG_CLOSE(loginfo.log_error_fdes);
  2768. loginfo.log_error_fdes = NULL;
  2769. PR_snprintf (buffer, sizeof(buffer), "%s", loginfo.log_error_file);
  2770. if (PR_Delete(buffer) != PR_SUCCESS) {
  2771. if (!locked) {
  2772. /* if locked, we should not call LDAPDebug,
  2773. which tries to get a lock internally. */
  2774. LDAPDebug(LDAP_DEBUG_TRACE,
  2775. "LOGINFO:Unable to remove file:%s\n", loginfo.log_error_file,0,0);
  2776. }
  2777. }
  2778. /* Delete the rotation file also. */
  2779. PR_snprintf (buffer, sizeof(buffer), "%s.rotationinfo", loginfo.log_error_file);
  2780. if (PR_Delete(buffer) != PR_SUCCESS) {
  2781. if (!locked) {
  2782. /* if locked, we should not call LDAPDebug,
  2783. which tries to get a lock internally. */
  2784. LDAPDebug(LDAP_DEBUG_TRACE,
  2785. "LOGINFO:Unable to remove file:%s.rotationinfo\n",
  2786. loginfo.log_error_file,0,0);
  2787. }
  2788. }
  2789. return 0;
  2790. }
  2791. /* If we have already the maximum number of log files, we
  2792. ** have to delete one any how.
  2793. */
  2794. if (++numoflogs > loginfo.log_error_maxnumlogs) {
  2795. logstr = "Exceeded max number of logs allowed";
  2796. syslog(LOG_ERR, "%s\n", logstr);
  2797. goto delete_logfile;
  2798. }
  2799. /* Now check based on the maxdiskspace */
  2800. if (loginfo.log_error_maxdiskspace > 0) {
  2801. logp = loginfo.log_error_logchain;
  2802. while (logp) {
  2803. total_size += logp->l_size;
  2804. logp = logp->l_next;
  2805. }
  2806. if ((f_size = log__getfilesize(loginfo.log_error_fdes)) == -1) {
  2807. /* then just assume the max size */
  2808. total_size += loginfo.log_error_maxlogsize;
  2809. } else {
  2810. total_size += f_size;
  2811. }
  2812. /* If we have exceeded the max disk space or we have less than the
  2813. ** minimum, then we have to delete a file.
  2814. */
  2815. if (total_size >= loginfo.log_error_maxdiskspace) {
  2816. logstr = "exceeded maximum log disk space";
  2817. goto delete_logfile;
  2818. }
  2819. }
  2820. /* Now check based on the free space */
  2821. if ( loginfo.log_error_minfreespace > 0) {
  2822. rv = log__enough_freespace(loginfo.log_error_file);
  2823. if ( rv == 0) {
  2824. /* Not enough free space */
  2825. logstr = "Not enough free disk space";
  2826. goto delete_logfile;
  2827. }
  2828. }
  2829. /* Now check based on the expiration time */
  2830. if ( loginfo.log_error_exptime_secs > 0 ) {
  2831. /* is the file old enough */
  2832. time (&cur_time);
  2833. prev_logp = logp = loginfo.log_error_logchain;
  2834. while (logp) {
  2835. if ((cur_time - logp->l_ctime) > loginfo.log_error_exptime_secs) {
  2836. delete_logp = logp;
  2837. p_delete_logp = prev_logp;
  2838. logstr = "The file is older than the log expiration time";
  2839. goto delete_logfile;
  2840. }
  2841. prev_logp = logp;
  2842. logp = logp->l_next;
  2843. }
  2844. }
  2845. /* No log files to delete */
  2846. return 0;
  2847. delete_logfile:
  2848. if (delete_logp == NULL) {
  2849. time_t oldest;
  2850. time(&oldest);
  2851. prev_logp = logp = loginfo.log_error_logchain;
  2852. while (logp) {
  2853. if (logp->l_ctime <= oldest) {
  2854. oldest = logp->l_ctime;
  2855. delete_logp = logp;
  2856. p_delete_logp = prev_logp;
  2857. }
  2858. prev_logp = logp;
  2859. logp = logp->l_next;
  2860. }
  2861. /* We might face this case if we have only one log file and
  2862. ** trying to delete it because of deletion requirement.
  2863. */
  2864. if (!delete_logp) {
  2865. return 0;
  2866. }
  2867. }
  2868. if (!locked) {
  2869. /* if locked, we should not call LDAPDebug,
  2870. which tries to get a lock internally. */
  2871. LDAPDebug(LDAP_DEBUG_TRACE,
  2872. "LOGINFO:Removing file:%s.%s because of (%s)\n",
  2873. loginfo.log_error_file, tbuf,
  2874. logstr);
  2875. }
  2876. if (p_delete_logp == delete_logp) {
  2877. /* then we are deleteing the first one */
  2878. loginfo.log_error_logchain = delete_logp->l_next;
  2879. } else {
  2880. p_delete_logp->l_next = delete_logp->l_next;
  2881. }
  2882. /* Delete the error file */
  2883. log_convert_time (delete_logp->l_ctime, tbuf, 1 /*short */);
  2884. PR_snprintf (buffer, sizeof(buffer), "%s.%s", loginfo.log_error_file, tbuf);
  2885. if (PR_Delete(buffer) != PR_SUCCESS) {
  2886. /* This function could be called in the ERROR WRITE LOCK,
  2887. * which causes the self deadlock if you call LDAPDebug for logging.
  2888. * Thus, instead of LDAPDebug, call log__error_emergency with locked == 1. */
  2889. PR_snprintf(buffer, sizeof(buffer), "LOGINFO:Unable to remove file:%s.%s\n",
  2890. loginfo.log_audit_file, tbuf);
  2891. log__error_emergency(buffer, 0, locked);
  2892. }
  2893. slapi_ch_free((void**)&delete_logp);
  2894. loginfo.log_numof_error_logs--;
  2895. return 1;
  2896. }
  2897. /******************************************************************************
  2898. * log__delete_audit_logfile
  2899. *
  2900. * Do we need to delete a logfile. Find out if we need to delete the log
  2901. * file based on expiration time, max diskspace, and minfreespace.
  2902. * Delete the file if we need to.
  2903. *
  2904. * Assumption: A WRITE lock has been acquired for the audit
  2905. ******************************************************************************/
  2906. static int
  2907. log__delete_audit_logfile()
  2908. {
  2909. struct logfileinfo *logp = NULL;
  2910. struct logfileinfo *delete_logp = NULL;
  2911. struct logfileinfo *p_delete_logp = NULL;
  2912. struct logfileinfo *prev_logp = NULL;
  2913. PRInt64 total_size=0;
  2914. time_t cur_time;
  2915. PRInt64 f_size;
  2916. int numoflogs=loginfo.log_numof_audit_logs;
  2917. int rv = 0;
  2918. char *logstr;
  2919. char buffer[BUFSIZ];
  2920. char tbuf[TBUFSIZE];
  2921. /* If we have only one log, then will delete this one */
  2922. if (loginfo.log_audit_maxnumlogs == 1) {
  2923. LOG_CLOSE(loginfo.log_audit_fdes);
  2924. loginfo.log_audit_fdes = NULL;
  2925. PR_snprintf(buffer, sizeof(buffer), "%s", loginfo.log_audit_file);
  2926. if (PR_Delete(buffer) != PR_SUCCESS) {
  2927. LDAPDebug(LDAP_DEBUG_TRACE,
  2928. "LOGINFO:Unable to remove file:%s\n", loginfo.log_audit_file,0,0);
  2929. }
  2930. /* Delete the rotation file also. */
  2931. PR_snprintf(buffer, sizeof(buffer), "%s.rotationinfo", loginfo.log_audit_file);
  2932. if (PR_Delete(buffer) != PR_SUCCESS) {
  2933. LDAPDebug(LDAP_DEBUG_TRACE,
  2934. "LOGINFO:Unable to remove file:%s.rotationinfo\n", loginfo.log_audit_file,0,0);
  2935. }
  2936. return 0;
  2937. }
  2938. /* If we have already the maximum number of log files, we
  2939. ** have to delete one any how.
  2940. */
  2941. if (++numoflogs > loginfo.log_audit_maxnumlogs) {
  2942. logstr = "Delete Error Log File: Exceeded max number of logs allowed";
  2943. goto delete_logfile;
  2944. }
  2945. /* Now check based on the maxdiskspace */
  2946. if (loginfo.log_audit_maxdiskspace > 0) {
  2947. logp = loginfo.log_audit_logchain;
  2948. while (logp) {
  2949. total_size += logp->l_size;
  2950. logp = logp->l_next;
  2951. }
  2952. if ((f_size = log__getfilesize(loginfo.log_audit_fdes)) == -1) {
  2953. /* then just assume the max size */
  2954. total_size += loginfo.log_audit_maxlogsize;
  2955. } else {
  2956. total_size += f_size;
  2957. }
  2958. /* If we have exceeded the max disk space or we have less than the
  2959. ** minimum, then we have to delete a file.
  2960. */
  2961. if (total_size >= loginfo.log_audit_maxdiskspace) {
  2962. logstr = "exceeded maximum log disk space";
  2963. goto delete_logfile;
  2964. }
  2965. }
  2966. /* Now check based on the free space */
  2967. if ( loginfo.log_audit_minfreespace > 0) {
  2968. rv = log__enough_freespace(loginfo.log_audit_file);
  2969. if ( rv == 0) {
  2970. /* Not enough free space */
  2971. logstr = "Not enough free disk space";
  2972. goto delete_logfile;
  2973. }
  2974. }
  2975. /* Now check based on the expiration time */
  2976. if ( loginfo.log_audit_exptime_secs > 0 ) {
  2977. /* is the file old enough */
  2978. time (&cur_time);
  2979. prev_logp = logp = loginfo.log_audit_logchain;
  2980. while (logp) {
  2981. if ((cur_time - logp->l_ctime) > loginfo.log_audit_exptime_secs) {
  2982. delete_logp = logp;
  2983. p_delete_logp = prev_logp;
  2984. logstr = "The file is older than the log expiration time";
  2985. goto delete_logfile;
  2986. }
  2987. prev_logp = logp;
  2988. logp = logp->l_next;
  2989. }
  2990. }
  2991. /* No log files to delete */
  2992. return 0;
  2993. delete_logfile:
  2994. if (delete_logp == NULL) {
  2995. time_t oldest;
  2996. time(&oldest);
  2997. prev_logp = logp = loginfo.log_audit_logchain;
  2998. while (logp) {
  2999. if (logp->l_ctime <= oldest) {
  3000. oldest = logp->l_ctime;
  3001. delete_logp = logp;
  3002. p_delete_logp = prev_logp;
  3003. }
  3004. prev_logp = logp;
  3005. logp = logp->l_next;
  3006. }
  3007. /* We might face this case if we have only one log file and
  3008. ** trying to delete it because of deletion requirement.
  3009. */
  3010. if (!delete_logp) {
  3011. return 0;
  3012. }
  3013. }
  3014. if (p_delete_logp == delete_logp) {
  3015. /* then we are deleteing the first one */
  3016. loginfo.log_audit_logchain = delete_logp->l_next;
  3017. } else {
  3018. p_delete_logp->l_next = delete_logp->l_next;
  3019. }
  3020. /* Delete the audit file */
  3021. log_convert_time (delete_logp->l_ctime, tbuf, 1 /*short */);
  3022. PR_snprintf(buffer, sizeof(buffer), "%s.%s", loginfo.log_audit_file, tbuf );
  3023. if (PR_Delete(buffer) != PR_SUCCESS) {
  3024. LDAPDebug(LDAP_DEBUG_TRACE,
  3025. "LOGINFO:Unable to remove file:%s.%s\n",
  3026. loginfo.log_audit_file, tbuf,0);
  3027. } else {
  3028. LDAPDebug(LDAP_DEBUG_TRACE,
  3029. "LOGINFO:Removed file:%s.%s because of (%s)\n",
  3030. loginfo.log_audit_file, tbuf,
  3031. logstr);
  3032. }
  3033. slapi_ch_free((void**)&delete_logp);
  3034. loginfo.log_numof_audit_logs--;
  3035. return 1;
  3036. }
  3037. /******************************************************************************
  3038. * log__error_rotationinfof
  3039. *
  3040. * Try to open the log file. If we have one already, then try to read the
  3041. * header and update the information.
  3042. *
  3043. * Assumption: Lock has been acquired already
  3044. ******************************************************************************/
  3045. static int
  3046. log__error_rotationinfof( char *pathname)
  3047. {
  3048. long f_ctime;
  3049. PRInt64 f_size;
  3050. int main_log = 1;
  3051. time_t now;
  3052. FILE *fp;
  3053. int rval, logfile_type = LOGFILE_REOPENED;
  3054. /*
  3055. ** Okay -- I confess, we want to use NSPR calls but I want to
  3056. ** use fgets and not use PR_Read() and implement a complicated
  3057. ** parsing module. Since this will be called only during the startup
  3058. ** and never aftre that, we can live by it.
  3059. */
  3060. if ((fp = fopen (pathname, "r")) == NULL) {
  3061. return LOGFILE_NEW;
  3062. }
  3063. loginfo.log_numof_error_logs = 0;
  3064. /*
  3065. ** We have reopened the log error file. Now we need to read the
  3066. ** log file info and update the values.
  3067. */
  3068. while ((rval = log__extract_logheader(fp, &f_ctime, &f_size)) == LOG_CONTINUE) {
  3069. /* first we would get the main log info */
  3070. if (f_ctime == 0 && f_size == 0)
  3071. continue;
  3072. time (&now);
  3073. if (main_log) {
  3074. if (f_ctime > 0L)
  3075. loginfo.log_error_ctime = f_ctime;
  3076. else {
  3077. loginfo.log_error_ctime = now;
  3078. }
  3079. main_log = 0;
  3080. } else {
  3081. struct logfileinfo *logp;
  3082. logp = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  3083. if (f_ctime > 0L)
  3084. logp->l_ctime = f_ctime;
  3085. else
  3086. logp->l_ctime = now;
  3087. if (f_size > 0)
  3088. logp->l_size = f_size;
  3089. else {
  3090. /* make it the max log size */
  3091. logp->l_size = loginfo.log_error_maxlogsize;
  3092. }
  3093. logp->l_next = loginfo.log_error_logchain;
  3094. loginfo.log_error_logchain = logp;
  3095. }
  3096. loginfo.log_numof_error_logs++;
  3097. }
  3098. if (LOG_DONE == rval)
  3099. rval = log__check_prevlogs(fp, pathname);
  3100. fclose (fp);
  3101. if (LOG_ERROR == rval)
  3102. if (LOG_SUCCESS == log__fix_rotationinfof(pathname))
  3103. logfile_type = LOGFILE_NEW;
  3104. /* Check if there is a rotation overdue */
  3105. if (loginfo.log_error_rotationsync_enabled &&
  3106. loginfo.log_error_rotationunit != LOG_UNIT_HOURS &&
  3107. loginfo.log_error_rotationunit != LOG_UNIT_MINS &&
  3108. loginfo.log_error_ctime < loginfo.log_error_rotationsyncclock - PR_ABS(loginfo.log_error_rotationtime_secs)) {
  3109. loginfo.log_error_rotationsyncclock -= PR_ABS(loginfo.log_error_rotationtime_secs);
  3110. }
  3111. return logfile_type;
  3112. }
  3113. /******************************************************************************
  3114. * log__audit_rotationinfof
  3115. *
  3116. * Try to open the log file. If we have one already, then try to read the
  3117. * header and update the information.
  3118. *
  3119. * Assumption: Lock has been acquired already
  3120. ******************************************************************************/
  3121. static int
  3122. log__audit_rotationinfof( char *pathname)
  3123. {
  3124. long f_ctime;
  3125. PRInt64 f_size;
  3126. int main_log = 1;
  3127. time_t now;
  3128. FILE *fp;
  3129. int rval, logfile_type = LOGFILE_REOPENED;
  3130. /*
  3131. ** Okay -- I confess, we want to use NSPR calls but I want to
  3132. ** use fgets and not use PR_Read() and implement a complicated
  3133. ** parsing module. Since this will be called only during the startup
  3134. ** and never aftre that, we can live by it.
  3135. */
  3136. if ((fp = fopen (pathname, "r")) == NULL) {
  3137. return LOGFILE_NEW;
  3138. }
  3139. loginfo.log_numof_audit_logs = 0;
  3140. /*
  3141. ** We have reopened the log audit file. Now we need to read the
  3142. ** log file info and update the values.
  3143. */
  3144. while ((rval = log__extract_logheader(fp, &f_ctime, &f_size)) == LOG_CONTINUE) {
  3145. /* first we would get the main log info */
  3146. if (f_ctime == 0 && f_size == 0)
  3147. continue;
  3148. time (&now);
  3149. if (main_log) {
  3150. if (f_ctime > 0L)
  3151. loginfo.log_audit_ctime = f_ctime;
  3152. else {
  3153. loginfo.log_audit_ctime = now;
  3154. }
  3155. main_log = 0;
  3156. } else {
  3157. struct logfileinfo *logp;
  3158. logp = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  3159. if (f_ctime > 0L)
  3160. logp->l_ctime = f_ctime;
  3161. else
  3162. logp->l_ctime = now;
  3163. if (f_size > 0)
  3164. logp->l_size = f_size;
  3165. else {
  3166. /* make it the max log size */
  3167. logp->l_size = loginfo.log_audit_maxlogsize;
  3168. }
  3169. logp->l_next = loginfo.log_audit_logchain;
  3170. loginfo.log_audit_logchain = logp;
  3171. }
  3172. loginfo.log_numof_audit_logs++;
  3173. }
  3174. if (LOG_DONE == rval)
  3175. rval = log__check_prevlogs(fp, pathname);
  3176. fclose (fp);
  3177. if (LOG_ERROR == rval)
  3178. if (LOG_SUCCESS == log__fix_rotationinfof(pathname))
  3179. logfile_type = LOGFILE_NEW;
  3180. /* Check if there is a rotation overdue */
  3181. if (loginfo.log_audit_rotationsync_enabled &&
  3182. loginfo.log_audit_rotationunit != LOG_UNIT_HOURS &&
  3183. loginfo.log_audit_rotationunit != LOG_UNIT_MINS &&
  3184. loginfo.log_audit_ctime < loginfo.log_audit_rotationsyncclock - PR_ABS(loginfo.log_audit_rotationtime_secs)) {
  3185. loginfo.log_audit_rotationsyncclock -= PR_ABS(loginfo.log_audit_rotationtime_secs);
  3186. }
  3187. return logfile_type;
  3188. }
  3189. static void
  3190. log__error_emergency(const char *errstr, int reopen, int locked)
  3191. {
  3192. syslog(LOG_ERR, "%s\n", errstr);
  3193. /* emergency open */
  3194. if (!reopen) {
  3195. return;
  3196. }
  3197. if (NULL != loginfo.log_error_fdes) {
  3198. LOG_CLOSE(loginfo.log_error_fdes);
  3199. }
  3200. if (! LOG_OPEN_APPEND(loginfo.log_error_fdes,
  3201. loginfo.log_error_file, loginfo.log_error_mode)) {
  3202. PRErrorCode prerr = PR_GetError();
  3203. syslog(LOG_ERR, "Failed to reopen errors log file, " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n", prerr, slapd_pr_strerror(prerr));
  3204. } else {
  3205. /* LDAPDebug locks ERROR_LOCK_WRITE internally */
  3206. if (locked) LOG_ERROR_UNLOCK_WRITE();
  3207. LDAPDebug(LDAP_DEBUG_ANY, "%s\n", errstr, 0, 0);
  3208. if (locked) LOG_ERROR_LOCK_WRITE( );
  3209. }
  3210. return;
  3211. }
  3212. /******************************************************************************
  3213. * log__open_errorlogfile
  3214. *
  3215. * Open a new log file. If we have run out of the max logs we can have
  3216. * then delete the oldest file.
  3217. ******************************************************************************/
  3218. static int
  3219. log__open_errorlogfile(int logfile_state, int locked)
  3220. {
  3221. time_t now;
  3222. LOGFD fp = NULL;
  3223. LOGFD fpinfo = NULL;
  3224. char tbuf[TBUFSIZE];
  3225. struct logfileinfo *logp;
  3226. char buffer[BUFSIZ];
  3227. struct passwd *pw = NULL;
  3228. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  3229. #ifndef _WIN32
  3230. if ( slapdFrontendConfig->localuser != NULL &&
  3231. slapdFrontendConfig->localuserinfo != NULL ) {
  3232. pw = slapdFrontendConfig->localuserinfo;
  3233. }
  3234. else {
  3235. PR_snprintf(buffer, sizeof(buffer),
  3236. "Invalid nsslapd-localuser. Cannot open the errors log. Exiting...");
  3237. log__error_emergency(buffer, 0, locked);
  3238. return LOG_UNABLE_TO_OPENFILE;
  3239. }
  3240. #endif
  3241. if (!locked) LOG_ERROR_LOCK_WRITE( );
  3242. /*
  3243. ** Here we are trying to create a new log file.
  3244. ** If we alredy have one, then we need to rename it as
  3245. ** "filename.time", close it and update it's information
  3246. ** in the array stack.
  3247. */
  3248. if (loginfo.log_error_fdes != NULL) {
  3249. struct logfileinfo *log;
  3250. char newfile[BUFSIZ];
  3251. PRInt64 f_size;
  3252. /* get rid of the old one */
  3253. if ((f_size = log__getfilesize(loginfo.log_error_fdes)) == -1) {
  3254. /* Then assume that we have the max size */
  3255. f_size = loginfo.log_error_maxlogsize;
  3256. }
  3257. /* Check if I have to delete any old file, delete it if it is required.*/
  3258. while (log__delete_error_logfile(1));
  3259. /* close the file */
  3260. if ( loginfo.log_error_fdes != NULL ) {
  3261. LOG_CLOSE(loginfo.log_error_fdes);
  3262. }
  3263. loginfo.log_error_fdes = NULL;
  3264. if ( loginfo.log_error_maxnumlogs > 1 ) {
  3265. log = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  3266. log->l_ctime = loginfo.log_error_ctime;
  3267. log->l_size = f_size;
  3268. log_convert_time (log->l_ctime, tbuf, 1/*short */);
  3269. PR_snprintf(newfile, sizeof(newfile), "%s.%s", loginfo.log_error_file, tbuf);
  3270. if (PR_Rename (loginfo.log_error_file, newfile) != PR_SUCCESS) {
  3271. PRErrorCode prerr = PR_GetError();
  3272. PR_snprintf(buffer, sizeof(buffer),
  3273. "Failed to rename errors log file, "
  3274. SLAPI_COMPONENT_NAME_NSPR " error %d (%s). Exiting...",
  3275. prerr, slapd_pr_strerror(prerr));
  3276. log__error_emergency(buffer, 1, 1);
  3277. slapi_ch_free((void **)&log);
  3278. if (!locked) LOG_ERROR_UNLOCK_WRITE();
  3279. return LOG_UNABLE_TO_OPENFILE;
  3280. }
  3281. /* add the log to the chain */
  3282. log->l_next = loginfo.log_error_logchain;
  3283. loginfo.log_error_logchain = log;
  3284. loginfo.log_numof_error_logs++;
  3285. }
  3286. }
  3287. /* open a new log file */
  3288. if (! LOG_OPEN_APPEND(fp, loginfo.log_error_file, loginfo.log_error_mode)) {
  3289. PR_snprintf(buffer, sizeof(buffer),
  3290. "Failed to open errors log file %s: error %d (%s); Exiting...",
  3291. loginfo.log_error_file, errno, slapd_system_strerror(errno));
  3292. log__error_emergency(buffer, 1, locked);
  3293. if (!locked) LOG_ERROR_UNLOCK_WRITE();
  3294. /* failed to write to the errors log. should not continue. */
  3295. g_set_shutdown( SLAPI_SHUTDOWN_EXIT );
  3296. /*if I have an old log file -- I should log a message
  3297. ** that I can't open the new file. Let the caller worry
  3298. ** about logging message.
  3299. */
  3300. return LOG_UNABLE_TO_OPENFILE;
  3301. }
  3302. #ifndef _WIN32
  3303. /* make sure the logfile is owned by the localuser. If one of the
  3304. * alternate ns-slapd modes, such as db2bak, tries to log an error
  3305. * at startup, it will create the logfile as root!
  3306. */
  3307. slapd_chown_if_not_owner(loginfo.log_error_file, pw->pw_uid, -1);
  3308. #endif
  3309. loginfo.log_error_fdes = fp;
  3310. if (logfile_state == LOGFILE_REOPENED) {
  3311. /* we have all the information */
  3312. if (!locked) LOG_ERROR_UNLOCK_WRITE( );
  3313. return LOG_SUCCESS;
  3314. }
  3315. loginfo.log_error_state |= LOGGING_NEED_TITLE;
  3316. if (! LOG_OPEN_WRITE(fpinfo, loginfo.log_errorinfo_file, loginfo.log_error_mode)) {
  3317. PR_snprintf(buffer, sizeof(buffer),
  3318. "Failed to open/write to errors log file %s: error %d (%s). Exiting...",
  3319. loginfo.log_error_file, errno, slapd_system_strerror(errno));
  3320. log__error_emergency(buffer, 1, locked);
  3321. if (!locked) LOG_ERROR_UNLOCK_WRITE();
  3322. return LOG_UNABLE_TO_OPENFILE;
  3323. }
  3324. /* write the header in the log */
  3325. now = current_time();
  3326. log_convert_time (now, tbuf, 2 /*long */);
  3327. PR_snprintf(buffer, sizeof(buffer),"LOGINFO:Log file created at: %s (%lu)\n", tbuf, now);
  3328. LOG_WRITE(fpinfo, buffer, strlen(buffer), 0);
  3329. logp = loginfo.log_error_logchain;
  3330. while (logp) {
  3331. log_convert_time (logp->l_ctime, tbuf, 1 /*short */);
  3332. PR_snprintf(buffer, sizeof(buffer), "LOGINFO:%s%s.%s (%lu) (%"
  3333. NSPRI64 "d)\n", PREVLOGFILE, loginfo.log_error_file, tbuf,
  3334. logp->l_ctime, logp->l_size);
  3335. LOG_WRITE(fpinfo, buffer, strlen(buffer), 0);
  3336. logp = logp->l_next;
  3337. }
  3338. /* Close the info file. We need only when we need to rotate to the
  3339. ** next log file.
  3340. */
  3341. if (fpinfo) LOG_CLOSE(fpinfo);
  3342. /* This is now the current error log */
  3343. loginfo.log_error_ctime = now;
  3344. if (!locked) LOG_ERROR_UNLOCK_WRITE( );
  3345. return LOG_SUCCESS;
  3346. }
  3347. /******************************************************************************
  3348. * log__open_auditlogfile
  3349. *
  3350. * Open a new log file. If we have run out of the max logs we can have
  3351. * then delete the oldest file.
  3352. ******************************************************************************/
  3353. static int
  3354. log__open_auditlogfile(int logfile_state, int locked)
  3355. {
  3356. time_t now;
  3357. LOGFD fp;
  3358. LOGFD fpinfo = NULL;
  3359. char tbuf[TBUFSIZE];
  3360. struct logfileinfo *logp;
  3361. char buffer[BUFSIZ];
  3362. if (!locked) LOG_AUDIT_LOCK_WRITE( );
  3363. /*
  3364. ** Here we are trying to create a new log file.
  3365. ** If we alredy have one, then we need to rename it as
  3366. ** "filename.time", close it and update it's information
  3367. ** in the array stack.
  3368. */
  3369. if (loginfo.log_audit_fdes != NULL) {
  3370. struct logfileinfo *log;
  3371. char newfile[BUFSIZ];
  3372. PRInt64 f_size;
  3373. /* get rid of the old one */
  3374. if ((f_size = log__getfilesize(loginfo.log_audit_fdes)) == -1) {
  3375. /* Then assume that we have the max size */
  3376. f_size = loginfo.log_audit_maxlogsize;
  3377. }
  3378. /* Check if I have to delete any old file, delete it if it is required. */
  3379. while (log__delete_audit_logfile());
  3380. /* close the file */
  3381. LOG_CLOSE(loginfo.log_audit_fdes);
  3382. loginfo.log_audit_fdes = NULL;
  3383. if ( loginfo.log_audit_maxnumlogs > 1 ) {
  3384. log = (struct logfileinfo *) slapi_ch_malloc (sizeof (struct logfileinfo));
  3385. log->l_ctime = loginfo.log_audit_ctime;
  3386. log->l_size = f_size;
  3387. log_convert_time (log->l_ctime, tbuf, 1 /*short */);
  3388. PR_snprintf(newfile, sizeof(newfile), "%s.%s", loginfo.log_audit_file, tbuf);
  3389. if (PR_Rename (loginfo.log_audit_file, newfile) != PR_SUCCESS) {
  3390. if (!locked) LOG_AUDIT_UNLOCK_WRITE();
  3391. return LOG_UNABLE_TO_OPENFILE;
  3392. }
  3393. /* add the log to the chain */
  3394. log->l_next = loginfo.log_audit_logchain;
  3395. loginfo.log_audit_logchain = log;
  3396. loginfo.log_numof_audit_logs++;
  3397. }
  3398. }
  3399. /* open a new log file */
  3400. if (! LOG_OPEN_APPEND(fp, loginfo.log_audit_file, loginfo.log_audit_mode)) {
  3401. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't open file %s. "
  3402. "errno %d (%s)\n",
  3403. loginfo.log_audit_file, errno, slapd_system_strerror(errno));
  3404. if (!locked) LOG_AUDIT_UNLOCK_WRITE();
  3405. /*if I have an old log file -- I should log a message
  3406. ** that I can't open the new file. Let the caller worry
  3407. ** about logging message.
  3408. */
  3409. return LOG_UNABLE_TO_OPENFILE;
  3410. }
  3411. loginfo.log_audit_fdes = fp;
  3412. if (logfile_state == LOGFILE_REOPENED) {
  3413. /* we have all the information */
  3414. if (!locked) LOG_AUDIT_UNLOCK_WRITE();
  3415. return LOG_SUCCESS;
  3416. }
  3417. loginfo.log_audit_state |= LOGGING_NEED_TITLE;
  3418. if (! LOG_OPEN_WRITE(fpinfo, loginfo.log_auditinfo_file, loginfo.log_audit_mode)) {
  3419. LDAPDebug(LDAP_DEBUG_ANY, "WARNING: can't open file %s. "
  3420. "errno %d (%s)\n",
  3421. loginfo.log_auditinfo_file, errno, slapd_system_strerror(errno));
  3422. if (!locked) LOG_AUDIT_UNLOCK_WRITE();
  3423. return LOG_UNABLE_TO_OPENFILE;
  3424. }
  3425. /* write the header in the log */
  3426. now = current_time();
  3427. log_convert_time (now, tbuf, 2 /*long */);
  3428. PR_snprintf(buffer, sizeof(buffer), "LOGINFO:Log file created at: %s (%lu)\n", tbuf, now);
  3429. LOG_WRITE(fpinfo, buffer, strlen(buffer), 0);
  3430. logp = loginfo.log_audit_logchain;
  3431. while ( logp) {
  3432. log_convert_time (logp->l_ctime, tbuf, 1 /*short */);
  3433. PR_snprintf(buffer, sizeof(buffer), "LOGINFO:%s%s.%s (%lu) (%"
  3434. NSPRI64 "d)\n", PREVLOGFILE, loginfo.log_audit_file, tbuf,
  3435. logp->l_ctime, logp->l_size);
  3436. LOG_WRITE(fpinfo, buffer, strlen(buffer), 0);
  3437. logp = logp->l_next;
  3438. }
  3439. /* Close the info file. We need only when we need to rotate to the
  3440. ** next log file.
  3441. */
  3442. if (fpinfo) LOG_CLOSE(fpinfo);
  3443. /* This is now the current audit log */
  3444. loginfo.log_audit_ctime = now;
  3445. if (!locked) LOG_AUDIT_UNLOCK_WRITE( );
  3446. return LOG_SUCCESS;
  3447. }
  3448. /*
  3449. ** Log Buffering
  3450. ** only supports access log at this time
  3451. */
  3452. static LogBufferInfo *log_create_buffer(size_t sz)
  3453. {
  3454. LogBufferInfo *lbi;
  3455. lbi = (LogBufferInfo *) slapi_ch_malloc(sizeof(LogBufferInfo));
  3456. lbi->top = (char *) slapi_ch_malloc(sz);
  3457. lbi->current = lbi->top;
  3458. lbi->maxsize = sz;
  3459. lbi->refcount = 0;
  3460. return lbi;
  3461. }
  3462. #if 0
  3463. /* for some reason, we never call this. */
  3464. static void log_destroy_buffer(LogBufferInfo *lbi)
  3465. {
  3466. slapi_ch_free((void *)&(lbi->top));
  3467. slapi_ch_free((void *)&lbi);
  3468. }
  3469. #endif
  3470. /*
  3471. Some notes about this function. It is written the
  3472. way it is for performance reasons.
  3473. Tests showed that on 4 processor systems, there is
  3474. significant contention for the
  3475. lbi->lock. This is because the lock was held for
  3476. the duration of the copy of the
  3477. log message into the buffer. Therefore the routine
  3478. was re-written to avoid holding
  3479. the lock for that time. Instead we gain the lock,
  3480. take a copy of the buffer pointer
  3481. where we need to copy our message, increase the
  3482. size, move the current pointer beyond
  3483. our portion of the buffer, then increment a reference
  3484. count.
  3485. Then we release the lock and do the actual copy
  3486. in to the reserved buffer area.
  3487. We then atomically decrement the reference count.
  3488. The reference count is used to ensure that when
  3489. the buffer is flushed to the
  3490. filesystem, there are no threads left copying
  3491. data into the buffer.
  3492. The wait on zero reference count is implemented
  3493. in the flush routine because
  3494. it is also called from log_access_flush().
  3495. Tests show this speeds up searches by 10% on 4-way systems.
  3496. */
  3497. static void log_append_buffer2(time_t tnl, LogBufferInfo *lbi, char *msg1, size_t size1, char *msg2, size_t size2)
  3498. {
  3499. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  3500. size_t size = size1 + size2;
  3501. char* insert_point = NULL;
  3502. /* While holding the lock, we determine if there is space in the buffer for our payload,
  3503. and if we need to flush.
  3504. */
  3505. PR_Lock(lbi->lock);
  3506. if ( ((lbi->current - lbi->top) + size > lbi->maxsize) ||
  3507. (tnl >= loginfo.log_access_rotationsyncclock &&
  3508. loginfo.log_access_rotationsync_enabled) ) {
  3509. log_flush_buffer(lbi, SLAPD_ACCESS_LOG,
  3510. 0 /* do not sync to disk right now */ );
  3511. }
  3512. insert_point = lbi->current;
  3513. lbi->current += size;
  3514. /* Increment the copy refcount */
  3515. PR_AtomicIncrement(&(lbi->refcount));
  3516. PR_Unlock(lbi->lock);
  3517. /* Now we can copy without holding the lock */
  3518. memcpy(insert_point, msg1, size1);
  3519. memcpy(insert_point + size1, msg2, size2);
  3520. /* Decrement the copy refcount */
  3521. PR_AtomicDecrement(&(lbi->refcount));
  3522. /* If we are asked to sync to disk immediately, do so */
  3523. if (!slapdFrontendConfig->accesslogbuffering) {
  3524. PR_Lock(lbi->lock);
  3525. log_flush_buffer(lbi, SLAPD_ACCESS_LOG, 1 /* sync to disk now */ );
  3526. PR_Unlock(lbi->lock);
  3527. }
  3528. }
  3529. /* this function assumes the lock is already acquired */
  3530. /* if sync_now is non-zero, data is flushed to physical storage */
  3531. static void log_flush_buffer(LogBufferInfo *lbi, int type, int sync_now)
  3532. {
  3533. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  3534. int err = 0;
  3535. if (type == SLAPD_ACCESS_LOG) {
  3536. /* It is only safe to flush once any other threads which are copying are finished */
  3537. while (lbi->refcount > 0) {
  3538. /* It's ok to sleep for a while because we only flush every second or so */
  3539. DS_Sleep (PR_MillisecondsToInterval(1));
  3540. }
  3541. if ((lbi->current - lbi->top) == 0) return;
  3542. if (log__needrotation(loginfo.log_access_fdes,
  3543. SLAPD_ACCESS_LOG) == LOG_ROTATE) {
  3544. if (log__open_accesslogfile(LOGFILE_NEW, 1) != LOG_SUCCESS) {
  3545. LDAPDebug(LDAP_DEBUG_ANY,
  3546. "LOGINFO: Unable to open access file:%s\n",
  3547. loginfo.log_access_file,0,0);
  3548. lbi->current = lbi->top; /* reset counter to prevent overwriting rest of lbi struct */
  3549. return;
  3550. }
  3551. while (loginfo.log_access_rotationsyncclock <= loginfo.log_access_ctime) {
  3552. loginfo.log_access_rotationsyncclock += PR_ABS(loginfo.log_access_rotationtime_secs);
  3553. }
  3554. }
  3555. if (loginfo.log_access_state & LOGGING_NEED_TITLE) {
  3556. log_write_title(loginfo.log_access_fdes);
  3557. loginfo.log_access_state &= ~LOGGING_NEED_TITLE;
  3558. }
  3559. if (!sync_now && slapdFrontendConfig->accesslogbuffering) {
  3560. LOG_WRITE(loginfo.log_access_fdes, lbi->top, lbi->current - lbi->top, 0);
  3561. } else {
  3562. LOG_WRITE_NOW(loginfo.log_access_fdes, lbi->top,
  3563. lbi->current - lbi->top, 0, err);
  3564. }
  3565. lbi->current = lbi->top;
  3566. }
  3567. }
  3568. void log_access_flush()
  3569. {
  3570. LOG_ACCESS_LOCK_WRITE();
  3571. log_flush_buffer(loginfo.log_access_buffer, SLAPD_ACCESS_LOG,
  3572. 1 /* sync to disk now */ );
  3573. LOG_ACCESS_UNLOCK_WRITE();
  3574. }
  3575. /*
  3576. *
  3577. * log_convert_time
  3578. * returns the time converted into the string format.
  3579. *
  3580. */
  3581. static void
  3582. log_convert_time (time_t ctime, char *tbuf, int type)
  3583. {
  3584. struct tm *tmsp, tms;
  3585. #ifdef _WIN32
  3586. {
  3587. struct tm *pt = localtime( &ctime );
  3588. tmsp = &tms;
  3589. memcpy(&tms, pt, sizeof(struct tm) );
  3590. }
  3591. #else
  3592. (void)localtime_r( &ctime, &tms );
  3593. tmsp = &tms;
  3594. #endif
  3595. if (type == 1) /* get the short form */
  3596. (void) strftime (tbuf, (size_t) TBUFSIZE, "%Y%m%d-%H%M%S",tmsp);
  3597. else /* wants the long form */
  3598. (void) strftime (tbuf, (size_t) TBUFSIZE, "%d/%b/%Y:%H:%M:%S",tmsp);
  3599. }
  3600. /*
  3601. * log_reverse_convert_time
  3602. * convert the given string formatted time (output from log_convert_time)
  3603. * into time_t
  3604. */
  3605. static time_t
  3606. log_reverse_convert_time(char *tbuf)
  3607. {
  3608. struct tm tm = {0};
  3609. if (strchr(tbuf, '-')) { /* short format */
  3610. strptime(tbuf, "%Y%m%d-%H%M%S", &tm);
  3611. } else if (strchr(tbuf, '/') && strchr(tbuf, ':')) { /* long format */
  3612. strptime(tbuf, "%d/%b/%Y:%H:%M:%S", &tm);
  3613. } else {
  3614. return 0;
  3615. }
  3616. return mktime(&tm);
  3617. }
  3618. int
  3619. check_log_max_size( char *maxdiskspace_str,
  3620. char *mlogsize_str,
  3621. int maxdiskspace, /* in megabytes */
  3622. int mlogsize, /* in megabytes */
  3623. char * returntext,
  3624. int logtype)
  3625. {
  3626. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  3627. int rc = LDAP_SUCCESS;
  3628. int current_mlogsize = -1; /* in megabytes */
  3629. int current_maxdiskspace = -1; /* in megabytes */
  3630. PRInt64 mlogsizeB; /* in bytes */
  3631. PRInt64 maxdiskspaceB; /* in bytes */
  3632. switch (logtype)
  3633. {
  3634. case SLAPD_ACCESS_LOG:
  3635. current_mlogsize = slapdFrontendConfig->accesslog_maxlogsize;
  3636. current_maxdiskspace = slapdFrontendConfig->accesslog_maxdiskspace;
  3637. break;
  3638. case SLAPD_ERROR_LOG:
  3639. current_mlogsize = slapdFrontendConfig->errorlog_maxlogsize;
  3640. current_maxdiskspace = slapdFrontendConfig->errorlog_maxdiskspace;
  3641. break;
  3642. case SLAPD_AUDIT_LOG:
  3643. current_mlogsize = slapdFrontendConfig->auditlog_maxlogsize;
  3644. current_maxdiskspace = slapdFrontendConfig->auditlog_maxdiskspace;
  3645. break;
  3646. default:
  3647. current_mlogsize = -1;
  3648. current_maxdiskspace = -1;
  3649. }
  3650. if ( maxdiskspace == -1 ) {
  3651. maxdiskspace = current_maxdiskspace;
  3652. }
  3653. maxdiskspaceB = (PRInt64)maxdiskspace * LOG_MB_IN_BYTES;
  3654. if ( mlogsize == -1 ) {
  3655. mlogsize = current_mlogsize;
  3656. }
  3657. mlogsizeB = (PRInt64)mlogsize * LOG_MB_IN_BYTES;
  3658. if ( maxdiskspace < mlogsize )
  3659. {
  3660. /* fail */
  3661. PR_snprintf ( returntext, SLAPI_DSE_RETURNTEXT_SIZE,
  3662. "%s: maxdiskspace \"%d (MB)\" is less than max log size \"%d (MB)\"",
  3663. maxdiskspace_str, maxdiskspace, mlogsize );
  3664. rc = LDAP_OPERATIONS_ERROR;
  3665. }
  3666. switch (logtype)
  3667. {
  3668. case SLAPD_ACCESS_LOG:
  3669. loginfo.log_access_maxlogsize = mlogsizeB;
  3670. loginfo.log_access_maxdiskspace = maxdiskspaceB;
  3671. break;
  3672. case SLAPD_ERROR_LOG:
  3673. loginfo.log_error_maxlogsize = mlogsizeB;
  3674. loginfo.log_error_maxdiskspace = maxdiskspaceB;
  3675. break;
  3676. case SLAPD_AUDIT_LOG:
  3677. loginfo.log_audit_maxlogsize = mlogsizeB;
  3678. loginfo.log_audit_maxdiskspace = maxdiskspaceB;
  3679. break;
  3680. default:
  3681. break;
  3682. }
  3683. return rc;
  3684. }
  3685. /************************************************************************************/
  3686. /* E N D */
  3687. /************************************************************************************/