log.c 123 KB

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