os-local.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* os-local.c -- platform-specific domain socket code */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Portions Copyright (c) 1995 Regents of the University of Michigan.
  17. * All rights reserved.
  18. */
  19. /* Portions (C) Copyright PADL Software Pty Ltd. 1999
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that this notice is preserved
  22. * and that due credit is given to PADL Software Pty Ltd. This software
  23. * is provided ``as is'' without express or implied warranty.
  24. */
  25. #include "portable.h"
  26. #ifdef LDAP_PF_LOCAL
  27. #include <stdio.h>
  28. #include <ac/stdlib.h>
  29. #include <ac/errno.h>
  30. #include <ac/socket.h>
  31. #include <ac/string.h>
  32. #include <ac/time.h>
  33. #include <ac/unistd.h>
  34. #ifdef HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #ifdef HAVE_SYS_UIO_H
  38. #include <sys/uio.h>
  39. #endif
  40. #ifdef HAVE_IO_H
  41. #include <io.h>
  42. #endif /* HAVE_IO_H */
  43. #ifdef HAVE_FCNTL_H
  44. #include <fcntl.h>
  45. #endif
  46. #include "ldap-int.h"
  47. #include "ldap_defaults.h"
  48. static void
  49. ldap_pvt_set_errno(int err)
  50. {
  51. errno = err;
  52. }
  53. static int
  54. ldap_pvt_ndelay_on(LDAP *ld, int fd)
  55. {
  56. Debug1(LDAP_DEBUG_TRACE, "ldap_ndelay_on: %d\n",fd );
  57. return ber_pvt_socket_set_nonblock( fd, 1 );
  58. }
  59. static int
  60. ldap_pvt_ndelay_off(LDAP *ld, int fd)
  61. {
  62. Debug1(LDAP_DEBUG_TRACE, "ldap_ndelay_off: %d\n",fd );
  63. return ber_pvt_socket_set_nonblock( fd, 0 );
  64. }
  65. static ber_socket_t
  66. ldap_pvt_socket(LDAP *ld)
  67. {
  68. ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
  69. Debug1(LDAP_DEBUG_TRACE, "ldap_new_socket: %d\n",s );
  70. #ifdef FD_CLOEXEC
  71. fcntl(s, F_SETFD, FD_CLOEXEC);
  72. #endif
  73. return ( s );
  74. }
  75. static int
  76. ldap_pvt_close_socket(LDAP *ld, int s)
  77. {
  78. Debug1(LDAP_DEBUG_TRACE, "ldap_close_socket: %d\n",s );
  79. return tcp_close(s);
  80. }
  81. #undef TRACE
  82. #define TRACE do { \
  83. char ebuf[128]; \
  84. int saved_errno = errno; \
  85. Debug3(LDAP_DEBUG_TRACE, "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
  86. s, \
  87. saved_errno, \
  88. AC_STRERROR_R(saved_errno, ebuf, sizeof ebuf)); \
  89. } while( 0 )
  90. /*
  91. * check the socket for errors after select returned.
  92. */
  93. static int
  94. ldap_pvt_is_socket_ready(LDAP *ld, int s)
  95. {
  96. Debug1(LDAP_DEBUG_TRACE, "ldap_is_sock_ready: %d\n",s );
  97. #if defined( notyet ) /* && defined( SO_ERROR ) */
  98. {
  99. int so_errno;
  100. ber_socklen_t dummy = sizeof(so_errno);
  101. if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
  102. == AC_SOCKET_ERROR )
  103. {
  104. return -1;
  105. }
  106. if ( so_errno ) {
  107. ldap_pvt_set_errno(so_errno);
  108. TRACE;
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. #else
  114. {
  115. /* error slippery */
  116. struct sockaddr_un sa;
  117. char ch;
  118. ber_socklen_t dummy = sizeof(sa);
  119. if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
  120. == AC_SOCKET_ERROR )
  121. {
  122. /* XXX: needs to be replace with ber_stream_read() */
  123. (void)read(s, &ch, 1);
  124. TRACE;
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. #endif
  130. return -1;
  131. }
  132. #undef TRACE
  133. #ifdef LDAP_PF_LOCAL_SENDMSG
  134. static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
  135. LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
  136. #endif
  137. static int
  138. ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
  139. {
  140. int rc;
  141. struct timeval tv, *opt_tv = NULL;
  142. if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
  143. tv = ld->ld_options.ldo_tm_net;
  144. opt_tv = &tv;
  145. }
  146. Debug3(LDAP_DEBUG_TRACE,
  147. "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
  148. s, opt_tv ? tv.tv_sec : -1L, async);
  149. if ( ldap_pvt_ndelay_on(ld, s) == -1 ) return -1;
  150. if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
  151. != AC_SOCKET_ERROR )
  152. {
  153. if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
  154. #ifdef LDAP_PF_LOCAL_SENDMSG
  155. /* Send a dummy message with access rights. Remote side will
  156. * obtain our uid/gid by fstat'ing this descriptor. The
  157. * descriptor permissions must match exactly, and we also
  158. * send the socket name, which must also match.
  159. */
  160. sendcred:
  161. {
  162. int fds[2];
  163. ber_socklen_t salen = sizeof(*sa);
  164. if (pipe(fds) == 0) {
  165. /* Abandon, noop, has no reply */
  166. struct iovec iov;
  167. struct msghdr msg = {0};
  168. # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
  169. # ifndef CMSG_SPACE
  170. # define CMSG_SPACE(len) (_CMSG_ALIGN( sizeof(struct cmsghdr)) + _CMSG_ALIGN(len) )
  171. # endif
  172. # ifndef CMSG_LEN
  173. # define CMSG_LEN(len) (_CMSG_ALIGN( sizeof(struct cmsghdr)) + (len) )
  174. # endif
  175. union {
  176. struct cmsghdr cm;
  177. unsigned char control[CMSG_SPACE(sizeof(int))];
  178. } control_un;
  179. struct cmsghdr *cmsg;
  180. # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
  181. msg.msg_name = NULL;
  182. msg.msg_namelen = 0;
  183. iov.iov_base = (char *) abandonPDU;
  184. iov.iov_len = sizeof abandonPDU;
  185. msg.msg_iov = &iov;
  186. msg.msg_iovlen = 1;
  187. # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
  188. msg.msg_control = control_un.control;
  189. msg.msg_controllen = sizeof( control_un.control );
  190. msg.msg_flags = 0;
  191. cmsg = CMSG_FIRSTHDR( &msg );
  192. cmsg->cmsg_len = CMSG_LEN( sizeof(int) );
  193. cmsg->cmsg_level = SOL_SOCKET;
  194. cmsg->cmsg_type = SCM_RIGHTS;
  195. *((int *)CMSG_DATA(cmsg)) = fds[0];
  196. # else
  197. msg.msg_accrights = (char *)fds;
  198. msg.msg_accrightslen = sizeof(int);
  199. # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
  200. getpeername( s, (struct sockaddr *) sa, &salen );
  201. fchmod( fds[0], S_ISUID|S_IRWXU );
  202. write( fds[1], sa, salen );
  203. sendmsg( s, &msg, 0 );
  204. close(fds[0]);
  205. close(fds[1]);
  206. }
  207. }
  208. #endif
  209. return 0;
  210. }
  211. if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) return -1;
  212. #ifdef notyet
  213. if ( async ) return -2;
  214. #endif
  215. #ifdef HAVE_POLL
  216. {
  217. struct pollfd fd;
  218. int timeout = INFTIM;
  219. if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
  220. fd.fd = s;
  221. fd.events = POLL_WRITE;
  222. do {
  223. fd.revents = 0;
  224. rc = poll( &fd, 1, timeout );
  225. } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
  226. LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
  227. if( rc == AC_SOCKET_ERROR ) return rc;
  228. if( fd.revents & POLL_WRITE ) {
  229. if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
  230. if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
  231. #ifdef LDAP_PF_LOCAL_SENDMSG
  232. goto sendcred;
  233. #else
  234. return ( 0 );
  235. #endif
  236. }
  237. }
  238. #else
  239. {
  240. fd_set wfds, *z=NULL;
  241. #ifdef FD_SETSIZE
  242. if ( s >= FD_SETSIZE ) {
  243. rc = AC_SOCKET_ERROR;
  244. tcp_close( s );
  245. ldap_pvt_set_errno( EMFILE );
  246. return rc;
  247. }
  248. #endif
  249. do {
  250. FD_ZERO(&wfds);
  251. FD_SET(s, &wfds );
  252. rc = select( ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL );
  253. } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
  254. LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
  255. if( rc == AC_SOCKET_ERROR ) return rc;
  256. if ( FD_ISSET(s, &wfds) ) {
  257. if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
  258. if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
  259. #ifdef LDAP_PF_LOCAL_SENDMSG
  260. goto sendcred;
  261. #else
  262. return ( 0 );
  263. #endif
  264. }
  265. }
  266. #endif
  267. Debug0(LDAP_DEBUG_TRACE, "ldap_connect_timeout: timed out\n" );
  268. ldap_pvt_set_errno( ETIMEDOUT );
  269. return ( -1 );
  270. }
  271. int
  272. ldap_connect_to_path(LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv, int async)
  273. {
  274. struct sockaddr_un server;
  275. ber_socket_t s;
  276. int rc;
  277. const char *path = srv->lud_host;
  278. Debug0(LDAP_DEBUG_TRACE, "ldap_connect_to_path\n" );
  279. if ( path == NULL || path[0] == '\0' ) {
  280. path = LDAPI_SOCK;
  281. } else {
  282. if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
  283. ldap_pvt_set_errno( ENAMETOOLONG );
  284. return -1;
  285. }
  286. }
  287. s = ldap_pvt_socket( ld );
  288. if ( s == AC_SOCKET_INVALID ) {
  289. return -1;
  290. }
  291. Debug1(LDAP_DEBUG_TRACE, "ldap_connect_to_path: Trying %s\n", path );
  292. memset( &server, '\0', sizeof(server) );
  293. server.sun_family = AF_LOCAL;
  294. strcpy( server.sun_path, path );
  295. rc = ldap_pvt_connect(ld, s, &server, async);
  296. if (rc == 0) {
  297. rc = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&server );
  298. }
  299. if ( rc ) {
  300. ldap_pvt_close_socket(ld, s);
  301. }
  302. return rc;
  303. }
  304. #else
  305. static int dummy; /* generate also a warning: 'dummy' defined but not used (at least here) */
  306. #endif /* LDAP_PF_LOCAL */