main.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <malloc.h>
  13. #include <ldap.h>
  14. #undef OFF
  15. #undef LITTLE_ENDIAN
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <sys/fcntl.h>
  21. #include <time.h>
  22. #include <stdarg.h>
  23. #include <signal.h>
  24. #include <stdlib.h>
  25. #include <sys/time.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <netdb.h>
  30. #include <pwd.h> /* getpwnam */
  31. #if !defined(LINUX)
  32. union semun {
  33. int val;
  34. struct semid_ds *buf;
  35. ushort *array;
  36. };
  37. #endif
  38. #include <unistd.h> /* dup2 */
  39. #include <sys/ipc.h>
  40. #include <sys/sem.h>
  41. #include <sys/param.h> /* MAXPATHLEN */
  42. #if defined(__sun)
  43. #include <sys/utsname.h>
  44. #include <sys/systeminfo.h>
  45. #endif
  46. #include "slap.h"
  47. #include "slapi-plugin.h"
  48. #include "prinit.h"
  49. #include "snmp_collator.h"
  50. #include "fe.h" /* client_auth_init() */
  51. #include "protect_db.h"
  52. #include "getopt_ext.h"
  53. #include "fe.h"
  54. #include <nss.h>
  55. #ifndef LDAP_DONT_USE_SMARTHEAP
  56. #include "smrtheap.h"
  57. #endif
  58. /* Forward Declarations */
  59. static void register_objects();
  60. static void process_command_line(int argc, char **argv, char *myname, char **extraname);
  61. static int slapd_exemode_ldif2db();
  62. static int slapd_exemode_db2ldif(int argc, char **argv);
  63. static int slapd_exemode_db2index();
  64. static int slapd_exemode_archive2db();
  65. static int slapd_exemode_db2archive();
  66. static int slapd_exemode_upgradedb();
  67. static int slapd_exemode_upgradednformat();
  68. static int slapd_exemode_dbverify();
  69. static int slapd_exemode_suffix2instance();
  70. static int slapd_debug_level_string2level( const char *s );
  71. static void slapd_debug_level_log( int level );
  72. static void slapd_debug_level_usage( void );
  73. static void cmd_set_shutdown(int);
  74. /*
  75. * global variables
  76. */
  77. static int slapd_exemode = SLAPD_EXEMODE_UNKNOWN;
  78. static int init_cmd_shutdown_detect()
  79. {
  80. /* First of all, we must reset the signal mask to get rid of any blockages
  81. * the process may have inherited from its parent (such as the console), which
  82. * might result in the process not delivering those blocked signals, and thus,
  83. * misbehaving....
  84. */
  85. {
  86. int rc;
  87. sigset_t proc_mask;
  88. LDAPDebug( LDAP_DEBUG_TRACE, "Reseting signal mask....\n", 0, 0, 0);
  89. (void)sigemptyset( &proc_mask );
  90. rc = pthread_sigmask( SIG_SETMASK, &proc_mask, NULL );
  91. LDAPDebug( LDAP_DEBUG_TRACE, " %s \n",
  92. rc ? "Failed to reset signal mask":"....Done (signal mask reset)!!", 0, 0 );
  93. }
  94. #if defined ( HPUX10 )
  95. PR_CreateThread ( PR_USER_THREAD,
  96. catch_signals,
  97. NULL,
  98. PR_PRIORITY_NORMAL,
  99. PR_GLOBAL_THREAD,
  100. PR_UNJOINABLE_THREAD,
  101. SLAPD_DEFAULT_THREAD_STACKSIZE);
  102. #elif defined ( HPUX11 )
  103. /* In the optimized builds for HPUX, the signal handler doesn't seem
  104. * to get set correctly unless the primordial thread gets a chance
  105. * to run before we make the call to SIGNAL. (At this point the
  106. * the primordial thread has spawned the daemon thread which called
  107. * this function.) The call to DS_Sleep will give the primordial
  108. * thread a chance to run. */
  109. DS_Sleep(0);
  110. #endif
  111. (void) SIGNAL( SIGPIPE, SIG_IGN );
  112. (void) SIGNAL( SIGCHLD, slapd_wait4child );
  113. #ifndef LINUX
  114. /* linux uses USR1/USR2 for thread synchronization, so we aren't
  115. * allowed to mess with those.
  116. */
  117. (void) SIGNAL( SIGUSR1, slapd_do_nothing );
  118. (void) SIGNAL( SIGUSR2, cmd_set_shutdown );
  119. #endif
  120. (void) SIGNAL( SIGTERM, cmd_set_shutdown );
  121. (void) SIGNAL( SIGHUP, cmd_set_shutdown );
  122. (void) SIGNAL( SIGINT, cmd_set_shutdown );
  123. return 0;
  124. }
  125. static void
  126. cmd_set_shutdown (int sig)
  127. {
  128. /* don't log anything from a signal handler:
  129. * you could be holding a lock when the signal was trapped. more
  130. * specifically, you could be holding the logfile lock (and deadlock
  131. * yourself).
  132. */
  133. c_set_shutdown();
  134. #ifndef LINUX
  135. /* don't mess with USR1/USR2 on linux, used by libpthread */
  136. (void) SIGNAL( SIGUSR2, cmd_set_shutdown );
  137. #endif
  138. (void) SIGNAL( SIGTERM, cmd_set_shutdown );
  139. (void) SIGNAL( SIGHUP, cmd_set_shutdown );
  140. }
  141. #ifdef HPUX10
  142. extern void collation_init();
  143. #endif
  144. /*
  145. Four cases:
  146. - change ownership of all files in directory (strip_fn=PR_FALSE)
  147. - change ownership of all files in directory; but trailing fn needs to be stripped (strip_fn=PR_TRUE)
  148. - fn is relative to root directory (/access); we print error message and let user shoot his foot
  149. - fn is relative to current directory (access); we print error message and let user shoot his other foot
  150. The docs say any valid filename.
  151. */
  152. static void
  153. chown_dir_files(char *name, struct passwd *pw, PRBool strip_fn, PRBool both)
  154. {
  155. PRDir *dir;
  156. PRDirEntry *entry;
  157. char file[MAXPATHLEN + 1];
  158. char *log=NULL, *ptr=NULL;
  159. int rc=0;
  160. log=slapi_ch_strdup(name);
  161. if(strip_fn)
  162. {
  163. if((ptr=strrchr(log,'/'))==NULL)
  164. {
  165. LDAPDebug(LDAP_DEBUG_ANY, "Caution changing ownership of ./%s \n",name,0,0);
  166. if(slapd_chown_if_not_owner(log, pw->pw_uid, -1 )){
  167. LDAPDebug(LDAP_DEBUG_ANY, "chown_dir_files: file (%s) chown failed (%d) %s.\n",
  168. log, errno, slapd_system_strerror(errno));
  169. }
  170. rc=1;
  171. } else if(log==ptr) {
  172. LDAPDebug(LDAP_DEBUG_ANY, "Caution changing ownership of / directory and its contents to %s\n",pw->pw_name,0,0);
  173. *(++ptr)='\0';
  174. } else {
  175. *ptr='\0';
  176. }
  177. }
  178. if ((!rc) && ((dir = PR_OpenDir(log)) != NULL ))
  179. {
  180. /* change the owner for each of the files in the dir */
  181. while( (entry = PR_ReadDir(dir , PR_SKIP_BOTH )) !=NULL )
  182. {
  183. PR_snprintf(file,MAXPATHLEN+1,"%s/%s",log,entry->name);
  184. if(slapd_chown_if_not_owner( file, pw->pw_uid, both?pw->pw_gid:-1 )){
  185. LDAPDebug(LDAP_DEBUG_ANY, "chown_dir_files: file (%s) chown failed (%d) %s.\n",
  186. file, errno, slapd_system_strerror(errno));
  187. }
  188. }
  189. PR_CloseDir( dir );
  190. }
  191. slapi_ch_free_string(&log);
  192. }
  193. /* Changes the owner of the files in the logs and
  194. * config directory to the user that the server runs as.
  195. */
  196. static void
  197. fix_ownership()
  198. {
  199. struct passwd* pw=NULL;
  200. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  201. if (slapdFrontendConfig->localuser == NULL) {
  202. LDAPDebug(LDAP_DEBUG_ANY,
  203. "Local user missing from frontend configuration\n",
  204. 0, 0, 0);
  205. return;
  206. }
  207. if (slapdFrontendConfig->localuserinfo == NULL) {
  208. pw = getpwnam( slapdFrontendConfig->localuser );
  209. if ( NULL == pw ) {
  210. LDAPDebug(LDAP_DEBUG_ANY,
  211. "Unable to find user %s in system account database, "
  212. "errno %d (%s)\n",
  213. slapdFrontendConfig->localuser, errno, strerror(errno));
  214. return;
  215. }
  216. slapdFrontendConfig->localuserinfo =
  217. (struct passwd *)slapi_ch_malloc(sizeof(struct passwd));
  218. memcpy(slapdFrontendConfig->localuserinfo, pw, sizeof(struct passwd));
  219. }
  220. pw = slapdFrontendConfig->localuserinfo;
  221. /* config directory needs to be owned by the local user */
  222. if (slapdFrontendConfig->configdir) {
  223. chown_dir_files(slapdFrontendConfig->configdir, pw, PR_FALSE, PR_FALSE);
  224. }
  225. /* do access log file, if any */
  226. if (slapdFrontendConfig->accesslog) {
  227. chown_dir_files(slapdFrontendConfig->accesslog, pw, PR_TRUE, PR_TRUE);
  228. }
  229. /* do audit log file, if any */
  230. if (slapdFrontendConfig->auditlog) {
  231. chown_dir_files(slapdFrontendConfig->auditlog, pw, PR_TRUE, PR_TRUE);
  232. }
  233. /* do error log file, if any */
  234. if (slapdFrontendConfig->errorlog) {
  235. chown_dir_files(slapdFrontendConfig->errorlog, pw, PR_TRUE, PR_TRUE);
  236. }
  237. }
  238. /* Changes identity to the named user
  239. * If username == NULL, does nothing.
  240. * Does nothing on NT regardless.
  241. */
  242. static int main_setuid(char *username)
  243. {
  244. if (username != NULL) {
  245. struct passwd *pw;
  246. /* Make sure everything in the log and config directory
  247. * is owned by the correct user */
  248. fix_ownership();
  249. pw = getpwnam (username);
  250. if (pw == NULL) {
  251. int oserr = errno;
  252. LDAPDebug (LDAP_DEBUG_ANY, "getpwnam(%s) == NULL, error %d (%s)\n",
  253. username, oserr, slapd_system_strerror(oserr));
  254. } else {
  255. if (setgid (pw->pw_gid) != 0) {
  256. int oserr = errno;
  257. LDAPDebug (LDAP_DEBUG_ANY, "setgid(%li) != 0, error %d (%s)\n",
  258. (long)pw->pw_gid, oserr, slapd_system_strerror(oserr));
  259. return -1;
  260. }
  261. if (setuid (pw->pw_uid) != 0) {
  262. int oserr = errno;
  263. LDAPDebug (LDAP_DEBUG_ANY, "setuid(%li) != 0, error %d (%s)\n",
  264. (long)pw->pw_uid, oserr, slapd_system_strerror(oserr));
  265. return -1;
  266. }
  267. }
  268. }
  269. return 0;
  270. }
  271. /* set good defaults for front-end config in referral mode */
  272. static void referral_set_defaults(void)
  273. {
  274. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  275. config_set_maxdescriptors( CONFIG_MAXDESCRIPTORS_ATTRIBUTE, "1024", errorbuf, 1);
  276. }
  277. static int
  278. name2exemode( char *progname, char *s, int exit_if_unknown )
  279. {
  280. int exemode;
  281. if ( strcmp( s, "db2ldif" ) == 0 ) {
  282. exemode = SLAPD_EXEMODE_DB2LDIF;
  283. } else if ( strcmp( s, "ldif2db" ) == 0 ) {
  284. exemode = SLAPD_EXEMODE_LDIF2DB;
  285. } else if ( strcmp( s, "archive2db" ) == 0 ) {
  286. exemode = SLAPD_EXEMODE_ARCHIVE2DB;
  287. } else if ( strcmp( s, "db2archive" ) == 0 ) {
  288. exemode = SLAPD_EXEMODE_DB2ARCHIVE;
  289. } else if ( strcmp( s, "server" ) == 0 ) {
  290. exemode = SLAPD_EXEMODE_SLAPD;
  291. } else if ( strcmp( s, "db2index" ) == 0 ) {
  292. exemode = SLAPD_EXEMODE_DB2INDEX;
  293. } else if ( strcmp( s, "refer" ) == 0 ) {
  294. exemode = SLAPD_EXEMODE_REFERRAL;
  295. } else if ( strcmp( s, "suffix2instance" ) == 0 ) {
  296. exemode = SLAPD_EXEMODE_SUFFIX2INSTANCE;
  297. } else if ( strcmp( s, "upgradedb" ) == 0 ) {
  298. exemode = SLAPD_EXEMODE_UPGRADEDB;
  299. } else if ( strcmp( s, "upgradednformat" ) == 0 ) {
  300. exemode = SLAPD_EXEMODE_UPGRADEDNFORMAT;
  301. } else if ( strcmp( s, "dbverify" ) == 0 ) {
  302. exemode = SLAPD_EXEMODE_DBVERIFY;
  303. }
  304. else if ( exit_if_unknown ) {
  305. fprintf( stderr, "usage: %s -D configdir "
  306. "[ldif2db | db2ldif | archive2db "
  307. "| db2archive | db2index | refer | suffix2instance "
  308. "| upgradedb | upgradednformat | dbverify] "
  309. "[options]\n", progname );
  310. exit( 1 );
  311. } else {
  312. exemode = SLAPD_EXEMODE_UNKNOWN;
  313. }
  314. return( exemode );
  315. }
  316. static void
  317. usage( char *name, char *extraname )
  318. {
  319. char *usagestr = NULL;
  320. char *extraspace;
  321. if ( extraname == NULL ) {
  322. extraspace = extraname = "";
  323. } else {
  324. extraspace = " ";
  325. }
  326. switch( slapd_exemode ) {
  327. case SLAPD_EXEMODE_DB2LDIF:
  328. usagestr = "usage: %s %s%s-D configdir [-n backend-instance-name] [-d debuglevel] "
  329. "[-N] [-a outputfile] [-r] [-C] [{-s includesuffix}*] "
  330. "[{-x excludesuffix}*] [-u] [-U] [-m] [-M] [-E] [-q]\n"
  331. "Note: either \"-n backend_instance_name\" or \"-s includesuffix\" is required.\n";
  332. break;
  333. case SLAPD_EXEMODE_LDIF2DB:
  334. usagestr = "usage: %s %s%s-D configdir [-d debuglevel] "
  335. "[-n backend_instance_name] [-O] [-g uniqueid_type] [--namespaceid uniqueID]"
  336. "[{-s includesuffix}*] [{-x excludesuffix}*] [-E] [-q] {-i ldif-file}*\n"
  337. "Note: either \"-n backend_instance_name\" or \"-s includesuffix\" is required.\n";
  338. break;
  339. case SLAPD_EXEMODE_DB2ARCHIVE:
  340. usagestr = "usage: %s %s%s-D configdir [-q] [-d debuglevel] -a archivedir\n";
  341. break;
  342. case SLAPD_EXEMODE_ARCHIVE2DB:
  343. usagestr = "usage: %s %s%s-D configdir [-q] [-d debuglevel] -a archivedir\n";
  344. break;
  345. case SLAPD_EXEMODE_DB2INDEX:
  346. usagestr = "usage: %s %s%s-D configdir -n backend-instance-name "
  347. "[-d debuglevel] {-t attributetype}* {-T VLV Search Name}*\n";
  348. /* JCM should say 'Address Book' or something instead of VLV */
  349. break;
  350. case SLAPD_EXEMODE_REFERRAL:
  351. usagestr = "usage: %s %s%s-D configdir -r referral-url [-p port]\n";
  352. break;
  353. case SLAPD_EXEMODE_SUFFIX2INSTANCE:
  354. usagestr = "usage: %s %s%s -D configdir {-s suffix}*\n";
  355. break;
  356. case SLAPD_EXEMODE_UPGRADEDB:
  357. usagestr = "usage: %s %s%s-D configdir [-d debuglevel] [-f] [-r] -a archivedir\n";
  358. break;
  359. case SLAPD_EXEMODE_UPGRADEDNFORMAT:
  360. usagestr = "usage: %s %s%s-D configdir [-d debuglevel] [-N] -n backend-instance-name -a fullpath-backend-instance-dir-full\n";
  361. break;
  362. case SLAPD_EXEMODE_DBVERIFY:
  363. usagestr = "usage: %s %s%s-D configdir [-d debuglevel] [-n backend-instance-name]\n";
  364. break;
  365. default: /* SLAPD_EXEMODE_SLAPD */
  366. usagestr = "usage: %s %s%s-D configdir [-d debuglevel] "
  367. "[-i pidlogfile] [-v] [-V]\n";
  368. }
  369. fprintf( stderr, usagestr, name, extraname, extraspace );
  370. }
  371. /*
  372. * These nasty globals are the settings collected from the
  373. * command line by the process_command_line function. The
  374. * various slapd_exemode functions read these to drive their
  375. * execution.
  376. */
  377. static char *extraname;
  378. static char *myname;
  379. static int n_port = 0;
  380. static int i_port = 0;
  381. static int s_port = 0;
  382. static char **ldif_file = NULL;
  383. static int ldif_files = 0;
  384. static char *cmd_line_instance_name = NULL;
  385. static char **cmd_line_instance_names = NULL;
  386. static int skip_db_protect_check = 0;
  387. static char **db2ldif_include = NULL;
  388. static char **db2ldif_exclude = NULL;
  389. static int ldif2db_removedupvals = 1;
  390. static int ldif2db_noattrindexes = 0;
  391. static char **db2index_attrs = NULL;
  392. static int ldif_printkey = EXPORT_PRINTKEY|EXPORT_APPENDMODE;
  393. static char *archive_name = NULL;
  394. static int db2ldif_dump_replica = 0;
  395. static int db2ldif_dump_uniqueid = 1;
  396. static int ldif2db_generate_uniqueid = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  397. static char *ldif2db_namespaceid = NULL;
  398. int importexport_encrypt = 0;
  399. static int upgradedb_flags = 0;
  400. static int upgradednformat_dryrun = 0;
  401. static int is_quiet = 0;
  402. /* dbverify options */
  403. static int dbverify_verbose = 0;
  404. static char *dbverify_dbdir = NULL;
  405. /* taken from idsktune */
  406. #if defined(__sun)
  407. static void ids_get_platform_solaris(char *buf)
  408. {
  409. struct utsname u;
  410. char sbuf[128];
  411. FILE *fp;
  412. #if defined(sparc) || defined(__sparc)
  413. int is_u = 0;
  414. sbuf[0] = '\0';
  415. sysinfo(SI_MACHINE,sbuf,128);
  416. if (strcmp(sbuf,"sun4u") == 0) {
  417. is_u = 1;
  418. }
  419. sbuf[0] = '\0';
  420. sysinfo(SI_PLATFORM,sbuf,128);
  421. PR_snprintf(buf,sizeof(buf),"%ssparc%s-%s-solaris",
  422. is_u ? "u" : "",
  423. sizeof(long) == 4 ? "" : "v9",
  424. sbuf);
  425. #else
  426. #if defined(i386) || defined(__i386)
  427. sprintf(buf,"i386-unknown-solaris");
  428. #else
  429. sprintf(buf,"unknown-unknown-solaris");
  430. #endif /* not i386 */
  431. #endif /* not sparc */
  432. uname(&u);
  433. if (isascii(u.release[0]) && isdigit(u.release[0])) strcat(buf,u.release);
  434. fp = fopen("/etc/release","r");
  435. if (fp != NULL) {
  436. char *rp;
  437. sbuf[0] = '\0';
  438. fgets(sbuf,128,fp);
  439. fclose(fp);
  440. rp = strstr(sbuf,"Solaris");
  441. if (rp) {
  442. rp += 8;
  443. while(*rp != 's' && *rp != '\0') rp++;
  444. if (*rp == 's') {
  445. char *rp2;
  446. rp2 = strchr(rp,' ');
  447. if (rp2) *rp2 = '\0';
  448. strcat(buf,"_");
  449. strcat(buf,rp);
  450. }
  451. }
  452. }
  453. }
  454. #endif
  455. static void slapd_print_version(int verbose)
  456. {
  457. #if defined(__sun)
  458. char buf[8192];
  459. #endif
  460. char *versionstring = config_get_versionstring();
  461. char *buildnum = config_get_buildnum();
  462. printf( SLAPD_VENDOR_NAME "\n%s B%s\n", versionstring, buildnum);
  463. if (strcmp(buildnum,BUILD_NUM) != 0) {
  464. printf( "ns-slapd: B%s\n", BUILD_NUM);
  465. }
  466. slapi_ch_free( (void **)&versionstring);
  467. slapi_ch_free( (void **)&buildnum);
  468. if (verbose == 0) return;
  469. #if defined(__sun)
  470. ids_get_platform_solaris(buf);
  471. printf("System: %s\n",buf);
  472. #endif
  473. /* this won't print much with the -v flag as the dse.ldif file
  474. * hasn't be read yet.
  475. */
  476. plugin_print_versions();
  477. }
  478. /* On UNIX, we create a file with our PID in it */
  479. static int
  480. write_start_pid_file()
  481. {
  482. FILE *fp = NULL;
  483. /*
  484. * The following section of code is closely coupled with the
  485. * admin programs. Please do not make changes here without
  486. * consulting the start/stop code for the admin code.
  487. */
  488. if ( (fp = fopen( start_pid_file, "w" )) != NULL ) {
  489. fprintf( fp, "%d\n", getpid() );
  490. fclose( fp );
  491. if ( chmod(start_pid_file, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH) != 0 ) {
  492. unlink(start_pid_file);
  493. } else {
  494. return 0;
  495. }
  496. }
  497. return -1;
  498. }
  499. #ifdef MEMPOOL_EXPERIMENTAL
  500. void _free_wrapper(void *ptr)
  501. {
  502. slapi_ch_free(&ptr);
  503. }
  504. #endif
  505. int
  506. main( int argc, char **argv)
  507. {
  508. int return_value = 0;
  509. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  510. daemon_ports_t ports_info = {0};
  511. #ifndef __LP64__
  512. #if defined(__hpux) && !defined(__ia64)
  513. /* for static constructors */
  514. _main();
  515. #endif
  516. #endif
  517. #ifdef MEMPOOL_EXPERIMENTAL
  518. /* to use LDAP C SDK lber functions seemlessly with slapi_ch_malloc funcs,
  519. * setting the slapi_ch_malloc funcs to lber option here. */
  520. {
  521. struct ldap_memalloc_fns memalloc_fns;
  522. memalloc_fns.ldapmem_malloc = (LDAP_MALLOC_CALLBACK *)slapi_ch_malloc;
  523. memalloc_fns.ldapmem_calloc = (LDAP_CALLOC_CALLBACK *)slapi_ch_calloc;
  524. memalloc_fns.ldapmem_realloc = (LDAP_REALLOC_CALLBACK *)slapi_ch_realloc;
  525. memalloc_fns.ldapmem_free = (LDAP_FREE_CALLBACK *)_free_wrapper;
  526. ldap_set_option( 0x1, LDAP_OPT_MEMALLOC_FN_PTRS, &memalloc_fns );
  527. }
  528. #endif
  529. #ifdef LINUX
  530. char *m = getenv( "SLAPD_MXFAST" );
  531. if(m){
  532. int val = atoi(m);
  533. int max = 80 * (sizeof(size_t) / 4);
  534. if(val >= 0 && val <= max){
  535. mallopt(M_MXFAST, val);
  536. }
  537. }
  538. #endif
  539. /*
  540. * Initialize NSPR very early. NSPR supports implicit initialization,
  541. * but it is not bulletproof -- so it is better to be explicit.
  542. */
  543. PR_Init( PR_USER_THREAD, PR_PRIORITY_NORMAL, 0 );
  544. FrontendConfig_init();
  545. /* Pause for the debugger if DEBUG_SLEEP is set in the environment */
  546. {
  547. char *s = getenv( "DEBUG_SLEEP" );
  548. if ( (s != NULL) && isdigit(*s) ) {
  549. int secs = atoi(s);
  550. printf("slapd pid is %d\n", getpid());
  551. sleep(secs);
  552. }
  553. }
  554. /* used to set configfile to the default config file name here */
  555. if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
  556. myname = slapi_ch_strdup( argv[0] );
  557. } else {
  558. myname = slapi_ch_strdup( myname + 1 );
  559. }
  560. process_command_line(argc,argv,myname,&extraname);
  561. if (NULL == slapdFrontendConfig->configdir) {
  562. usage( myname, extraname );
  563. exit( 1 );
  564. }
  565. /* display debugging level if it is anything other than the default */
  566. if ( 0 != ( slapd_ldap_debug & ~LDAP_DEBUG_ANY )) {
  567. slapd_debug_level_log( slapd_ldap_debug );
  568. }
  569. #ifndef LDAP_DONT_USE_SMARTHEAP
  570. MemRegisterTask();
  571. #endif
  572. slapd_init();
  573. g_log_init(1);
  574. vattr_init();
  575. if (slapd_exemode == SLAPD_EXEMODE_REFERRAL) {
  576. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  577. /* make up the config stuff */
  578. referral_set_defaults();
  579. /*
  580. * Process the config files.
  581. */
  582. if (0 == slapd_bootstrap_config(slapdFrontendConfig->configdir)) {
  583. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  584. "The configuration files in directory %s could not be read or were not found. Please refer to the error log or output for more information.\n",
  585. slapdFrontendConfig->configdir);
  586. exit(1);
  587. }
  588. n_port = config_get_port();
  589. s_port = config_get_secureport();
  590. register_objects();
  591. } else {
  592. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  593. /* The 2 calls below have been moved to this place to make sure that
  594. * they are called before setup_internal_backends to avoid bug 524439 */
  595. /*
  596. * The 2 calls below where being sometimes called AFTER
  597. * ldapi_register_extended_op (such fact was being stated and
  598. * reproducible for some optimized installations at startup (bug
  599. * 524439)... Such bad call was happening in the context of
  600. * setup_internal_backends -> dse_read_file -> load_plugin_entry ->
  601. * plugin_setup -> replication_multimaster_plugin_init ->
  602. * slapi_register_plugin -> plugin_setup ->
  603. * multimaster_start_extop_init -> * slapi_pblock_set ->
  604. * ldapi_register_extended_op... Unfortunately, the server
  605. * design is such that it is assumed that ldapi_init_extended_ops is
  606. * always called first.
  607. * THE FIX: Move the two calls below before a call to
  608. * setup_internal_backends (down in this same function)
  609. */
  610. ldapi_init_extended_ops();
  611. /*
  612. * Initialize the default backend. This should be done before we
  613. * process the config. files
  614. */
  615. defbackend_init();
  616. /*
  617. * Register the extensible objects with the factory.
  618. */
  619. register_objects();
  620. /*
  621. * Register the controls that we support.
  622. */
  623. init_controls();
  624. /*
  625. * Initialize the global plugin list lock
  626. */
  627. global_plugin_init();
  628. /*
  629. * Process the config files.
  630. */
  631. if (0 == slapd_bootstrap_config(slapdFrontendConfig->configdir)) {
  632. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  633. "The configuration files in directory %s could not be read or were not found. Please refer to the error log or output for more information.\n",
  634. slapdFrontendConfig->configdir);
  635. exit(1);
  636. }
  637. /* We need to init sasl after we load the bootstrap config since
  638. * the config may be setting the sasl plugin path.
  639. */
  640. init_saslmechanisms();
  641. /* -sduloutre: must be done before any internal search */
  642. /* do it before splitting off to other modes too -robey */
  643. /* -richm: must be done before reading config files */
  644. if (0 != (return_value = compute_init())) {
  645. LDAPDebug(LDAP_DEBUG_ANY, "Initialization Failed 0 %d\n",return_value,0,0);
  646. exit (1);
  647. }
  648. entry_computed_attr_init();
  649. if (0 == setup_internal_backends(slapdFrontendConfig->configdir)) {
  650. slapi_log_error(SLAPI_LOG_FATAL, "startup",
  651. "The configuration files in directory %s could not be read or were not found. Please refer to the error log or output for more information.\n",
  652. slapdFrontendConfig->configdir);
  653. exit(1);
  654. }
  655. n_port = config_get_port();
  656. s_port = config_get_secureport();
  657. }
  658. raise_process_limits(); /* should be done ASAP once config file read */
  659. #ifdef PUMPKIN_HOUR
  660. if ( time( NULL ) > (PUMPKIN_HOUR - 10) ) {
  661. LDAPDebug( LDAP_DEBUG_ANY,
  662. "ERROR: ** This beta software has expired **\n", 0, 0, 0 );
  663. exit( 1 );
  664. }
  665. #endif
  666. /* Set entry points in libslapd */
  667. set_entry_points();
  668. /*
  669. * After we read the config file we should make
  670. * sure that everything we needed to read in has
  671. * been read in and we'll start whatever threads,
  672. * etc the backends need to start
  673. */
  674. /* Important: up 'till here we could be running as root (on unix).
  675. * we believe that we've not created any files before here, otherwise
  676. * they'd be owned by root, which is bad. We're about to change identity
  677. * to some non-root user, but before we do, we call the daemon code
  678. * to let it open the listen sockets. If these sockets are low-numbered,
  679. * we need to be root in order to open them.
  680. */
  681. if ((slapd_exemode == SLAPD_EXEMODE_SLAPD) ||
  682. (slapd_exemode == SLAPD_EXEMODE_REFERRAL)) {
  683. char *listenhost = config_get_listenhost();
  684. char *securelistenhost = config_get_securelistenhost();
  685. ports_info.n_port = (unsigned short)n_port;
  686. if ( slapd_listenhost2addr( listenhost,
  687. &ports_info.n_listenaddr ) != 0 ||
  688. ports_info.n_listenaddr == NULL ) {
  689. slapi_ch_free_string(&listenhost);
  690. slapi_ch_free_string(&securelistenhost);
  691. return(1);
  692. }
  693. slapi_ch_free_string(&listenhost);
  694. ports_info.s_port = (unsigned short)s_port;
  695. if ( slapd_listenhost2addr( securelistenhost,
  696. &ports_info.s_listenaddr ) != 0 ||
  697. ports_info.s_listenaddr == NULL ) {
  698. slapi_ch_free_string(&securelistenhost);
  699. return(1);
  700. }
  701. slapi_ch_free_string(&securelistenhost);
  702. #if defined(ENABLE_LDAPI)
  703. if( config_get_ldapi_switch() &&
  704. config_get_ldapi_filename() != 0)
  705. {
  706. i_port = ports_info.i_port = 1; /* flag ldapi as on */
  707. ports_info.i_listenaddr = (PRNetAddr **)slapi_ch_calloc(2, sizeof(PRNetAddr *));
  708. *ports_info.i_listenaddr = (PRNetAddr *)slapi_ch_calloc(1, sizeof(PRNetAddr));
  709. (*ports_info.i_listenaddr)->local.family = PR_AF_LOCAL;
  710. PL_strncpyz((*ports_info.i_listenaddr)->local.path,
  711. config_get_ldapi_filename(),
  712. sizeof((*ports_info.i_listenaddr)->local.path));
  713. unlink((*ports_info.i_listenaddr)->local.path);
  714. }
  715. #endif /* ENABLE_LDAPI */
  716. return_value = daemon_pre_setuid_init(&ports_info);
  717. if (0 != return_value) {
  718. LDAPDebug( LDAP_DEBUG_ANY, "Failed to init daemon\n",
  719. 0, 0, 0 );
  720. exit(1);
  721. }
  722. }
  723. /* Now, sockets are open, so we can safely change identity now */
  724. return_value = main_setuid(slapdFrontendConfig->localuser);
  725. if (0 != return_value) {
  726. LDAPDebug( LDAP_DEBUG_ANY, "Failed to change user and group identity to that of %s\n",
  727. slapdFrontendConfig->localuser, 0, 0 );
  728. exit(1);
  729. }
  730. /* Do NSS and/or SSL init for those modes other than listening modes */
  731. if ((slapd_exemode != SLAPD_EXEMODE_REFERRAL) &&
  732. (slapd_exemode != SLAPD_EXEMODE_SLAPD)) {
  733. if (slapd_do_all_nss_ssl_init(slapd_exemode, importexport_encrypt,
  734. s_port, &ports_info)) {
  735. return_value = 1;
  736. goto cleanup;
  737. }
  738. }
  739. /*
  740. * if we were called upon to do special database stuff, do it and be
  741. * done.
  742. */
  743. switch ( slapd_exemode ) {
  744. case SLAPD_EXEMODE_LDIF2DB:
  745. return_value = slapd_exemode_ldif2db();
  746. goto cleanup;
  747. break;
  748. case SLAPD_EXEMODE_DB2LDIF:
  749. return_value = slapd_exemode_db2ldif(argc,argv);
  750. goto cleanup;
  751. break;
  752. case SLAPD_EXEMODE_DB2INDEX:
  753. return_value = slapd_exemode_db2index();
  754. goto cleanup;
  755. break;
  756. case SLAPD_EXEMODE_ARCHIVE2DB:
  757. return_value = slapd_exemode_archive2db();
  758. goto cleanup;
  759. break;
  760. case SLAPD_EXEMODE_DB2ARCHIVE:
  761. return_value = slapd_exemode_db2archive();
  762. goto cleanup;
  763. break;
  764. case SLAPD_EXEMODE_REFERRAL:
  765. /* check that all the necessary info was given, then go on */
  766. if (! config_check_referral_mode()) {
  767. LDAPDebug(LDAP_DEBUG_ANY,
  768. "ERROR: No referral URL supplied\n", 0, 0, 0);
  769. usage( myname, extraname );
  770. exit(1);
  771. }
  772. break;
  773. case SLAPD_EXEMODE_SUFFIX2INSTANCE:
  774. return_value = slapd_exemode_suffix2instance();
  775. goto cleanup;
  776. break;
  777. case SLAPD_EXEMODE_UPGRADEDB:
  778. return_value = slapd_exemode_upgradedb();
  779. goto cleanup;
  780. break;
  781. case SLAPD_EXEMODE_UPGRADEDNFORMAT:
  782. return_value = slapd_exemode_upgradednformat();
  783. goto cleanup;
  784. break;
  785. case SLAPD_EXEMODE_DBVERIFY:
  786. return_value = slapd_exemode_dbverify();
  787. goto cleanup;
  788. break;
  789. case SLAPD_EXEMODE_PRINTVERSION:
  790. slapd_print_version(1);
  791. return_value = 1;
  792. goto cleanup;
  793. break;
  794. default:
  795. {
  796. char *rundir = config_get_rundir();
  797. /* Ensure that we can read from and write to our rundir */
  798. if (access(rundir, R_OK | W_OK)) {
  799. LDAPDebug(LDAP_DEBUG_ANY, "Unable to access " CONFIG_RUNDIR_ATTRIBUTE ": %s\n",
  800. slapd_system_strerror(errno), 0, 0);
  801. LDAPDebug(LDAP_DEBUG_ANY, "Ensure that user \"%s\" has read and write "
  802. "permissions on %s\n",
  803. slapdFrontendConfig->localuser, rundir, 0);
  804. LDAPDebug(LDAP_DEBUG_ANY, "Shutting down.\n", 0, 0, 0);
  805. slapi_ch_free_string(&rundir);
  806. return_value = 1;
  807. goto cleanup;
  808. }
  809. slapi_ch_free_string(&rundir);
  810. break;
  811. }
  812. }
  813. /* initialize the normalized DN cache */
  814. ndn_cache_init();
  815. global_backend_lock_init();
  816. /*
  817. * Detach ourselves from the terminal (unless running in debug mode).
  818. * We must detach before we start any threads since detach forks() on
  819. * UNIX.
  820. * Have to detach after ssl_init - the user may be prompted for the PIN
  821. * on the terminal, so it must be open.
  822. */
  823. if (detach(slapd_exemode, importexport_encrypt,
  824. s_port, &ports_info)) {
  825. return_value = 1;
  826. goto cleanup;
  827. }
  828. /*
  829. * Now write our PID to the startup PID file.
  830. * This is used by the start up script to determine our PID quickly
  831. * after we fork, without needing to wait for the 'real' pid file to be
  832. * written. That could take minutes. And the start script will wait
  833. * that long looking for it. With this new 'early pid' file, it can avoid
  834. * doing that, by detecting the pid and watching for the process exiting.
  835. * This removes the blank stares all round from start-slapd when the server
  836. * fails to start for some reason
  837. */
  838. write_start_pid_file();
  839. /* Make sure we aren't going to run slapd in
  840. * a mode that is going to conflict with other
  841. * slapd processes that are currently running
  842. */
  843. if ((slapd_exemode != SLAPD_EXEMODE_REFERRAL) &&
  844. ( add_new_slapd_process(slapd_exemode, db2ldif_dump_replica,
  845. skip_db_protect_check) == -1 )) {
  846. LDAPDebug( LDAP_DEBUG_ANY,
  847. "Shutting down due to possible conflicts with other slapd processes\n",
  848. 0, 0, 0 );
  849. return_value = 1;
  850. goto cleanup;
  851. }
  852. /*
  853. * Now it is safe to log our first startup message. If we were to
  854. * log anything earlier than now it would appear on the admin startup
  855. * screen twice because before we detach everything is sent to both
  856. * stderr and our error log. Yuck.
  857. */
  858. if (1) {
  859. char *versionstring = config_get_versionstring();
  860. char *buildnum = config_get_buildnum();
  861. LDAPDebug( LDAP_DEBUG_ANY, "%s B%s starting up\n",
  862. versionstring, buildnum, 0 );
  863. slapi_ch_free((void **)&buildnum);
  864. slapi_ch_free((void **)&versionstring);
  865. }
  866. /* -sduloutre: compute_init() and entry_computed_attr_init() moved up */
  867. if (slapd_exemode != SLAPD_EXEMODE_REFERRAL) {
  868. int rc;
  869. Slapi_DN *sdn;
  870. fedse_create_startOK(DSE_FILENAME, DSE_STARTOKFILE,
  871. slapdFrontendConfig->configdir);
  872. eq_init(); /* must be done before plugins started */
  873. /* Start the SNMP collator if counters are enabled. */
  874. if (config_get_slapi_counters()) {
  875. snmp_collator_start();
  876. }
  877. ps_init_psearch_system(); /* must come before plugin_startall() */
  878. /* Initailize the mapping tree */
  879. if (mapping_tree_init())
  880. {
  881. LDAPDebug( LDAP_DEBUG_ANY, "Failed to init mapping tree\n",
  882. 0, 0, 0 );
  883. return_value = 1;
  884. goto cleanup;
  885. }
  886. /* initialize UniqueID generator - must be done once backends are started
  887. and event queue is initialized but before plugins are started */
  888. /* Note: This DN is no need to be normalized. */
  889. sdn = slapi_sdn_new_ndn_byval ("cn=uniqueid generator,cn=config");
  890. rc = uniqueIDGenInit (NULL, sdn, slapd_exemode == SLAPD_EXEMODE_SLAPD);
  891. slapi_sdn_free (&sdn);
  892. if (rc != UID_SUCCESS)
  893. {
  894. LDAPDebug( LDAP_DEBUG_ANY,
  895. "Fatal Error---Failed to initialize uniqueid generator; error = %d. "
  896. "Exiting now.\n", rc, 0, 0 );
  897. return_value = 1;
  898. goto cleanup;
  899. }
  900. /* --ugaston: register the start-tls plugin */
  901. if ( slapd_security_library_is_initialized() != 0 ) {
  902. start_tls_register_plugin();
  903. LDAPDebug( LDAP_DEBUG_PLUGIN, "Start TLS plugin registered.\n",
  904. 0, 0, 0 );
  905. }
  906. passwd_modify_register_plugin();
  907. LDAPDebug( LDAP_DEBUG_PLUGIN,
  908. "Password Modify plugin registered.\n", 0, 0, 0 );
  909. /* Cleanup old tasks that may still be in the DSE from a previous
  910. session. Call before plugin_startall since cleanup needs to be
  911. done before plugin_startall where user defined task plugins could
  912. be started.
  913. */
  914. task_cleanup();
  915. /* init the thread data indexes */
  916. slapi_td_dn_init();
  917. slapi_td_plugin_lock_init();
  918. /*
  919. * Initialize password storage in entry extension.
  920. * Need to be initialized before plugin_startall in case stucked
  921. * changes are replicated as soon as the replication plugin is started.
  922. */
  923. pw_exp_init ();
  924. plugin_print_lists();
  925. plugin_startall(argc, argv, 1 /* Start Backends */, 1 /* Start Globals */);
  926. compute_plugins_started();
  927. if (housekeeping_start((time_t)0, NULL) == NULL) {
  928. return_value = 1;
  929. goto cleanup;
  930. }
  931. eq_start(); /* must be done after plugins started */
  932. #ifdef HPUX10
  933. /* HPUX linker voodoo */
  934. if (collation_init == NULL) {
  935. return_value = 1;
  936. goto cleanup;
  937. }
  938. #endif /* HPUX */
  939. normalize_oc();
  940. if (n_port) {
  941. } else if (i_port) {
  942. } else if ( config_get_security()) {
  943. } else {
  944. LDAPDebug( LDAP_DEBUG_ANY,
  945. "Fatal Error---No ports specified. "
  946. "Exiting now.\n", 0, 0, 0 );
  947. return_value = 1;
  948. goto cleanup;
  949. }
  950. }
  951. if (slapd_exemode != SLAPD_EXEMODE_REFERRAL) {
  952. /* else do this after seteuid() */
  953. /* setup cn=tasks tree */
  954. task_init();
  955. /* pw_init() needs to be here since it uses aci function calls. */
  956. pw_init();
  957. /* Initialize the sasl mapping code */
  958. if (sasl_map_init()) {
  959. LDAPDebug( LDAP_DEBUG_ANY, "Failed to initialize sasl mapping code\n", 0, 0, 0 );
  960. }
  961. }
  962. /*
  963. * search_register_reslimits() and daemon_register_reslimits() can
  964. * be called any time before we start accepting client connections.
  965. * We call these even when running in referral mode because they
  966. * do little harm and registering at least one resource limit forces
  967. * the reslimit subsystem to initialize itself... which prevents
  968. * strange error messages from being logged to the error log for
  969. * the first LDAP connection.
  970. */
  971. if ( search_register_reslimits() != SLAPI_RESLIMIT_STATUS_SUCCESS ||
  972. daemon_register_reslimits() != SLAPI_RESLIMIT_STATUS_SUCCESS ) {
  973. return_value = 1;
  974. goto cleanup;
  975. }
  976. {
  977. time( &starttime );
  978. slapd_daemon(&ports_info);
  979. }
  980. LDAPDebug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
  981. reslimit_cleanup();
  982. compute_terminate();
  983. vattr_cleanup();
  984. sasl_map_done();
  985. cleanup:
  986. SSL_ShutdownServerSessionIDCache();
  987. SSL_ClearSessionCache();
  988. ndn_cache_destroy();
  989. NSS_Shutdown();
  990. PR_Cleanup();
  991. #if defined( hpux )
  992. exit( return_value );
  993. #else
  994. return return_value;
  995. #endif
  996. }
  997. #if defined( hpux )
  998. void
  999. signal2sigaction( int s, void *a )
  1000. {
  1001. struct sigaction act;
  1002. memset(&act, 0, sizeof(struct sigaction));
  1003. act.sa_handler = (VFP)a;
  1004. act.sa_flags = 0;
  1005. (void)sigemptyset( &act.sa_mask );
  1006. (void)sigaddset( &act.sa_mask, s );
  1007. (void)sigaction( s, &act, NULL );
  1008. }
  1009. #endif /* hpux */
  1010. static void
  1011. register_objects()
  1012. {
  1013. get_operation_object_type();
  1014. daemon_register_connection();
  1015. get_entry_object_type();
  1016. mapping_tree_get_extension_type ();
  1017. }
  1018. static void
  1019. process_command_line(int argc, char **argv, char *myname,
  1020. char **extraname)
  1021. {
  1022. int i;
  1023. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  1024. char *opts;
  1025. static struct opt_ext *long_opts;
  1026. int longopt_index=0;
  1027. /*
  1028. * Refer to the file getopt_ext.h for an overview of how to use the
  1029. * long option names
  1030. *
  1031. */
  1032. /*
  1033. * when a new option letter is used, please move it from the "available"
  1034. * list to the "used" list.
  1035. *
  1036. */
  1037. /*
  1038. * single-letter options already in use:
  1039. *
  1040. * a C c D E d f G g i
  1041. * L l N m n O o P p r S s T t
  1042. * u v V w x Z z
  1043. *
  1044. * 1
  1045. *
  1046. */
  1047. /*
  1048. * single-letter options still available:
  1049. *
  1050. * A B b e F H h I J j
  1051. * K k M Q q R
  1052. * W X Y y
  1053. *
  1054. * 2 3 4 5 6 7 8 9 0
  1055. *
  1056. */
  1057. char *opts_db2ldif = "vd:D:ENa:rs:x:CSut:n:UmMo1q";
  1058. struct opt_ext long_options_db2ldif[] = {
  1059. {"version",ArgNone,'v'},
  1060. {"debug",ArgRequired,'d'},
  1061. {"dontPrintKey",ArgNone,'n'},
  1062. {"archive",ArgRequired,'a'},
  1063. {"replica",ArgNone,'r'},
  1064. {"include",ArgRequired,'s'},
  1065. {"exclude",ArgRequired,'x'},
  1066. /*{"whatshouldwecallthis",ArgNone,'C'},*/
  1067. {"allowMultipleProcesses",ArgNone,'S'},
  1068. {"noUniqueIds",ArgNone,'u'},
  1069. {"configDir",ArgRequired,'D'},
  1070. {"encrypt",ArgOptional,'E'},
  1071. {"nowrap",ArgNone,'U'},
  1072. {"minimalEncode",ArgNone,'m'},
  1073. {"oneOutputFile",ArgNone,'o'},
  1074. {"multipleOutputFile",ArgNone,'M'},
  1075. {"noVersionNum",ArgNone,'1'},
  1076. {"quiet",ArgNone,'q'},
  1077. {0,0,0}};
  1078. char *opts_ldif2db = "vd:i:g:G:n:s:x:NOCc:St:D:Eq";
  1079. struct opt_ext long_options_ldif2db[] = {
  1080. {"version",ArgNone,'v'},
  1081. {"debug",ArgRequired,'d'},
  1082. {"ldiffile",ArgRequired,'i'},
  1083. {"generateUniqueId",ArgOptional,'g'},
  1084. {"backend",ArgRequired,'n'},
  1085. {"include",ArgRequired,'s'},
  1086. {"exclude",ArgRequired,'x'},
  1087. {"noindex",ArgNone,'O'},
  1088. /*{"whatshouldwecallthis",ArgNone,'C'},*/
  1089. /*{"whatshouldwecallthis",ArgRequired,'c'},*/
  1090. {"allowMultipleProcesses",ArgNone,'S'},
  1091. {"namespaceid", ArgRequired, 'G'},
  1092. {"nostate",ArgNone,'Z'},
  1093. {"configDir",ArgRequired,'D'},
  1094. {"encrypt",ArgOptional,'E'},
  1095. {"quiet",ArgNone,'q'},
  1096. {0,0,0}};
  1097. char *opts_archive2db = "vd:i:a:n:SD:q";
  1098. struct opt_ext long_options_archive2db[] = {
  1099. {"version",ArgNone,'v'},
  1100. {"debug",ArgRequired,'d'},
  1101. {"pidfile",ArgRequired,'i'},
  1102. {"archive",ArgRequired,'a'},
  1103. {"backEndInstName",ArgRequired,'n'},
  1104. {"allowMultipleProcesses",ArgNone,'S'},
  1105. {"configDir",ArgRequired,'D'},
  1106. {"quiet",ArgNone,'q'},
  1107. {0,0,0}};
  1108. char *opts_db2archive = "vd:i:a:SD:q";
  1109. struct opt_ext long_options_db2archive[] = {
  1110. {"version",ArgNone,'v'},
  1111. {"debug",ArgRequired,'d'},
  1112. {"pidfile",ArgRequired,'i'},
  1113. {"archive",ArgRequired,'a'},
  1114. {"allowMultipleProcesses",ArgNone,'S'},
  1115. {"configDir",ArgRequired,'D'},
  1116. {"quiet",ArgNone,'q'},
  1117. {0,0,0}};
  1118. char *opts_db2index = "vd:a:t:T:SD:n:s:x:";
  1119. struct opt_ext long_options_db2index[] = {
  1120. {"version",ArgNone,'v'},
  1121. {"debug",ArgRequired,'d'},
  1122. {"backend",ArgRequired,'n'},
  1123. {"archive",ArgRequired,'a'},
  1124. {"indexAttribute",ArgRequired,'t'},
  1125. {"vlvIndex",ArgRequired,'T'},
  1126. {"allowMultipleProcesses",ArgNone,'S'},
  1127. {"configDir",ArgRequired,'D'},
  1128. {"include",ArgRequired,'s'},
  1129. {"exclude",ArgRequired,'x'},
  1130. {0,0,0}};
  1131. char *opts_upgradedb = "vfrd:a:D:";
  1132. struct opt_ext long_options_upgradedb[] = {
  1133. {"version",ArgNone,'v'},
  1134. {"debug",ArgRequired,'d'},
  1135. {"force",ArgNone,'f'},
  1136. {"dn2rdn",ArgNone,'r'},
  1137. {"archive",ArgRequired,'a'},
  1138. {"configDir",ArgRequired,'D'},
  1139. {0,0,0}};
  1140. char *opts_upgradednformat = "vd:a:n:D:N";
  1141. struct opt_ext long_options_upgradednformat[] = {
  1142. {"version",ArgNone,'v'},
  1143. {"debug",ArgRequired,'d'},
  1144. {"backend",ArgRequired,'n'},
  1145. {"archive",ArgRequired,'a'}, /* Path to the work db instance dir */
  1146. {"configDir",ArgRequired,'D'},
  1147. {"dryrun",ArgNone,'N'},
  1148. {0,0,0}};
  1149. char *opts_dbverify = "vVfd:n:D:a:";
  1150. struct opt_ext long_options_dbverify[] = {
  1151. {"version",ArgNone,'v'},
  1152. {"debug",ArgRequired,'d'},
  1153. {"backend",ArgRequired,'n'},
  1154. {"configDir",ArgRequired,'D'},
  1155. {"verbose",ArgNone,'V'},
  1156. {"dbdir",ArgRequired,'a'},
  1157. {0,0,0}};
  1158. char *opts_referral = "vd:p:r:SD:";
  1159. struct opt_ext long_options_referral[] = {
  1160. {"version",ArgNone,'v'},
  1161. {"debug",ArgRequired,'d'},
  1162. {"port",ArgRequired,'p'},
  1163. {"referralMode",ArgRequired,'r'},
  1164. {"allowMultipleProcesses",ArgNone,'S'},
  1165. {"configDir",ArgRequired,'D'},
  1166. {0,0,0}};
  1167. char *opts_suffix2instance = "s:D:";
  1168. struct opt_ext long_options_suffix2instance[] = {
  1169. {"suffix",ArgRequired,'s'},
  1170. {"instanceDir",ArgRequired,'D'},
  1171. {0,0,0}};
  1172. char *opts_slapd = "vVd:i:SD:w:";
  1173. struct opt_ext long_options_slapd[] = {
  1174. {"version",ArgNone,'v'},
  1175. {"versionFull",ArgNone,'V'},
  1176. {"debug",ArgRequired,'d'},
  1177. {"pidfile",ArgRequired,'i'},
  1178. {"allowMultipleProcesses",ArgNone,'S'},
  1179. {"configDir",ArgRequired,'D'},
  1180. {"startpidfile",ArgRequired,'w'},
  1181. {0,0,0}};
  1182. /*
  1183. * determine which of serveral modes we are executing in.
  1184. */
  1185. *extraname = NULL;
  1186. if (( slapd_exemode = name2exemode( myname, myname, 0 ))
  1187. == SLAPD_EXEMODE_UNKNOWN ) {
  1188. if ( argv[1] != NULL && argv[1][0] != '-' ) {
  1189. slapd_exemode = name2exemode( myname, argv[1], 1 );
  1190. *extraname = argv[1];
  1191. optind_ext = 2; /* make getopt() skip argv[1] */
  1192. optind = 2;
  1193. }
  1194. }
  1195. if ( slapd_exemode == SLAPD_EXEMODE_UNKNOWN ) {
  1196. slapd_exemode = SLAPD_EXEMODE_SLAPD; /* default */
  1197. }
  1198. /*
  1199. * richm: If running in regular slapd server mode, allow the front
  1200. * end dse files (dse.ldif and ldbm.ldif) to be written in case of
  1201. * additions or modifications. In all other modes, these files
  1202. * should only be read and never written.
  1203. */
  1204. if (slapd_exemode == SLAPD_EXEMODE_SLAPD ||
  1205. slapd_exemode == SLAPD_EXEMODE_ARCHIVE2DB || /* bak2db adjusts config */
  1206. slapd_exemode == SLAPD_EXEMODE_UPGRADEDB) /* update idl-switch */
  1207. dse_unset_dont_ever_write_dse_files();
  1208. /* maintain compatibility with pre-5.x options */
  1209. switch( slapd_exemode ) {
  1210. case SLAPD_EXEMODE_DB2LDIF:
  1211. opts = opts_db2ldif;
  1212. long_opts = long_options_db2ldif;
  1213. break;
  1214. case SLAPD_EXEMODE_LDIF2DB:
  1215. opts = opts_ldif2db;
  1216. long_opts = long_options_ldif2db;
  1217. break;
  1218. case SLAPD_EXEMODE_ARCHIVE2DB:
  1219. opts = opts_archive2db;
  1220. long_opts = long_options_archive2db;
  1221. break;
  1222. case SLAPD_EXEMODE_DB2ARCHIVE:
  1223. init_cmd_shutdown_detect();
  1224. opts = opts_db2archive;
  1225. long_opts = long_options_db2archive;
  1226. break;
  1227. case SLAPD_EXEMODE_DB2INDEX:
  1228. opts = opts_db2index;
  1229. long_opts = long_options_db2index;
  1230. break;
  1231. case SLAPD_EXEMODE_REFERRAL:
  1232. opts = opts_referral;
  1233. long_opts = long_options_referral;
  1234. break;
  1235. case SLAPD_EXEMODE_SUFFIX2INSTANCE:
  1236. opts = opts_suffix2instance;
  1237. long_opts = long_options_suffix2instance;
  1238. break;
  1239. case SLAPD_EXEMODE_UPGRADEDB:
  1240. opts = opts_upgradedb;
  1241. long_opts = long_options_upgradedb;
  1242. break;
  1243. case SLAPD_EXEMODE_UPGRADEDNFORMAT:
  1244. opts = opts_upgradednformat;
  1245. long_opts = long_options_upgradednformat;
  1246. break;
  1247. case SLAPD_EXEMODE_DBVERIFY:
  1248. opts = opts_dbverify;
  1249. long_opts = long_options_dbverify;
  1250. break;
  1251. default: /* SLAPD_EXEMODE_SLAPD */
  1252. opts = opts_slapd;
  1253. long_opts = long_options_slapd;
  1254. }
  1255. while ( (i = getopt_ext( argc, argv, opts,
  1256. long_opts, &longopt_index)) != EOF ) {
  1257. char *configdir = 0;
  1258. switch ( i ) {
  1259. #ifdef LDAP_DEBUG
  1260. case 'd': /* turn on debugging */
  1261. if ( optarg_ext[0] == '?'
  1262. || 0 == strcasecmp( optarg_ext, "help" )) {
  1263. slapd_debug_level_usage();
  1264. exit( 1 );
  1265. } else {
  1266. should_detach = 0;
  1267. slapd_ldap_debug = slapd_debug_level_string2level( optarg_ext );
  1268. if ( slapd_ldap_debug < 0 ) {
  1269. slapd_debug_level_usage();
  1270. exit( 1 );
  1271. }
  1272. slapd_ldap_debug |= LDAP_DEBUG_ANY;
  1273. }
  1274. break;
  1275. #else
  1276. case 'd': /* turn on debugging */
  1277. fprintf( stderr,
  1278. "must compile with LDAP_DEBUG for debugging\n" );
  1279. break;
  1280. #endif
  1281. case 'D': /* config dir */
  1282. configdir = rel2abspath( optarg_ext );
  1283. if ( config_set_configdir( "configdir (-D)",
  1284. configdir, errorbuf, 1) != LDAP_SUCCESS ) {
  1285. fprintf( stderr, "%s: aborting now\n", errorbuf );
  1286. usage( myname, *extraname );
  1287. exit( 1 );
  1288. }
  1289. slapi_ch_free((void **)&configdir);
  1290. break;
  1291. case 'p': /* port on which to listen (referral mode only) */
  1292. if ( config_set_port ( "portnumber (-p)", optarg_ext,
  1293. errorbuf, CONFIG_APPLY ) != LDAP_SUCCESS ) {
  1294. fprintf( stderr, "%s: aborting now\n", errorbuf );
  1295. usage( myname, *extraname );
  1296. exit( 1 );
  1297. }
  1298. break;
  1299. case 'i': /* set pid log file or ldif2db LDIF file */
  1300. if ( slapd_exemode == SLAPD_EXEMODE_LDIF2DB ) {
  1301. char *p;
  1302. /* if LDIF comes through standard input, skip path checking */
  1303. if ( optarg_ext[0] != '-' || strlen(optarg_ext) != 1) {
  1304. if ( optarg_ext[ 0 ] != '/' ) {
  1305. fprintf( stderr, "%s file could not be opened: absolute path "
  1306. " required.\n", optarg_ext );
  1307. break;
  1308. }
  1309. }
  1310. p = (char *) slapi_ch_malloc(strlen(optarg_ext) + 1);
  1311. strcpy(p, optarg_ext);
  1312. charray_add(&ldif_file, p);
  1313. ldif_files++;
  1314. } else {
  1315. pid_file = rel2abspath( optarg_ext );
  1316. }
  1317. break;
  1318. case 'w': /* set startup pid file */
  1319. start_pid_file = rel2abspath( optarg_ext );
  1320. break;
  1321. case 'n': /* which backend to do ldif2db/bak2db for */
  1322. if (slapd_exemode == SLAPD_EXEMODE_LDIF2DB ||
  1323. slapd_exemode == SLAPD_EXEMODE_UPGRADEDNFORMAT ||
  1324. slapd_exemode == SLAPD_EXEMODE_DB2INDEX ||
  1325. slapd_exemode == SLAPD_EXEMODE_ARCHIVE2DB) {
  1326. /* The -n argument will give the name of a backend instance. */
  1327. cmd_line_instance_name = optarg_ext;
  1328. } else if (slapd_exemode == SLAPD_EXEMODE_DB2LDIF ||
  1329. slapd_exemode == SLAPD_EXEMODE_DBVERIFY) {
  1330. char *s = slapi_ch_strdup(optarg_ext);
  1331. charray_add(&cmd_line_instance_names, s);
  1332. }
  1333. break;
  1334. case 's': /* which suffix to include in import/export */
  1335. {
  1336. int rc = charray_normdn_add(&db2ldif_include, optarg_ext, NULL);
  1337. if (rc < 0) {
  1338. fprintf(stderr, "Invalid dn: -s %s\n", optarg_ext);
  1339. usage(myname, *extraname);
  1340. exit(1);
  1341. }
  1342. }
  1343. break;
  1344. case 'x': /* which suffix to exclude in import/export */
  1345. {
  1346. int rc = charray_normdn_add(&db2ldif_exclude, optarg_ext, NULL);
  1347. if (rc < 0) {
  1348. fprintf(stderr, "Invalid dn: -x %s\n", optarg_ext);
  1349. usage(myname, *extraname);
  1350. exit(1);
  1351. }
  1352. }
  1353. break;
  1354. case 'r': /* db2ldif for replication */
  1355. if (slapd_exemode == SLAPD_EXEMODE_REFERRAL) {
  1356. if (config_set_referral_mode( "referral (-r)", optarg_ext,
  1357. errorbuf, CONFIG_APPLY) != LDAP_SUCCESS) {
  1358. fprintf(stderr, "%s: aborting now\n", errorbuf);
  1359. usage(myname, *extraname);
  1360. exit(1);
  1361. }
  1362. break;
  1363. } else if ( slapd_exemode == SLAPD_EXEMODE_UPGRADEDB ) {
  1364. upgradedb_flags |= SLAPI_UPGRADEDB_DN2RDN;
  1365. break;
  1366. } else if (slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1367. usage( myname, *extraname );
  1368. exit( 1 );
  1369. }
  1370. db2ldif_dump_replica = 1;
  1371. break;
  1372. case 'N': /* do not do ldif2db duplicate value check */
  1373. /* Or dryrun mode for upgradednformat */
  1374. if ( slapd_exemode != SLAPD_EXEMODE_LDIF2DB &&
  1375. slapd_exemode != SLAPD_EXEMODE_DB2LDIF &&
  1376. slapd_exemode != SLAPD_EXEMODE_UPGRADEDNFORMAT) {
  1377. usage( myname, *extraname );
  1378. exit( 1 );
  1379. }
  1380. /*
  1381. * -N flag is obsolete, but we silently accept it
  1382. * so we don't break customer's scripts.
  1383. */
  1384. /* The -N flag now does what the -n flag used to do for db2ldif.
  1385. * This is so -n cane be used for the instance name just like
  1386. * with ldif2db. */
  1387. if ( slapd_exemode == SLAPD_EXEMODE_DB2LDIF ) {
  1388. ldif_printkey &= ~EXPORT_PRINTKEY;
  1389. }
  1390. if ( slapd_exemode == SLAPD_EXEMODE_UPGRADEDNFORMAT ) {
  1391. upgradednformat_dryrun = 1;
  1392. }
  1393. break;
  1394. case 'U': /* db2ldif only */
  1395. if ( slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1396. usage( myname, *extraname );
  1397. exit( 1 );
  1398. }
  1399. /*
  1400. * don't fold (wrap) long lines (default is to fold),
  1401. * as of ldapsearch -T
  1402. */
  1403. ldif_printkey |= EXPORT_NOWRAP;
  1404. break;
  1405. case 'm': /* db2ldif only */
  1406. if ( slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1407. usage( myname, *extraname );
  1408. exit( 1 );
  1409. }
  1410. /* minimal base64 encoding */
  1411. ldif_printkey |= EXPORT_MINIMAL_ENCODING;
  1412. break;
  1413. case 'M': /* db2ldif only */
  1414. if ( slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1415. usage( myname, *extraname );
  1416. exit( 1 );
  1417. }
  1418. /*
  1419. * output ldif is stored in several file called intance_filename.
  1420. * by default, all instances are stored in the single filename.
  1421. */
  1422. ldif_printkey &= ~EXPORT_APPENDMODE;
  1423. break;
  1424. case 'o': /* db2ldif only */
  1425. if ( slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1426. usage( myname, *extraname );
  1427. exit( 1 );
  1428. }
  1429. /*
  1430. * output ldif is stored in one file.
  1431. * by default, each instance is stored in instance_filename.
  1432. */
  1433. ldif_printkey |= EXPORT_APPENDMODE;
  1434. break;
  1435. case 'C':
  1436. if (slapd_exemode == SLAPD_EXEMODE_LDIF2DB) {
  1437. /* used to mean "Cool new import" (which is now
  1438. * the default) -- ignore
  1439. */
  1440. break;
  1441. }
  1442. if (slapd_exemode == SLAPD_EXEMODE_DB2LDIF) {
  1443. /* possibly corrupted db -- don't look at any
  1444. * file except id2entry. yet another overloaded
  1445. * flag.
  1446. */
  1447. ldif_printkey |= EXPORT_ID2ENTRY_ONLY;
  1448. break;
  1449. }
  1450. usage( myname, *extraname );
  1451. exit( 1 );
  1452. case 'c': /* merge chunk size for Cool new import */
  1453. if ( slapd_exemode != SLAPD_EXEMODE_LDIF2DB ) {
  1454. usage( myname, *extraname );
  1455. exit( 1 );
  1456. }
  1457. ldif2db_removedupvals = atoi(optarg_ext); /* We overload this flag---ok since we always check for dupes in the new code */
  1458. break;
  1459. case 'O': /* only create core db, no attr indexes */
  1460. if ( slapd_exemode != SLAPD_EXEMODE_LDIF2DB ) {
  1461. usage( myname, *extraname );
  1462. exit( 1 );
  1463. }
  1464. ldif2db_noattrindexes = 1;
  1465. break;
  1466. case 't': /* attribute type to index - may be repeated */
  1467. case 'T': /* VLV Search to index - may be repeated */
  1468. if ( slapd_exemode == SLAPD_EXEMODE_DB2INDEX ) {
  1469. char *p= slapi_ch_smprintf("%c%s",i,optarg_ext);
  1470. charray_add( &db2index_attrs, p);
  1471. break;
  1472. }
  1473. usage( myname, *extraname );
  1474. exit(1);
  1475. case 'v': /* print version and exit */
  1476. slapd_print_version(0);
  1477. exit( 1 );
  1478. break;
  1479. case 'V':
  1480. if ( slapd_exemode == SLAPD_EXEMODE_DBVERIFY ) {
  1481. dbverify_verbose = 1;
  1482. } else {
  1483. slapd_exemode = SLAPD_EXEMODE_PRINTVERSION;
  1484. }
  1485. break;
  1486. case 'a': /* archive pathname for db */
  1487. if ( slapd_exemode == SLAPD_EXEMODE_DBVERIFY ) {
  1488. dbverify_dbdir = optarg_ext;
  1489. } else {
  1490. archive_name = optarg_ext;
  1491. }
  1492. break;
  1493. case 'Z':
  1494. if (slapd_exemode == SLAPD_EXEMODE_LDIF2DB)
  1495. {
  1496. break;
  1497. }
  1498. usage( myname, *extraname );
  1499. exit(1);
  1500. case 'S': /* skip the check for slad running in conflicting modes */
  1501. skip_db_protect_check = 1;
  1502. break;
  1503. case 'u': /* do not dump uniqueid for db2ldif */
  1504. if ( slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1505. usage( myname, *extraname );
  1506. exit( 1 );
  1507. }
  1508. db2ldif_dump_uniqueid = 0;
  1509. break;
  1510. case 'g': /* generate uniqueid for ldif2db */
  1511. if ( slapd_exemode != SLAPD_EXEMODE_LDIF2DB ) {
  1512. usage( myname, *extraname );
  1513. exit( 1 );
  1514. }
  1515. if (optarg_ext == NULL){
  1516. printf ("ldif2db: generation type is not specified for -g; "
  1517. "random generation is used\n");
  1518. ldif2db_generate_uniqueid = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  1519. }
  1520. else if (strcasecmp (optarg_ext, "none") == 0)
  1521. ldif2db_generate_uniqueid = SLAPI_UNIQUEID_GENERATE_NONE;
  1522. else if (strcasecmp (optarg_ext, "deterministic") == 0) /* name based */
  1523. ldif2db_generate_uniqueid = SLAPI_UNIQUEID_GENERATE_NAME_BASED;
  1524. else /* default - time based */
  1525. ldif2db_generate_uniqueid = SLAPI_UNIQUEID_GENERATE_TIME_BASED;
  1526. break;
  1527. case 'G': /* namespace id for name based uniqueid generation for ldif2db */
  1528. if ( slapd_exemode != SLAPD_EXEMODE_LDIF2DB ) {
  1529. usage( myname, *extraname );
  1530. exit( 1 );
  1531. }
  1532. ldif2db_namespaceid = optarg_ext;
  1533. break;
  1534. case 'E': /* encrypt data if importing, decrypt if exporting */
  1535. if ( (slapd_exemode != SLAPD_EXEMODE_LDIF2DB) && (slapd_exemode != SLAPD_EXEMODE_DB2LDIF)) {
  1536. usage( myname, *extraname );
  1537. exit( 1 );
  1538. }
  1539. importexport_encrypt = 1;
  1540. break;
  1541. case 'f': /* upgradedb only */
  1542. if ( slapd_exemode != SLAPD_EXEMODE_UPGRADEDB ) {
  1543. usage( myname, *extraname );
  1544. exit( 1 );
  1545. }
  1546. upgradedb_flags |= SLAPI_UPGRADEDB_FORCE;
  1547. break;
  1548. case '1': /* db2ldif only */
  1549. if ( slapd_exemode != SLAPD_EXEMODE_DB2LDIF ) {
  1550. usage( myname, *extraname );
  1551. exit( 1 );
  1552. }
  1553. /*
  1554. * do not output "version: 1" to the ldif file
  1555. */
  1556. ldif_printkey |= EXPORT_NOVERSION;
  1557. break;
  1558. case 'q': /* quiet option for db2ldif, ldif2db, db2bak, bak2db */
  1559. is_quiet = 1;
  1560. break;
  1561. default:
  1562. usage( myname, *extraname );
  1563. exit( 1 );
  1564. }
  1565. }
  1566. if ((NULL != cmd_line_instance_names)
  1567. && (NULL != cmd_line_instance_names[1])
  1568. && (ldif_printkey & EXPORT_APPENDMODE))
  1569. {
  1570. fprintf(stderr, "WARNING: several backends are being"
  1571. " exported to a single ldif file\n");
  1572. fprintf(stderr, " use option -M to export to"
  1573. " multiple ldif files\n");
  1574. }
  1575. /* Any leftover arguments? */
  1576. if ( optind_last > optind ) {
  1577. usage( myname, *extraname );
  1578. exit( 1 );
  1579. }
  1580. return;
  1581. }
  1582. static int
  1583. lookup_instance_name_by_suffix(char *suffix,
  1584. char ***suffixes, char ***instances, int isexact)
  1585. {
  1586. Slapi_PBlock *pb = slapi_pblock_new();
  1587. Slapi_Entry **entries = NULL, **ep;
  1588. char *query;
  1589. char *backend;
  1590. char *fullsuffix;
  1591. int rval = -1;
  1592. if (pb == NULL)
  1593. goto done;
  1594. if (isexact) {
  1595. query = slapi_filter_sprintf("(&(objectclass=nsmappingtree)(|(cn=\"%s%s\")(cn=%s%s)))",
  1596. ESC_NEXT_VAL, suffix, ESC_NEXT_VAL, suffix);
  1597. if (query == NULL)
  1598. goto done;
  1599. /* Note: This DN is no need to be normalized. */
  1600. slapi_search_internal_set_pb(pb, "cn=mapping tree,cn=config",
  1601. LDAP_SCOPE_SUBTREE, query, NULL, 0, NULL, NULL,
  1602. (void *)plugin_get_default_component_id(), 0);
  1603. slapi_search_internal_pb(pb);
  1604. slapi_ch_free((void **)&query);
  1605. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  1606. if (rval != LDAP_SUCCESS)
  1607. goto done;
  1608. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1609. if ((entries == NULL) || (entries[0] == NULL))
  1610. goto done;
  1611. } else {
  1612. char *suffixp = suffix;
  1613. while (NULL != suffixp && strlen(suffixp) > 0) {
  1614. query = slapi_filter_sprintf("(&(objectclass=nsmappingtree)(|(cn=*%s%s\")(cn=*%s%s)))",
  1615. ESC_NEXT_VAL, suffixp, ESC_NEXT_VAL, suffixp);
  1616. if (query == NULL)
  1617. goto done;
  1618. /* Note: This DN is no need to be normalized. */
  1619. slapi_search_internal_set_pb(pb, "cn=mapping tree,cn=config",
  1620. LDAP_SCOPE_SUBTREE, query, NULL, 0, NULL, NULL,
  1621. (void *)plugin_get_default_component_id(), 0);
  1622. slapi_search_internal_pb(pb);
  1623. slapi_ch_free((void **)&query);
  1624. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);
  1625. if (rval != LDAP_SUCCESS)
  1626. goto done;
  1627. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1628. if ((entries == NULL) || (entries[0] == NULL)) {
  1629. suffixp = strchr(suffixp, ','); /* get a parent dn */
  1630. if (NULL != suffixp) {
  1631. suffixp++;
  1632. }
  1633. } else {
  1634. break; /* found backend entries */
  1635. }
  1636. }
  1637. }
  1638. rval = 0;
  1639. for (ep = entries; ep && *ep; ep++) {
  1640. backend = slapi_entry_attr_get_charptr(*ep, "nsslapd-backend");
  1641. if (backend) {
  1642. charray_add(instances, backend);
  1643. if (suffixes) {
  1644. fullsuffix = slapi_entry_attr_get_charptr(*ep, "cn");
  1645. charray_add(suffixes, fullsuffix); /* NULL is ok */
  1646. }
  1647. }
  1648. }
  1649. done:
  1650. slapi_free_search_results_internal(pb);
  1651. slapi_pblock_destroy(pb);
  1652. return rval;
  1653. }
  1654. int
  1655. lookup_instance_name_by_suffixes(char **included, char **excluded,
  1656. char ***instances)
  1657. {
  1658. char **incl_instances, **excl_instances;
  1659. char **p;
  1660. int rval = -1;
  1661. incl_instances = NULL;
  1662. for (p = included; p && *p; p++) {
  1663. if (lookup_instance_name_by_suffix(*p, NULL, &incl_instances, 0) < 0)
  1664. return rval;
  1665. }
  1666. excl_instances = NULL;
  1667. for (p = excluded; p && *p; p++) {
  1668. if (lookup_instance_name_by_suffix(*p, NULL, &excl_instances, 0) < 0)
  1669. return rval;
  1670. }
  1671. rval = 0;
  1672. charray_subtract(incl_instances, excl_instances, NULL);
  1673. charray_free(excl_instances);
  1674. *instances = incl_instances;
  1675. return rval;
  1676. }
  1677. /* helper function for ldif2db & friends -- given an instance name, lookup
  1678. * the plugin name in the DSE. this assumes the DSE has already been loaded.
  1679. */
  1680. static struct slapdplugin *lookup_plugin_by_instance_name(const char *name)
  1681. {
  1682. Slapi_Entry **entries = NULL;
  1683. Slapi_PBlock *pb = slapi_pblock_new();
  1684. struct slapdplugin *plugin;
  1685. char *query, *dn, *cn;
  1686. int ret = 0;
  1687. if (pb == NULL)
  1688. return NULL;
  1689. query = slapi_filter_sprintf("(&(cn=%s%s)(objectclass=nsBackendInstance))", ESC_AND_NORM_NEXT_VAL, name);
  1690. if (query == NULL) {
  1691. slapi_pblock_destroy(pb);
  1692. return NULL;
  1693. }
  1694. /* Note: This DN is no need to be normalized. */
  1695. slapi_search_internal_set_pb(pb, "cn=plugins,cn=config",
  1696. LDAP_SCOPE_SUBTREE, query, NULL, 0, NULL, NULL,
  1697. (void *)plugin_get_default_component_id(), 0);
  1698. slapi_search_internal_pb(pb);
  1699. slapi_ch_free((void **)&query);
  1700. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1701. if (ret != LDAP_SUCCESS) {
  1702. slapi_free_search_results_internal(pb);
  1703. slapi_pblock_destroy(pb);
  1704. return NULL;
  1705. }
  1706. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1707. if ((entries == NULL) || (entries[0] == NULL)) {
  1708. slapi_free_search_results_internal(pb);
  1709. slapi_pblock_destroy(pb);
  1710. return NULL;
  1711. }
  1712. /* okay -- have the entry for this instance, now let's chop up the dn */
  1713. /* parent dn is the plugin */
  1714. dn = slapi_dn_parent(slapi_entry_get_dn(entries[0]));
  1715. /* clean up */
  1716. slapi_free_search_results_internal(pb);
  1717. entries = NULL;
  1718. slapi_pblock_destroy(pb);
  1719. pb = NULL; /* this seems redundant . . . until we add code after this line */
  1720. /* now... look up the parent */
  1721. pb = slapi_pblock_new();
  1722. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_BASE,
  1723. "(objectclass=nsSlapdPlugin)", NULL, 0, NULL, NULL,
  1724. (void *)plugin_get_default_component_id(), 0);
  1725. slapi_search_internal_pb(pb);
  1726. slapi_ch_free((void **)&dn);
  1727. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  1728. if (ret != LDAP_SUCCESS) {
  1729. slapi_free_search_results_internal(pb);
  1730. slapi_pblock_destroy(pb);
  1731. return NULL;
  1732. }
  1733. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1734. if ((entries == NULL) || (entries[0] == NULL)) {
  1735. slapi_free_search_results_internal(pb);
  1736. slapi_pblock_destroy(pb);
  1737. return NULL;
  1738. }
  1739. cn = slapi_entry_attr_get_charptr(entries[0], "cn");
  1740. slapi_free_search_results_internal(pb);
  1741. slapi_pblock_destroy(pb);
  1742. plugin = plugin_get_by_name(cn);
  1743. slapi_ch_free((void **)&cn);
  1744. return plugin;
  1745. }
  1746. static int
  1747. slapd_exemode_ldif2db()
  1748. {
  1749. int return_value= 0;
  1750. Slapi_PBlock pb;
  1751. struct slapdplugin *plugin;
  1752. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  1753. if ( ldif_file == NULL ) {
  1754. LDAPDebug( LDAP_DEBUG_ANY,
  1755. "ERROR: Required argument -i <ldiffile> missing\n",
  1756. 0, 0, 0 );
  1757. usage( myname, extraname );
  1758. return 1;
  1759. }
  1760. /* this should be the first time to be called! if the init order
  1761. * is ever changed, these lines should be changed (or erased)!
  1762. */
  1763. mapping_tree_init();
  1764. /*
  1765. * if instance is given, just use it to get the backend.
  1766. * otherwise, we use included/excluded suffix list to specify a backend.
  1767. */
  1768. if (NULL == cmd_line_instance_name) {
  1769. char **instances, **ip;
  1770. int counter;
  1771. if (lookup_instance_name_by_suffixes(db2ldif_include, db2ldif_exclude,
  1772. &instances) < 0) {
  1773. LDAPDebug(LDAP_DEBUG_ANY,
  1774. "ERROR: backend instances name [-n <name>] or "
  1775. "included suffix [-s <suffix>] need to be specified.\n",
  1776. 0, 0, 0);
  1777. return 1;
  1778. }
  1779. if (instances) {
  1780. for (ip = instances, counter = 0; ip && *ip; ip++, counter++)
  1781. ;
  1782. if (counter == 0) {
  1783. LDAPDebug(LDAP_DEBUG_ANY,
  1784. "ERROR 1: There is no backend instance to import to.\n",
  1785. 0, 0, 0);
  1786. return 1;
  1787. } else if (counter > 1) {
  1788. int i;
  1789. LDAPDebug(LDAP_DEBUG_ANY,
  1790. "ERROR: There are multiple backend instances specified:\n",
  1791. 0, 0, 0);
  1792. for (i = 0; i < counter; i++)
  1793. LDAPDebug(LDAP_DEBUG_ANY, " : %s\n",
  1794. instances[i], 0, 0);
  1795. return 1;
  1796. } else {
  1797. LDAPDebug(LDAP_DEBUG_ANY, "Backend Instance: %s\n",
  1798. *instances, 0, 0);
  1799. cmd_line_instance_name = *instances;
  1800. }
  1801. } else {
  1802. LDAPDebug(LDAP_DEBUG_ANY,
  1803. "ERROR 2: There is no backend instance to import to.\n",
  1804. 0, 0, 0);
  1805. return 1;
  1806. }
  1807. }
  1808. plugin = lookup_plugin_by_instance_name(cmd_line_instance_name);
  1809. if (plugin == NULL) {
  1810. LDAPDebug(LDAP_DEBUG_ANY,
  1811. "ERROR: Could not find backend '%s'.\n",
  1812. cmd_line_instance_name, 0, 0);
  1813. return 1;
  1814. }
  1815. /* Make sure we aren't going to run slapd in
  1816. * a mode that is going to conflict with other
  1817. * slapd processes that are currently running
  1818. */
  1819. if ( add_new_slapd_process(slapd_exemode, db2ldif_dump_replica,
  1820. skip_db_protect_check) == -1 ) {
  1821. LDAPDebug( LDAP_DEBUG_ANY,
  1822. "Shutting down due to possible conflicts with other slapd processes\n",
  1823. 0, 0, 0 );
  1824. return 1;
  1825. }
  1826. /* check for slapi v2 support */
  1827. if (! SLAPI_PLUGIN_IS_V2(plugin)) {
  1828. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: %s is too old to reindex all.\n",
  1829. plugin->plg_name, 0, 0);
  1830. return 1;
  1831. }
  1832. if (!is_quiet) {
  1833. slapd_ldap_debug |= LDAP_DEBUG_BACKLDBM;
  1834. }
  1835. if (!(slapd_ldap_debug & LDAP_DEBUG_BACKLDBM)) {
  1836. g_set_detached(1);
  1837. }
  1838. memset( &pb, '\0', sizeof(pb) );
  1839. pb.pb_backend = NULL;
  1840. pb.pb_plugin = plugin;
  1841. pb.pb_removedupvals = ldif2db_removedupvals;
  1842. pb.pb_ldif2db_noattrindexes = ldif2db_noattrindexes;
  1843. pb.pb_ldif_generate_uniqueid = ldif2db_generate_uniqueid;
  1844. pb.pb_ldif_namespaceid = ldif2db_namespaceid;
  1845. pb.pb_ldif_encrypt = importexport_encrypt;
  1846. pb.pb_instance_name = cmd_line_instance_name;
  1847. pb.pb_ldif_files = ldif_file;
  1848. pb.pb_ldif_include = db2ldif_include;
  1849. pb.pb_ldif_exclude = db2ldif_exclude;
  1850. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  1851. main_setuid(slapdFrontendConfig->localuser);
  1852. if ( plugin->plg_ldif2db != NULL ) {
  1853. return_value = (*plugin->plg_ldif2db)( &pb );
  1854. } else {
  1855. LDAPDebug( LDAP_DEBUG_ANY,
  1856. "ERROR: no ldif2db function defined for "
  1857. "%s\n", plugin->plg_name, 0, 0 );
  1858. return_value = -1;
  1859. }
  1860. slapi_ch_free((void**)&myname );
  1861. charray_free( db2index_attrs );
  1862. charray_free(ldif_file);
  1863. return( return_value );
  1864. }
  1865. static int
  1866. slapd_exemode_db2ldif(int argc, char** argv)
  1867. {
  1868. int return_value= 0;
  1869. Slapi_PBlock pb;
  1870. struct slapdplugin *plugin;
  1871. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  1872. char *my_ldiffile;
  1873. char **instp;
  1874. /* this should be the first time this are called! if the init order
  1875. * is ever changed, these lines should be changed (or erased)!
  1876. */
  1877. mapping_tree_init();
  1878. /*
  1879. * if instance is given, just pass it to the backend.
  1880. * otherwise, we use included/excluded suffix list to specify a backend.
  1881. */
  1882. if (NULL == cmd_line_instance_names) {
  1883. char **instances, **ip;
  1884. int counter;
  1885. if (lookup_instance_name_by_suffixes(db2ldif_include, db2ldif_exclude,
  1886. &instances) < 0) {
  1887. LDAPDebug(LDAP_DEBUG_ANY,
  1888. "ERROR: backend instances name [-n <name>] or "
  1889. "included suffix [-s <suffix>] need to be specified.\n",
  1890. 0, 0, 0);
  1891. return 1;
  1892. }
  1893. if (instances) {
  1894. for (ip = instances, counter = 0; ip && *ip; ip++, counter++)
  1895. ;
  1896. if (counter == 0) {
  1897. LDAPDebug(LDAP_DEBUG_ANY,
  1898. "ERROR 1: There is no backend instance to export from.\n",
  1899. 0, 0, 0);
  1900. return 1;
  1901. } else {
  1902. LDAPDebug(LDAP_DEBUG_ANY, "Backend Instance(s): \n", 0, 0, 0);
  1903. for (ip = instances, counter = 0; ip && *ip; ip++, counter++) {
  1904. LDAPDebug(LDAP_DEBUG_ANY, "\t%s\n", *ip, 0, 0);
  1905. }
  1906. cmd_line_instance_names = instances;
  1907. }
  1908. } else {
  1909. LDAPDebug(LDAP_DEBUG_ANY,
  1910. "ERROR 2: There is no backend instance to export from.\n",
  1911. 0, 0, 0);
  1912. return 1;
  1913. }
  1914. }
  1915. /* [622984] db2lidf -r changes database file ownership
  1916. * should call setuid before "db2ldif_dump_replica" */
  1917. main_setuid(slapdFrontendConfig->localuser);
  1918. for (instp = cmd_line_instance_names; instp && *instp; instp++) {
  1919. int release_me = 0;
  1920. plugin = lookup_plugin_by_instance_name(*instp);
  1921. if (plugin == NULL) {
  1922. LDAPDebug(LDAP_DEBUG_ANY,
  1923. "ERROR: Could not find backend '%s'.\n",
  1924. *instp, 0, 0);
  1925. return 1;
  1926. }
  1927. if (plugin->plg_db2ldif == NULL) {
  1928. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: no db2ldif function defined for "
  1929. "backend %s - cannot export\n", *instp, 0, 0);
  1930. return 1;
  1931. }
  1932. /* Make sure we aren't going to run slapd in
  1933. * a mode that is going to conflict with other
  1934. * slapd processes that are currently running
  1935. */
  1936. if ( add_new_slapd_process(slapd_exemode, db2ldif_dump_replica,
  1937. skip_db_protect_check) == -1 ) {
  1938. LDAPDebug( LDAP_DEBUG_ANY,
  1939. "Shutting down due to possible conflicts "
  1940. "with other slapd processes\n",
  1941. 0, 0, 0 );
  1942. return 1;
  1943. }
  1944. if (! (SLAPI_PLUGIN_IS_V2(plugin))) {
  1945. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: %s is too old to do exports.\n",
  1946. plugin->plg_name, 0, 0);
  1947. return 1;
  1948. }
  1949. if (!is_quiet) {
  1950. slapd_ldap_debug |= LDAP_DEBUG_BACKLDBM;
  1951. }
  1952. if (!(slapd_ldap_debug & LDAP_DEBUG_BACKLDBM)) {
  1953. g_set_detached(1);
  1954. }
  1955. memset( &pb, '\0', sizeof(pb) );
  1956. pb.pb_backend = NULL;
  1957. pb.pb_plugin = plugin;
  1958. pb.pb_ldif_include = db2ldif_include;
  1959. pb.pb_ldif_exclude = db2ldif_exclude;
  1960. pb.pb_ldif_dump_replica = db2ldif_dump_replica;
  1961. pb.pb_ldif_dump_uniqueid = db2ldif_dump_uniqueid;
  1962. pb.pb_ldif_encrypt = importexport_encrypt;
  1963. pb.pb_instance_name = *instp;
  1964. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  1965. if (is_slapd_running())
  1966. pb.pb_server_running = 1;
  1967. else
  1968. pb.pb_server_running = 0;
  1969. if (db2ldif_dump_replica) {
  1970. eq_init(); /* must be done before plugins started */
  1971. ps_init_psearch_system(); /* must come before plugin_startall() */
  1972. plugin_startall(argc, argv, 1 /* Start Backends */,
  1973. 1 /* Start Globals */);
  1974. eq_start(); /* must be done after plugins started */
  1975. }
  1976. pb.pb_ldif_file = NULL;
  1977. if ( archive_name ) { /* redirect stdout to this file: */
  1978. char *p, *q;
  1979. char sep = '/';
  1980. my_ldiffile = archive_name;
  1981. if (ldif_printkey & EXPORT_APPENDMODE) {
  1982. if (instp == cmd_line_instance_names) { /* first export */
  1983. ldif_printkey |= EXPORT_APPENDMODE_1;
  1984. } else {
  1985. ldif_printkey &= ~EXPORT_APPENDMODE_1;
  1986. }
  1987. } else { /* not APPENDMODE */
  1988. if (strcmp(archive_name, "-")) { /* not '-' */
  1989. my_ldiffile =
  1990. (char *)slapi_ch_malloc((unsigned long)(strlen(archive_name)
  1991. + strlen(*instp) + 2));
  1992. p = strrchr(archive_name, sep);
  1993. if (NULL == p) {
  1994. sprintf(my_ldiffile, "%s_%s", *instp, archive_name);
  1995. } else {
  1996. q = p + 1;
  1997. *p = '\0';
  1998. sprintf(my_ldiffile, "%s%c%s_%s",
  1999. archive_name, sep, *instp, q);
  2000. *p = sep;
  2001. }
  2002. release_me = 1;
  2003. }
  2004. }
  2005. if (!is_quiet) {
  2006. fprintf(stderr, "ldiffile: %s\n", my_ldiffile);
  2007. }
  2008. /* just send the filename to the backend and let
  2009. * the backend open it (so they can do special
  2010. * stuff for 64-bit fs)
  2011. */
  2012. pb.pb_ldif_file = my_ldiffile;
  2013. pb.pb_ldif_printkey = ldif_printkey;
  2014. }
  2015. return_value = (plugin->plg_db2ldif)( &pb );
  2016. if (release_me) {
  2017. slapi_ch_free((void **)&my_ldiffile);
  2018. }
  2019. }
  2020. slapi_ch_free( (void**)&myname );
  2021. if (db2ldif_dump_replica) {
  2022. eq_stop(); /* event queue should be shutdown before closing
  2023. all plugins (especailly, replication plugin) */
  2024. plugin_closeall( 1 /* Close Backends */, 1 /* Close Globals */);
  2025. }
  2026. return( return_value );
  2027. }
  2028. static int
  2029. slapd_exemode_suffix2instance()
  2030. {
  2031. int return_value = 0;
  2032. char **instances = NULL;
  2033. char **suffixes = NULL;
  2034. char **p, **q, **r;
  2035. /* this should be the first time this are called! if the init order
  2036. * is ever changed, these lines should be changed (or erased)!
  2037. */
  2038. mapping_tree_init();
  2039. for (p = db2ldif_include; p && *p; p++) {
  2040. if (lookup_instance_name_by_suffix(*p, &suffixes, &instances, 0) < 0)
  2041. continue;
  2042. fprintf(stderr, "Suffix, Instance name pair(s) under \"%s\":\n", *p);
  2043. if (instances)
  2044. for (q = suffixes, r = instances; *r; q++, r++)
  2045. fprintf(stderr, "\tsuffix %s; instance name \"%s\"\n",
  2046. *q?*q:"-", *r);
  2047. else
  2048. fprintf(stderr, "\tNo instance\n");
  2049. charray_free(suffixes);
  2050. suffixes = NULL;
  2051. charray_free(instances);
  2052. instances = NULL;
  2053. }
  2054. return (return_value);
  2055. }
  2056. static int slapd_exemode_db2index()
  2057. {
  2058. int return_value= 0;
  2059. struct slapdplugin *plugin;
  2060. Slapi_PBlock pb;
  2061. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  2062. mapping_tree_init();
  2063. /*
  2064. * if instance is given, just use it to get the backend.
  2065. * otherwise, we use included/excluded suffix list to specify a backend.
  2066. */
  2067. if (NULL == cmd_line_instance_name) {
  2068. char **instances, **ip;
  2069. int counter;
  2070. if (lookup_instance_name_by_suffixes(db2ldif_include, db2ldif_exclude,
  2071. &instances) < 0) {
  2072. LDAPDebug(LDAP_DEBUG_ANY,
  2073. "ERROR: backend instances name [-n <name>] or "
  2074. "included suffix [-s <suffix>] need to be specified.\n",
  2075. 0, 0, 0);
  2076. return 1;
  2077. }
  2078. if (instances) {
  2079. for (ip = instances, counter = 0; ip && *ip; ip++, counter++)
  2080. ;
  2081. if (counter == 0) {
  2082. LDAPDebug(LDAP_DEBUG_ANY,
  2083. "ERROR 1: There is no backend instance to import to.\n",
  2084. 0, 0, 0);
  2085. return 1;
  2086. } else if (counter > 1) {
  2087. int i;
  2088. LDAPDebug(LDAP_DEBUG_ANY,
  2089. "ERROR: There are multiple backend instances specified:\n",
  2090. 0, 0, 0);
  2091. for (i = 0; i < counter; i++)
  2092. LDAPDebug(LDAP_DEBUG_ANY, " : %s\n",
  2093. instances[i], 0, 0);
  2094. return 1;
  2095. } else {
  2096. LDAPDebug(LDAP_DEBUG_ANY, "Backend Instance: %s\n",
  2097. *instances, 0, 0);
  2098. cmd_line_instance_name = *instances;
  2099. }
  2100. } else {
  2101. LDAPDebug(LDAP_DEBUG_ANY,
  2102. "ERROR 2: There is no backend instance to import to.\n",
  2103. 0, 0, 0);
  2104. return 1;
  2105. }
  2106. }
  2107. plugin = lookup_plugin_by_instance_name(cmd_line_instance_name);
  2108. if (plugin == NULL) {
  2109. LDAPDebug(LDAP_DEBUG_ANY,
  2110. "ERROR: Could not find backend '%s'.\n",
  2111. cmd_line_instance_name, 0, 0);
  2112. return 1;
  2113. }
  2114. /* make sure nothing else is running */
  2115. if (add_new_slapd_process(slapd_exemode, db2ldif_dump_replica,
  2116. skip_db_protect_check) == -1) {
  2117. LDAPDebug(LDAP_DEBUG_ANY,
  2118. "Shutting down due to possible conflicts with other "
  2119. "slapd processes.\n", 0, 0, 0);
  2120. return 1;
  2121. }
  2122. if ( db2index_attrs == NULL ) {
  2123. usage( myname, extraname );
  2124. return 1;
  2125. }
  2126. memset( &pb, '\0', sizeof(pb) );
  2127. pb.pb_backend = NULL;
  2128. pb.pb_plugin = plugin;
  2129. pb.pb_db2index_attrs = db2index_attrs;
  2130. pb.pb_instance_name = cmd_line_instance_name;
  2131. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  2132. main_setuid(slapdFrontendConfig->localuser);
  2133. return_value = (*plugin->plg_db2index)( &pb );
  2134. slapi_ch_free( (void**)&myname );
  2135. return( return_value );
  2136. }
  2137. static int
  2138. slapd_exemode_db2archive()
  2139. {
  2140. int return_value= 0;
  2141. Slapi_PBlock pb;
  2142. struct slapdplugin *backend_plugin;
  2143. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  2144. if ((backend_plugin = plugin_get_by_name("ldbm database")) == NULL) {
  2145. LDAPDebug(LDAP_DEBUG_ANY,
  2146. "ERROR: Could not find the ldbm backend plugin.\n",
  2147. 0, 0, 0);
  2148. return 1;
  2149. }
  2150. if (NULL == archive_name) {
  2151. LDAPDebug( LDAP_DEBUG_ANY,
  2152. "ERROR: no archive directory supplied\n",
  2153. 0, 0, 0 );
  2154. return 1;
  2155. }
  2156. /* Make sure we aren't going to run slapd in
  2157. * a mode that is going to conflict with other
  2158. * slapd processes that are currently running
  2159. */
  2160. if ( add_new_slapd_process(slapd_exemode, db2ldif_dump_replica,
  2161. skip_db_protect_check) == -1 ) {
  2162. LDAPDebug( LDAP_DEBUG_ANY,
  2163. "Shutting down due to possible conflicts with other slapd processes\n",
  2164. 0, 0, 0 );
  2165. return 1;
  2166. }
  2167. if (compute_init()) {
  2168. LDAPDebug(LDAP_DEBUG_ANY, "Initialization Failed 0 %d\n",return_value,0,0);
  2169. return 1;
  2170. }
  2171. if (!is_quiet) {
  2172. slapd_ldap_debug |= LDAP_DEBUG_BACKLDBM;
  2173. }
  2174. if (!(slapd_ldap_debug & LDAP_DEBUG_BACKLDBM)) {
  2175. g_set_detached(1);
  2176. }
  2177. memset( &pb, '\0', sizeof(pb) );
  2178. pb.pb_backend = NULL;
  2179. pb.pb_plugin = backend_plugin;
  2180. pb.pb_instance_name = NULL;
  2181. pb.pb_seq_val = archive_name;
  2182. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  2183. main_setuid(slapdFrontendConfig->localuser);
  2184. return_value = (backend_plugin->plg_db2archive)( &pb );
  2185. return return_value;
  2186. }
  2187. static int
  2188. slapd_exemode_archive2db()
  2189. {
  2190. int return_value= 0;
  2191. Slapi_PBlock pb;
  2192. struct slapdplugin *backend_plugin;
  2193. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  2194. if ((backend_plugin = plugin_get_by_name("ldbm database")) == NULL) {
  2195. LDAPDebug(LDAP_DEBUG_ANY,
  2196. "ERROR: Could not find the ldbm backend plugin.\n",
  2197. 0, 0, 0);
  2198. return 1;
  2199. }
  2200. if (NULL == archive_name) {
  2201. LDAPDebug( LDAP_DEBUG_ANY,
  2202. "ERROR: no archive directory supplied\n",
  2203. 0, 0, 0 );
  2204. return 1;
  2205. }
  2206. /* Make sure we aren't going to run slapd in
  2207. * a mode that is going to conflict with other
  2208. * slapd processes that are currently running
  2209. */
  2210. if ( add_new_slapd_process(slapd_exemode, db2ldif_dump_replica,
  2211. skip_db_protect_check) == -1 ) {
  2212. LDAPDebug( LDAP_DEBUG_ANY,
  2213. "Shutting down due to possible conflicts with other slapd processes\n",
  2214. 0, 0, 0 );
  2215. return 1;
  2216. }
  2217. if (compute_init()) {
  2218. LDAPDebug(LDAP_DEBUG_ANY, "Initialization Failed 0 %d\n",return_value,0,0);
  2219. return 1;
  2220. }
  2221. if (!is_quiet) {
  2222. slapd_ldap_debug |= LDAP_DEBUG_BACKLDBM;
  2223. }
  2224. if (!(slapd_ldap_debug & LDAP_DEBUG_BACKLDBM)) {
  2225. g_set_detached(1);
  2226. }
  2227. memset( &pb, '\0', sizeof(pb) );
  2228. pb.pb_backend = NULL;
  2229. pb.pb_plugin = backend_plugin;
  2230. pb.pb_instance_name = cmd_line_instance_name;
  2231. pb.pb_seq_val = archive_name;
  2232. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  2233. main_setuid(slapdFrontendConfig->localuser);
  2234. return_value = (backend_plugin->plg_archive2db)( &pb );
  2235. return return_value;
  2236. }
  2237. /*
  2238. * functions to convert idl from the old format to the new one
  2239. * (604921) Support a database uprev process any time post-install
  2240. */
  2241. static int
  2242. slapd_exemode_upgradedb()
  2243. {
  2244. int return_value= 0;
  2245. Slapi_PBlock pb;
  2246. struct slapdplugin *backend_plugin;
  2247. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  2248. if ( archive_name == NULL ) {
  2249. LDAPDebug( LDAP_DEBUG_ANY,
  2250. "ERROR: Required argument -a <backup_dir> missing\n",
  2251. 0, 0, 0 );
  2252. usage( myname, extraname );
  2253. return 1;
  2254. }
  2255. /* this should be the first time to be called! if the init order
  2256. * is ever changed, these lines should be changed (or erased)!
  2257. */
  2258. mapping_tree_init();
  2259. if ((backend_plugin = plugin_get_by_name("ldbm database")) == NULL) {
  2260. LDAPDebug(LDAP_DEBUG_ANY,
  2261. "ERROR: Could not find the ldbm backend plugin.\n",
  2262. 0, 0, 0);
  2263. return 1;
  2264. }
  2265. /* Make sure we aren't going to run slapd in
  2266. * a mode that is going to conflict with other
  2267. * slapd processes that are currently running
  2268. */
  2269. if (add_new_slapd_process(slapd_exemode, 0, skip_db_protect_check) == -1) {
  2270. LDAPDebug( LDAP_DEBUG_ANY,
  2271. "Shutting down due to possible conflicts with other slapd processes\n",
  2272. 0, 0, 0 );
  2273. return 1;
  2274. }
  2275. /* check for slapi v2 support */
  2276. if (! SLAPI_PLUGIN_IS_V2(backend_plugin)) {
  2277. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: %s is too old to do convert idl.\n",
  2278. backend_plugin->plg_name, 0, 0);
  2279. return 1;
  2280. }
  2281. memset( &pb, '\0', sizeof(pb) );
  2282. pb.pb_backend = NULL;
  2283. pb.pb_plugin = backend_plugin;
  2284. pb.pb_seq_val = archive_name;
  2285. pb.pb_seq_type = upgradedb_flags;
  2286. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  2287. /* borrowing import code, so need to set up the import variables */
  2288. pb.pb_ldif_generate_uniqueid = ldif2db_generate_uniqueid;
  2289. pb.pb_ldif_namespaceid = ldif2db_namespaceid;
  2290. pb.pb_ldif2db_noattrindexes = 0;
  2291. pb.pb_removedupvals = 0;
  2292. main_setuid(slapdFrontendConfig->localuser);
  2293. if ( backend_plugin->plg_upgradedb != NULL ) {
  2294. return_value = (*backend_plugin->plg_upgradedb)( &pb );
  2295. } else {
  2296. LDAPDebug( LDAP_DEBUG_ANY,
  2297. "ERROR: no upgradedb function defined for "
  2298. "%s\n", backend_plugin->plg_name, 0, 0 );
  2299. return_value = -1;
  2300. }
  2301. slapi_ch_free((void**)&myname );
  2302. return( return_value );
  2303. }
  2304. /* Command to upgrade the old dn format to the new style */
  2305. static int
  2306. slapd_exemode_upgradednformat()
  2307. {
  2308. int rc = -1; /* error, by default */
  2309. Slapi_PBlock pb;
  2310. struct slapdplugin *backend_plugin;
  2311. slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
  2312. if ( archive_name == NULL ) {
  2313. LDAPDebug0Args(LDAP_DEBUG_ANY, "ERROR: Required argument "
  2314. "\"-a <path to work db instance dir>\" is missing\n");
  2315. usage( myname, extraname );
  2316. goto bail;
  2317. }
  2318. /* this should be the first time to be called! if the init order
  2319. * is ever changed, these lines should be changed (or erased)!
  2320. */
  2321. mapping_tree_init();
  2322. if ((backend_plugin = plugin_get_by_name("ldbm database")) == NULL) {
  2323. LDAPDebug0Args(LDAP_DEBUG_ANY,
  2324. "ERROR: Could not find the ldbm backend plugin.\n");
  2325. goto bail;
  2326. }
  2327. /* Make sure we aren't going to run slapd in
  2328. * a mode that is going to conflict with other
  2329. * slapd processes that are currently running
  2330. * Pretending to execute import.
  2331. */
  2332. if (add_new_slapd_process(slapd_exemode, 0, skip_db_protect_check)
  2333. == -1) {
  2334. LDAPDebug0Args(LDAP_DEBUG_ANY, "Shutting down due to possible "
  2335. "conflicts with other slapd processes\n");
  2336. goto bail;
  2337. }
  2338. /* check for slapi v2 support */
  2339. if (! SLAPI_PLUGIN_IS_V2(backend_plugin)) {
  2340. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  2341. "ERROR: %s is too old to upgrade dn format.\n",
  2342. backend_plugin->plg_name);
  2343. goto bail;
  2344. }
  2345. memset( &pb, '\0', sizeof(pb) );
  2346. pb.pb_backend = NULL;
  2347. pb.pb_plugin = backend_plugin;
  2348. pb.pb_instance_name = cmd_line_instance_name;
  2349. if (upgradednformat_dryrun) {
  2350. pb.pb_seq_type = SLAPI_UPGRADEDNFORMAT|SLAPI_DRYRUN;
  2351. } else {
  2352. pb.pb_seq_type = SLAPI_UPGRADEDNFORMAT;
  2353. }
  2354. pb.pb_seq_val = archive_name; /* Path to the work db instance dir */
  2355. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  2356. /* borrowing import code, so need to set up the import variables */
  2357. pb.pb_ldif_generate_uniqueid = ldif2db_generate_uniqueid;
  2358. pb.pb_ldif_namespaceid = ldif2db_namespaceid;
  2359. pb.pb_ldif2db_noattrindexes = 0;
  2360. pb.pb_removedupvals = 0;
  2361. main_setuid(slapdFrontendConfig->localuser);
  2362. if ( backend_plugin->plg_upgradednformat != NULL ) {
  2363. rc = (*backend_plugin->plg_upgradednformat)( &pb );
  2364. } else {
  2365. LDAPDebug( LDAP_DEBUG_ANY,
  2366. "ERROR: no upgradednformat function defined for "
  2367. "%s\n", backend_plugin->plg_name, 0, 0 );
  2368. }
  2369. bail:
  2370. slapi_ch_free((void**)&myname );
  2371. return( rc );
  2372. }
  2373. /*
  2374. * function to perform DB verify
  2375. */
  2376. static int
  2377. slapd_exemode_dbverify()
  2378. {
  2379. int return_value = 0;
  2380. Slapi_PBlock pb;
  2381. struct slapdplugin *backend_plugin;
  2382. /* this should be the first time to be called! if the init order
  2383. * is ever changed, these lines should be changed (or erased)!
  2384. */
  2385. mapping_tree_init();
  2386. if ((backend_plugin = plugin_get_by_name("ldbm database")) == NULL) {
  2387. LDAPDebug(LDAP_DEBUG_ANY,
  2388. "ERROR: Could not find the ldbm backend plugin.\n",
  2389. 0, 0, 0);
  2390. return 1;
  2391. }
  2392. /* check for slapi v2 support */
  2393. if (! SLAPI_PLUGIN_IS_V2(backend_plugin)) {
  2394. LDAPDebug(LDAP_DEBUG_ANY, "ERROR: %s is too old to do dbverify.\n",
  2395. backend_plugin->plg_name, 0, 0);
  2396. return 1;
  2397. }
  2398. memset( &pb, '\0', sizeof(pb) );
  2399. pb.pb_backend = NULL;
  2400. pb.pb_seq_type = dbverify_verbose;
  2401. pb.pb_plugin = backend_plugin;
  2402. pb.pb_instance_name = (char *)cmd_line_instance_names;
  2403. pb.pb_task_flags = SLAPI_TASK_RUNNING_FROM_COMMANDLINE;
  2404. pb.pb_dbverify_dbdir = dbverify_dbdir;
  2405. if ( backend_plugin->plg_dbverify != NULL ) {
  2406. return_value = (*backend_plugin->plg_dbverify)( &pb );
  2407. } else {
  2408. LDAPDebug( LDAP_DEBUG_ANY,
  2409. "ERROR: no db verify function defined for "
  2410. "%s\n", backend_plugin->plg_name, 0, 0 );
  2411. return_value = -1;
  2412. }
  2413. return( return_value );
  2414. }
  2415. #ifdef LDAP_DEBUG
  2416. /*
  2417. * Table to associate a string with a debug level.
  2418. */
  2419. static struct slapd_debug_level_entry {
  2420. int dle_level; /* LDAP_DEBUG_XXX value */
  2421. const char *dle_string; /* string equivalent; NULL marks end of list */
  2422. char dle_hide;
  2423. } slapd_debug_level_map[] = {
  2424. { LDAP_DEBUG_TRACE, "trace", 0 },
  2425. { LDAP_DEBUG_PACKETS, "packets", 0 },
  2426. { LDAP_DEBUG_ARGS, "arguments", 0 },
  2427. { LDAP_DEBUG_ARGS, "args", 1 },
  2428. { LDAP_DEBUG_CONNS, "connections", 0 },
  2429. { LDAP_DEBUG_CONNS, "conn", 1 },
  2430. { LDAP_DEBUG_CONNS, "conns", 1 },
  2431. { LDAP_DEBUG_BER, "ber", 0 },
  2432. { LDAP_DEBUG_FILTER, "filters", 0 },
  2433. { LDAP_DEBUG_CONFIG, "config", 0 },
  2434. { LDAP_DEBUG_ACL, "accesscontrol", 0 },
  2435. { LDAP_DEBUG_ACL, "acl", 1 },
  2436. { LDAP_DEBUG_ACL, "acls", 1 },
  2437. { LDAP_DEBUG_STATS, "stats", 0 },
  2438. { LDAP_DEBUG_STATS2, "stats2", 0 },
  2439. { LDAP_DEBUG_SHELL, "shell", 1 },
  2440. { LDAP_DEBUG_PARSE, "parsing", 0 },
  2441. { LDAP_DEBUG_HOUSE, "housekeeping", 0 },
  2442. { LDAP_DEBUG_REPL, "replication", 0 },
  2443. { LDAP_DEBUG_REPL, "repl", 1 },
  2444. { LDAP_DEBUG_ANY, "errors", 0 },
  2445. { LDAP_DEBUG_ANY, "ANY", 1 },
  2446. { LDAP_DEBUG_ANY, "error", 1 },
  2447. { LDAP_DEBUG_CACHE, "caches", 0 },
  2448. { LDAP_DEBUG_CACHE, "cache", 1 },
  2449. { LDAP_DEBUG_PLUGIN, "plugins", 0 },
  2450. { LDAP_DEBUG_PLUGIN, "plugin", 1 },
  2451. { LDAP_DEBUG_TIMING, "timing", 0 },
  2452. { LDAP_DEBUG_ACLSUMMARY,"accesscontrolsummary", 0 },
  2453. { LDAP_DEBUG_BACKLDBM, "backend", 0 },
  2454. { LDAP_DEBUG_ALL_LEVELS,"ALL", 0 },
  2455. { 0, NULL, 0 }
  2456. };
  2457. /*
  2458. * Given a string represention of a debug level, map it to a integer value
  2459. * and return that value. -1 is returned upon error, with a message
  2460. * printed to stderr.
  2461. */
  2462. static int
  2463. slapd_debug_level_string2level( const char *s )
  2464. {
  2465. int level, i;
  2466. char *cur, *next, *scopy;
  2467. level = 0;
  2468. scopy = slapi_ch_strdup( s );
  2469. for ( cur = scopy; cur != NULL; cur = next ) {
  2470. if (( next = strchr( cur, '+' )) != NULL ) {
  2471. *next++ = '\0';
  2472. }
  2473. if ( isdigit( *cur )) {
  2474. level |= atoi( cur );
  2475. } else {
  2476. for ( i = 0; NULL != slapd_debug_level_map[i].dle_string; ++i ) {
  2477. if ( strcasecmp( cur, slapd_debug_level_map[i].dle_string )
  2478. == 0 ) {
  2479. level |= slapd_debug_level_map[i].dle_level;
  2480. break;
  2481. }
  2482. }
  2483. if ( NULL == slapd_debug_level_map[i].dle_string ) {
  2484. fprintf( stderr, "Unrecognized debug level \"%s\"\n", cur );
  2485. slapi_ch_free_string(&scopy);
  2486. return -1;
  2487. }
  2488. }
  2489. }
  2490. slapi_ch_free_string(&scopy);
  2491. return level;
  2492. }
  2493. /*
  2494. * Print to stderr the string equivalent of level.
  2495. * The ANY level is omitted because it is always present.
  2496. */
  2497. static void
  2498. slapd_debug_level_log( int level )
  2499. {
  2500. int i, count, len;
  2501. char *msg, *p;
  2502. level &= ~LDAP_DEBUG_ANY;
  2503. /* first pass: determine space needed for the debug level string */
  2504. len = 1; /* room for '\0' terminator */
  2505. count = 0;
  2506. for ( i = 0; NULL != slapd_debug_level_map[i].dle_string; ++i ) {
  2507. if ( !slapd_debug_level_map[i].dle_hide &&
  2508. slapd_debug_level_map[i].dle_level != LDAP_DEBUG_ALL_LEVELS
  2509. && 0 != ( level & slapd_debug_level_map[i].dle_level )) {
  2510. if ( count > 0 ) {
  2511. ++len; /* room for '+' character */
  2512. }
  2513. len += strlen( slapd_debug_level_map[i].dle_string );
  2514. ++count;
  2515. }
  2516. }
  2517. /* second pass: construct the debug level string */
  2518. p = msg = slapi_ch_malloc( len );
  2519. count = 0;
  2520. for ( i = 0; NULL != slapd_debug_level_map[i].dle_string; ++i ) {
  2521. if ( !slapd_debug_level_map[i].dle_hide &&
  2522. slapd_debug_level_map[i].dle_level != LDAP_DEBUG_ALL_LEVELS
  2523. && 0 != ( level & slapd_debug_level_map[i].dle_level )) {
  2524. if ( count > 0 ) {
  2525. *p++ = '+';
  2526. }
  2527. strcpy( p, slapd_debug_level_map[i].dle_string );
  2528. p += strlen( p );
  2529. ++count;
  2530. }
  2531. }
  2532. slapi_log_error( SLAPI_LOG_FATAL, SLAPD_VERSION_STR,
  2533. "%s: %s (%d)\n", "debug level", msg, level );
  2534. slapi_ch_free( (void **)&msg );
  2535. }
  2536. /*
  2537. * Display usage/help for the debug level flag (-d)
  2538. */
  2539. static void
  2540. slapd_debug_level_usage( void )
  2541. {
  2542. int i;
  2543. fprintf( stderr, "Debug levels:\n" );
  2544. for ( i = 0; NULL != slapd_debug_level_map[i].dle_string; ++i ) {
  2545. if ( !slapd_debug_level_map[i].dle_hide
  2546. && slapd_debug_level_map[i].dle_level
  2547. != LDAP_DEBUG_ALL_LEVELS) {
  2548. fprintf( stderr, " %6d - %s%s\n",
  2549. slapd_debug_level_map[i].dle_level,
  2550. slapd_debug_level_map[i].dle_string,
  2551. ( 0 == ( slapd_debug_level_map[i].dle_level &
  2552. LDAP_DEBUG_ANY )) ? "" :
  2553. " (always logged)" );
  2554. }
  2555. }
  2556. fprintf( stderr, "To activate multiple levels, add the numeric"
  2557. " values together or separate the\n"
  2558. "values with a + character, e.g., all of the following"
  2559. " have the same effect:\n"
  2560. " -d connections+filters\n"
  2561. " -d 8+32\n"
  2562. " -d 40\n" );
  2563. }
  2564. #endif /* LDAP_DEBUG */
  2565. static int
  2566. force_to_disable_security(const char *what, int *init_ssl, daemon_ports_t *ports_info)
  2567. {
  2568. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  2569. errorbuf[0] = '\0';
  2570. LDAPDebug2Args(LDAP_DEBUG_ANY, "ERROR: %s Initialization Failed. Disabling %s.\n", what, what);
  2571. ports_info->s_socket = SLAPD_INVALID_SOCKET;
  2572. ports_info->s_port = 0;
  2573. *init_ssl = 0;
  2574. if (config_set_security(CONFIG_SECURITY_ATTRIBUTE, "off", errorbuf, 1)) {
  2575. LDAPDebug2Args(LDAP_DEBUG_ANY, "ERROR: Failed to disable %s: \"%s\".\n",
  2576. CONFIG_SECURITY_ATTRIBUTE, errorbuf[0]?errorbuf:"no error message");
  2577. return 1;
  2578. }
  2579. return 0;
  2580. }
  2581. /*
  2582. This function does all NSS and SSL related initialization
  2583. required during startup. We use this function rather
  2584. than just call this code from main because we must perform
  2585. all of this initialization after the fork() but before
  2586. we detach from the controlling terminal. This is because
  2587. the NSS softokn requires that NSS_Init is called after the
  2588. fork - this was always the case, but it is a hard error in
  2589. NSS 3.11.99 and later. We also have to call NSS_Init before
  2590. doing the detach because NSS may prompt the user for the
  2591. token (h/w or softokn) password on stdin. So we use this
  2592. function that we can call from detach() if running in
  2593. regular slapd exemode or from main() if running in other
  2594. modes (or just not detaching).
  2595. */
  2596. int
  2597. slapd_do_all_nss_ssl_init(int slapd_exemode, int importexport_encrypt,
  2598. int s_port, daemon_ports_t *ports_info)
  2599. {
  2600. /*
  2601. * Initialise NSS once for the whole slapd process, whether SSL
  2602. * is enabled or not. We use NSS for random number generation and
  2603. * other things even if we are not going to accept SSL connections.
  2604. * We also need NSS for attribute encryption/decryption on import and export.
  2605. */
  2606. int init_ssl = config_get_security();
  2607. if (slapd_exemode == SLAPD_EXEMODE_SLAPD) {
  2608. init_ssl = init_ssl && (0 != s_port) && (s_port <= LDAP_PORT_MAX);
  2609. } else {
  2610. init_ssl = init_ssl && importexport_encrypt;
  2611. }
  2612. /* As of DS 6.1, always do a full initialization so that other
  2613. * modules can assume NSS is available
  2614. */
  2615. if ( slapd_nss_init((slapd_exemode == SLAPD_EXEMODE_SLAPD),
  2616. (slapd_exemode != SLAPD_EXEMODE_REFERRAL) /* have config? */ )) {
  2617. if (force_to_disable_security("NSS", &init_ssl, ports_info)) {
  2618. return 1;
  2619. }
  2620. }
  2621. if (slapd_exemode == SLAPD_EXEMODE_SLAPD) {
  2622. client_auth_init();
  2623. }
  2624. if (init_ssl && slapd_ssl_init()) {
  2625. if (force_to_disable_security("SSL", &init_ssl, ports_info)) {
  2626. return 1;
  2627. }
  2628. }
  2629. if ((slapd_exemode == SLAPD_EXEMODE_SLAPD) ||
  2630. (slapd_exemode == SLAPD_EXEMODE_REFERRAL)) {
  2631. if ( init_ssl ) {
  2632. PRFileDesc **sock;
  2633. for (sock = ports_info->s_socket; sock && *sock; sock++) {
  2634. if ( slapd_ssl_init2(sock, 0) ) {
  2635. if (force_to_disable_security("SSL2", &init_ssl, ports_info)) {
  2636. return 1;
  2637. }
  2638. }
  2639. }
  2640. }
  2641. }
  2642. return 0;
  2643. }