Пример #1
0
int main(int argc, char *argv[])
{
    unsigned char lb[2];
    unsigned char buf[BUFSIZ];
    char *user;
    char *pwd;
    char *mode;
    int sid;
    int rval;
    struct session *sessp;

    // test clause
    if (argc == 4 ) {
        /* ./epam authmodule user passwd */
        printf("testing service=%s u=%s pwd=%s\n", argv[1],argv[2], argv[3]);
        do_auth(argv[1], argv[2], argv[3], "AS", 33);
        exit(0);
    }
    wstart();
    while (1) {
        if (read_fill(0, lb, 2) != 2)
            exit(1);
        rval = get_int16(lb);
        if (read_fill(0, buf, rval) != rval)
            exit(1);
        switch (buf[0]) {
        case 'a': 
            // auth a user
            user = (char *)&buf[1];
            pwd = user + strlen(user) + 1;
            mode= pwd + strlen(pwd) + 1;
            sid = atoi(mode + strlen(mode) + 1);
            
            do_auth(argv[1], user, pwd, mode, sid);
            break;
        case 'c': 
            // close session
            sid = atoi((char *)&buf[1]);
            if ((sessp = del_session(&sessions, sid)) == NULL) {
                fprintf(stderr, "Couldn't find session %d\r\n", sid); 
                break;
            }
            if (sessp->session_mode == 1) {
                pam_close_session(sessp->pamh, 0);
                /*fprintf(stderr, "did ok close sess \n\r");*/
            }
            pam_end(sessp->pamh, PAM_SUCCESS); 
            free(sessp);
            break;
        default:
            fprintf(stderr, "Bad op \n\r");
        }
    }
}
Пример #2
0
void stop_cli(EpmdVars *g, char *name)
{
    char buf[1024];
    int fd, rval, bsize;

    bsize = strlen(name);
    if (bsize > 1000) {
	printf("epmd: Name too long!");
	epmd_cleanup_exit(g, 1);
    }

    fd = conn_to_epmd(g);
    bsize++;
    put_int16(bsize, buf);
    buf[2] = EPMD_STOP_REQ;
    bsize += 2;
    strcpy(buf+3, name);

    if (write(fd, buf, bsize) != bsize) {
	printf("epmd: Can't write to epmd\n");
	epmd_cleanup_exit(g,1);
    }
    if ((rval = read_fill(fd,buf,7)) == 7) {
	buf[7] = '\000';
	printf("%s\n", buf);
	epmd_cleanup_exit(g,0);
    } else if (rval < 0) {
	printf("epmd: failed to read answer from local epmd\n");
	epmd_cleanup_exit(g,1);
    } else { 			/* rval is now 0 or 1 */
	buf[rval] = '\0';
	printf("epmd: local epmd responded with <%s>\n", buf);
	epmd_cleanup_exit(g,1);
    }
}
Пример #3
0
void kill_epmd(EpmdVars *g)
{
    char buf[5];
    int fd, rval;

    fd = conn_to_epmd(g);
    put_int16(1,buf);
    buf[2] = EPMD_KILL_REQ;
    if (write(fd, buf, 3) != 3) {
	printf("epmd: Can't write to epmd\n");
	epmd_cleanup_exit(g,1);
    }
    if ((rval = read_fill(fd,buf,2)) == 2) {
	if (buf[0] == 'O' && buf[1] == 'K') {
	    printf("Killed\n");
	} else {
	    printf("Killing not allowed - living nodes in database.\n");
	}
	epmd_cleanup_exit(g,0);
    } else if (rval < 0) {
	printf("epmd: failed to read answer from local epmd\n");
	epmd_cleanup_exit(g,1);
    } else { 			/* rval is now 0 or 1 */
	buf[rval] = '\0';
	printf("epmd: local epmd responded with <%s>\n", buf);
	epmd_cleanup_exit(g,1);
    }
}
Пример #4
0
int ossp_slave_process_command(int cmd_fd,
			       ossp_action_fn_t const *action_fn_tbl,
			       int (*action_pre_fn)(void),
			       void (*action_post_fn)(void))
{
	static struct sized_buf carg_sbuf = { }, rarg_sbuf = { };
	static struct sized_buf din_sbuf = { }, dout_sbuf = { };
	struct ossp_cmd cmd;
	int fd = -1;
	char cmsg_buf[CMSG_SPACE(sizeof(fd))];
	struct iovec iov = { &cmd, sizeof(cmd) };
	struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1,
			      .msg_control = cmsg_buf,
			      .msg_controllen = sizeof(cmsg_buf) };
	struct cmsghdr *cmsg;
	size_t carg_size, din_size, rarg_size, dout_size;
	char *carg = NULL, *din = NULL, *rarg = NULL, *dout = NULL;
	struct ossp_reply reply = { .magic = OSSP_REPLY_MAGIC };
	ssize_t ret;

	ret = recvmsg(cmd_fd, &msg, 0);
	if (ret == 0)
		return 0;
	if (ret < 0) {
		ret = -errno;
		err_e(ret, "failed to read command channel");
		return ret;
	}

	if (ret != sizeof(cmd)) {
		err("command struct size mismatch (%zu, should be %zu)",
		    ret, sizeof(cmd));
		return -EINVAL;
	}

	if (cmd.magic != OSSP_CMD_MAGIC) {
		err("illegal command magic 0x%x", cmd.magic);
		return -EINVAL;
	}

	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
	     cmsg = CMSG_NXTHDR(&msg, cmsg)) {
		if (cmsg->cmsg_level == SOL_SOCKET &&
		    cmsg->cmsg_type == SCM_RIGHTS)
			fd = *(int *)CMSG_DATA(cmsg);
		else {
			err("unknown cmsg %d:%d received (opcode %d)",
			    cmsg->cmsg_level, cmsg->cmsg_type, cmd.opcode);
			return -EINVAL;
		}
	}

	if (cmd.opcode >= OSSP_NR_OPCODES) {
		err("unknown opcode %d", cmd.opcode);
		return -EINVAL;
	}

	carg_size = ossp_arg_sizes[cmd.opcode].carg_size;
	din_size = cmd.din_size;
	rarg_size = ossp_arg_sizes[cmd.opcode].rarg_size;
	dout_size = cmd.dout_size;

	if ((fd >= 0) != ossp_arg_sizes[cmd.opcode].has_fd) {
		err("fd=%d unexpected for opcode %d", fd, cmd.opcode);
		return -EINVAL;
	}

	if (ensure_sbuf_size(&carg_sbuf, carg_size) ||
	    ensure_sbuf_size(&din_sbuf, din_size) ||
	    ensure_sbuf_size(&rarg_sbuf, rarg_size) ||
	    ensure_sbuf_size(&dout_sbuf, dout_size)) {
		err("failed to allocate command buffers");
		return -ENOMEM;
	}

	if (carg_size) {
		carg = carg_sbuf.buf;
		ret = read_fill(cmd_fd, carg, carg_size);
		if (ret < 0)
			return ret;
	}
	if (din_size) {
		din = din_sbuf.buf;
		ret = read_fill(cmd_fd, din, din_size);
		if (ret < 0)
			return ret;
	}
	if (rarg_size)
		rarg = rarg_sbuf.buf;
	if (dout_size)
		dout = dout_sbuf.buf;

	ret = -EINVAL;
	if (action_fn_tbl[cmd.opcode]) {
		ret = action_pre_fn();
		if (ret == 0) {
			ret = action_fn_tbl[cmd.opcode](cmd.opcode, carg,
							din, din_size, rarg,
							dout, &dout_size, fd);
			action_post_fn();
		}
	}

	reply.result = ret;
	if (ret >= 0)
		reply.dout_size = dout_size;
	else {
		rarg_size = 0;
		dout_size = 0;
	}

	if (write_fill(cmd_fd, &reply, sizeof(reply)) < 0 ||
	    write_fill(cmd_fd, rarg, rarg_size) < 0 ||
	    write_fill(cmd_fd, dout, dout_size) < 0)
		return -EIO;

	return 1;
}