log.c 111 KB

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