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() */
49 using namespace shibsp;
50 using namespace xmltooling;
51 using namespace xercesc;
56 class UnixListener : virtual public SocketListener
59 UnixListener(const DOMElement* e);
60 ~UnixListener() {if (m_bound) unlink(m_address.c_str());}
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;
68 int send(ShibSocket& s, const char* buf, int len) const {
69 return ::send(s, buf, len, 0);
72 int recv(ShibSocket& s, char* buf, int buflen) const {
73 return ::recv(s, buf, buflen, 0);
81 ListenerService* SHIBSP_DLLLOCAL UnixListenerServiceFactory(const DOMElement* const & e)
83 return new UnixListener(e);
86 static const XMLCh address[] = UNICODE_LITERAL_7(a,d,d,r,e,s,s);
89 UnixListener::UnixListener(const DOMElement* e)
90 : SocketListener(e), m_address(XMLHelper::getAttrString(e, getenv("SHIBSP_LISTENER_ADDRESS"), address)), m_bound(false)
92 if (m_address.empty())
93 m_address = "shibd.sock";
94 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_address, PathResolver::XMLTOOLING_RUN_FILE);
98 #define UNIX_PATH_MAX 100
101 bool UnixListener::create(ShibSocket& sock) const
103 sock = socket(PF_UNIX, SOCK_STREAM, 0);
105 return log_error("socket");
109 bool UnixListener::bind(ShibSocket& s, bool force) const
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);
117 unlink(m_address.c_str());
119 if (::bind(s, (struct sockaddr *)&addr, sizeof (addr)) < 0) {
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) {
130 unlink(m_address.c_str());
138 bool UnixListener::connect(ShibSocket& s) const
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);
145 if (::connect(s, (struct sockaddr *)&addr, sizeof (addr)) < 0)
146 return log_error("connect");
150 bool UnixListener::close(ShibSocket& s) const
156 bool UnixListener::accept(ShibSocket& listener, ShibSocket& s) const
158 s=::accept(listener,nullptr,nullptr);
160 return log_error("accept");