1 /*- 2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/types.h> 29 30 #include <sys/event.h> 31 #include <sys/socket.h> 32 #include <sys/uio.h> 33 #include <sys/un.h> 34 35 #include <assert.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 42 #include "debug.h" 43 #include "nscdcli.h" 44 #include "protocol.h" 45 46 #define DEFAULT_NSCD_IO_TIMEOUT 4 47 48 static int safe_write(struct nscd_connection_ *, const void *, size_t); 49 static int safe_read(struct nscd_connection_ *, void *, size_t); 50 static int send_credentials(struct nscd_connection_ *, int); 51 52 static int 53 safe_write(struct nscd_connection_ *connection, const void *data, 54 size_t data_size) 55 { 56 struct kevent eventlist; 57 int nevents; 58 size_t result; 59 ssize_t s_result; 60 struct timespec timeout; 61 62 if (data_size == 0) 63 return (0); 64 65 timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT; 66 timeout.tv_nsec = 0; 67 result = 0; 68 do { 69 nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 70 1, &timeout); 71 if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) { 72 s_result = write(connection->sockfd, 73 (char *)data + result, 74 (size_t)eventlist.data < data_size - result ? 75 (size_t)eventlist.data : data_size - result); 76 if (s_result == -1) 77 return (-1); 78 else 79 result += s_result; 80 81 if (eventlist.flags & EV_EOF) 82 return (result < data_size ? -1 : 0); 83 } else 84 return (-1); 85 } while (result < data_size); 86 87 return (0); 88 } 89 90 static int 91 safe_read(struct nscd_connection_ *connection, void *data, size_t data_size) 92 { 93 struct kevent eventlist; 94 size_t result; 95 ssize_t s_result; 96 struct timespec timeout; 97 int nevents; 98 99 if (data_size == 0) 100 return (0); 101 102 timeout.tv_sec = DEFAULT_NSCD_IO_TIMEOUT; 103 timeout.tv_nsec = 0; 104 result = 0; 105 do { 106 nevents = kevent(connection->read_queue, NULL, 0, &eventlist, 1, 107 &timeout); 108 if ((nevents == 1) && (eventlist.filter == EVFILT_READ)) { 109 s_result = read(connection->sockfd, 110 (char *)data + result, 111 (size_t)eventlist.data <= data_size - result ? 112 (size_t)eventlist.data : data_size - result); 113 if (s_result == -1) 114 return (-1); 115 else 116 result += s_result; 117 118 if (eventlist.flags & EV_EOF) 119 return (result < data_size ? -1 : 0); 120 } else 121 return (-1); 122 } while (result < data_size); 123 124 return (0); 125 } 126 127 static int 128 send_credentials(struct nscd_connection_ *connection, int type) 129 { 130 union { 131 struct cmsghdr hdr; 132 char pad[CMSG_SPACE(sizeof(struct cmsgcred))]; 133 } cmsg; 134 struct msghdr mhdr; 135 struct iovec iov; 136 struct kevent eventlist; 137 int nevents; 138 ssize_t result; 139 140 TRACE_IN(send_credentials); 141 memset(&cmsg, 0, sizeof(cmsg)); 142 cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred)); 143 cmsg.hdr.cmsg_level = SOL_SOCKET; 144 cmsg.hdr.cmsg_type = SCM_CREDS; 145 146 memset(&mhdr, 0, sizeof(mhdr)); 147 mhdr.msg_iov = &iov; 148 mhdr.msg_iovlen = 1; 149 mhdr.msg_control = &cmsg; 150 mhdr.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred)); 151 152 iov.iov_base = &type; 153 iov.iov_len = sizeof(int); 154 155 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD, 156 NOTE_LOWAT, sizeof(int), NULL); 157 kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL); 158 159 nevents = kevent(connection->write_queue, NULL, 0, &eventlist, 1, NULL); 160 if ((nevents == 1) && (eventlist.filter == EVFILT_WRITE)) { 161 result = sendmsg(connection->sockfd, &mhdr, 0) == -1 ? -1 : 0; 162 EV_SET(&eventlist, connection->sockfd, EVFILT_WRITE, EV_ADD, 163 0, 0, NULL); 164 kevent(connection->write_queue, &eventlist, 1, NULL, 0, NULL); 165 TRACE_OUT(send_credentials); 166 return (result); 167 } else { 168 TRACE_OUT(send_credentials); 169 return (-1); 170 } 171 } 172 173 struct nscd_connection_ * 174 open_nscd_connection__(struct nscd_connection_params const *params) 175 { 176 struct nscd_connection_ *retval; 177 struct kevent eventlist; 178 struct sockaddr_un client_address; 179 int client_address_len, client_socket; 180 int res; 181 182 TRACE_IN(open_nscd_connection); 183 assert(params != NULL); 184 185 client_socket = socket(PF_LOCAL, SOCK_STREAM, 0); 186 client_address.sun_family = PF_LOCAL; 187 strlcpy(client_address.sun_path, params->socket_path, 188 sizeof(client_address.sun_path)); 189 client_address_len = sizeof(client_address.sun_family) + 190 strlen(client_address.sun_path) + 1; 191 192 res = connect(client_socket, (struct sockaddr *)&client_address, 193 client_address_len); 194 if (res == -1) { 195 close(client_socket); 196 TRACE_OUT(open_nscd_connection); 197 return (NULL); 198 } 199 fcntl(client_socket, F_SETFL, O_NONBLOCK); 200 201 retval = calloc(1, sizeof(*retval)); 202 assert(retval != NULL); 203 204 retval->sockfd = client_socket; 205 206 retval->write_queue = kqueue(); 207 assert(retval->write_queue != -1); 208 209 EV_SET(&eventlist, retval->sockfd, EVFILT_WRITE, EV_ADD, 210 0, 0, NULL); 211 res = kevent(retval->write_queue, &eventlist, 1, NULL, 0, NULL); 212 213 retval->read_queue = kqueue(); 214 assert(retval->read_queue != -1); 215 216 EV_SET(&eventlist, retval->sockfd, EVFILT_READ, EV_ADD, 217 0, 0, NULL); 218 res = kevent(retval->read_queue, &eventlist, 1, NULL, 0, NULL); 219 220 TRACE_OUT(open_nscd_connection); 221 return (retval); 222 } 223 224 void 225 close_nscd_connection__(struct nscd_connection_ *connection) 226 { 227 228 TRACE_IN(close_nscd_connection); 229 assert(connection != NULL); 230 231 close(connection->sockfd); 232 close(connection->read_queue); 233 close(connection->write_queue); 234 free(connection); 235 TRACE_OUT(close_nscd_connection); 236 } 237 238 int 239 nscd_transform__(struct nscd_connection_ *connection, 240 const char *entry_name, int transformation_type) 241 { 242 size_t name_size; 243 int error_code; 244 int result; 245 246 TRACE_IN(nscd_transform); 247 248 error_code = -1; 249 result = 0; 250 result = send_credentials(connection, CET_TRANSFORM_REQUEST); 251 if (result != 0) 252 goto fin; 253 254 if (entry_name != NULL) 255 name_size = strlen(entry_name); 256 else 257 name_size = 0; 258 259 result = safe_write(connection, &name_size, sizeof(size_t)); 260 if (result != 0) 261 goto fin; 262 263 result = safe_write(connection, &transformation_type, sizeof(int)); 264 if (result != 0) 265 goto fin; 266 267 if (entry_name != NULL) { 268 result = safe_write(connection, entry_name, name_size); 269 if (result != 0) 270 goto fin; 271 } 272 273 result = safe_read(connection, &error_code, sizeof(int)); 274 if (result != 0) 275 error_code = -1; 276 277 fin: 278 TRACE_OUT(nscd_transform); 279 return (error_code); 280 } 281