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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * Unix Domain-based SocketListener implementation.
28 #include "remoting/impl/SocketListener.h"
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>
37 # include <sys/socket.h>
40 # include <arpa/inet.h>
43 #include <sys/types.h>
44 #include <sys/stat.h> /* for chmod() */
50 using namespace shibsp;
51 using namespace xmltooling;
52 using namespace xercesc;
57 class UnixListener : virtual public SocketListener
60 UnixListener(const DOMElement* e);
61 ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
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;
69 int send(ShibSocket& s, const char* buf, int len) const {
70 return ::send(s, buf, len, 0);
73 int recv(ShibSocket& s, char* buf, int buflen) const {
74 return ::recv(s, buf, buflen, 0);
82 ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
84 return new UnixListener(e);
87 static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
90 UnixListener::UnixListener(const DOMElement* e)
91 : SocketListener(e), m_address(XMLHelper::getAttrString(e, getenv("SHIBSP_LISTENER_ADDRESS"), address)), m_bound(false)
93 if (m_address.empty())
94 m_address = "shibd.sock";
95 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
99 #define UNIX_PATH_MAX 100
102 bool UnixListener::create(ShibSocket& s) const
104 int type = SOCK_STREAM;
105 #ifdef HAVE_SOCK_CLOEXEC
106 type |= SOCK_CLOEXEC;
108 s = socket(PF_UNIX, type, 0);
110 return log_error("socket");
112 #if !defined(HAVE_SOCK_CLOEXEC) && defined(HAVE_FD_CLOEXEC)
113 int fdflags = fcntl(s, F_GETFD);
115 fdflags |= FD_CLOEXEC;
116 fcntl(s, F_SETFD, fdflags);
123 bool UnixListener::bind(ShibSocket& s, bool force) const
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);
131 unlink(m_address.c_str());
133 if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
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) {
144 unlink(m_address.c_str());
152 bool UnixListener::connect(ShibSocket& s) const
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);
159 if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
160 return log_error("connect");
164 bool UnixListener::close(ShibSocket& s) const
170 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
172 s=::accept(listener,nullptr,nullptr);
174 return log_error("accept");