deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sftp-common.c,v 1.28 2015/01/20 23:14:00 deraadt Exp $ */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001 Markus Friedl. All rights reserved. |
| 4 | * Copyright (c) 2001 Damien Miller. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 28 | |
deraadt@openbsd.org | 087266e | 2015-01-20 23:14:00 +0000 | [diff] [blame] | 29 | #include <sys/param.h> /* MAX */ |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 32 | |
Damien Miller | 427a1d5 | 2006-07-10 20:20:33 +1000 | [diff] [blame] | 33 | #include <grp.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 34 | #include <pwd.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 35 | #include <stdio.h> |
Damien Miller | 0600c70 | 2013-11-21 13:55:43 +1100 | [diff] [blame] | 36 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 37 | #include <string.h> |
Damien Miller | 5598b4f | 2006-07-24 14:09:40 +1000 | [diff] [blame] | 38 | #include <time.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | #include <stdarg.h> |
Darren Tucker | 096630d | 2010-01-13 23:00:38 +1100 | [diff] [blame] | 40 | #ifdef HAVE_UTIL_H |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 41 | #include <util.h> |
Darren Tucker | 096630d | 2010-01-13 23:00:38 +1100 | [diff] [blame] | 42 | #endif |
Damien Miller | 427a1d5 | 2006-07-10 20:20:33 +1000 | [diff] [blame] | 43 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 44 | #include "xmalloc.h" |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 45 | #include "ssherr.h" |
| 46 | #include "sshbuf.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 47 | #include "log.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 48 | |
| 49 | #include "sftp.h" |
| 50 | #include "sftp-common.h" |
| 51 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 52 | /* Clear contents of attributes structure */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 53 | void |
| 54 | attrib_clear(Attrib *a) |
| 55 | { |
| 56 | a->flags = 0; |
| 57 | a->size = 0; |
| 58 | a->uid = 0; |
| 59 | a->gid = 0; |
| 60 | a->perm = 0; |
| 61 | a->atime = 0; |
| 62 | a->mtime = 0; |
| 63 | } |
| 64 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 65 | /* Convert from struct stat to filexfer attribs */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 66 | void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 67 | stat_to_attrib(const struct stat *st, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 68 | { |
| 69 | attrib_clear(a); |
| 70 | a->flags = 0; |
| 71 | a->flags |= SSH2_FILEXFER_ATTR_SIZE; |
| 72 | a->size = st->st_size; |
| 73 | a->flags |= SSH2_FILEXFER_ATTR_UIDGID; |
| 74 | a->uid = st->st_uid; |
| 75 | a->gid = st->st_gid; |
| 76 | a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 77 | a->perm = st->st_mode; |
| 78 | a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME; |
| 79 | a->atime = st->st_atime; |
| 80 | a->mtime = st->st_mtime; |
| 81 | } |
| 82 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 83 | /* Convert from filexfer attribs to struct stat */ |
| 84 | void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 85 | attrib_to_stat(const Attrib *a, struct stat *st) |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 86 | { |
| 87 | memset(st, 0, sizeof(*st)); |
| 88 | |
| 89 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 90 | st->st_size = a->size; |
| 91 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 92 | st->st_uid = a->uid; |
| 93 | st->st_gid = a->gid; |
| 94 | } |
| 95 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 96 | st->st_mode = a->perm; |
| 97 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 98 | st->st_atime = a->atime; |
| 99 | st->st_mtime = a->mtime; |
| 100 | } |
| 101 | } |
| 102 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 103 | /* Decode attributes in buffer */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 104 | int |
| 105 | decode_attrib(struct sshbuf *b, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 106 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 107 | int r; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 108 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 109 | attrib_clear(a); |
| 110 | if ((r = sshbuf_get_u32(b, &a->flags)) != 0) |
| 111 | return r; |
| 112 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
| 113 | if ((r = sshbuf_get_u64(b, &a->size)) != 0) |
| 114 | return r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 115 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 116 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 117 | if ((r = sshbuf_get_u32(b, &a->uid)) != 0 || |
| 118 | (r = sshbuf_get_u32(b, &a->gid)) != 0) |
| 119 | return r; |
| 120 | } |
| 121 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
| 122 | if ((r = sshbuf_get_u32(b, &a->perm)) != 0) |
| 123 | return r; |
| 124 | } |
| 125 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 126 | if ((r = sshbuf_get_u32(b, &a->atime)) != 0 || |
| 127 | (r = sshbuf_get_u32(b, &a->mtime)) != 0) |
| 128 | return r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 129 | } |
| 130 | /* vendor-specific extensions */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 131 | if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) { |
| 132 | char *type; |
| 133 | u_char *data; |
| 134 | size_t dlen; |
| 135 | u_int i, count; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 136 | |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 137 | if ((r = sshbuf_get_u32(b, &count)) != 0) |
| 138 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 139 | for (i = 0; i < count; i++) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 140 | if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 || |
| 141 | (r = sshbuf_get_string(b, &data, &dlen)) != 0) |
| 142 | return r; |
| 143 | debug3("Got file attribute \"%.100s\" len %zu", |
| 144 | type, dlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 145 | free(type); |
| 146 | free(data); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 147 | } |
| 148 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 149 | return 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 150 | } |
| 151 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 152 | /* Encode attributes to buffer */ |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 153 | int |
| 154 | encode_attrib(struct sshbuf *b, const Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 155 | { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 156 | int r; |
| 157 | |
| 158 | if ((r = sshbuf_put_u32(b, a->flags)) != 0) |
| 159 | return r; |
| 160 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) { |
| 161 | if ((r = sshbuf_put_u64(b, a->size)) != 0) |
| 162 | return r; |
| 163 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 164 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 165 | if ((r = sshbuf_put_u32(b, a->uid)) != 0 || |
| 166 | (r = sshbuf_put_u32(b, a->gid)) != 0) |
| 167 | return r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 168 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 169 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) { |
| 170 | if ((r = sshbuf_put_u32(b, a->perm)) != 0) |
| 171 | return r; |
| 172 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 173 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 174 | if ((r = sshbuf_put_u32(b, a->atime)) != 0 || |
| 175 | (r = sshbuf_put_u32(b, a->mtime)) != 0) |
| 176 | return r; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 177 | } |
djm@openbsd.org | 7d845f4 | 2015-01-14 13:54:13 +0000 | [diff] [blame] | 178 | return 0; |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 179 | } |
| 180 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 181 | /* Convert from SSH2_FX_ status to text error message */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 182 | const char * |
| 183 | fx2txt(int status) |
| 184 | { |
| 185 | switch (status) { |
| 186 | case SSH2_FX_OK: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 187 | return("No error"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 188 | case SSH2_FX_EOF: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 189 | return("End of file"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 190 | case SSH2_FX_NO_SUCH_FILE: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 191 | return("No such file or directory"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 192 | case SSH2_FX_PERMISSION_DENIED: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 193 | return("Permission denied"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 194 | case SSH2_FX_FAILURE: |
| 195 | return("Failure"); |
| 196 | case SSH2_FX_BAD_MESSAGE: |
| 197 | return("Bad message"); |
| 198 | case SSH2_FX_NO_CONNECTION: |
| 199 | return("No connection"); |
| 200 | case SSH2_FX_CONNECTION_LOST: |
| 201 | return("Connection lost"); |
| 202 | case SSH2_FX_OP_UNSUPPORTED: |
| 203 | return("Operation unsupported"); |
| 204 | default: |
| 205 | return("Unknown status"); |
Ben Lindstrom | d05487d | 2001-12-06 17:50:03 +0000 | [diff] [blame] | 206 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 207 | /* NOTREACHED */ |
| 208 | } |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh |
| 212 | */ |
| 213 | char * |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 214 | ls_file(const char *name, const struct stat *st, int remote, int si_units) |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 215 | { |
| 216 | int ulen, glen, sz = 0; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 217 | struct tm *ltime = localtime(&st->st_mtime); |
| 218 | char *user, *group; |
| 219 | char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 220 | char sbuf[FMT_SCALED_STRSIZE]; |
Damien Miller | e00e413 | 2014-01-10 10:40:45 +1100 | [diff] [blame] | 221 | time_t now; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 222 | |
| 223 | strmode(st->st_mode, mode); |
Darren Tucker | a788de2 | 2010-01-15 11:45:33 +1100 | [diff] [blame] | 224 | if (!remote) { |
| 225 | user = user_from_uid(st->st_uid, 0); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 226 | } else { |
| 227 | snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); |
| 228 | user = ubuf; |
| 229 | } |
Darren Tucker | a788de2 | 2010-01-15 11:45:33 +1100 | [diff] [blame] | 230 | if (!remote) { |
| 231 | group = group_from_gid(st->st_gid, 0); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 232 | } else { |
| 233 | snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); |
| 234 | group = gbuf; |
| 235 | } |
| 236 | if (ltime != NULL) { |
Damien Miller | e00e413 | 2014-01-10 10:40:45 +1100 | [diff] [blame] | 237 | now = time(NULL); |
| 238 | if (now - (365*24*60*60)/2 < st->st_mtime && |
| 239 | now >= st->st_mtime) |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 240 | sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); |
| 241 | else |
| 242 | sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); |
| 243 | } |
| 244 | if (sz == 0) |
| 245 | tbuf[0] = '\0'; |
| 246 | ulen = MAX(strlen(user), 8); |
| 247 | glen = MAX(strlen(group), 8); |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 248 | if (si_units) { |
| 249 | fmt_scaled((long long)st->st_size, sbuf); |
| 250 | snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, |
| 251 | (u_int)st->st_nlink, ulen, user, glen, group, |
| 252 | sbuf, tbuf, name); |
| 253 | } else { |
| 254 | snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, |
| 255 | (u_int)st->st_nlink, ulen, user, glen, group, |
| 256 | (unsigned long long)st->st_size, tbuf, name); |
| 257 | } |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 258 | return xstrdup(buf); |
| 259 | } |