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