00001
00002
00003
00004
00005
00006
00007 #include "uniconfdaemon.h"
00008 #include "uniconfdaemonconn.h"
00009
00010 #ifndef _WIN32
00011 #include "uniconfpamconn.h"
00012 #include "wvunixsocket.h"
00013 #endif
00014
00015 #include "wvtcp.h"
00016 #include "wvsslstream.h"
00017 #include "uninullgen.h"
00018
00019
00020 UniConfDaemon::UniConfDaemon(const UniConf &_cfg,
00021 bool auth, IUniConfGen *_permgen)
00022 : cfg(_cfg), log("UniConf Daemon"), debug(log.split(WvLog::Debug1))
00023 {
00024 authenticate = auth;
00025
00026 #ifdef _WIN32
00027 assert(!authenticate);
00028 #endif
00029
00030 permgen = _permgen ? _permgen : new UniNullGen();
00031 debug("Starting.\n");
00032 }
00033
00034
00035 UniConfDaemon::~UniConfDaemon()
00036 {
00037 close();
00038 WVRELEASE(permgen);
00039 }
00040
00041
00042 void UniConfDaemon::close()
00043 {
00044 if (!closed)
00045 {
00046 debug("Saving changes.\n");
00047 cfg.commit();
00048 debug("Done saving changes.\n");
00049 }
00050
00051 WvIStreamList::close();
00052 }
00053
00054
00055 void UniConfDaemon::accept(WvStream *stream)
00056 {
00057 debug("Accepting connection from %s.\n", *stream->src());
00058
00059
00060
00061
00062 #ifndef _WIN32
00063 if (authenticate)
00064 append(new UniConfPamConn(stream, cfg,
00065 new UniPermGen(permgen)), true, "ucpamconn");
00066 else
00067 #endif
00068 append(new UniConfDaemonConn(stream, cfg), true, "ucdaemonconn");
00069 }
00070
00071
00072 #ifndef _WIN32
00073 void UniConfDaemon::unixcallback(WvStream &l, void *)
00074 {
00075 debug("Incoming Unix domain connection.\n");
00076 WvUnixListener *listener = static_cast<WvUnixListener*>(& l);
00077 WvStream *s = listener->accept();
00078 accept(s);
00079 }
00080 #endif
00081
00082
00083 void UniConfDaemon::tcpcallback(WvStream &l, void *)
00084 {
00085 WvTCPListener *listener = static_cast<WvTCPListener*>(& l);
00086 WvStream *s = listener->accept();
00087 debug("Incoming TCP connection from %s.\n", *s->src());
00088 accept(s);
00089 }
00090
00091
00092 void UniConfDaemon::sslcallback(WvStream &l, void *userdata)
00093 {
00094 WvX509Mgr *x509 = static_cast<WvX509Mgr *>(userdata);
00095 WvTCPListener *listener = static_cast<WvTCPListener *>(&l);
00096 WvStream *s = listener->accept();
00097 debug("Incoming TCP/SSL connection from %s.\n", *s->src());
00098 accept(new WvSSLStream(s, x509, 0, true));
00099 }
00100
00101
00102 #ifndef _WIN32
00103 bool UniConfDaemon::setupunixsocket(WvStringParm path, int create_mode)
00104 {
00105 WvUnixListener *listener = new WvUnixListener(path, create_mode);
00106 if (! listener->isok())
00107 {
00108 log(WvLog::Error, "Could not create Unix domain socket: %s\n",
00109 listener->errstr());
00110 WVRELEASE(listener);
00111 return false;
00112 }
00113 listener->setcallback(WvStreamCallback(this,
00114 &UniConfDaemon::unixcallback), NULL);
00115 append(listener, true, "unix listen");
00116 debug("Listening on Unix socket '%s'\n", path);
00117 return true;
00118 }
00119 #endif
00120
00121
00122 bool UniConfDaemon::setuptcpsocket(const WvIPPortAddr &addr)
00123 {
00124 WvTCPListener *listener = new WvTCPListener(addr);
00125 if (! listener->isok())
00126 {
00127 log(WvLog::Error, "Could not create TCP socket: %s\n",
00128 listener->errstr());
00129 WVRELEASE(listener);
00130 return false;
00131 }
00132 listener->setcallback(WvStreamCallback(this,
00133 &UniConfDaemon::tcpcallback), NULL);
00134 append(listener, true, "tcp listen");
00135 debug("Listening for TCP at %s.\n", addr);
00136 return true;
00137 }
00138
00139
00140 bool UniConfDaemon::setupsslsocket(const WvIPPortAddr &addr, WvX509Mgr *x509)
00141 {
00142 WvTCPListener *listener = new WvTCPListener(addr);
00143 if (! listener->isok())
00144 {
00145 log(WvLog::Error, "Could not create SSL socket: %s\n",
00146 listener->errstr());
00147 WVRELEASE(listener);
00148 return false;
00149 }
00150 listener->setcallback(WvStreamCallback(this,
00151 &UniConfDaemon::sslcallback), x509);
00152 append(listener, true, "ssl listen");
00153 debug("Listening for TCP/SSL at %s.\n", addr);
00154 return true;
00155 }