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