static int write_string (char *s, int just, int m) { int status; int l = strlen(s); int pre; /* number of blanks before string */ if (m < 0) m = l; else if (l > m) { write_fill(m, '*'); return 0; } pre = (just == '<') ? 0 : (just == '>') ? m-l : (m-l)/2; status = write_fill(pre, ' '); status = status && fprintf(out, "%s", s) >= 0; status = status && write_fill(m-(l+pre), ' '); return status; }
static void wstart() { char buf[5]; sprintf(&buf[2], "ok"); put_int16(2, &buf[0]); if (write_fill(1, buf, 4) != 4) { exit(1); } }
static void wok(int sid) { char buf[BUFSIZ]; int len; sprintf(&buf[2], "pam %d yes", sid); len = strlen(&buf[2]); put_int16(len, &buf[0]); if (write_fill(1, buf, len+2) != len+2) exit(1); }
static void werr(pam_handle_t *pamh, int sid, int ecode, char *phase) { char buf[BUFSIZ]; int len; sprintf(&buf[2], "pam %d no %s %s", sid, phase, pam_strerror(pamh, ecode)); len = strlen(&buf[2]); put_int16(len, &buf[0]); if (write_fill(1, buf, len+2) != len+2) exit(1); }
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; }