log.c 119 KB

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