/[resiprocate]/branches/b-directory-reorg/resip/dum/test/certServer.cxx
ViewVC logotype

Annotation of /branches/b-directory-reorg/resip/dum/test/certServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3593 - (hide annotations) (download)
Tue Nov 23 06:13:16 2004 UTC (15 years ago) by jason
Original Path: main/sip/resiprocate/dum/test/certServer.cxx
File size: 12034 byte(s)
fix certServer (stub out some calls)

1 jason 3530 #ifdef WIN32
2     # define usleep(t) Sleep(t)
3     #endif
4    
5 jason 3593 #define HAVE_POPT_H
6    
7 jason 3530 #if defined (HAVE_POPT_H)
8     #include <popt.h>
9     #endif
10    
11     #include <signal.h>
12    
13 jason 3546 #include "resiprocate/NameAddr.hxx"
14     #include "resiprocate/Pkcs8Contents.hxx"
15 jason 3530 #include "resiprocate/SipMessage.hxx"
16     #include "resiprocate/Symbols.hxx"
17     #include "resiprocate/Uri.hxx"
18     #include "resiprocate/X509Contents.hxx"
19     #include "resiprocate/dum/AppDialogSet.hxx"
20     #include "resiprocate/dum/ClientAuthManager.hxx"
21     #include "resiprocate/dum/DialogUsageManager.hxx"
22 jason 3546 #include "resiprocate/dum/DumShutdownHandler.hxx"
23 jason 3530 #include "resiprocate/dum/OutOfDialogHandler.hxx"
24     #include "resiprocate/dum/Profile.hxx"
25 jason 3546 #include "resiprocate/dum/PublicationHandler.hxx"
26 jason 3530 #include "resiprocate/dum/RegistrationHandler.hxx"
27 jason 3546 #include "resiprocate/dum/ServerPublication.hxx"
28     #include "resiprocate/dum/ServerSubscription.hxx"
29 jason 3530 #include "resiprocate/dum/SubscriptionHandler.hxx"
30     #include "resiprocate/os/Log.hxx"
31     #include "resiprocate/os/Logger.hxx"
32     #include "resiprocate/os/Random.hxx"
33     #include "resiprocate/os/Subsystem.hxx"
34    
35     #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
36    
37     using namespace std;
38     using namespace resip;
39    
40     static bool finished=false;
41    
42     void
43     signalHandler(int signo)
44     {
45     std::cerr << "Shutting down" << endl;
46     finished = true;
47     }
48    
49 jason 3546 // When a publish comes in, we should let any outstanding subscriptions know
50     // about it.
51    
52     class CertSubscriptionHandler;
53     class PrivateKeySubscriptionHandler;
54    
55     class CertPublicationHandler : public ServerPublicationHandler
56 jason 3530 {
57     public:
58 jason 3546 CertPublicationHandler(Security& security) : mSecurity(security)
59 jason 3530 {
60     }
61    
62 jason 3546 virtual void onInitial(ServerPublicationHandle h,
63     const Data& etag,
64     const SipMessage& pub,
65     const Contents* contents,
66     const SecurityAttributes* attrs,
67     int expires)
68 jason 3530 {
69     add(h, contents);
70     }
71    
72     virtual void onExpired(ServerPublicationHandle h, const Data& etag)
73     {
74 jason 3546 mSecurity.removeUserCert(h->getPublisher());
75 jason 3530 }
76    
77 jason 3546 virtual void onRefresh(ServerPublicationHandle,
78     const Data& etag,
79     const SipMessage& pub,
80     const Contents* contents,
81     const SecurityAttributes* attrs,
82     int expires)
83 jason 3530 {
84     }
85    
86 jason 3546 virtual void onUpdate(ServerPublicationHandle h,
87     const Data& etag,
88     const SipMessage& pub,
89     const Contents* contents,
90     const SecurityAttributes* attrs,
91     int expires)
92 jason 3530 {
93     add(h, contents);
94     }
95    
96 jason 3546 virtual void onRemoved(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
97 jason 3530 {
98 jason 3546 mSecurity.removeUserCert(h->getPublisher());
99 jason 3530 }
100     private:
101 jason 3546 void add(ServerPublicationHandle h, const Contents* contents)
102 jason 3530 {
103 jason 3550 if (h->getDocumentKey() == h->getPublisher())
104     {
105     const X509Contents* x509 = dynamic_cast<const X509Contents*>(contents);
106     assert(x509);
107     mSecurity.addUserCertDER(h->getPublisher(), x509->getBodyData());
108     h->send(h->accept(200));
109     }
110     else
111     {
112     h->send(h->accept(403)); // !jf! is this the correct code?
113     }
114 jason 3530 }
115    
116     Security& mSecurity;
117     };
118    
119     class PrivateKeyPublicationHandler : public ServerPublicationHandler
120     {
121     public:
122     PrivateKeyPublicationHandler(Security& security) : mSecurity(security)
123     {
124     }
125    
126 jason 3546 virtual void onInitial(ServerPublicationHandle h,
127     const Data& etag,
128     const SipMessage& pub,
129     const Contents* contents,
130     const SecurityAttributes* attrs,
131     int expires)
132 jason 3530 {
133     add(h, contents);
134     }
135    
136     virtual void onExpired(ServerPublicationHandle h, const Data& etag)
137     {
138 jason 3546 mSecurity.removeUserPrivateKey(h->getPublisher());
139 jason 3530 }
140    
141 jason 3546 virtual void onRefresh(ServerPublicationHandle,
142     const Data& etag,
143     const SipMessage& pub,
144     const Contents* contents,
145     const SecurityAttributes* attrs,
146     int expires)
147 jason 3530 {
148     }
149    
150 jason 3546 virtual void onUpdate(ServerPublicationHandle h,
151     const Data& etag,
152     const SipMessage& pub,
153     const Contents* contents,
154     const SecurityAttributes* attrs,
155     int expires)
156 jason 3530 {
157     add(h, contents);
158     }
159    
160 jason 3546 virtual void onRemoved(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
161 jason 3530 {
162 jason 3546 mSecurity.removeUserPrivateKey(h->getPublisher());
163 jason 3530 }
164    
165     private:
166 jason 3546 void add(ServerPublicationHandle h, const Contents* contents)
167 jason 3530 {
168 jason 3550 if (h->getDocumentKey() == h->getPublisher())
169     {
170     const Pkcs8Contents* pkcs8 = dynamic_cast<const Pkcs8Contents*>(contents);
171     assert(pkcs8);
172     mSecurity.addUserPrivateKeyDER(h->getPublisher(), pkcs8->getBodyData());
173     }
174     else
175     {
176     h->send(h->accept(403)); // !jf! is this the correct code?
177     }
178 jason 3530 }
179    
180     Security& mSecurity;
181     };
182    
183     class CertSubscriptionHandler : public ServerSubscriptionHandler
184     {
185     public:
186     CertSubscriptionHandler(Security& security) : mSecurity(security)
187     {
188     }
189    
190     virtual void onNewSubscription(ServerSubscriptionHandle h, const SipMessage& sub)
191     {
192 jason 3550 if (!mSecurity.hasUserCert(h->getDocumentKey()))
193     {
194     // !jf! really need to do this async. send neutral state in the meantime,
195     // blah blah blah
196     mSecurity.generateUserCert(h->getDocumentKey(), Data::Empty);
197     }
198    
199 jason 3546 if (mSecurity.hasUserCert(h->getDocumentKey()))
200 jason 3530 {
201 jason 3546 X509Contents x509(mSecurity.getUserCertDER(h->getDocumentKey()));
202 jason 3530 h->send(h->update(&x509));
203     }
204     else
205     {
206     h->reject(404);
207     }
208     }
209    
210 jason 3546 virtual void onPublished(ServerSubscriptionHandle associated,
211     ServerPublicationHandle publication,
212     const Contents* contents,
213     const SecurityAttributes* attrs)
214     {
215     associated->send(associated->update(contents));
216     }
217    
218    
219 jason 3530 virtual void onTerminated(ServerSubscriptionHandle)
220     {
221     }
222    
223     virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
224     {
225     }
226    
227     private:
228     Security& mSecurity;
229     };
230    
231     class PrivateKeySubscriptionHandler : public ServerSubscriptionHandler
232     {
233     public:
234     PrivateKeySubscriptionHandler(Security& security) : mSecurity(security)
235     {
236     }
237    
238 jason 3546 virtual void onNewSubscription(ServerSubscriptionHandle h, const SipMessage& sub)
239 jason 3530 {
240 jason 3551 if (h->getDocumentKey() != h->getSubscriber())
241 jason 3530 {
242 jason 3550 h->send(h->accept(403)); // !jf! is this the correct code?
243     }
244     else if (mSecurity.hasUserCert(h->getDocumentKey()))
245     {
246 jason 3546 Pkcs8Contents pkcs(mSecurity.getUserPrivateKeyDER(h->getDocumentKey()));
247 jason 3530 h->send(h->update(&pkcs));
248     }
249     else
250     {
251     h->reject(404);
252     }
253     }
254    
255 jason 3546 virtual void onPublished(ServerSubscriptionHandle associated,
256     ServerPublicationHandle publication,
257     const Contents* contents,
258     const SecurityAttributes* attrs)
259     {
260     associated->send(associated->update(contents));
261     }
262    
263 jason 3530 virtual void onTerminated(ServerSubscriptionHandle)
264     {
265     }
266    
267     virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
268     {
269     }
270 jason 3550
271 jason 3530 private:
272     Security& mSecurity;
273     };
274    
275    
276    
277     class CertServer : public OutOfDialogHandler, public DialogUsageManager
278     {
279     public:
280     CertServer(const resip::NameAddr& me) :
281     DialogUsageManager(),
282 jason 3546 mCertServer(getSecurity()),
283     mPrivateKeyServer(getSecurity()),
284 jason 3530 mCertUpdater(getSecurity()),
285 jason 3546 mPrivateKeyUpdater(getSecurity())
286 jason 3530 {
287     addTransport(UDP, 5100);
288     addTransport(TCP, 5100);
289 jason 3551 addTransport(TLS, 5101, V4, Data::Empty, me.uri().host(), Data::Empty);
290 jason 3530
291     mProfile.clearSupportedMethods();
292     mProfile.addSupportedMethod(PUBLISH);
293     mProfile.addSupportedMethod(SUBSCRIBE);
294     mProfile.validateAcceptEnabled() = true;
295     mProfile.validateContentEnabled() = true;
296     mProfile.addSupportedMimeType(Pkcs8Contents::getStaticType());
297     mProfile.addSupportedMimeType(X509Contents::getStaticType());
298    
299     mProfile.setDefaultFrom(me);
300     setProfile(&mProfile);
301    
302     addServerSubscriptionHandler(Symbols::Credential, &mPrivateKeyServer);
303     addServerSubscriptionHandler(Symbols::Certificate, &mCertServer);
304     addServerPublicationHandler(Symbols::Credential, &mPrivateKeyUpdater);
305     addServerPublicationHandler(Symbols::Certificate, &mCertUpdater);
306     addOutOfDialogHandler(OPTIONS, this);
307    
308     //setServerAuthManager(std::auto_ptr<ServerAuthManager>(new ServerAuthManager(mProfile)));
309    
310     DialogUsageManager::run();
311     }
312    
313     ~CertServer()
314     {
315     }
316    
317     void run()
318     {
319     while ( !mDone )
320     {
321     while (process());
322     usleep(5);
323    
324     if (finished)
325     {
326     // graceful shutdown
327     exit(0);
328     }
329     }
330     }
331    
332     virtual void onSuccess(ClientOutOfDialogReqHandle, const SipMessage& successResponse)
333     {
334     }
335    
336     virtual void onFailure(ClientOutOfDialogReqHandle, const SipMessage& errorResponse)
337     {
338     }
339    
340     virtual void onReceivedRequest(ServerOutOfDialogReqHandle, const SipMessage& request)
341     {
342     }
343    
344     private:
345     Profile mProfile;
346 jason 3546 CertSubscriptionHandler mCertServer;
347     PrivateKeySubscriptionHandler mPrivateKeyServer;
348 jason 3530 CertPublicationHandler mCertUpdater;
349     PrivateKeyPublicationHandler mPrivateKeyUpdater;
350     bool mDone;
351     };
352    
353     int
354     main (int argc, char** argv)
355     {
356     char* logType = "COUT";
357     char* logLevel = "DEBUG";
358     char* myUrl = "sip:localhost:7001";
359     char* bindAddr = 0;
360     int v6 = 0;
361    
362     #if defined(HAVE_POPT_H)
363     struct poptOption table[] = {
364     {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"},
365     {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
366     {"name" , 'n', POPT_ARG_STRING, &myUrl, 0, "my url", 0},
367     {"bind", 'b', POPT_ARG_STRING, &bindAddr, 0, "interface address to bind to",0},
368     {"v6", '6', POPT_ARG_NONE, &v6 , 0, "ipv6", 0},
369     POPT_AUTOHELP
370     { NULL, 0, 0, NULL, 0 }
371     };
372     poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
373     poptGetNextOpt(context);
374     #endif
375     Log::initialize(logType, logLevel, argv[0]);
376    
377     #ifndef _WIN32
378     if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
379     {
380     cerr << "Couldn't install signal handler for SIGPIPE" << endl;
381     exit(-1);
382     }
383    
384     if ( signal( SIGINT, signalHandler ) == SIG_ERR )
385     {
386     cerr << "Couldn't install signal handler for SIGINT" << endl;
387     exit( -1 );
388     }
389    
390     if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
391     {
392     cerr << "Couldn't install signal handler for SIGTERM" << endl;
393     exit( -1 );
394     }
395     #endif
396    
397     NameAddr domain(myUrl);
398     CertServer server(domain);
399     server.run();
400     return 0;
401     }
402    

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27