nis-util  1.0.D108
nis-util-netid/map.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2002, 2008, 2009, 2011, 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or (at
00008 // your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include <algorithm>
00020 #include <map>
00021 
00022 #include <lib/colon/group.h>
00023 #include <lib/colon/passwd.h>
00024 #include <lib/output/file.h>
00025 #include <lib/space/hosts.h>
00026 #include <lib/space/netid.h>
00027 
00028 #include <nis-util-netid/map.h>
00029 
00030 
00031 void
00032 map(const rcstring &etc_netid, const rcstring &etc_passwd,
00033     const rcstring &etc_group, const rcstring &etc_hosts,
00034     const rcstring &domain, const rcstring &ofn)
00035 {
00036     typedef std::map<rcstring, space_netid::record::pointer> result_t;
00037     result_t result;
00038 
00039     typedef std::map<rcstring, space_netid::record::pointer> user_t;
00040     user_t user;
00041 
00042     //
00043     // Read the netid file.
00044     //
00045     space_netid::pointer netid = space_netid::create(etc_netid);
00046     for (;;)
00047     {
00048         space_netid::record::pointer rp = netid->get();
00049         if (!rp)
00050             break;
00051         result[rp->name()] = rp;
00052     }
00053 
00054     //
00055     // Read the passwd file.
00056     //
00057     colon_passwd::pointer passwd = colon_passwd::create(etc_passwd);
00058     for (;;)
00059     {
00060         colon_passwd::record::pointer rp = passwd->get();
00061         if (!rp)
00062             break;
00063 
00064         space_netid::record::gids_t gids;
00065         gids.push_back(rp->get_gid());
00066         space_netid::record::pointer tmp =
00067             space_netid::record::create
00068             (
00069                 rp->get_source_location(),
00070                 rp->get_uid(),
00071                 domain,
00072                 rp->get_uid(),
00073                 gids
00074             );
00075         result_t::iterator rit = result.find(tmp->name());
00076         if (rit == result.end())
00077         {
00078             result.insert(result_t::value_type(tmp->name(), tmp));
00079             user.insert(user_t::value_type(rp->get_name(), tmp));
00080         }
00081         else
00082         {
00083             user.insert(user_t::value_type(rp->get_name(), rit->second));
00084         }
00085     }
00086 
00087     //
00088     // Read the group file.
00089     //
00090     colon_group::pointer group = colon_group::create(etc_group);
00091     for (;;)
00092     {
00093         colon_group::record::pointer rp = group->get();
00094         if (!rp)
00095             break;
00096         const colon_group::record::members_t &members = rp->get_members();
00097         for
00098         (
00099             colon_group::record::members_t::const_iterator it = members.begin();
00100             it != members.end();
00101             ++it
00102         )
00103         {
00104             user_t::iterator uit = user.find(*it);
00105             if (uit != user.end())
00106                 uit->second->append(rp->get_gid());
00107         }
00108     }
00109 
00110     //
00111     // Read the hosts file.
00112     //
00113     space_hosts::pointer hosts = space_hosts::create(etc_hosts);
00114     for (;;)
00115     {
00116         space_hosts::record::pointer rp = hosts->get();
00117         if (!rp)
00118             break;
00119 
00120         const space_hosts::record::names_t &names = rp->get_names();
00121         for
00122         (
00123             space_hosts::record::names_t::const_iterator nit = names.begin();
00124             nit != names.end();
00125             ++nit
00126         )
00127         {
00128             // glue the domain onto all of the host records
00129             space_netid::record::gids_t gids;
00130             gids.push_back(*nit);
00131             space_netid::record::pointer tmp =
00132                 space_netid::record::create
00133                 (
00134                     rp->get_source_location(),
00135                     *nit,
00136                     domain,
00137                     "0",
00138                     gids
00139                 );
00140             rcstring name = tmp->name();
00141             result_t::iterator rit = result.find(name);
00142             if (rit == result.end())
00143             {
00144                 result.insert(result_t::value_type(name, tmp));
00145             }
00146             else
00147             {
00148                 // This means there was a duplicate host name
00149                 // in the hosts file.  Oops.
00150                 rit->second->append(*nit);
00151             }
00152         }
00153     }
00154 
00155     //
00156     // Open the output file.
00157     //
00158     output::pointer ofp = output_file::create(ofn);
00159 
00160     //
00161     // Write out all the results.
00162     // (The sort is for predictable test results.)
00163     // (The std::map implementation gives us a sorted iteration order.)
00164     //
00165     for
00166     (
00167         result_t::const_iterator it = result.begin();
00168         it != result.end();
00169         ++it
00170     )
00171     {
00172         // FIXME: check for record length?
00173         ofp->put(it->second->representation());
00174         ofp->put('\n');
00175     }
00176 }
00177 
00178 
00179 // vim: set ts=8 sw=4 et :