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