home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / darwin / darwinx86.iso / usr / libexec / create_nidb < prev    next >
Encoding:
Text File  |  2001-09-17  |  7.3 KB  |  398 lines

  1. #!/usr/bin/perl
  2.  
  3. ##
  4. # Create a NetInfo database from flat files.
  5. # Usage: create_nidb [tag [masterhostname [root]]
  6. #
  7. # Default tag is local.
  8. # Default master hostname for tag local is localhost.
  9. # Default master hostname for other tags is system hostname.
  10. ##
  11.  
  12. use Sys::Hostname;
  13.  
  14. my $nipath = "/var/db/netinfo";
  15. my $filepath = "/etc";
  16. my $root = "/";
  17.  
  18. if ($ARGV[0]) { $tag = $ARGV[0]; }
  19. else { $tag = "local"; }
  20.  
  21. if ($ARGV[1]) { $master = $ARGV[1]; }
  22. else 
  23. {
  24.     if (${tag} eq "local") { $master = "localhost"; }
  25.     else { $master = hostname; }
  26. }
  27.  
  28. if ($ARGV[2]) { $root = $ARGV[2]; }
  29.  
  30. my $nidb = "${root}/${nipath}/${tag}.nidb";
  31.  
  32. die "Error: database ${nidb} exists\n" if (-d ${nidb});
  33.  
  34. my $nicl = "nicl -q -raw ${nidb}";
  35.  
  36. ##
  37. # For debugging:
  38. # open(NICL, ">/dev/tty"); 
  39. ##
  40.  
  41. ##
  42. # Create the database
  43. ##
  44.  
  45. system("${nicl} -create");
  46. open(NICL, "|${nicl}");
  47.  
  48. ##
  49. # Setup root directory
  50. ##
  51.  
  52. print NICL "create / master ${master}/${tag}\n";
  53. if (${tag} eq "local") { print NICL "create / trusted_networks\n" };
  54.  
  55. print NICL "create /users\n";
  56. print NICL "create /groups\n";
  57. print NICL "create /machines\n";
  58. print NICL "create /networks\n";
  59. print NICL "create /protocols\n";
  60. print NICL "create /rpcs\n";
  61. print NICL "create /services\n";
  62. print NICL "create /aliases\n";
  63. print NICL "create /mounts\n";
  64. print NICL "create /printers\n";
  65.  
  66. ##
  67. # Users
  68. ##
  69.  
  70. print NICL "cd /users\n";
  71.  
  72. my $checkforroot = 0;
  73.  
  74. open(PASSWD, "${root}/${filepath}/master.passwd");
  75.  
  76. while (<PASSWD>)
  77. {
  78.     chop;
  79.     s/#.*$//;
  80.  
  81.     my ($user, $passwd, $uid, $gid, $class, $change, $expire, $gecos, $dir, $shell) = split(':', $_);
  82.  
  83.     if ($user)
  84.     {
  85.         die "Found invalid passwd entry $_\n" if ($uid eq "" || $gid eq "");
  86.         if (${user} eq "root") { $checkforroot = 1; }
  87.  
  88.         print NICL "create ${user}\n";
  89.         print NICL "cd ${user}\n";
  90.         print NICL "create . passwd \"${passwd}\"\n";
  91.         print NICL "create . uid \"${uid}\"\n";
  92.         print NICL "create . gid \"${gid}\"\n";
  93.         print NICL "create . class \"${class}\"\n" if ($class ne "");
  94.         print NICL "create . change \"${change}\"\n" if ($change ne "");
  95.         print NICL "create . expire \"${expire}\"\n" if ($expire ne "");
  96.         print NICL "create . realname \"${gecos}\"\n";
  97.         print NICL "create . home \"${dir}\"\n" if ($dir ne "");
  98.         print NICL "create . shell    \"${shell}\"\n" if ($shell ne "");
  99.         print NICL "create . _writers_passwd \"${user}\"\n";
  100.         print NICL "cd ..\n";
  101.     }
  102. }
  103.  
  104. close (PASSWD);
  105.  
  106. ##
  107. # Make sure there is a root user
  108. ##
  109.  
  110. if (${checkforroot} eq 0)
  111. {
  112.     print NICL "create root\n";
  113.     print NICL "cd root\n";
  114.     print NICL "create . passwd *\n";
  115.     print NICL "create . uid 0\n";
  116.     print NICL "create . gid 0\n";
  117.     print NICL "create . change 0\n";
  118.     print NICL "create . expire 0\n";
  119.     print NICL "create . realname \"System Administrator\"\n";
  120.     print NICL "create . home /var/root\n";
  121.     print NICL "create . shell /bin/tcsh\n";
  122.     print NICL "create . _writers_passwd root\n";
  123.     print NICL "cd ..\n";
  124. }
  125.  
  126. ##
  127. # Groups
  128. ##
  129.  
  130. print NICL "cd /groups\n";
  131.  
  132. open(GROUP, "${root}/${filepath}/group");
  133.  
  134. while (<GROUP>)
  135. {
  136.     chop;
  137.     s/#.*$//;
  138.  
  139.     my ($group, $passwd, $gid, $users) = split(':', $_);
  140.     my (@users) = split(',', $users);
  141.  
  142.     if ($group)
  143.     {
  144.         die "Found invalid group entry $_\n" if ($gid eq "");
  145.  
  146.         print NICL "create ${group}\n";
  147.         print NICL "cd ${group}\n";
  148.         print NICL "create . gid \"${gid}\"\n";
  149.         print NICL "create . passwd \"${passwd}\"\n" if ($passwd ne "");
  150.         print NICL "create . users \"".join('" "', @users)."\"\n" if (@users);
  151.         print NICL "cd ..\n";
  152.     }
  153. }
  154.  
  155. close (GROUP);
  156.  
  157. ##
  158. # Hosts
  159. ##
  160.  
  161. open(HOSTS, "${root}/${filepath}/hosts");
  162.  
  163. print NICL "cd /machines\n";
  164.  
  165. while (<HOSTS>)
  166. {
  167.     chop;
  168.     s/#.*$//;
  169.     s/\s+/ /g;
  170.  
  171.     my ($ip_addr, $name, @names) = split(" ", $_);
  172.     my $namecount = unshift(@names, $name);
  173.  
  174.     if ($ip_addr)
  175.     {
  176.         die "Found invalid hosts entry $_\n" if ($name eq "");
  177.         $_ = $name;
  178.         s/\//\\\\\//g;
  179.         $name = $_;
  180.  
  181.         if ($namecount gt 1)
  182.         {
  183.             print NICL "create \"$name\" name \"".join('" "', @names)."\"\n";
  184.         }
  185.         print NICL "create $name ip_address \"${ip_addr}\"\n";
  186.     }
  187. }
  188.  
  189. print NICL "create localhost ip_address     \"127.0.0.1\"\n";
  190. print NICL "create localhost serves     \"./local\"\n";
  191. print NICL "create broadcasthost ip_address     \"255.255.255.255\"\n";
  192. print NICL "create broadcasthost serves     \"../network\"\n";
  193.  
  194. close (HOSTS);
  195.  
  196. ##
  197. # Networks
  198. ##
  199.  
  200. open(NETWORKS, "${root}/${filepath}/networks");
  201.  
  202. print NICL "cd /networks\n";
  203.  
  204. while (<NETWORKS>)
  205. {
  206.     chop;
  207.     s/#.*$//;
  208.     s/\s+/ /g;
  209.  
  210.     my ($name, $network, @names) = split(" ", $_);
  211.     my $namecount = unshift(@names, $name);
  212.  
  213.     if ($name)
  214.     {
  215.         die "Found invalid networks entry $_\n" if ($network eq "");
  216.  
  217.         $_ = $name;
  218.         s/\//\\\\\//g;
  219.         $name = $_;
  220.  
  221.         if ($namecount gt 1)
  222.         {
  223.             print NICL "create \"$name\" name \"".join('" "', @names)."\"\n";
  224.         }
  225.         print NICL "create \"$name\" address \"${network}\"\n";
  226.     }
  227. }
  228.  
  229. close (NETWORKS);
  230.  
  231. ##
  232. # Protocols
  233. ##
  234.  
  235. open(PROTOCOLS, "${root}/${filepath}/protocols");
  236.  
  237. print NICL "cd /protocols\n";
  238.  
  239. while (<PROTOCOLS>)
  240. {
  241.     chop;
  242.     s/#.*$//;
  243.     s/\s+/ /g;
  244.  
  245.     my ($name, $number, @names) = split(" ", $_);
  246.     my $namecount = unshift(@names, $name);
  247.  
  248.     if ($name)
  249.     {
  250.         die "Found invalid protocols entry $_\n" if ($number eq "");
  251.  
  252.         $_ = $name;
  253.         s/\//\\\\\//g;
  254.         $name = $_;
  255.  
  256.         if ($namecount gt 1)
  257.         {
  258.             print NICL "create \"$name\" name \"".join('" "', @names)."\"\n";
  259.         }
  260.         print NICL "create \"$name\" number \"${number}\"\n";
  261.     }
  262. }
  263.  
  264. close (PROTOCOLS);
  265.  
  266. ##
  267. # RPCs
  268. ##
  269.  
  270. open(RPC, "${root}/${filepath}/rpc");
  271.  
  272. print NICL "cd /rpcs\n";
  273.  
  274. while (<RPC>)
  275. {
  276.     chop;
  277.     s/#.*$//;
  278.     s/\s+/ /g;
  279.  
  280.     my ($name, $number, @names) = split(" ", $_);
  281.     my $namecount = unshift(@names, $name);
  282.  
  283.     if ($name)
  284.     {
  285.         die "Found invalid rpc entry $_\n" if ($number eq "");
  286.  
  287.         $_ = $name;
  288.         s/\//\\\\\//g;
  289.         $name = $_;
  290.     
  291.         if ($namecount gt 1)
  292.         {
  293.             print NICL "create \"$name\" name \"".join('" "', @names)."\"\n";
  294.         }
  295.         print NICL "create \"$name\" number \"${number}\"\n";
  296.     }
  297. }
  298.  
  299. close (RPC);
  300.  
  301. ##
  302. # Services
  303. ##
  304.  
  305. open(SERVICES, "${root}/${filepath}/services");
  306.  
  307. print NICL "cd /services\n";
  308.  
  309. while (<SERVICES>)
  310. {
  311.     chomp;
  312.     if (!grep /^[a-zA-Z0-9]/, $_)
  313.     {
  314.         next;
  315.     }
  316.  
  317.     @line = split;
  318.     ($port, $prot) = split /\//, $line[1];
  319.     $service{$line[0]}{'port'} = $port;
  320.     $service{$line[0]}{'protocols'}{$prot} = 1;
  321.  
  322.     for ($n = 2; defined $line[$n]; $n++)
  323.     {
  324.         if (grep(/^\#/,$line[$n]))
  325.         {
  326.             last;
  327.         }
  328.         $service{$line[0]}{'names'}{$line[$n]} = 1;
  329.     }
  330. }
  331.  
  332. foreach $key (keys %service)
  333. {
  334.     $_ = $key;
  335.     s/\//\\\\\//g;
  336.     $nkey = $_;
  337.  
  338.     print NICL "create \"$nkey\" port $service{$key}{'port'}\n";
  339.  
  340.     foreach $prot (keys %{$service{$key}{'protocols'}})
  341.     {
  342.         $protocols = "$protocols \"$prot\"";
  343.     }
  344.  
  345.     if ($protocols)
  346.     {
  347.         print NICL "create \"$nkey\" protocol $protocols\n";
  348.     }
  349.  
  350.     foreach $name (keys %{$service{$key}{'names'}})
  351.     {
  352.         $names = "$names \"$name\"";
  353.     }
  354.  
  355.     if (defined $names)
  356.     {
  357.         $names = "\"$key\" $names";
  358.         print NICL "create \"$nkey\" name $names\n";
  359.     }
  360.  
  361.     undef $protocols;
  362.     undef $names;
  363. }
  364.  
  365. ##
  366. # Aliases
  367. ##
  368.  
  369. print NICL "cd /aliases\n";
  370.  
  371. print NICL "create administrator members root\n";
  372. print NICL "create postmaster members root\n";
  373. print NICL "create MAILER-DAEMON members postmaster\n";
  374. print NICL "create MAILER-AGENT members postmaster\n";
  375. print NICL "create nobody members root\n";
  376. print NICL "create dumper members root\n";
  377. print NICL "create manager members root\n";
  378. print NICL "create operator members root\n";
  379.  
  380. ##
  381. # Mounts
  382. ##
  383.  
  384. print NICL "cd /mounts\n";
  385.  
  386. ##
  387. # Printers
  388. ##
  389.  
  390. print NICL "cd /printers\n";
  391.  
  392. ##
  393. # Clean up
  394. ##
  395.  
  396. print NICL "quit\n";
  397. close (NICL);
  398.