[BACK]Return to activate.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / mount_portal

Annotation of src/sbin/mount_portal/activate.c, Revision 1.15

1.15    ! lukem       1: /*     $NetBSD: activate.c,v 1.14 2007/07/02 18:07:44 pooka Exp $      */
1.4       cgd         2:
1.1       cgd         3: /*
                      4:  * Copyright (c) 1992, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software donated to Berkeley by
                      8:  * Jan-Simon Pendry.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.12      agc        18:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  *
1.2       mycroft    34:  *     from: Id: activate.c,v 1.2 1992/05/27 07:09:27 jsp Exp
1.7       lukem      35:  *     @(#)activate.c  8.3 (Berkeley) 4/28/95
1.1       cgd        36:  */
                     37:
1.6       lukem      38: #include <sys/cdefs.h>
                     39: #ifndef lint
1.15    ! lukem      40: __RCSID("$NetBSD: activate.c,v 1.14 2007/07/02 18:07:44 pooka Exp $");
1.6       lukem      41: #endif /* not lint */
                     42:
1.1       cgd        43: #include <stdio.h>
                     44: #include <stdlib.h>
                     45: #include <unistd.h>
                     46: #include <string.h>
                     47: #include <errno.h>
                     48: #include <signal.h>
                     49: #include <sys/types.h>
                     50: #include <sys/param.h>
                     51: #include <sys/socket.h>
                     52: #include <sys/un.h>
                     53: #include <sys/syslog.h>
                     54: #include <sys/uio.h>
                     55:
                     56: #include "portald.h"
                     57:
1.15    ! lukem      58: static int     get_request(int, struct portal_cred *, char *, size_t);
1.13      xtraeme    59: static void    send_reply(int, int, int);
1.6       lukem      60:
1.1       cgd        61: /*
                     62:  * Scan the providers list and call the
                     63:  * appropriate function.
                     64:  */
1.14      pooka      65: int
                     66: activate_argv(struct portal_cred *pcr, char *key, char **v, int *fdp)
1.1       cgd        67: {
                     68:        provider *pr;
                     69:
                     70:        for (pr = providers; pr->pr_match; pr++)
                     71:                if (strcmp(v[0], pr->pr_match) == 0)
1.14      pooka      72:                        return ((*pr->pr_func)(pcr, key, v, fdp));
1.1       cgd        73:
                     74:        return (ENOENT);
                     75: }
                     76:
1.6       lukem      77: static int
1.15    ! lukem      78: get_request(int so, struct portal_cred *pcr, char *key, size_t klen)
1.1       cgd        79: {
                     80:        struct iovec iov[2];
                     81:        struct msghdr msg;
1.15    ! lukem      82:        ssize_t n;
1.1       cgd        83:
                     84:        iov[0].iov_base = (caddr_t) pcr;
                     85:        iov[0].iov_len = sizeof(*pcr);
                     86:        iov[1].iov_base = key;
                     87:        iov[1].iov_len = klen;
                     88:
1.3       mycroft    89:        memset(&msg, 0, sizeof(msg));
1.1       cgd        90:        msg.msg_iov = iov;
                     91:        msg.msg_iovlen = 2;
                     92:
                     93:        n = recvmsg(so, &msg, 0);
                     94:        if (n < 0)
                     95:                return (errno);
                     96:
1.15    ! lukem      97:        if (n <= (ssize_t)sizeof(*pcr))
1.1       cgd        98:                return (EINVAL);
                     99:
                    100:        n -= sizeof(*pcr);
                    101:        key[n] = '\0';
                    102:
                    103:        return (0);
                    104: }
                    105:
1.6       lukem     106: static void
1.13      xtraeme   107: send_reply(int so, int fd, int error)
1.1       cgd       108: {
                    109:        int n;
                    110:        struct iovec iov;
                    111:        struct msghdr msg;
1.9       thorpej   112:        void *ctl = NULL;
                    113:        struct cmsghdr *cmsg;
                    114:        int *files;
                    115:        socklen_t cmsgsize;
1.1       cgd       116:
                    117:        /*
                    118:         * Line up error code.  Don't worry about byte ordering
                    119:         * because we must be sending to the local machine.
                    120:         */
                    121:        iov.iov_base = (caddr_t) &error;
                    122:        iov.iov_len = sizeof(error);
                    123:
                    124:        /*
                    125:         * Build a msghdr
                    126:         */
1.3       mycroft   127:        memset(&msg, 0, sizeof(msg));
1.1       cgd       128:        msg.msg_iov = &iov;
                    129:        msg.msg_iovlen = 1;
                    130:
                    131:        /*
                    132:         * If there is a file descriptor to send then
                    133:         * construct a suitable rights control message.
                    134:         */
                    135:        if (fd >= 0) {
1.11      atatat    136:                cmsgsize = CMSG_LEN(sizeof(*files));
1.9       thorpej   137:
                    138:                ctl = malloc(cmsgsize);
                    139:                if (ctl == NULL) {
1.10      lukem     140:                        syslog(LOG_WARNING, "malloc control message: %m");
1.9       thorpej   141:                        return;
                    142:                }
                    143:                memset(ctl, 0, cmsgsize);
                    144:
                    145:                cmsg = (struct cmsghdr *) ctl;
                    146:                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
                    147:                cmsg->cmsg_level = SOL_SOCKET;
                    148:                cmsg->cmsg_type = SCM_RIGHTS;
                    149:
                    150:                files = (int *)CMSG_DATA(cmsg);
                    151:                files[0] = fd;
                    152:
                    153:                msg.msg_control = ctl;
                    154:                msg.msg_controllen = cmsgsize;
1.1       cgd       155:        }
                    156:
                    157:        /*
                    158:         * Send to kernel...
                    159:         */
                    160:        if ((n = sendmsg(so, &msg, MSG_EOR)) < 0)
1.10      lukem     161:                syslog(LOG_WARNING, "send: %m");
1.1       cgd       162: #ifdef DEBUG
                    163:        fprintf(stderr, "sent %d bytes\n", n);
                    164: #endif
                    165:        sleep(1);       /*XXX*/
                    166: #ifdef notdef
                    167:        if (shutdown(so, 2) < 0)
1.10      lukem     168:                syslog(LOG_WARNING, "shutdown: %m");
1.1       cgd       169: #endif
                    170:        /*
1.9       thorpej   171:         * Throw away the open file descriptor and control
                    172:         * message buffer.
1.1       cgd       173:         */
1.9       thorpej   174:        if (fd >= 0)
                    175:                (void) close(fd);
                    176:        if (ctl != NULL)
                    177:                free(ctl);
1.1       cgd       178: }
                    179:
1.6       lukem     180: void
1.13      xtraeme   181: activate(qelem *q, int so)
1.1       cgd       182: {
                    183:        struct portal_cred pcred;
                    184:        char key[MAXPATHLEN+1];
                    185:        int error;
                    186:        char **v;
                    187:        int fd = -1;
                    188:
                    189:        /*
                    190:         * Read the key from the socket
                    191:         */
                    192:        error = get_request(so, &pcred, key, sizeof(key));
                    193:        if (error) {
1.10      lukem     194:                syslog(LOG_WARNING, "activate: recvmsg: %m");
1.1       cgd       195:                goto drop;
                    196:        }
                    197:
                    198: #ifdef DEBUG
                    199:        fprintf(stderr, "lookup key %s\n", key);
                    200: #endif
                    201:
                    202:        /*
                    203:         * Find a match in the configuration file
                    204:         */
                    205:        v = conf_match(q, key);
                    206:
                    207:        /*
                    208:         * If a match existed, then find an appropriate portal
                    209:         * otherwise simply return ENOENT.
                    210:         */
                    211:        if (v) {
1.14      pooka     212:                error = activate_argv(&pcred, key, v, &fd);
1.1       cgd       213:                if (error)
                    214:                        fd = -1;
                    215:                else if (fd < 0)
                    216:                        error = -1;
1.8       enami     217:        } else
1.1       cgd       218:                error = ENOENT;
                    219:
                    220:        if (error >= 0)
                    221:                send_reply(so, fd, error);
                    222:
                    223: drop:;
                    224:        close(so);
                    225: }

CVSweb <webmaster@jp.NetBSD.org>