Annotation of src/usr.bin/tmux/cmd-set-password.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
! 5: *
! 6: * Permission to use, copy, modify, and distribute this software for any
! 7: * purpose with or without fee is hereby granted, provided that the above
! 8: * copyright notice and this permission notice appear in all copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
! 15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
! 16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 17: */
! 18:
! 19: #include <sys/types.h>
! 20:
! 21: #include <pwd.h>
! 22: #include <unistd.h>
! 23:
! 24: #include "tmux.h"
! 25:
! 26: /*
! 27: * Set server password.
! 28: */
! 29:
! 30: int cmd_set_password_parse(struct cmd *, int, char **, char **);
! 31: int cmd_set_password_exec(struct cmd *, struct cmd_ctx *);
! 32: void cmd_set_password_send(struct cmd *, struct buffer *);
! 33: void cmd_set_password_recv(struct cmd *, struct buffer *);
! 34: void cmd_set_password_free(struct cmd *);
! 35: void cmd_set_password_init(struct cmd *, int);
! 36: size_t cmd_set_password_print(struct cmd *, char *, size_t);
! 37:
! 38: struct cmd_set_password_data {
! 39: char *password;
! 40: int flag_encrypted;
! 41: };
! 42:
! 43: const struct cmd_entry cmd_set_password_entry = {
! 44: "set-password", "pass",
! 45: "[-c] password",
! 46: 0,
! 47: cmd_set_password_init,
! 48: cmd_set_password_parse,
! 49: cmd_set_password_exec,
! 50: cmd_set_password_send,
! 51: cmd_set_password_recv,
! 52: cmd_set_password_free,
! 53: cmd_set_password_print
! 54: };
! 55:
! 56: void
! 57: cmd_set_password_init(struct cmd *self, unused int arg)
! 58: {
! 59: struct cmd_set_password_data *data;
! 60:
! 61: self->data = data = xmalloc(sizeof *data);
! 62: data->password = NULL;
! 63: data->flag_encrypted = 0;
! 64: }
! 65:
! 66: int
! 67: cmd_set_password_parse(struct cmd *self, int argc, char **argv, char **cause)
! 68: {
! 69: struct cmd_set_password_data *data;
! 70: int opt;
! 71: char *out;
! 72:
! 73: self->entry->init(self, 0);
! 74: data = self->data;
! 75:
! 76: while ((opt = getopt(argc, argv, "c")) != -1) {
! 77: switch (opt) {
! 78: case 'c':
! 79: data->flag_encrypted = 1;
! 80: break;
! 81: default:
! 82: goto usage;
! 83: }
! 84: }
! 85: argc -= optind;
! 86: argv += optind;
! 87: if (argc != 1)
! 88: goto usage;
! 89:
! 90: if (!data->flag_encrypted) {
! 91: if ((out = crypt(argv[0], "$1")) != NULL)
! 92: data->password = xstrdup(out);
! 93: } else
! 94: data->password = xstrdup(argv[0]);
! 95:
! 96: return (0);
! 97:
! 98: usage:
! 99: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
! 100:
! 101: self->entry->free(self);
! 102: return (-1);
! 103: }
! 104:
! 105: int
! 106: cmd_set_password_exec(struct cmd *self, struct cmd_ctx *ctx)
! 107: {
! 108: struct cmd_set_password_data *data = self->data;
! 109:
! 110: if (data->password == NULL) {
! 111: ctx->error(ctx, "failed to encrypt password");
! 112: return (-1);
! 113: }
! 114:
! 115: if (server_password != NULL)
! 116: xfree(server_password);
! 117: if (*data->password == '\0')
! 118: server_password = NULL;
! 119: else
! 120: server_password = xstrdup(data->password);
! 121: log_debug("pw now %s", server_password);
! 122:
! 123: return (0);
! 124: }
! 125:
! 126: void
! 127: cmd_set_password_send(struct cmd *self, struct buffer *b)
! 128: {
! 129: struct cmd_set_password_data *data = self->data;
! 130:
! 131: buffer_write(b, data, sizeof *data);
! 132: cmd_send_string(b, data->password);
! 133: }
! 134:
! 135: void
! 136: cmd_set_password_recv(struct cmd *self, struct buffer *b)
! 137: {
! 138: struct cmd_set_password_data *data;
! 139:
! 140: self->data = data = xmalloc(sizeof *data);
! 141: buffer_read(b, data, sizeof *data);
! 142: data->password = cmd_recv_string(b);
! 143: }
! 144:
! 145: void
! 146: cmd_set_password_free(struct cmd *self)
! 147: {
! 148: struct cmd_set_password_data *data = self->data;
! 149:
! 150: if (data->password != NULL)
! 151: xfree(data->password);
! 152: xfree(data);
! 153: }
! 154:
! 155: size_t
! 156: cmd_set_password_print(struct cmd *self, char *buf, size_t len)
! 157: {
! 158: struct cmd_set_password_data *data = self->data;
! 159: size_t off = 0;
! 160:
! 161: off += xsnprintf(buf, len, "%s", self->entry->name);
! 162: if (data == NULL)
! 163: return (off);
! 164: if (off < len && data->flag_encrypted)
! 165: off += xsnprintf(buf + off, len - off, " -c");
! 166: if (off < len && data->password != NULL)
! 167: off += xsnprintf(buf + off, len - off, " password");
! 168: return (off);
! 169: }