main.c 85 KB

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