Annotation of src/usr.bin/tmux/cmd-set-password.c, Revision 1.5
1.5 ! nicm 1: /* $OpenBSD: cmd-set-password.c,v 1.4 2009/07/26 12:58:44 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_free(struct cmd *);
33: void cmd_set_password_init(struct cmd *, int);
34: size_t cmd_set_password_print(struct cmd *, char *, size_t);
35:
36: struct cmd_set_password_data {
37: char *password;
38: int flag_encrypted;
39: };
40:
41: const struct cmd_entry cmd_set_password_entry = {
42: "set-password", "pass",
43: "[-c] password",
1.2 nicm 44: 0, 0,
1.1 nicm 45: cmd_set_password_init,
46: cmd_set_password_parse,
47: cmd_set_password_exec,
48: cmd_set_password_free,
49: cmd_set_password_print
50: };
51:
52: void
53: cmd_set_password_init(struct cmd *self, unused int arg)
54: {
55: struct cmd_set_password_data *data;
56:
57: self->data = data = xmalloc(sizeof *data);
58: data->password = NULL;
59: data->flag_encrypted = 0;
60: }
61:
62: int
63: cmd_set_password_parse(struct cmd *self, int argc, char **argv, char **cause)
64: {
65: struct cmd_set_password_data *data;
66: int opt;
67: char *out;
68:
1.5 ! nicm 69: self->entry->init(self, KEYC_NONE);
1.1 nicm 70: data = self->data;
71:
72: while ((opt = getopt(argc, argv, "c")) != -1) {
73: switch (opt) {
74: case 'c':
75: data->flag_encrypted = 1;
76: break;
77: default:
78: goto usage;
79: }
80: }
81: argc -= optind;
82: argv += optind;
83: if (argc != 1)
84: goto usage;
85:
86: if (!data->flag_encrypted) {
87: if ((out = crypt(argv[0], "$1")) != NULL)
88: data->password = xstrdup(out);
89: } else
90: data->password = xstrdup(argv[0]);
91:
92: return (0);
93:
94: usage:
95: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
96:
97: self->entry->free(self);
98: return (-1);
99: }
100:
101: int
102: cmd_set_password_exec(struct cmd *self, struct cmd_ctx *ctx)
103: {
104: struct cmd_set_password_data *data = self->data;
105:
106: if (data->password == NULL) {
107: ctx->error(ctx, "failed to encrypt password");
108: return (-1);
109: }
110:
111: if (server_password != NULL)
112: xfree(server_password);
113: if (*data->password == '\0')
114: server_password = NULL;
115: else
116: server_password = xstrdup(data->password);
117:
118: return (0);
119: }
120:
121: void
122: cmd_set_password_free(struct cmd *self)
123: {
124: struct cmd_set_password_data *data = self->data;
125:
126: if (data->password != NULL)
127: xfree(data->password);
128: xfree(data);
129: }
130:
131: size_t
132: cmd_set_password_print(struct cmd *self, char *buf, size_t len)
133: {
134: struct cmd_set_password_data *data = self->data;
135: size_t off = 0;
136:
137: off += xsnprintf(buf, len, "%s", self->entry->name);
138: if (data == NULL)
139: return (off);
140: if (off < len && data->flag_encrypted)
141: off += xsnprintf(buf + off, len - off, " -c");
142: if (off < len && data->password != NULL)
143: off += xsnprintf(buf + off, len - off, " password");
144: return (off);
145: }