static int xsasl_dovecot_server_connect(XSASL_DOVECOT_SERVER_IMPL *xp)
{
    const char *myname = "xsasl_dovecot_server_connect";
    VSTRING *line_str;
    VSTREAM *sasl_stream;
    char   *line, *cmd, *mech_name;
    unsigned int major_version, minor_version;
    int     fd, success;
    int     sec_props;
    const char *path;

    if (msg_verbose)
        msg_info("%s: Connecting", myname);

    /*
     * Not documented, but necessary for testing.
     */
    path = xp->socket_path;
    if (strncmp(path, "inet:", 5) == 0) {
        fd = inet_connect(path + 5, BLOCKING, AUTH_TIMEOUT);
    } else {
        if (strncmp(path, "unix:", 5) == 0)
            path += 5;
        fd = unix_connect(path, BLOCKING, AUTH_TIMEOUT);
    }
    if (fd < 0) {
        msg_warn("SASL: Connect to %s failed: %m", xp->socket_path);
        return (-1);
    }
    sasl_stream = vstream_fdopen(fd, O_RDWR);
    vstream_control(sasl_stream,
                    VSTREAM_CTL_PATH, xp->socket_path,
                    VSTREAM_CTL_TIMEOUT, AUTH_TIMEOUT,
                    VSTREAM_CTL_END);

    /* XXX Encapsulate for logging. */
    vstream_fprintf(sasl_stream,
                    "VERSION\t%u\t%u\n"
                    "CPID\t%u\n",
                    AUTH_PROTOCOL_MAJOR_VERSION,
                    AUTH_PROTOCOL_MINOR_VERSION,
                    (unsigned int) getpid());
    if (vstream_fflush(sasl_stream) == VSTREAM_EOF) {
        msg_warn("SASL: Couldn't send handshake: %m");
        return (-1);
    }
    success = 0;
    line_str = vstring_alloc(256);
    /* XXX Encapsulate for logging. */
    while (vstring_get_nonl(line_str, sasl_stream) != VSTREAM_EOF) {
        line = vstring_str(line_str);

        if (msg_verbose)
            msg_info("%s: auth reply: %s", myname, line);

        cmd = line;
        line = split_at(line, '\t');

        if (strcmp(cmd, "VERSION") == 0) {
            if (sscanf(line, "%u\t%u", &major_version, &minor_version) != 2) {
                msg_warn("SASL: Protocol version error");
                break;
            }
            if (major_version != AUTH_PROTOCOL_MAJOR_VERSION) {
                /* Major version is different from ours. */
                msg_warn("SASL: Protocol version mismatch (%d vs. %d)",
                         major_version, AUTH_PROTOCOL_MAJOR_VERSION);
                break;
            }
        } else if (strcmp(cmd, "MECH") == 0 && line != NULL) {
            mech_name = line;
            line = split_at(line, '\t');
            if (line != 0) {
                sec_props =
                    name_mask_delim_opt(myname,
                                        xsasl_dovecot_serv_sec_props,
                                        line, "\t",
                                        NAME_MASK_ANY_CASE | NAME_MASK_IGNORE);
                if ((sec_props & SEC_PROPS_PRIVATE) != 0)
                    continue;
            } else
                sec_props = 0;
            xsasl_dovecot_server_mech_append(&xp->mechanism_list, mech_name,
                                             sec_props);
        } else if (strcmp(cmd, "DONE") == 0) {
            /* Handshake finished. */
            success = 1;
            break;
        } else {
            /* ignore any unknown commands */
        }
    }
    vstring_free(line_str);

    if (!success) {
        /* handshake failed */
        (void) vstream_fclose(sasl_stream);
        return (-1);
    }
    xp->sasl_stream = sasl_stream;
    return (0);
}
Esempio n. 2
0
int unix_socket_outgoing(const char *path)
{
    return unix_connect(path);
}
Esempio n. 3
0
int main (int argc, char **argv)
#endif
{
    int optchar, action = 0, count = 1, starting_nas_idx = -1,
                 t2a_err, srv_fd, nbytes, header = 0;
    char *endptr;
    char name[MAX_GROUPNAME_LEN] = {};
    struct be_msg smsg, rmsg;
    struct be_memstat rstatmsg;
    struct sockaddr_atmpvc vc;
    struct passwd *pw;
    // brcm
    int encap=BR2684_ENCAPS_LLC, proto_filter=0, mode=0;
    unsigned short vlan_id=-1;

#ifdef SUPPORT_VDSL
    /* start of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
    int trafficmode =0;
    /* end of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
#endif

    bzero(&vc, sizeof(vc));

    while(1) {
        /* start of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
        optchar = getopt(argc, argv, "Madli:v:c:k:g:erft:");
        /* end of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
        if (optchar == -1)
            break;

        switch(optchar) {

        case 'M':

            srv_fd = unix_connect();
            smsg.msgtype = MEM_STATS;

            if( send(srv_fd, &smsg, sizeof(smsg), 0) < 0 )
                clean_exit("Can't send MEM_STAT message");

            nbytes = recv(srv_fd, &rstatmsg, sizeof(rstatmsg), 0);
            if(nbytes == 0 )
                clean_exit("connection closed waiting for MEM_STAT response\n");
            else if(nbytes < 0)
                clean_exit("Can't receive MEM_STAT response");

            printf("VC mallocs: %d\nVC frees: %d\nGroup mallocs: %d\nGroup frees:%d\n",
                   rstatmsg.vc_mallocs,rstatmsg.vc_frees,
                   rstatmsg.group_mallocs,rstatmsg.group_frees);
            exit(0);
            break;

        case 'a':
        case 'd':
        case 'l':
            action = optchar;
            break;

        case 'i':
// brcm
            /*
                  starting_nas_idx = strtol(optarg, &endptr, 10);
                  if(*endptr || starting_nas_idx < 0) {
            	printf("Invalid interface index specified\n");
            	exit(1);
                  }
            */
            break;

        case 'v':
            if((t2a_err = (text2atm(optarg, (struct sockaddr *)&vc, sizeof(vc), T2A_PVC)))) {
                printf ("can't parse \"%s\".\n", optarg);
                exit(1);
            }
// brcm
            {
                int retval, num[3];
                retval = sscanf(optarg, "%d.%d.%d", num, num+1, num+2);
                starting_nas_idx = OFFSET*num[1]+num[2];
            }
            break;

        case 'c':
            count = strtol(optarg, &endptr, 10);
            if(*endptr || count <= 0) {
                printf("Invalid count specified\n");
                exit(1);
            }
            break;

// brcm
        case 'e':
            encap=BR2684_ENCAPS_VC;
            break;
#ifdef SUPPORT_VDSL
        /* start of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
        case 'k':
            trafficmode = strtol(optarg, &endptr, 10);
            printf("pvc2684ctl_main trafficmode:%d\n", trafficmode);
            break;
            /* end of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
#endif
// brcm
        case 'f':
            proto_filter = FILTER_PPPOE;
            break;

// brcm
        case 'r':
            mode=1;
            break;
// brcm  add vlan id flag
        case 't':
            vlan_id = atoi(optarg);
            break;

        case 'g':
            strncpy(name,optarg,MAX_GROUPNAME_LEN);

        case '?':

            break;

        default:
            printf ("getopt returned char %c\n", optchar);
        }
    }

    int hasname = 31;
    for(; hasname>0; hasname--) {
        if(name[hasname] != '\0') break;
    }

    switch(action) {

    case 'a':
    case 'd':

        if(vc.sap_family == AF_ATMPVC) {

            if(starting_nas_idx < 0) {

                /* If you specify a PVC, you must specify an interface too  */
                printf("No interface specified. Use the -i option.\n");
                usage(1);
            }

        } else if(action == 'a') {

            /* If this is an ADD, you need to specify the PVC.  */
            printf("No VC specified. Use the -v option.\n");
            usage(1);

        } else if (!hasname) {

            /* If it's not an ADD, it must be a DELETE. You need at least a name.  */
            printf("No VC or VC group name specified. Use the -v or -g options.\n");
            usage(1);

        } else {

            /* If we're here, no VC was specified, it's a DELETE and a group
            name was specified. Send a DELETE_GROUP message and exit.
                */

            bzero(&rmsg, sizeof(rmsg));
            srv_fd = unix_connect();
            smsg.msgtype = DELETE_GROUP;
            strncpy(smsg.name,name,MAX_GROUPNAME_LEN);

            if( send(srv_fd, &smsg, sizeof(smsg), 0) < 0 )
                clean_exit("Can't send DELETE_GROUP message");

            nbytes = recv(srv_fd, &rmsg, sizeof(rmsg), 0);
            if(nbytes == 0 )
                clean_exit("connection closed waiting for DELETE_GROUP response\n");
            else if(nbytes < 0)
                clean_exit("Can't receive response to DELETE_GROUP message");

            if(rmsg.msgtype == GROUP_NOT_FOUND)
                printf("group \"%s\" doesn't exist\n", name);

            exit(0);
        }

        smsg.msgtype = (action == 'a' ? ADD : DELETE);
        smsg.nas_idx = starting_nas_idx;
        smsg.pvc     = vc;
        strncpy(smsg.name,name,MAX_GROUPNAME_LEN);
        // brcm
        smsg.encap   = encap;
        smsg.proto_filter   = proto_filter;
        smsg.mode   = mode;
        smsg.vlan_id = vlan_id;

#ifdef SUPPORT_VDSL
        /* start of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
        //for ptm
        smsg.trafficmode = trafficmode;
        /* end of PROTOCOL PTM by zhouyi 00037589 2006年7月11日 */
#endif

        bzero(&rmsg, sizeof(rmsg));
        srv_fd = unix_connect();

        do {

            if( send(srv_fd, &smsg, sizeof(smsg), 0) < 0 )
                clean_exit("Can't send ADD or DELETE message");

            nbytes = recv(srv_fd, &rmsg, sizeof(rmsg), 0);
            if(nbytes == 0 )
                clean_exit("connection closed waiting for ADD or DELETE response\n");
            else if(nbytes < 0)
                clean_exit("Can't receive response to ADD or DELETE message");

            switch(rmsg.msgtype) {

            case VC_NOT_FOUND:

                printf("No VC %d/%d on nas%d\n",
                       smsg.pvc.sap_addr.vpi,
                       smsg.pvc.sap_addr.vci,
                       smsg.nas_idx);
                break;

            case NOT_OWNER:

                pw = getpwuid(rmsg.uid);
                printf("Can't delete VC %d/%d on nas%d. owner is %s\n",
                       smsg.pvc.sap_addr.vpi,
                       smsg.pvc.sap_addr.vci,
                       smsg.nas_idx,
                       pw->pw_name);
                break;

            case SOCK_FAILED:

                printf("Interface nas%d already in use\n", smsg.nas_idx);
                break;

            case INTERFACE_FAILED:

                printf("Interface nas%d couldn't be created\n", smsg.nas_idx);
                break;

            case NOMEM:

                printf("Server can't allocate memory\n");
                break;

            case OK:

                break;

            default:

                printf("Weird error: %d\n", rmsg.msgtype);
            }

            smsg.nas_idx++;
            smsg.pvc.sap_addr.vci++;

        } while(--count > 0);

        break;

    case 'l':

        srv_fd = unix_connect();
        smsg.msgtype = (strlen(name) ? LIST_GROUP : LIST_ALL );
        strncpy(smsg.name,name,MAX_GROUPNAME_LEN);

        if( send(srv_fd, &smsg, sizeof(smsg), 0) < 0 )
            clean_exit("Can't send LIST_GROUP or LIST_ALL message");


        do {

            nbytes = recv(srv_fd, &rmsg, sizeof(rmsg), 0);
            if(nbytes == 0 )
                clean_exit("connection closed waiting for LIST_GROUP or LIST_ALL response\n");
            else if(nbytes < 0)
                clean_exit("Can't receive response to LIST_GROUP or LIST_ALL message");

            if(rmsg.msgtype == GROUP_NOT_FOUND) {
                if(strlen(name))
                    printf("Group %s not found\n", name);
                else
                    printf("No VCs defined\n");
                break;
            } else if(!header) {
// brcm
                printf("\nVC          interface  mode             vlan_id\n");
                printf("--          ---------  ------           ----------\n");
//	printf("\nVC          interface  group                            owner\n");
//	printf("--          ---------  ------                           -----\n");
                header = 1;
            }

            if(rmsg.msgtype == OK) {
                printf("%2d/%4d     nas%-6d  %-16s %d (X%02X)\n",
                       rmsg.pvc.sap_addr.vpi,
                       rmsg.pvc.sap_addr.vci,
                       rmsg.nas_idx,
                       rmsg.mode? "Routing":	"Bridging",
                       rmsg.vlan_id, rmsg.vlan_id
                      );
            }
        } while(rmsg.msgtype != LIST_END);

        printf("\n");

        break;

    default:
        printf("No command specified. Use -a, -d, or -l\n");
    }
    exit(0);
}
Esempio n. 4
0
/*
 * call-seq:
 *
 *	Kgio::UNIXSocket.start("/path/to/unix/socket") -> socket
 *
 * Creates a new Kgio::UNIXSocket object and initiates a
 * non-blocking connection.  The caller should select/poll
 * on the socket for writability before attempting to write
 * or optimistically attempt a write and handle :wait_writable
 * or Errno::EAGAIN.
 */
static VALUE kgio_unix_start(VALUE klass, VALUE path)
{
	return unix_connect(klass, path, 0);
}