log.c 125 KB

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