Damien Miller | e00e413 | 2014-01-10 10:40:45 +1100 | [diff] [blame] | 1 | /* $OpenBSD: sftp-common.c,v 1.26 2014/01/09 03:26:00 guenther 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 | |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 31 | #include <sys/param.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" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 45 | #include "buffer.h" |
| 46 | #include "log.h" |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 47 | |
| 48 | #include "sftp.h" |
| 49 | #include "sftp-common.h" |
| 50 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 51 | /* Clear contents of attributes structure */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 52 | void |
| 53 | attrib_clear(Attrib *a) |
| 54 | { |
| 55 | a->flags = 0; |
| 56 | a->size = 0; |
| 57 | a->uid = 0; |
| 58 | a->gid = 0; |
| 59 | a->perm = 0; |
| 60 | a->atime = 0; |
| 61 | a->mtime = 0; |
| 62 | } |
| 63 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 64 | /* Convert from struct stat to filexfer attribs */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 65 | void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 66 | stat_to_attrib(const struct stat *st, Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 67 | { |
| 68 | attrib_clear(a); |
| 69 | a->flags = 0; |
| 70 | a->flags |= SSH2_FILEXFER_ATTR_SIZE; |
| 71 | a->size = st->st_size; |
| 72 | a->flags |= SSH2_FILEXFER_ATTR_UIDGID; |
| 73 | a->uid = st->st_uid; |
| 74 | a->gid = st->st_gid; |
| 75 | a->flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; |
| 76 | a->perm = st->st_mode; |
| 77 | a->flags |= SSH2_FILEXFER_ATTR_ACMODTIME; |
| 78 | a->atime = st->st_atime; |
| 79 | a->mtime = st->st_mtime; |
| 80 | } |
| 81 | |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 82 | /* Convert from filexfer attribs to struct stat */ |
| 83 | void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 84 | attrib_to_stat(const Attrib *a, struct stat *st) |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 85 | { |
| 86 | memset(st, 0, sizeof(*st)); |
| 87 | |
| 88 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 89 | st->st_size = a->size; |
| 90 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 91 | st->st_uid = a->uid; |
| 92 | st->st_gid = a->gid; |
| 93 | } |
| 94 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 95 | st->st_mode = a->perm; |
| 96 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 97 | st->st_atime = a->atime; |
| 98 | st->st_mtime = a->mtime; |
| 99 | } |
| 100 | } |
| 101 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 102 | /* Decode attributes in buffer */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 103 | Attrib * |
| 104 | decode_attrib(Buffer *b) |
| 105 | { |
| 106 | static Attrib a; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 107 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 108 | attrib_clear(&a); |
| 109 | a.flags = buffer_get_int(b); |
| 110 | if (a.flags & SSH2_FILEXFER_ATTR_SIZE) |
| 111 | a.size = buffer_get_int64(b); |
| 112 | if (a.flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 113 | a.uid = buffer_get_int(b); |
| 114 | a.gid = buffer_get_int(b); |
| 115 | } |
| 116 | if (a.flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 117 | a.perm = buffer_get_int(b); |
| 118 | if (a.flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 119 | a.atime = buffer_get_int(b); |
| 120 | a.mtime = buffer_get_int(b); |
| 121 | } |
| 122 | /* vendor-specific extensions */ |
| 123 | if (a.flags & SSH2_FILEXFER_ATTR_EXTENDED) { |
| 124 | char *type, *data; |
| 125 | int i, count; |
Ben Lindstrom | b1f483f | 2002-06-23 21:27:18 +0000 | [diff] [blame] | 126 | |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 127 | count = buffer_get_int(b); |
| 128 | for (i = 0; i < count; i++) { |
| 129 | type = buffer_get_string(b, NULL); |
| 130 | data = buffer_get_string(b, NULL); |
| 131 | debug3("Got file attribute \"%s\"", type); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 132 | free(type); |
| 133 | free(data); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | return &a; |
| 137 | } |
| 138 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 139 | /* Encode attributes to buffer */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 140 | void |
Damien Miller | f58b58c | 2003-11-17 21:18:23 +1100 | [diff] [blame] | 141 | encode_attrib(Buffer *b, const Attrib *a) |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 142 | { |
| 143 | buffer_put_int(b, a->flags); |
| 144 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) |
| 145 | buffer_put_int64(b, a->size); |
| 146 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) { |
| 147 | buffer_put_int(b, a->uid); |
| 148 | buffer_put_int(b, a->gid); |
| 149 | } |
| 150 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) |
| 151 | buffer_put_int(b, a->perm); |
| 152 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) { |
| 153 | buffer_put_int(b, a->atime); |
| 154 | buffer_put_int(b, a->mtime); |
| 155 | } |
| 156 | } |
| 157 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 158 | /* Convert from SSH2_FX_ status to text error message */ |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 159 | const char * |
| 160 | fx2txt(int status) |
| 161 | { |
| 162 | switch (status) { |
| 163 | case SSH2_FX_OK: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 164 | return("No error"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 165 | case SSH2_FX_EOF: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 166 | return("End of file"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 167 | case SSH2_FX_NO_SUCH_FILE: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 168 | return("No such file or directory"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 169 | case SSH2_FX_PERMISSION_DENIED: |
Damien Miller | d7686fd | 2001-02-10 00:40:03 +1100 | [diff] [blame] | 170 | return("Permission denied"); |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 171 | case SSH2_FX_FAILURE: |
| 172 | return("Failure"); |
| 173 | case SSH2_FX_BAD_MESSAGE: |
| 174 | return("Bad message"); |
| 175 | case SSH2_FX_NO_CONNECTION: |
| 176 | return("No connection"); |
| 177 | case SSH2_FX_CONNECTION_LOST: |
| 178 | return("Connection lost"); |
| 179 | case SSH2_FX_OP_UNSUPPORTED: |
| 180 | return("Operation unsupported"); |
| 181 | default: |
| 182 | return("Unknown status"); |
Ben Lindstrom | d05487d | 2001-12-06 17:50:03 +0000 | [diff] [blame] | 183 | } |
Damien Miller | 3380426 | 2001-02-04 23:20:18 +1100 | [diff] [blame] | 184 | /* NOTREACHED */ |
| 185 | } |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh |
| 189 | */ |
| 190 | char * |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 191 | 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] | 192 | { |
| 193 | int ulen, glen, sz = 0; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 194 | struct tm *ltime = localtime(&st->st_mtime); |
| 195 | char *user, *group; |
| 196 | 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] | 197 | char sbuf[FMT_SCALED_STRSIZE]; |
Damien Miller | e00e413 | 2014-01-10 10:40:45 +1100 | [diff] [blame] | 198 | time_t now; |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 199 | |
| 200 | strmode(st->st_mode, mode); |
Darren Tucker | a788de2 | 2010-01-15 11:45:33 +1100 | [diff] [blame] | 201 | if (!remote) { |
| 202 | user = user_from_uid(st->st_uid, 0); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 203 | } else { |
| 204 | snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); |
| 205 | user = ubuf; |
| 206 | } |
Darren Tucker | a788de2 | 2010-01-15 11:45:33 +1100 | [diff] [blame] | 207 | if (!remote) { |
| 208 | group = group_from_gid(st->st_gid, 0); |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 209 | } else { |
| 210 | snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); |
| 211 | group = gbuf; |
| 212 | } |
| 213 | if (ltime != NULL) { |
Damien Miller | e00e413 | 2014-01-10 10:40:45 +1100 | [diff] [blame] | 214 | now = time(NULL); |
| 215 | if (now - (365*24*60*60)/2 < st->st_mtime && |
| 216 | now >= st->st_mtime) |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 217 | sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); |
| 218 | else |
| 219 | sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); |
| 220 | } |
| 221 | if (sz == 0) |
| 222 | tbuf[0] = '\0'; |
| 223 | ulen = MAX(strlen(user), 8); |
| 224 | glen = MAX(strlen(group), 8); |
Darren Tucker | 2901e2d | 2010-01-13 22:44:06 +1100 | [diff] [blame] | 225 | if (si_units) { |
| 226 | fmt_scaled((long long)st->st_size, sbuf); |
| 227 | snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, |
| 228 | (u_int)st->st_nlink, ulen, user, glen, group, |
| 229 | sbuf, tbuf, name); |
| 230 | } else { |
| 231 | snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, |
| 232 | (u_int)st->st_nlink, ulen, user, glen, group, |
| 233 | (unsigned long long)st->st_size, tbuf, name); |
| 234 | } |
Damien Miller | e1a4981 | 2002-09-12 09:54:25 +1000 | [diff] [blame] | 235 | return xstrdup(buf); |
| 236 | } |