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