WvStreams
uniconfdaemon.cc
1 /*
2  * Worldvisions Weaver Software
3  * Copyright (C) 1997 - 2004 Net Integration Technologies Inc.
4  *
5  * Daemon program for the uniconf configuration system.
6  */
7 #include "uniconfdaemon.h"
8 #include "uniconfdaemonconn.h"
9 #include "wvlistener.h"
10 #include "uninullgen.h"
11 
12 #ifndef _WIN32
13 #include "uniconfpamconn.h"
14 #endif
15 
16 
18  bool auth, IUniConfGen *_permgen)
19  : cfg(_cfg), log("UniConf Daemon"), debug(log.split(WvLog::Debug1))
20 {
21  authenticate = auth;
22 
23 #ifdef _WIN32
24  assert(!authenticate);
25 #endif
26 
27  permgen = _permgen ? _permgen : new UniNullGen();
28  debug("Starting.\n");
29 }
30 
31 
32 UniConfDaemon::~UniConfDaemon()
33 {
34  close();
35  WVRELEASE(permgen);
36 }
37 
38 
40 {
41  if (!closed)
42  {
43  debug("Saving changes.\n");
44  cfg.commit();
45  debug("Done saving changes.\n");
46  }
47 
49 }
50 
51 
52 void UniConfDaemon::accept(WvStream *stream)
53 {
54  // FIXME: permgen should be used regardless of whether we authenticate,
55  // and there should be a command to authenticate explicitly. That way we
56  // can support access control for anonymous connections.
57 #ifndef _WIN32
58  if (authenticate)
59  append(new UniConfPamConn(stream, cfg,
60  new UniPermGen(permgen)), true, "ucpamconn");
61  else
62 #endif
63  append(new UniConfDaemonConn(stream, cfg), true, "ucdaemonconn");
64 }
65 
66 
67 void UniConfDaemon::listencallback(IWvStream *s)
68 {
69  const WvAddr *a = s->src();
70  if (a)
71  debug("Incoming connection from %s.\n", *a);
72  else
73  debug("Incoming connection from UNKNOWN.\n");
74  if (s->geterr())
75  {
76  debug("Error: %s\n", s->errstr());
77  WVRELEASE(s);
78  }
79  else
80  accept(new WvStreamClone(s));
81 }
82 
83 
85 {
86  IWvListener *l = IWvListener::create(lmoniker);
87  debug("Listening on %s.\n", *l->src());
88  if (!l->isok())
89  {
90  log(WvLog::Error, "Can't listen: %s\n", l->errstr());
91  seterr_both(l->geterr(), l->errstr());
92  WVRELEASE(l);
93  }
94  else
95  {
96  l->onaccept(wv::bind(&UniConfDaemon::listencallback, this, _1));
97  append(l, true, "listener");
98  }
99 }