DSCreate.pm.in 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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) 2007 Red Hat, Inc.
  35. # All rights reserved.
  36. # END COPYRIGHT BLOCK
  37. #
  38. ###########################
  39. #
  40. # This perl module provides a way to create a new instance of
  41. # directory server.
  42. #
  43. ##########################
  44. package DSCreate;
  45. use DSUtil;
  46. use Inf;
  47. use FileConn;
  48. use Net::Domain qw(hostfqdn);
  49. # tempfiles
  50. use File::Temp qw(tempfile tempdir);
  51. use File::Path;
  52. use File::Copy;
  53. use File::Basename qw(basename dirname);
  54. use POSIX qw(:errno_h);
  55. # load perldap
  56. use Mozilla::LDAP::Conn;
  57. use Mozilla::LDAP::Utils qw(normalizeDN);
  58. use Mozilla::LDAP::API qw(ldap_explode_dn);
  59. use Mozilla::LDAP::LDIF;
  60. use Exporter;
  61. @ISA = qw(Exporter);
  62. @EXPORT = qw(createDSInstance removeDSInstance setDefaults createInstanceScripts
  63. makeOtherConfigFiles installSchema updateSelinuxPolicy updateTmpfilesDotD
  64. get_initconfigdir updateSystemD makeDSDirs);
  65. @EXPORT_OK = qw(createDSInstance removeDSInstance setDefaults createInstanceScripts
  66. makeOtherConfigFiles installSchema updateSelinuxPolicy updateTmpfilesDotD
  67. get_initconfigdir updateSystemD makeDSDirs);
  68. use strict;
  69. use SetupLog;
  70. sub get_initconfigdir {
  71. # determine initconfig_dir
  72. if (getLogin eq 'root') {
  73. return "@initconfigdir@";
  74. } else {
  75. return "$ENV{HOME}/.@package_name@";
  76. }
  77. }
  78. sub checkPort {
  79. my $inf = shift;
  80. # allow port 0 if ldapi is used
  81. if ("@enable_ldapi@") {
  82. if ($inf->{slapd}->{ldapifilepath} &&
  83. ($inf->{slapd}->{ServerPort} == 0)) {
  84. return ();
  85. }
  86. }
  87. if ($inf->{slapd}->{ServerPort} !~ /^\d+$/) {
  88. return ('error_port_invalid', $inf->{slapd}->{ServerPort});
  89. }
  90. if (!portAvailable($inf->{slapd}->{ServerPort})) {
  91. return ('error_port_available', $inf->{slapd}->{ServerPort}, $!);
  92. }
  93. return ();
  94. }
  95. # checks the parameters in $inf to make sure the supplied values
  96. # are valid
  97. # returns null if successful, or an error string for use with getText()
  98. sub sanityCheckParams {
  99. my $inf = shift;
  100. my @errs = ();
  101. # if we don't need to start the server right away, we can skip the
  102. # port number checks
  103. if (!defined($inf->{slapd}->{start_server}) or
  104. ($inf->{slapd}->{start_server} == 1)) {
  105. if (@errs = checkPort($inf)) {
  106. return @errs;
  107. }
  108. }
  109. if (!isValidServerID($inf->{slapd}->{ServerIdentifier})) {
  110. return ('error_invalid_serverid', $inf->{slapd}->{ServerIdentifier});
  111. } elsif (-d $inf->{slapd}->{config_dir}) {
  112. return ('error_server_already_exists', $inf->{slapd}->{config_dir});
  113. }
  114. if (@errs = isValidUser($inf->{General}->{SuiteSpotUserID})) {
  115. return @errs;
  116. }
  117. if (@errs = isValidGroup($inf->{General}->{SuiteSpotGroup})) {
  118. return @errs;
  119. }
  120. if (!isValidDN($inf->{slapd}->{Suffix})) {
  121. return ('dialog_dssuffix_error', $inf->{slapd}->{Suffix});
  122. }
  123. if (!isValidDN($inf->{slapd}->{RootDN})) {
  124. return ('dialog_dsrootdn_error', $inf->{slapd}->{RootDN});
  125. }
  126. if ($inf->{slapd}->{RootDNPwd} =~ /^\{\w+\}.+/) {
  127. debug(1, "The root password is already hashed - no checking will be performed\n");
  128. } elsif (length($inf->{slapd}->{RootDNPwd}) < 8) {
  129. debug(0, "WARNING: The root password is less than 8 characters long. You should choose a longer one.\n");
  130. }
  131. my $str = checkHostname($inf->{General}->{FullMachineName});
  132. if ($str) {
  133. debug(0, $str);
  134. }
  135. return ();
  136. }
  137. sub getMode {
  138. my $inf = shift;
  139. my $mode = shift;
  140. my $rest = shift;
  141. if (!$rest) {
  142. $rest = "0";
  143. }
  144. if (defined($inf->{General}->{SuiteSpotGroup})) {
  145. $mode = "0" . $mode . $mode . $rest;
  146. } else {
  147. $mode = "0" . $mode . $rest . $rest;
  148. }
  149. return oct($mode);
  150. }
  151. # This is used to change the ownership and permissions of files and directories
  152. # The mode is just a single digit octal number (e.g. 4 6 7)
  153. # If there is a group, the ownership and permissions will allow group access
  154. # otherwise, only the owner will be allowed access
  155. sub changeOwnerMode {
  156. my $inf = shift;
  157. my $mode = shift;
  158. my $it = shift;
  159. my $gidonly = shift;
  160. my $othermode = shift;
  161. my $uid = getpwnam $inf->{General}->{SuiteSpotUserID};
  162. my $gid = -1; # default to leave it alone
  163. my $mode_string = "";
  164. if (defined($inf->{General}->{SuiteSpotGroup})) {
  165. $gid = getgrnam $inf->{General}->{SuiteSpotGroup};
  166. }
  167. $mode = getMode($inf, $mode, $othermode);
  168. $! = 0; # clear errno
  169. chmod $mode, $it;
  170. if ($!) {
  171. return ('error_chmoding_file', $it, $!);
  172. }
  173. $mode_string = sprintf "%lo", $mode;
  174. debug(1, "changeOwnerMode: changed mode of $it to $mode_string\n");
  175. $! = 0; # clear errno
  176. if ( $gidonly ) {
  177. chown -1, $gid, $it;
  178. } else {
  179. chown $uid, $gid, $it;
  180. }
  181. if ($!) {
  182. return ('error_chowning_file', $it, $inf->{General}->{SuiteSpotUserID}, $!);
  183. }
  184. if ( $gidonly ) {
  185. debug(1, "changeOwnerMode: changed group ownership of $it to group $gid\n");
  186. } else {
  187. debug(1, "changeOwnerMode: changed ownership of $it to user $uid group $gid\n");
  188. }
  189. return ();
  190. }
  191. sub makeDSDirs {
  192. my $inf = shift;
  193. my $verbose = ($DSUtil::debuglevel > 0);
  194. my $mode = getMode($inf, 7);
  195. my @errs;
  196. # These paths are owned by the SuiteSpotGroup
  197. # This allows the admin server to run as a different,
  198. # more privileged user than the directory server, but
  199. # still allows the admin server to manage directory
  200. # server files/dirs without being root
  201. for my $kw (qw(inst_dir config_dir schema_dir log_dir lock_dir run_dir tmp_dir
  202. cert_dir db_dir ldif_dir bak_dir)) {
  203. my $dir = $inf->{slapd}->{$kw};
  204. @errs = makePaths($dir, $mode, $inf->{General}->{SuiteSpotUserID},
  205. $inf->{General}->{SuiteSpotGroup});
  206. if (@errs) {
  207. return @errs;
  208. }
  209. }
  210. # run_dir is a special case because it is usually shared among
  211. # all instances and the admin server
  212. # all instances must be able to write to it
  213. # if the SuiteSpotUserID is root or 0, we can just skip
  214. # this because root will have access to it - we really
  215. # shouldn't be using root anyway, primarily just for
  216. # legacy migration support
  217. # if there are two different user IDs that need access
  218. # to this directory, then SuiteSpotGroup must be defined,
  219. # and both users must be members of the SuiteSpotGroup
  220. if (($inf->{General}->{SuiteSpotUserID} eq 'root') ||
  221. (defined($inf->{General}->{SuiteSpotUserID}) &&
  222. ($inf->{General}->{SuiteSpotUserID} =~ /^0$/))) {
  223. # skip
  224. debug(3, "Root user " . $inf->{General}->{SuiteSpotUserID} . " already has access to $inf->{slapd}->{run_dir} - skipping\n");
  225. } else {
  226. my $dir = $inf->{slapd}->{run_dir};
  227. # rwx by user only, or by user & group if a group is defined. Also only change the group ownership.
  228. @errs = changeOwnerMode($inf, 7, $dir, 1);
  229. debug(3, "\t" . `/bin/ls -ld $dir`);
  230. }
  231. # set the group of the parent dir of config_dir and inst_dir
  232. if (defined($inf->{General}->{SuiteSpotGroup})) {
  233. for my $kw (qw(inst_dir config_dir)) {
  234. my $dir = $inf->{slapd}->{$kw};
  235. my $parent = dirname($dir);
  236. # changeOwnerMode(inf, mode, file, gidonly, othermode);
  237. @errs = changeOwnerMode($inf, 7, $parent, 1, 5);
  238. if (@errs) {
  239. return @errs;
  240. }
  241. }
  242. }
  243. return @errs;
  244. }
  245. sub createInstanceScripts {
  246. my $inf = shift;
  247. my $skip = shift;
  248. my $perlexec = "@perlexec@" || "/usr/bin/env perl";
  249. my $myperl = "!$perlexec";
  250. my $mydevnull = (-f "/dev/null" ? " /dev/null " : " NUL ");
  251. # determine initconfig_dir
  252. my $initconfig_dir = $inf->{slapd}->{initconfig_dir} || get_initconfigdir();
  253. my %maptable = (
  254. "DS-ROOT" => $inf->{General}->{prefix},
  255. "SEP" => "/", # works on all platforms
  256. "SERVER-NAME" => $inf->{General}->{FullMachineName},
  257. "SERVER-PORT" => $inf->{slapd}->{ServerPort},
  258. "PERL-EXEC" => $myperl,
  259. "DEV-NULL" => $mydevnull,
  260. "ROOT-DN" => $inf->{slapd}->{RootDN},
  261. "LDIF-DIR" => $inf->{slapd}->{ldif_dir},
  262. "SERV-ID" => $inf->{slapd}->{ServerIdentifier},
  263. "BAK-DIR" => $inf->{slapd}->{bak_dir},
  264. "SERVER-DIR" => $inf->{General}->{ServerRoot},
  265. "CONFIG-DIR" => $inf->{slapd}->{config_dir},
  266. "INITCONFIG-DIR" => $initconfig_dir,
  267. "INST-DIR" => $inf->{slapd}->{inst_dir},
  268. "RUN-DIR" => $inf->{slapd}->{run_dir},
  269. "PRODUCT-NAME" => "slapd",
  270. "SERVERBIN-DIR" => $inf->{slapd}->{sbindir},
  271. "DB-DIR" => $inf->{slapd}->{db_dir}
  272. );
  273. my $dir = "$inf->{General}->{prefix}@taskdir@";
  274. for my $file (glob("$dir/template-*")) {
  275. my $basename = $file;
  276. $basename =~ s/^.*template-//;
  277. my $destfile = "$inf->{slapd}->{inst_dir}/$basename";
  278. next if ($skip and -f $destfile); # in skip mode, skip files that already exist
  279. if (!open(SRC, "< $file")) {
  280. return ("error_opening_scripttmpl", $file, $!);
  281. }
  282. if (!open(DEST, "> $destfile")) {
  283. return ("error_opening_scripttmpl", $destfile, $!);
  284. }
  285. my $contents; # slurp entire file into memory
  286. read SRC, $contents, int(-s $file);
  287. close(SRC);
  288. while (my ($key, $val) = each %maptable) {
  289. $contents =~ s/\{\{$key\}\}/$val/g;
  290. }
  291. print DEST $contents;
  292. close(DEST);
  293. my @errs = changeOwnerMode($inf, 5, $destfile);
  294. if (@errs) {
  295. return @errs;
  296. }
  297. }
  298. return ();
  299. }
  300. sub createConfigFile {
  301. my $inf = shift;
  302. my $conffile = "$inf->{slapd}->{config_dir}/dse.ldif";
  303. my $conn = new FileConn;
  304. my @errs;
  305. # first, create the basic config
  306. my $mapper = new Inf("$inf->{General}->{prefix}@infdir@/dscreate.map");
  307. my $dsinf = new Inf("$inf->{General}->{prefix}@infdir@/slapd.inf");
  308. if (!$inf->{slapd}->{ds_bename}) {
  309. $inf->{slapd}->{ds_bename} = "userRoot"; # for suffix-db
  310. }
  311. $mapper = process_maptbl($mapper, \@errs, $inf, $dsinf);
  312. if (!$mapper or @errs) {
  313. $conn->close();
  314. if (!@errs) {
  315. @errs = ('error_creating_file', $conffile, $!);
  316. }
  317. return @errs;
  318. }
  319. my @ldiffiles = ("$inf->{General}->{prefix}@templatedir@/template-dse.ldif",
  320. "$inf->{General}->{prefix}@templatedir@/template-suffix-db.ldif",
  321. "$inf->{General}->{prefix}@templatedir@/template-sasl.ldif");
  322. if ("@enable_pam_passthru@") {
  323. push @ldiffiles, "$inf->{General}->{prefix}@templatedir@/template-pampta.ldif";
  324. }
  325. if ("@enable_bitwise@") {
  326. push @ldiffiles, "$inf->{General}->{prefix}@templatedir@/template-bitwise.ldif";
  327. }
  328. if ("@enable_dna@") {
  329. push @ldiffiles, "$inf->{General}->{prefix}@templatedir@/template-dnaplugin.ldif";
  330. }
  331. if (-f "$inf->{General}->{prefix}@updatedir@/50replication-plugins.ldif") {
  332. push @ldiffiles, "$inf->{General}->{prefix}@updatedir@/50replication-plugins.ldif";
  333. }
  334. if (-f "$inf->{General}->{prefix}@templatedir@/90betxn-plugins.ldif") {
  335. push @ldiffiles, "$inf->{General}->{prefix}@templatedir@/90betxn-plugins.ldif";
  336. }
  337. # additional configuration LDIF files
  338. if (exists($inf->{slapd}->{ConfigFile})) {
  339. if (ref($inf->{slapd}->{ConfigFile})) {
  340. push @ldiffiles, @{$inf->{slapd}->{ConfigFile}};
  341. } else {
  342. push @ldiffiles, $inf->{slapd}->{ConfigFile};
  343. }
  344. }
  345. getMappedEntries($mapper, \@ldiffiles, \@errs, \&check_and_add_entry,
  346. [$conn]);
  347. if (@errs) {
  348. $conn->close();
  349. return @errs;
  350. }
  351. if ("@enable_ldapi@") {
  352. my $ent = $conn->search("cn=config", "base", "(objectclass=*)");
  353. if (defined($inf->{slapd}->{ldapifilepath})) {
  354. $ent->setValues("nsslapd-ldapifilepath", $inf->{slapd}->{ldapifilepath});
  355. $ent->setValues("nsslapd-ldapilisten", "on");
  356. } else {
  357. my $parent = dirname($inf->{slapd}->{run_dir});
  358. $ent->setValues("nsslapd-ldapifilepath",
  359. "$parent/slapd-$inf->{slapd}->{ServerIdentifier}.socket");
  360. $ent->setValues("nsslapd-ldapilisten", "off");
  361. }
  362. if ("@enable_autobind@") {
  363. $ent->setValues("nsslapd-ldapiautobind", "off");
  364. $ent->setValues("nsslapd-ldapimaprootdn", $inf->{slapd}->{RootDN});
  365. $ent->setValues("nsslapd-ldapimaptoentries", "off");
  366. $ent->setValues("nsslapd-ldapiuidnumbertype", "uidNumber");
  367. $ent->setValues("nsslapd-ldapigidnumbertype", "gidNumber");
  368. $ent->setValues("nsslapd-ldapientrysearchbase", $inf->{slapd}->{Suffix});
  369. if ("@enable_auto_dn_suffix@") {
  370. $ent->setValues("nsslapd-ldapiautodnsuffix", "cn=peercred,cn=external,cn=auth");
  371. }
  372. }
  373. $ent->setValues("nsslapd-defaultNamingContext", $inf->{slapd}->{Suffix});
  374. if (!$conn->update($ent)) {
  375. $conn->close();
  376. return ("error_enabling_feature", "ldapi", $conn->getErrorString());
  377. }
  378. }
  379. if ($inf->{slapd}->{sasl_path}) {
  380. my $ent = $conn->search("cn=config", "base", "(objectclass=*)");
  381. $ent->setValues("nsslapd-saslpath", $inf->{slapd}->{sasl_path});
  382. if (!$conn->update($ent)) {
  383. $conn->close();
  384. return ("error_enabling_feature", "sasl_path", $conn->getErrorString());
  385. }
  386. }
  387. if (!$conn->write($conffile)) {
  388. $conn->close();
  389. return ("error_writing_ldif", $conffile, $!);
  390. }
  391. $conn->close();
  392. if (@errs = changeOwnerMode($inf, 6, $conffile)) {
  393. return @errs;
  394. }
  395. # make a copy
  396. my $origconf = "$inf->{slapd}->{config_dir}/dse_original.ldif";
  397. $! = 0; # clear errno
  398. copy($conffile, $origconf);
  399. if ($!) {
  400. return ('error_copying_file', $conffile, $origconf, $!);
  401. }
  402. if (@errs = changeOwnerMode($inf, 4, $origconf)) {
  403. return @errs;
  404. }
  405. return @errs;
  406. }
  407. sub makeOtherConfigFiles {
  408. my $inf = shift;
  409. my $skip = shift;
  410. my @errs;
  411. my %maptable = (
  412. "DS-ROOT" => $inf->{General}->{prefix},
  413. "SERVER-DIR" => $inf->{General}->{ServerRoot},
  414. "CONFIG-DIR" => $inf->{slapd}->{config_dir},
  415. "INST-DIR" => $inf->{slapd}->{inst_dir},
  416. "RUN-DIR" => $inf->{slapd}->{run_dir},
  417. "PRODUCT-NAME" => "slapd",
  418. "SERVERBIN-DIR" => $inf->{slapd}->{sbindir},
  419. );
  420. # install certmap.conf at <configdir>
  421. my $src = "$inf->{General}->{prefix}@configdir@/certmap.conf";
  422. my $dest = "$inf->{slapd}->{config_dir}/certmap.conf";
  423. $! = 0; # clear errno
  424. copy($src, $dest);
  425. if ($!) {
  426. return ('error_copying_file', $src, $dest, $!);
  427. }
  428. if (@errs = changeOwnerMode($inf, 4, $dest)) {
  429. return @errs;
  430. }
  431. $src = "$inf->{General}->{prefix}@configdir@/slapd-collations.conf";
  432. $dest = "$inf->{slapd}->{config_dir}/slapd-collations.conf";
  433. $! = 0; # clear errno
  434. #in skip mode, skip files that already exist
  435. unless ($skip and -f $dest) {
  436. copy($src, $dest);
  437. if ($!) {
  438. return ('error_copying_file', $src, $dest, $!);
  439. }
  440. if (@errs = changeOwnerMode($inf, 4, $dest)) {
  441. return @errs;
  442. }
  443. }
  444. # determine initconfig_dir
  445. my $initconfig_dir = $inf->{slapd}->{initconfig_dir} || get_initconfigdir();
  446. # install instance specific initconfig script
  447. $src = "$inf->{General}->{prefix}@configdir@/template-initconfig";
  448. $dest = "$initconfig_dir/@package_name@-$inf->{slapd}->{ServerIdentifier}";
  449. $! = 0; # clear errno
  450. # in skip mode, skip files that already exist
  451. unless ($skip and -f $dest) {
  452. if (!open(SRC, "< $src")) {
  453. return ("error_opening_scripttmpl", $src, $!);
  454. }
  455. if (!open(DEST, "> $dest")) {
  456. return ("error_opening_scripttmpl", $dest, $!);
  457. }
  458. my $contents; # slurp entire file into memory
  459. read SRC, $contents, int(-s $src);
  460. close(SRC);
  461. while (my ($key, $val) = each %maptable) {
  462. $contents =~ s/\{\{$key\}\}/$val/g;
  463. }
  464. print DEST $contents;
  465. close(DEST);
  466. if (@errs = changeOwnerMode($inf, 4, $dest)) {
  467. return @errs;
  468. }
  469. }
  470. return ();
  471. }
  472. sub installSchema {
  473. my $inf = shift;
  474. my $skip = shift;
  475. my @errs;
  476. my @schemafiles = ();
  477. if (!defined($inf->{slapd}->{install_full_schema}) or
  478. $inf->{slapd}->{install_full_schema}) {
  479. push @schemafiles, glob("$inf->{General}->{prefix}@schemadir@/*");
  480. } else {
  481. push @schemafiles, "$inf->{General}->{prefix}@schemadir@/00core.ldif",
  482. "$inf->{General}->{prefix}@schemadir@/01core389.ldif";
  483. }
  484. # additional schema files
  485. if (exists($inf->{slapd}->{SchemaFile})) {
  486. if (ref($inf->{slapd}->{SchemaFile})) {
  487. push @schemafiles, @{$inf->{slapd}->{SchemaFile}};
  488. } else {
  489. push @schemafiles, $inf->{slapd}->{SchemaFile};
  490. }
  491. }
  492. for my $file (@schemafiles) {
  493. my $src = $file;
  494. my $basename = basename($src);
  495. my $dest = "$inf->{slapd}->{schema_dir}/$basename";
  496. next if ($skip and -f $dest); # skip files that already exist
  497. $! = 0; # clear errno
  498. copy($src, $dest);
  499. if ($!) {
  500. return ('error_copying_file', $src, $dest, $!);
  501. }
  502. my $mode = 4; # default read only
  503. if ($basename eq "99user.ldif") {
  504. $mode = 6; # read write
  505. }
  506. if (@errs = changeOwnerMode($inf, $mode, $dest)) {
  507. return @errs;
  508. }
  509. }
  510. return ();
  511. }
  512. # maps the suffix attr to the filename to use
  513. my %suffixTable = (
  514. 'o' => "@templatedir@/template-org.ldif",
  515. 'dc' => "@templatedir@/template-domain.ldif",
  516. 'ou' => "@templatedir@/template-orgunit.ldif",
  517. 'st' => "@templatedir@/template-state.ldif",
  518. 'l' => "@templatedir@/template-locality.ldif",
  519. 'c' => "@templatedir@/template-country.ldif"
  520. );
  521. sub initDatabase {
  522. my $inf = shift;
  523. my $istempldif = 0;
  524. # If the user has specified an LDIF file to use to initialize the database,
  525. # load it now
  526. my $ldiffile = $inf->{slapd}->{InstallLdifFile};
  527. if ($ldiffile =~ /none/i) {
  528. debug(1, "No ldif file or org entries specified - no initial database will be created\n");
  529. return ();
  530. } elsif ($ldiffile && ($ldiffile !~ /suggest/i)) {
  531. debug(1, "Loading initial ldif file $ldiffile\n");
  532. if (! -r $ldiffile) {
  533. return ('error_opening_init_ldif', $ldiffile);
  534. }
  535. } elsif (($inf->{slapd}->{Suffix} =~ /^(.*?)=/) && $suffixTable{$1}) {
  536. my @errs;
  537. my $template = $inf->{General}->{prefix} . $suffixTable{$1};
  538. my $mapper = new Inf("$inf->{General}->{prefix}@infdir@/dsorgentries.map");
  539. my $dsinf = new Inf("$inf->{General}->{prefix}@infdir@/slapd.inf");
  540. my @rdns = ldap_explode_dn($inf->{slapd}->{Suffix}, 1);
  541. $inf->{slapd}->{naming_value} = $rdns[0];
  542. $mapper = process_maptbl($mapper, \@errs, $inf, $dsinf);
  543. if (!$mapper or @errs) {
  544. return @errs;
  545. }
  546. my @ldiffiles = ($template, "$inf->{General}->{prefix}@templatedir@/template-baseacis.ldif");
  547. # default is to create org entries unless explicitly set to none
  548. if (!exists($inf->{slapd}->{InstallLdifFile}) or
  549. ($inf->{slapd}->{InstallLdifFile} =~ /suggest/i)) {
  550. push @ldiffiles, "$inf->{General}->{prefix}@templatedir@/template.ldif";
  551. }
  552. my ($fh, $templdif) = tempfile("ldifXXXXXX", SUFFIX => ".ldif", OPEN => 0,
  553. DIR => File::Spec->tmpdir);
  554. if (!$templdif) {
  555. return ('error_creating_templdif', $!);
  556. }
  557. my $conn = new FileConn;
  558. $conn->setNamingContext($inf->{slapd}->{Suffix});
  559. getMappedEntries($mapper, \@ldiffiles, \@errs, \&check_and_add_entry,
  560. [$conn]);
  561. if (@errs) {
  562. $conn->close();
  563. return @errs;
  564. }
  565. if (!$conn->write($templdif)) {
  566. $conn->close();
  567. return ('error_writing_ldif', $templdif, $!);
  568. }
  569. $conn->close();
  570. if (@errs) {
  571. return @errs;
  572. }
  573. if (@errs = changeOwnerMode($inf, 4, $templdif)) {
  574. unlink($ldiffile);
  575. return @errs;
  576. }
  577. # $templdif now contains the ldif to import
  578. $ldiffile = $templdif;
  579. $istempldif = 1;
  580. }
  581. if (!$ldiffile) {
  582. return ();
  583. }
  584. my $cmd = "$inf->{slapd}->{inst_dir}/ldif2db -n $inf->{slapd}->{ds_bename} -i \'$ldiffile\'";
  585. $? = 0; # clear error condition
  586. my $output = `$cmd 2>&1`;
  587. my $result = $?;
  588. if ($istempldif) {
  589. unlink($ldiffile);
  590. }
  591. if ($result) {
  592. return ('error_importing_ldif', $ldiffile, $result, $output);
  593. }
  594. debug(1, $output);
  595. return ();
  596. }
  597. sub startServer {
  598. my $inf = shift;
  599. return () if (defined($inf->{slapd}->{start_server}) && !$inf->{slapd}->{start_server});
  600. my @errs;
  601. # get error log
  602. my $errLog = "$inf->{slapd}->{log_dir}/errors";
  603. my $startcmd = "$inf->{slapd}->{inst_dir}/start-slapd";
  604. if ("@systemdsystemunitdir@" and (getLogin() eq 'root')) {
  605. $startcmd = "/bin/systemctl start @package_name@\@$inf->{slapd}->{ServerIdentifier}.service";
  606. }
  607. # emulate tail -f
  608. # if the last line we see does not contain "slapd started", try again
  609. my $done = 0;
  610. my $started = 0;
  611. my $code = 0;
  612. my $lastLine = "";
  613. my $cmdPat = 'slapd started\.';
  614. my $timeout = $inf->{slapd}->{startup_timeout};
  615. $timeout = $timeout?$timeout:600; # default is 10 minutes
  616. $timeout = time + $timeout;
  617. debug(1, "Starting the server: $startcmd\n");
  618. $? = 0; # clear error condition
  619. my $output = `$startcmd 2>&1`;
  620. $code = $?;
  621. debug(1, "Started the server: code $code\n");
  622. if ($code) {
  623. debug(0, $output);
  624. } else {
  625. debug(1, $output);
  626. }
  627. # try to open the server error log
  628. my $ii = 0;
  629. while (time < $timeout) {
  630. if (open(IN, $errLog)) {
  631. last;
  632. }
  633. sleep(1);
  634. if (!($ii % 10)) {
  635. debug(0, "Attempting to obtain server status . . .\n");
  636. }
  637. ++$ii;
  638. }
  639. if (! -f $errLog) {
  640. debug(0, "Error: Could not read error log $errLog to get server startup status. Error: $!\n");
  641. return ('error_starting_server', $startcmd, "no status", $!);
  642. }
  643. if (time >= $timeout) {
  644. debug(0, "Error: timed out waiting for the server to start and write to $errLog");
  645. return ('error_starting_server', $startcmd, "timeout", 0);
  646. }
  647. my $pos = tell(IN);
  648. my $line;
  649. while (($done == 0) && (time < $timeout)) {
  650. for (; ($done == 0) && ($line = <IN>); $pos = tell(IN)) {
  651. $lastLine = $line;
  652. debug(1, $line);
  653. if ($line =~ /$cmdPat/) {
  654. $done = 1;
  655. $started = 1;
  656. } elsif ($line =~ /Initialization Failed/) {
  657. debug(1, "Server failed to start, retrying . . .\n");
  658. $code = system($startcmd);
  659. } elsif ($line =~ /exiting\./) {
  660. debug(1, "Server failed to start, retrying . . .\n");
  661. $code = system($startcmd);
  662. }
  663. }
  664. if ($lastLine =~ /PR_Bind/) {
  665. # server port conflicts with another one, just report and punt
  666. debug(0, $lastLine);
  667. @errs = ('error_port_available', $inf->{slapd}->{ServerPort}, $!);
  668. $done = 1;
  669. }
  670. if ($done == 0) {
  671. # rest a bit, then . . .
  672. sleep(2);
  673. # . . . reset the EOF status of the file desc
  674. seek(IN, $pos, 0);
  675. }
  676. }
  677. close(IN);
  678. if (!$started) {
  679. $! = $code;
  680. my $now = time;
  681. if ($now > $timeout) {
  682. debug(0, "Possible timeout starting server: timeout=$timeout now=$now\n");
  683. }
  684. @errs = ('error_starting_server', $startcmd, $lastLine, $!);
  685. } else {
  686. debug(1, "Your new directory server has been started.\n");
  687. }
  688. return @errs;
  689. }
  690. sub set_path_attribute {
  691. my $val = shift;
  692. my $defaultval = shift;
  693. my $prefix = shift;
  694. if ($val) {
  695. return "$prefix" . "$val";
  696. } else {
  697. return "$prefix" . "$defaultval";
  698. }
  699. }
  700. sub setDefaults {
  701. my $inf = shift;
  702. # set default values
  703. # this turns off the warnings
  704. if (!defined($inf->{General}->{prefix})) {
  705. $inf->{General}->{prefix} = "";
  706. }
  707. if (!$inf->{General}->{FullMachineName}) {
  708. $inf->{General}->{FullMachineName} = hostfqdn;
  709. }
  710. if (!$inf->{General}->{SuiteSpotUserID}) {
  711. if ($> != 0) { # if not root, use the user's uid
  712. $inf->{General}->{SuiteSpotUserID} = getLogin;
  713. }
  714. # otherwise, the uid must be specified
  715. }
  716. if (!$inf->{General}->{SuiteSpotGroup}) {
  717. # If the group wasn't specified, use the primary group
  718. # of the SuiteSpot user
  719. $inf->{General}->{SuiteSpotGroup} = getGroup($inf->{General}->{SuiteSpotUserID});
  720. }
  721. if (!$inf->{slapd}->{RootDN}) {
  722. $inf->{slapd}->{RootDN} = "cn=Directory Manager";
  723. }
  724. if (!$inf->{slapd}->{Suffix}) {
  725. my $suffix = $inf->{General}->{FullMachineName};
  726. # convert fqdn to dc= domain components
  727. $suffix =~ s/^[^\.]*\.//; # just the domain part
  728. $suffix = "dc=$suffix";
  729. $suffix =~ s/\./,dc=/g;
  730. $inf->{slapd}->{Suffix} = $suffix;
  731. }
  732. $inf->{slapd}->{Suffix} = normalizeDN($inf->{slapd}->{Suffix});
  733. if (!$inf->{slapd}->{ServerIdentifier}) {
  734. my $servid = $inf->{General}->{FullMachineName};
  735. # strip out the leftmost domain component
  736. $servid =~ s/\..*$//;
  737. $inf->{slapd}->{ServerIdentifier} = $servid;
  738. }
  739. if ("@with_fhs_opt@") {
  740. $inf->{General}->{ServerRoot} = "$inf->{General}->{prefix}/opt/@PACKAGE_NAME@";
  741. } else {
  742. $inf->{General}->{ServerRoot} = "$inf->{General}->{prefix}@serverdir@";
  743. }
  744. if (!defined($inf->{slapd}->{sasl_path})) {
  745. if ($ ne "linux") {
  746. $inf->{slapd}->{sasl_path} = "$inf->{General}->{prefix}@libdir@/sasl2";
  747. }
  748. }
  749. if (!defined($inf->{slapd}->{ServerPort}) and
  750. !defined($inf->{slapd}->{ldapifilepath})) {
  751. if ("@enable_ldapi@") {
  752. return ('error_missing_port_and_ldapi');
  753. } else {
  754. return ('error_missing_port');
  755. }
  756. }
  757. if (!defined($inf->{slapd}->{ServerPort})) {
  758. $inf->{slapd}->{ServerPort} = 0;
  759. }
  760. $inf->{slapd}->{HashedRootDNPwd} = getHashedPassword($inf->{slapd}->{RootDNPwd});
  761. $inf->{slapd}->{localstatedir} = set_path_attribute($inf->{slapd}->{localstatedir},
  762. "@localstatedir@",
  763. $inf->{General}->{prefix});
  764. my $localstatedir = $inf->{slapd}->{localstatedir};
  765. my $servid = $inf->{slapd}->{ServerIdentifier};
  766. $inf->{slapd}->{sysconfdir} = set_path_attribute($inf->{slapd}->{sysconfdir},
  767. "@sysconfdir@",
  768. $inf->{General}->{prefix});
  769. my $sysconfdir = $inf->{slapd}->{sysconfdir};
  770. $inf->{slapd}->{bindir} = set_path_attribute($inf->{slapd}->{bindir},
  771. "@bindir@",
  772. $inf->{General}->{prefix});
  773. $inf->{slapd}->{sbindir} = set_path_attribute($inf->{slapd}->{sbindir},
  774. "@sbindir@",
  775. $inf->{General}->{prefix});
  776. $inf->{slapd}->{datadir} = set_path_attribute($inf->{slapd}->{datadir},
  777. "@datadir@",
  778. $inf->{General}->{prefix});
  779. if (!defined($inf->{slapd}->{inst_dir})) {
  780. $inf->{slapd}->{inst_dir} = "$inf->{General}->{ServerRoot}/slapd-$servid";
  781. }
  782. if (!defined($inf->{slapd}->{config_dir})) {
  783. $inf->{slapd}->{config_dir} = "$inf->{General}->{prefix}@instconfigdir@/slapd-$servid";
  784. }
  785. $ENV{DS_CONFIG_DIR} = $inf->{slapd}->{config_dir};
  786. if (!defined($inf->{slapd}->{schema_dir})) {
  787. $inf->{slapd}->{schema_dir} = "$sysconfdir/@PACKAGE_NAME@/slapd-$servid/schema";
  788. }
  789. if (!defined($inf->{slapd}->{lock_dir})) {
  790. if ("@with_fhs_opt@") {
  791. $inf->{slapd}->{lock_dir} = "$localstatedir/@PACKAGE_NAME@/slapd-$servid/lock";
  792. } else {
  793. $inf->{slapd}->{lock_dir} = "$localstatedir/lock/@PACKAGE_NAME@/slapd-$servid";
  794. }
  795. }
  796. if (!defined($inf->{slapd}->{log_dir})) {
  797. if ("@with_fhs_opt@") {
  798. $inf->{slapd}->{log_dir} = "$localstatedir/@PACKAGE_NAME@/slapd-$servid/log";
  799. } else {
  800. $inf->{slapd}->{log_dir} = "$localstatedir/log/@PACKAGE_NAME@/slapd-$servid";
  801. }
  802. }
  803. if (!defined($inf->{slapd}->{run_dir})) {
  804. if ("@with_fhs_opt@") {
  805. $inf->{slapd}->{run_dir} = "$localstatedir/@PACKAGE_NAME@/slapd-$servid/run";
  806. } else {
  807. $inf->{slapd}->{run_dir} = "$localstatedir/run/@PACKAGE_NAME@";
  808. }
  809. }
  810. $ENV{DS_RUN_DIR} = $inf->{slapd}->{run_dir};
  811. if (!defined($inf->{slapd}->{db_dir})) {
  812. if ("@with_fhs_opt@") {
  813. $inf->{slapd}->{db_dir} = "$localstatedir/@PACKAGE_NAME@/slapd-$servid/db";
  814. } else {
  815. $inf->{slapd}->{db_dir} = "$localstatedir/lib/@PACKAGE_NAME@/slapd-$servid/db";
  816. }
  817. }
  818. if (!defined($inf->{slapd}->{bak_dir})) {
  819. if ("@with_fhs_opt@") {
  820. $inf->{slapd}->{bak_dir} = "$localstatedir/@PACKAGE_NAME@/slapd-$servid/bak";
  821. } else {
  822. $inf->{slapd}->{bak_dir} = "$localstatedir/lib/@PACKAGE_NAME@/slapd-$servid/bak";
  823. }
  824. }
  825. $ENV{DS_BAK_DIR} = $inf->{slapd}->{bak_dir};
  826. if (!defined($inf->{slapd}->{ldif_dir})) {
  827. if ("@with_fhs_opt@") {
  828. $inf->{slapd}->{ldif_dir} = "$localstatedir/@PACKAGE_NAME@/slapd-$servid/ldif";
  829. } else {
  830. $inf->{slapd}->{ldif_dir} = "$localstatedir/lib/@PACKAGE_NAME@/slapd-$servid/ldif";
  831. }
  832. }
  833. if (!defined($inf->{slapd}->{tmp_dir})) {
  834. if ("@with_fhs_opt@") {
  835. $inf->{slapd}->{tmp_dir} = "/tmp";
  836. } else {
  837. $inf->{slapd}->{tmp_dir} = "/tmp";
  838. }
  839. }
  840. $ENV{DS_TMP_DIR} = $inf->{slapd}->{tmp_dir};
  841. if (!defined($inf->{slapd}->{cert_dir})) {
  842. $inf->{slapd}->{cert_dir} = $inf->{slapd}->{config_dir};
  843. }
  844. return ();
  845. }
  846. sub updateSelinuxPolicy {
  847. my $inf = shift;
  848. # if selinux is not available, do nothing
  849. if ("@with_selinux@") {
  850. my $localstatedir = $inf->{slapd}->{localstatedir};
  851. # run restorecon on all of the parent directories we
  852. # may have created (this only happens if this is the
  853. # first instance created).
  854. if ("@with_fhs_opt@") {
  855. system("restorecon -R $localstatedir/@PACKAGE_NAME@");
  856. } else {
  857. system("restorecon -R $localstatedir/lock/@PACKAGE_NAME@");
  858. system("restorecon -R $localstatedir/log/@PACKAGE_NAME@");
  859. system("restorecon -R $localstatedir/run/@PACKAGE_NAME@");
  860. system("restorecon -R $localstatedir/lib/@PACKAGE_NAME@");
  861. }
  862. # run restorecon on all instance directories we created
  863. for my $kw (qw(inst_dir config_dir schema_dir log_dir lock_dir run_dir tmp_dir
  864. cert_dir db_dir ldif_dir bak_dir)) {
  865. my $dir = $inf->{slapd}->{$kw};
  866. system("restorecon -R $dir");
  867. }
  868. # label the selected port as ldap_port_t
  869. if ($inf->{slapd}->{ServerPort} != 0) {
  870. my $need_label = 1;
  871. # check if the port is already labeled properly
  872. my $portline = `semanage port -l | grep ldap_port_t | grep tcp`;
  873. chomp($portline);
  874. $portline =~ s/ldap_port_t\s+tcp\s+//g;
  875. my @labeledports = split(/,\s+/, $portline);
  876. foreach my $labeledport (@labeledports) {
  877. if ($inf->{slapd}->{ServerPort} == $labeledport) {
  878. $need_label = 0;
  879. last;
  880. }
  881. }
  882. if ($need_label == 1) {
  883. system("semanage port -a -t ldap_port_t -p tcp $inf->{slapd}->{ServerPort}");
  884. }
  885. }
  886. }
  887. }
  888. sub updateTmpfilesDotD {
  889. my $inf = shift;
  890. my $dir = "@with_tmpfiles_d@";
  891. # if tmpfiles.d is not available, do nothing
  892. if ($dir and -d $dir) {
  893. my $filename = "$dir/@package_name@-$inf->{slapd}->{ServerIdentifier}.conf";
  894. if (! -f $filename) {
  895. debug(3, "Creating $filename\n");
  896. my $username = "";
  897. my $groupname = "";
  898. my $conffile = "$inf->{slapd}->{config_dir}/dse.ldif";
  899. # use the owner:group from the dse.ldif for the instance
  900. if (-f $conffile) {
  901. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  902. $atime,$mtime,$ctime,$blksize,$blocks)
  903. = stat(_);
  904. $username = getpwuid($uid);
  905. if (!$username) {
  906. debug(1, "Error: could not get username from uid $uid\n");
  907. }
  908. $groupname = getgrgid($gid);
  909. }
  910. # else, see if we were passed in values to use
  911. if (!$username) {
  912. $username = $inf->{General}->{SuiteSpotUserID};
  913. }
  914. if (!$groupname) {
  915. if (defined($inf->{General}->{SuiteSpotGroup})) {
  916. $groupname = $inf->{General}->{SuiteSpotGroup};
  917. } else { # $groupname
  918. $groupname = "-"; # use default
  919. }
  920. }
  921. my $parent = dirname($inf->{slapd}->{lock_dir});
  922. if (!open(DOTDFILE, ">$filename")) {
  923. return ( [ 'error_creating_file', $filename, $! ] );
  924. }
  925. # Type Path Mode UID GID Age
  926. # d /var/run/user 0755 root root 10d
  927. # we don't use age
  928. print DOTDFILE "d $inf->{slapd}->{run_dir} 0770 $username $groupname\n";
  929. print DOTDFILE "d $parent 0770 $username $groupname\n";
  930. print DOTDFILE "d $inf->{slapd}->{lock_dir} 0770 $username $groupname\n";
  931. close DOTDFILE;
  932. } else {
  933. debug(3, "$filename exists - skipping\n");
  934. }
  935. } else {
  936. debug(3, "no tmpfiles.d - skipping\n");
  937. }
  938. return ();
  939. }
  940. sub updateSystemD {
  941. my $inf = shift;
  942. my $unitdir = "@systemdsystemunitdir@";
  943. my $confbasedir = "@systemdsystemconfdir@";
  944. my $confdir = "$confbasedir/@[email protected]";
  945. if (!$unitdir or !$confdir or ! -d $unitdir or ! -d $confdir) {
  946. debug(3, "no systemd - skipping\n");
  947. return ();
  948. }
  949. my @errs = ();
  950. my $initconfigdir = $inf->{slapd}->{initconfigdir} || get_initconfigdir();
  951. debug(1, "updating systemd files in $unitdir and $confdir for all directory server instances in $initconfigdir\n");
  952. my $pkgname = "@package_name@";
  953. my $changes = 0;
  954. # installation should already have put down the files and
  955. # directories - we just need to update the symlinks
  956. my $servicefile = "$unitdir/$pkgname\@.service";
  957. # first, look for new instances
  958. for my $file (glob("$initconfigdir/$pkgname-*")) {
  959. my $inst = $file;
  960. $inst =~ s/^.*$pkgname-//;
  961. # see if this is the admin or snmp or some other service
  962. if (-f "$unitdir/$pkgname-$inst.service") {
  963. debug(1, "$unitdir/$pkgname-$inst.service already exists - skipping\n");
  964. next;
  965. } elsif (-f "$confbasedir/$pkgname-$inst.service") {
  966. debug(1, "$confbasedir/$pkgname-$inst.service already exists - skipping\n");
  967. next;
  968. } else {
  969. my $servicelink = "$confdir/$pkgname\@$inst.service";
  970. if (! -l $servicelink) {
  971. if (!symlink($servicefile, $servicelink)) {
  972. debug(1, "error updating link $servicelink to $servicefile - $!\n");
  973. push @errs, [ 'error_linking_file', $servicefile, $servicelink, $! ];
  974. } else {
  975. debug(2, "updated link $servicelink to $servicefile\n");
  976. }
  977. $changes++;
  978. }
  979. }
  980. }
  981. # next, look for instances that have been removed
  982. for my $file (glob("$confdir/$pkgname\@*.service")) {
  983. my $inst = $file;
  984. $inst =~ s/^.*$pkgname\@(.*?).service$/$1/;
  985. if (! -f "$initconfigdir/$pkgname-$inst") {
  986. if (!unlink($file)) {
  987. debug(1, "error removing $file - $!\n");
  988. push @errs, [ 'error_removing_path', $file, $! ];
  989. } else {
  990. debug(2, "removed systemd file $file for removed instance $inst\n");
  991. }
  992. $changes++;
  993. }
  994. }
  995. if ($changes > 0) {
  996. $? = 0;
  997. my $cmd = '/bin/systemctl --system daemon-reload';
  998. # run the reload command
  999. my $output = `$cmd 2>&1`;
  1000. my $status = $?;
  1001. if ($status) {
  1002. debug(1, "Error: $cmd failed - output $output: $!\n");
  1003. push @errs, [ 'error_running_command', $cmd, $output, $! ];
  1004. } else {
  1005. debug(2, "$cmd succeeded\n");
  1006. }
  1007. } else {
  1008. debug(1, "No changes to $unitdir or $confdir\n");
  1009. }
  1010. return @errs;
  1011. }
  1012. sub createDSInstance {
  1013. my $inf = shift;
  1014. my @errs;
  1015. if (@errs = setDefaults($inf)) {
  1016. return @errs;
  1017. }
  1018. if (@errs = sanityCheckParams($inf)) {
  1019. return @errs;
  1020. }
  1021. if (@errs = makeDSDirs($inf)) {
  1022. return @errs;
  1023. }
  1024. if (@errs = createConfigFile($inf)) {
  1025. return @errs;
  1026. }
  1027. if (@errs = makeOtherConfigFiles($inf)) {
  1028. return @errs;
  1029. }
  1030. if (@errs = createInstanceScripts($inf)) {
  1031. return @errs;
  1032. }
  1033. if (@errs = installSchema($inf)) {
  1034. return @errs;
  1035. }
  1036. if (@errs = initDatabase($inf)) {
  1037. return @errs;
  1038. }
  1039. updateSelinuxPolicy($inf);
  1040. if (@errs = updateTmpfilesDotD($inf)) {
  1041. return @errs;
  1042. }
  1043. if (@errs = updateSystemD($inf)) {
  1044. return @errs;
  1045. }
  1046. if (@errs = startServer($inf)) {
  1047. return @errs;
  1048. }
  1049. return @errs;
  1050. }
  1051. sub stopServer {
  1052. my $instancedir = shift;
  1053. my $prog = $instancedir . "/stop-slapd";
  1054. if (-x $prog) {
  1055. $? = 0;
  1056. # run the stop command
  1057. my $output = `$prog 2>&1`;
  1058. my $status = $?;
  1059. debug(3, "stopping server $instancedir returns status $status: output $output\n");
  1060. if ($status) {
  1061. debug(1,"Warning: Could not stop directory server: status $status: output $output\n");
  1062. # if the server is not running, that's ok
  1063. if ($output =~ /not running/) {
  1064. $! = ENOENT;
  1065. return 1;
  1066. }
  1067. # else, some other error (e.g. permission) - return false for error
  1068. return;
  1069. }
  1070. } else {
  1071. debug(1, "stopping server: no such program $prog: cannot stop server\n");
  1072. return;
  1073. }
  1074. debug(1, "Successfully stopped server $instancedir\n");
  1075. return 1;
  1076. }
  1077. # NOTE: Returns a list of array ref - each array ref is suitable for passing
  1078. # to Resource::getText
  1079. sub removeDSInstance {
  1080. my $inst = shift;
  1081. my $force = shift;
  1082. my $all = shift;
  1083. my $initconfig_dir = shift || get_initconfigdir();
  1084. my $baseconfigdir = $ENV{DS_CONFIG_DIR} || "@instconfigdir@";
  1085. my $instname = "slapd-$inst";
  1086. my $configdir;
  1087. my $rundir;
  1088. my $product_name;
  1089. my @errs;
  1090. my $initconfig = "$initconfig_dir/@package_name@-$inst";
  1091. # Get the configdir, rundir and product_name from the instance initconfig script.
  1092. unless(open(INFILE, $initconfig)) {
  1093. return ( [ 'error_no_such_instance', $instname, $! ] );
  1094. }
  1095. my $line;
  1096. while($line = <INFILE>) {
  1097. if ($line =~ /CONFIG_DIR=(.*) ; export CONFIG_DIR/) {
  1098. $configdir = $1;
  1099. } elsif ($line =~ /CONFIG_DIR=(.*)$/) {
  1100. $configdir = $1;
  1101. } elsif ($line =~ /RUN_DIR=(.*) ; export RUN_DIR/) {
  1102. $rundir = $1;
  1103. } elsif ($line =~ /RUN_DIR=(.*)$/) {
  1104. $rundir = $1;
  1105. } elsif ($line =~ /PRODUCT_NAME=(.*) ; export PRODUCT_NAME/) {
  1106. $product_name = $1;
  1107. } elsif ($line =~ /PRODUCT_NAME=(.*)$/) {
  1108. $product_name = $1;
  1109. }
  1110. }
  1111. close(INFILE);
  1112. if ( ! -d $configdir )
  1113. {
  1114. debug(1, "Error: $configdir does not exist: $!\n");
  1115. return ( [ 'error_no_such_instance', $configdir, $! ] );
  1116. }
  1117. # read the config file to find out the paths
  1118. my $dseldif = "$configdir/dse.ldif";
  1119. my $conn = new FileConn($dseldif, 1);
  1120. if (!$conn) {
  1121. debug(1, "Error: Could not open config file $dseldif: Error $!\n");
  1122. return ( [ 'error_opening_dseldif', $dseldif, $! ] );
  1123. }
  1124. my $dn = "cn=config";
  1125. my $entry = $conn->search($dn, "base", "(cn=*)", 0);
  1126. if (!$entry)
  1127. {
  1128. debug(1, "Error: Search $dn in $dseldif failed: $entry\n");
  1129. push @errs, [ 'error_finding_config_entry', $dn, $dseldif, $conn->getErrorString() ];
  1130. }
  1131. $dn = "cn=config,cn=ldbm database,cn=plugins,cn=config";
  1132. my $dbentry = $conn->search($dn, "base", "(cn=*)", 0);
  1133. if (!$dbentry)
  1134. {
  1135. debug(1, "Error: Search $dn in $dseldif failed: $dbentry\n");
  1136. push @errs, [ 'error_finding_config_entry', $dn, $dseldif, $conn->getErrorString() ];
  1137. }
  1138. $conn->close();
  1139. # stop the server
  1140. my $instdir = "";
  1141. if ($entry) {
  1142. foreach my $path ( @{$entry->{"nsslapd-instancedir"}} )
  1143. {
  1144. if (!stopServer($path)) {
  1145. if ($force) {
  1146. debug(1, "Warning: Could not stop directory server - Error: $! - forcing continue\n");
  1147. } elsif ($! == ENOENT) { # stop script not found or server not running
  1148. debug(1, "Warning: Could not stop directory server: already removed or not running\n");
  1149. push @errs, [ 'error_stopping_server', $path, $! ];
  1150. } else { # real error
  1151. debug(1, "Error: Could not stop directory server - aborting - use -f flag to force removal\n");
  1152. push @errs, [ 'error_stopping_server', $path, $! ];
  1153. return @errs;
  1154. }
  1155. }
  1156. $instdir = $path;
  1157. }
  1158. }
  1159. # remove physical dirs/files
  1160. if ($dbentry) {
  1161. push @errs, remove_tree($dbentry, "nsslapd-directory", $instname, 1);
  1162. push @errs, remove_tree($dbentry, "nsslapd-db-logdirectory", $instname, 1);
  1163. }
  1164. if ($entry) {
  1165. push @errs, remove_tree($entry, "nsslapd-lockdir", $instname, 0);
  1166. push @errs, remove_tree($entry, "nsslapd-tmpdir", $instname, 0);
  1167. push @errs, remove_tree($entry, "nsslapd-bakdir", $instname, 1);
  1168. push @errs, remove_tree($entry, "nsslapd-errorlog", $instname, 1);
  1169. }
  1170. # instance dir
  1171. if ( -d $instdir && $instdir =~ /$instname/ )
  1172. {
  1173. # clean up pid files (if any)
  1174. remove_pidfile("STARTPIDFILE", $inst, $instdir, $instname, $rundir, $product_name);
  1175. remove_pidfile("PIDFILE", $inst, $instdir, $instname, $rundir, $product_name);
  1176. my $rc = rmtree($instdir);
  1177. if ( 0 == $rc )
  1178. {
  1179. push @errs, [ 'error_removing_path', $instdir, $! ];
  1180. debug(1, "Warning: $instdir was not removed. Error: $!\n");
  1181. }
  1182. }
  1183. # Finally, config dir
  1184. if ($all) {
  1185. push @errs, remove_tree($entry, "nsslapd-schemadir", $instname, 1);
  1186. } else {
  1187. push @errs, remove_tree($entry, "nsslapd-schemadir", $instname, 1, "\.db\$");
  1188. }
  1189. # Remove the instance specific initconfig script
  1190. if ( -f $initconfig ) {
  1191. my $rc = unlink($initconfig);
  1192. if ( 0 == $rc )
  1193. {
  1194. push @errs, [ 'error_removing_path', $initconfig, $! ];
  1195. debug(1, "Warning: $initconfig was not removed. Error: $!\n");
  1196. }
  1197. }
  1198. my $tmpfilesdir = "@with_tmpfiles_d@";
  1199. my $tmpfilesname = "$tmpfilesdir/@package_name@-$inst";
  1200. if ($tmpfilesdir and -d $tmpfilesdir and -f $tmpfilesname) {
  1201. my $rc = unlink($tmpfilesname);
  1202. if ( 0 == $rc )
  1203. {
  1204. push @errs, [ 'error_removing_path', $tmpfilesname, $! ];
  1205. debug(1, "Warning: $tmpfilesname was not removed. Error: $!\n");
  1206. }
  1207. }
  1208. # remove the selinux label from the ports if needed
  1209. if ("@with_selinux@") {
  1210. foreach my $port (@{$entry->{"nsslapd-port"}})
  1211. {
  1212. my $semanage_err = `semanage port -d -t ldap_port_t -p tcp $port 2>&1`;
  1213. if ($? != 0) {
  1214. if (($semanage_err !~ /defined in policy, cannot be deleted/) && ($semanage_err !~ /is not defined/)) {
  1215. push @errs, [ 'error_removing_port_label', $port, $semanage_err];
  1216. debug(1, "Warning: Port $port not removed from selinux policy correctly. Error: $semanage_err\n");
  1217. }
  1218. }
  1219. }
  1220. foreach my $secureport (@{$entry->{"nsslapd-secureport"}})
  1221. {
  1222. my $semanage_err = `semanage port -d -t ldap_port_t -p tcp $secureport 2>&1`;
  1223. if ($? != 0) {
  1224. if (($semanage_err !~ /defined in policy, cannot be deleted/) && ($semanage_err !~ /is not defined/)) {
  1225. push @errs, [ 'error_removing_port_label', $secureport, $semanage_err];
  1226. debug(1, "Warning: Port $secureport not removed from selinux policy correctly. Error: $semanage_err\n");
  1227. }
  1228. }
  1229. }
  1230. }
  1231. # update systemd files
  1232. push @errs, updateSystemD();
  1233. # if we got here, report success
  1234. if (@errs) {
  1235. debug(1, "Could not successfully remove $instname\n");
  1236. } else {
  1237. debug(1, "Instance $instname removed.\n");
  1238. }
  1239. return @errs;
  1240. }
  1241. 1;
  1242. # emacs settings
  1243. # Local Variables:
  1244. # mode:perl
  1245. # indent-tabs-mode: nil
  1246. # tab-width: 4
  1247. # End: