00001
00002
00003
00004
00005
00006
00007
00008 #include "ocString.h"
00009 #include "ocAdmin.h"
00010 #include <sys/types.h>
00011 #include <unistd.h>
00012 #include "ocSockServer.h"
00013 #include "ocFileSys.h"
00014 #include <string>
00015 #include <fstream>
00016 #include <iomanip>
00017
00018
00019 #include "openLogger.h"
00020
00021 class uAdmin: public ocSockServer
00022 {
00023 public:
00024 uAdmin( int port, int connections ):
00025 ocSockServer(port, connections ){ ; }
00026 private:
00027
00028 bool readFile ( ocString filename, string & filter )
00029 {
00030 char linebuffer[1024];
00031
00032 bool bret = true;
00033 writelog2("filename",filename);
00034
00035 if ( filename.regExMatch("[*]") )
00036 {
00037 writelog2( "found wildcard: ", filename );
00038 filename = filename.remove("*");
00039 string::size_type idx = filename.find_last_of('/');
00040 if( idx != string::npos )
00041 {
00042 string path = filename.substr(0,idx);
00043 writelog2( "path: ", path );
00044 ocString pattern = filename.substr(idx);
00045 writelog2( "pattern: ", pattern );
00046 ocFileSys fs;
00047 if( fs.openDir( path ) )
00048 {
00049 ocDirectory & dir = fs.getDirectoryEntries();
00050 for( int i=0; i<dir.size(); ++i)
00051 {
00052 ocString test = "/";
00053 test += dir[i].name;
00054 if( test.regExMatch( pattern.c_str() ))
00055 {
00056
00057 string fullpath = path;
00058 fullpath += test;
00059 writelog2( "read: ", fullpath );
00060 readFile ( fullpath, filter );
00061 }
00062 }
00063 }
00064 }
00065 }
00066 else
00067 {
00068 ifstream ifile;
00069 ifile.open(filename.c_str());
00070
00071 while( ifile )
00072 {
00073 ifile.getline(linebuffer,sizeof(linebuffer));
00074 ocString line = linebuffer;
00075 line += "\n";
00076 if( filter.length() )
00077 {
00078 if( line.regExMatch(filter.c_str() ) )
00079 {
00080 Write(line.c_str(),line.length() );
00081 writelog2("wrote",line);
00082 }
00083 }
00084 else
00085 {
00086 Write(line.c_str(),line.length() );
00087 writelog2("wrote",line);
00088 }
00089 }
00090 writelog("end of file found");
00091 ifile.close();
00092 }
00093 return bret;
00094 }
00095
00096 virtual void daemonAction( void )
00097 {
00098 ocAdmin admin( "/usr/bin/", "/usr/sbin/");
00099 ocString cmd = ReadLine();
00100 string response = "bad";
00101 string type = cmd.parse( "," );
00102 string user, passwd, dir, grp;
00103
00104
00105 if( type == "au" )
00106 {
00107 user = cmd.parse( "," );
00108 passwd = cmd.parse( "," );
00109 dir = cmd.parse( "," );
00110 grp = cmd.parse( "\n" );
00111 if( admin.addUser( user, passwd, dir, grp ) ) response = "good";
00112 }
00113 else if( type == "cp" )
00114 {
00115 user = cmd.parse( "," );
00116 passwd = cmd.parse( "\n" );
00117
00118 if( admin.changePassword( user, passwd ) ) response = "good";
00119 }
00120 else if( type == "du" )
00121 {
00122 user = cmd.parse( "\n" );
00123 if( admin.deleteUser( user ) ) response = "good";
00124 }
00125 else if( type == "ag" )
00126 {
00127 grp = cmd.parse( "\n" );
00128 if( admin.addGroup( grp ) ) response = "good";
00129 }
00130 else if( type == "dg" )
00131 {
00132 grp = cmd.parse( "\n" );
00133 if( admin.deleteGroup( grp ) ) response = "good";
00134 }
00135 else if( type == "re" )
00136 {
00137
00138 string file = cmd.parse( "," );
00139
00140 string filter = cmd.parse( "\n" );
00141
00142 readFile( file, filter );
00143 response = "";
00144 }
00145 response += "\n";
00146 Write(response.c_str(), response.length());
00147 }
00148 };
00149
00150 int main( int argc, char ** argv )
00151 {
00152 uAdmin u( 50001, 20 );
00153 u.daemonServe();
00154 return 0;
00155 }
00156