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