nis-util
1.0.D108
|
00001 // 00002 // nis-util - NIS Administration Utilities 00003 // Copyright (C) 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 along 00016 // with this program. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 00019 #include <lib/ac/stdio.h> 00020 #include <libexplain/fflush.h> 00021 #include <libexplain/output.h> 00022 #include <list> 00023 00024 #include <lib/colon/group/slurp.h> 00025 #include <lib/colon/passwd/functor/group_closure.h> 00026 #include <lib/colon/passwd/slurp.h> 00027 #include <lib/output/file.h> 00028 00029 #include <nis-util-group-members/arglex/group-members.h> 00030 00031 00032 int 00033 main(int argc, char **argv) 00034 { 00035 arglex_group_members cmdline(argc, argv); 00036 cmdline.token_first(); 00037 typedef std::list<rcstring> group_names_t; 00038 group_names_t group_names; 00039 rcstring passwd_file_name; 00040 rcstring group_file_name; 00041 rcstring output_filename; 00042 while (cmdline.token_cur() != arglex::token_eoln) 00043 { 00044 switch (cmdline.token_cur()) 00045 { 00046 default: 00047 cmdline.generic_argument(); 00048 break; 00049 00050 case arglex_group_members::token_passwd: 00051 cmdline.get_filename_or_stdin(passwd_file_name); 00052 break; 00053 00054 case arglex_group_members::token_group: 00055 cmdline.get_filename_or_stdin(group_file_name); 00056 break; 00057 00058 case arglex::token_string: 00059 group_names.push_back(cmdline.get_string_value()); 00060 break; 00061 00062 case arglex_group_members::token_output: 00063 cmdline.get_filename_or_stdout(output_filename); 00064 break; 00065 } 00066 cmdline.token_next(); 00067 } 00068 00069 // 00070 // Read the password file. 00071 // 00072 if (passwd_file_name.empty()) 00073 explain_output_error_and_die("no passwd file specified"); 00074 colon_passwd_slurp::pointer psw = 00075 colon_passwd_slurp::create(passwd_file_name); 00076 psw->read_and_process(); 00077 00078 // 00079 // Read the group file. 00080 // 00081 if (group_file_name.empty()) 00082 explain_output_error_and_die("no group file specified"); 00083 colon_group_slurp::pointer grp = colon_group_slurp::create(group_file_name); 00084 grp->read_and_process(); 00085 00086 // For each user, add that user to the group correponding to the 00087 // user's default gid. 00088 { 00089 colon_passwd_functor_group_closure w(grp); 00090 psw->walk(w); 00091 } 00092 00093 output::pointer op = output_file::create(output_filename); 00094 00095 // 00096 // for each group named, emit its members 00097 // 00098 if (group_names.empty()) 00099 { 00100 explain_output_error("no group names specified"); 00101 cmdline.usage(); 00102 } 00103 int number_of_errors = 0; 00104 for 00105 ( 00106 group_names_t::const_iterator git = group_names.begin(); 00107 git != group_names.end(); 00108 ++git 00109 ) 00110 { 00111 rcstring name = *git; 00112 colon_group::record::pointer gp = grp->query_by_name(name); 00113 if (!gp) 00114 { 00115 explain_output_error 00116 ( 00117 "%s: group %s unknown", 00118 group_file_name.c_str(), 00119 name.quote_c().c_str() 00120 ); 00121 ++number_of_errors; 00122 } 00123 00124 // 00125 // emit the names 00126 // 00127 op->put(name); 00128 op->put(':'); 00129 const colon_group::record::members_t &members = gp->get_members(); 00130 for 00131 ( 00132 colon_group::record::members_t::const_iterator it = 00133 members.begin(); 00134 it != members.end(); 00135 ++it 00136 ) 00137 { 00138 if (it != members.begin()) 00139 op->put(','); 00140 op->put(' '); 00141 op->put(*it); 00142 } 00143 op->put('\n'); 00144 } 00145 op.reset(); 00146 00147 if (number_of_errors > 0) 00148 { 00149 explain_output_error_and_die 00150 ( 00151 "found %d fatal error%s", 00152 number_of_errors, 00153 (number_of_errors == 1 ? "" : "s") 00154 ); 00155 } 00156 return 0; 00157 } 00158 00159 00160 // vim: set ts=8 sw=4 et :