Bug fix, linefeed fix, move keygen to install hook and add script to dist.
[shibboleth/sp.git] / shibsp / remoting / impl / UnixListener.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * UnixListener.cpp
19  * 
20  * Unix Domain-based SocketListener implementation
21  */
22
23 #include "internal.h"
24 #include "remoting/impl/SocketListener.h"
25
26 #include <xercesc/util/XMLUniDefs.hpp>
27 #include <xmltooling/XMLToolingConfig.h>
28 #include <xmltooling/unicode.h>
29 #include <xmltooling/util/PathResolver.h>
30
31 #ifdef HAVE_UNISTD_H
32 # include <sys/socket.h>
33 # include <sys/un.h>
34 # include <unistd.h>
35 # include <arpa/inet.h>
36 #endif
37
38 #include <sys/types.h>
39 #include <sys/stat.h>           /* for chmod() */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <errno.h>
43
44 using namespace shibsp;
45 using namespace xmltooling;
46 using namespace xercesc;
47 using namespace std;
48
49
50 namespace shibsp {
51     static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
52
53     class UnixListener : virtual public SocketListener
54     {
55     public:
56         UnixListener(const DOMElement* e);
57         ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
58
59         bool create(ShibSocket& s) const;
60         bool bind(ShibSocket& s, bool force=false) const;
61         bool connect(ShibSocket& s) const;
62         bool close(ShibSocket& s) const;
63         bool accept(ShibSocket& listener, ShibSocket& s) const;
64
65         int send(ShibSocket& s, const char* buf, int len) const {
66             return ::send(s, buf, len, 0);
67         }
68         
69         int recv(ShibSocket& s, char* buf, int buflen) const {
70             return ::recv(s, buf, buflen, 0);
71         }
72         
73     private:
74         string m_address;
75         mutable bool m_bound;
76     };
77
78     ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
79     {
80         return new UnixListener(e);
81     }
82 };
83
84 UnixListener::UnixListener(const DOMElement* e) : SocketListener(e), m_address("/var/run/shar-socket"), m_bound(false)
85 {
86     const XMLCh* tag=e->getAttributeNS(NULL,address);
87     if (tag && *tag) {
88         auto_ptr_char a(tag);
89         m_address=a.get();
90         XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
91     }
92 }
93
94 #ifndef UNIX_PATH_MAX
95 #define UNIX_PATH_MAX 100
96 #endif
97
98 bool UnixListener::create(ShibSocket& sock) const
99 {
100     sock = socket(PF_UNIX, SOCK_STREAM, 0);
101     if (sock < 0)
102         return log_error();
103     return true;
104 }
105
106 bool UnixListener::bind(ShibSocket& s, bool force) const
107 {
108     struct sockaddr_un addr;
109     memset(&addr, 0, sizeof (addr));
110     addr.sun_family = AF_UNIX;
111     strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
112
113     if (force)
114         unlink(m_address.c_str());
115
116     if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
117         log_error();
118         close(s);
119         return false;
120     }
121
122     // Make sure that only the creator can read -- we don't want just
123     // anyone connecting, do we?
124     if (chmod(m_address.c_str(),0777) < 0) {
125         log_error();
126         close(s);
127         unlink(m_address.c_str());
128         return false;
129     }
130
131     listen(s, 3);
132     return m_bound=true;
133 }
134
135 bool UnixListener::connect(ShibSocket& s) const
136 {
137     struct sockaddr_un addr;
138     memset(&addr, 0, sizeof (addr));
139     addr.sun_family = AF_UNIX;
140     strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
141
142     if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
143         return log_error();
144     return true;
145 }
146
147 bool UnixListener::close(ShibSocket& s) const
148 {
149     ::close(s);
150     return true;
151 }
152
153 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
154 {
155     s=::accept(listener,NULL,NULL);
156     if (s < 0)
157         return log_error();
158     return true;
159 }