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