3bd80ff0e19320f9bc45d68bf073b32fc2a8f748
[shibboleth/cpp-sp.git] / shibsp / remoting / impl / UnixListener.cpp
1 /*
2  *  Copyright 2001-2010 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 #include <xmltooling/util/XMLHelper.h>
31
32 #ifdef HAVE_UNISTD_H
33 # include <sys/socket.h>
34 # include <sys/un.h>
35 # include <unistd.h>
36 # include <arpa/inet.h>
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/stat.h>           /* for chmod() */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <errno.h>
44
45 using namespace shibsp;
46 using namespace xmltooling;
47 using namespace xercesc;
48 using namespace std;
49
50
51 namespace shibsp {
52     class UnixListener : virtual public SocketListener
53     {
54     public:
55         UnixListener(const DOMElement* e);
56         ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
57
58         bool create(ShibSocket& s) const;
59         bool bind(ShibSocket& s, bool force=false) const;
60         bool connect(ShibSocket& s) const;
61         bool close(ShibSocket& s) const;
62         bool accept(ShibSocket& listener, ShibSocket& s) const;
63
64         int send(ShibSocket& s, const char* buf, int len) const {
65             return ::send(s, buf, len, 0);
66         }
67         
68         int recv(ShibSocket& s, char* buf, int buflen) const {
69             return ::recv(s, buf, buflen, 0);
70         }
71         
72     private:
73         string m_address;
74         mutable bool m_bound;
75     };
76
77     ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
78     {
79         return new UnixListener(e);
80     }
81
82     static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
83 };
84
85 UnixListener::UnixListener(const DOMElement* e)
86     : SocketListener(e), m_address(XMLHelper::getAttrString(e, getenv("SHIBSP_LISTENER_ADDRESS"), address)), m_bound(false)
87 {
88     if (m_address.empty())
89         m_address = "shibd.sock";
90     XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
91 }
92
93 #ifndef UNIX_PATH_MAX
94 #define UNIX_PATH_MAX 100
95 #endif
96
97 bool UnixListener::create(ShibSocket& sock) const
98 {
99     sock = socket(PF_UNIX, SOCK_STREAM, 0);
100     if (sock < 0)
101         return log_error();
102     return true;
103 }
104
105 bool UnixListener::bind(ShibSocket& s, bool force) const
106 {
107     struct sockaddr_un addr;
108     memset(&addr, 0, sizeof (addr));
109     addr.sun_family = AF_UNIX;
110     strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
111
112     if (force)
113         unlink(m_address.c_str());
114
115     if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
116         log_error();
117         close(s);
118         return false;
119     }
120
121     // Make sure that only the creator can read -- we don't want just
122     // anyone connecting, do we?
123     if (chmod(m_address.c_str(),0777) < 0) {
124         log_error();
125         close(s);
126         unlink(m_address.c_str());
127         return false;
128     }
129
130     listen(s, 3);
131     return m_bound=true;
132 }
133
134 bool UnixListener::connect(ShibSocket& s) const
135 {
136     struct sockaddr_un addr;
137     memset(&addr, 0, sizeof (addr));
138     addr.sun_family = AF_UNIX;
139     strncpy(addr.sun_path, m_address.c_str(), UNIX_PATH_MAX);
140
141     if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
142         return log_error();
143     return true;
144 }
145
146 bool UnixListener::close(ShibSocket& s) const
147 {
148     ::close(s);
149     return true;
150 }
151
152 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
153 {
154     s=::accept(listener,nullptr,nullptr);
155     if (s < 0)
156         return log_error();
157     return true;
158 }