log.c 124 KB

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