[BACK]Return to cmd-set-password.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-set-password.c, Revision 1.3

1.3     ! nicm        1: /* $OpenBSD: cmd-set-password.c,v 1.2 2009/07/13 23:11:35 nicm Exp $ */
1.1       nicm        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",
1.2       nicm       46:        0, 0,
1.1       nicm       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:
                    122:        return (0);
                    123: }
                    124:
                    125: void
                    126: cmd_set_password_send(struct cmd *self, struct buffer *b)
                    127: {
                    128:        struct cmd_set_password_data    *data = self->data;
                    129:
                    130:        buffer_write(b, data, sizeof *data);
                    131:        cmd_send_string(b, data->password);
                    132: }
                    133:
                    134: void
                    135: cmd_set_password_recv(struct cmd *self, struct buffer *b)
                    136: {
                    137:        struct cmd_set_password_data    *data;
                    138:
                    139:        self->data = data = xmalloc(sizeof *data);
                    140:        buffer_read(b, data, sizeof *data);
                    141:        data->password = cmd_recv_string(b);
                    142: }
                    143:
                    144: void
                    145: cmd_set_password_free(struct cmd *self)
                    146: {
                    147:        struct cmd_set_password_data    *data = self->data;
                    148:
                    149:        if (data->password != NULL)
                    150:                xfree(data->password);
                    151:        xfree(data);
                    152: }
                    153:
                    154: size_t
                    155: cmd_set_password_print(struct cmd *self, char *buf, size_t len)
                    156: {
                    157:        struct cmd_set_password_data    *data = self->data;
                    158:        size_t                           off = 0;
                    159:
                    160:        off += xsnprintf(buf, len, "%s", self->entry->name);
                    161:        if (data == NULL)
                    162:                return (off);
                    163:        if (off < len && data->flag_encrypted)
                    164:                off += xsnprintf(buf + off, len - off, " -c");
                    165:        if (off < len && data->password != NULL)
                    166:                off += xsnprintf(buf + off, len - off, " password");
                    167:        return (off);
                    168: }