| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssh/src/sftp-common.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /* $OpenBSD: sftp-common.c,v 1.31 2018/09/13 15:23:32 millert Exp $ */ | - | ||||||
| 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" | - | ||||||
| 28 | - | |||||||
| 29 | #include <sys/types.h> | - | ||||||
| 30 | #include <sys/stat.h> | - | ||||||
| 31 | - | |||||||
| 32 | #include <grp.h> | - | ||||||
| 33 | #include <pwd.h> | - | ||||||
| 34 | #include <stdio.h> | - | ||||||
| 35 | #include <stdlib.h> | - | ||||||
| 36 | #include <string.h> | - | ||||||
| 37 | #include <time.h> | - | ||||||
| 38 | #include <stdarg.h> | - | ||||||
| 39 | #ifdef HAVE_UTIL_H | - | ||||||
| 40 | #include <util.h> | - | ||||||
| 41 | #endif | - | ||||||
| 42 | - | |||||||
| 43 | #include "xmalloc.h" | - | ||||||
| 44 | #include "ssherr.h" | - | ||||||
| 45 | #include "sshbuf.h" | - | ||||||
| 46 | #include "log.h" | - | ||||||
| 47 | #include "misc.h" | - | ||||||
| 48 | - | |||||||
| 49 | #include "sftp.h" | - | ||||||
| 50 | #include "sftp-common.h" | - | ||||||
| 51 | - | |||||||
| 52 | /* Clear contents of attributes structure */ | - | ||||||
| 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 | } never executed: end of block | 0 | ||||||
| 64 | - | |||||||
| 65 | /* Convert from struct stat to filexfer attribs */ | - | ||||||
| 66 | void | - | ||||||
| 67 | stat_to_attrib(const struct stat *st, Attrib *a) | - | ||||||
| 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 | } never executed: end of block | 0 | ||||||
| 82 | - | |||||||
| 83 | /* Convert from filexfer attribs to struct stat */ | - | ||||||
| 84 | void | - | ||||||
| 85 | attrib_to_stat(const Attrib *a, struct stat *st) | - | ||||||
| 86 | { | - | ||||||
| 87 | memset(st, 0, sizeof(*st)); | - | ||||||
| 88 | - | |||||||
| 89 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
| 0 | ||||||
| 90 | st->st_size = a->size; never executed: st->st_size = a->size; | 0 | ||||||
| 91 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
| 0 | ||||||
| 92 | st->st_uid = a->uid; | - | ||||||
| 93 | st->st_gid = a->gid; | - | ||||||
| 94 | } never executed: end of block | 0 | ||||||
| 95 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
| 0 | ||||||
| 96 | st->st_mode = a->perm; never executed: st->st_mode = a->perm; | 0 | ||||||
| 97 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
| 0 | ||||||
| 98 | st->st_atime = a->atime; | - | ||||||
| 99 | st->st_mtime = a->mtime; | - | ||||||
| 100 | } never executed: end of block | 0 | ||||||
| 101 | } never executed: end of block | 0 | ||||||
| 102 | - | |||||||
| 103 | /* Decode attributes in buffer */ | - | ||||||
| 104 | int | - | ||||||
| 105 | decode_attrib(struct sshbuf *b, Attrib *a) | - | ||||||
| 106 | { | - | ||||||
| 107 | int r; | - | ||||||
| 108 | - | |||||||
| 109 | attrib_clear(a); | - | ||||||
| 110 | if ((r = sshbuf_get_u32(b, &a->flags)) != 0)
| 0 | ||||||
| 111 | return r; never executed: return r; | 0 | ||||||
| 112 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
| 0 | ||||||
| 113 | if ((r = sshbuf_get_u64(b, &a->size)) != 0)
| 0 | ||||||
| 114 | return r; never executed: return r; | 0 | ||||||
| 115 | } never executed: end of block | 0 | ||||||
| 116 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
| 0 | ||||||
| 117 | if ((r = sshbuf_get_u32(b, &a->uid)) != 0 ||
| 0 | ||||||
| 118 | (r = sshbuf_get_u32(b, &a->gid)) != 0)
| 0 | ||||||
| 119 | return r; never executed: return r; | 0 | ||||||
| 120 | } never executed: end of block | 0 | ||||||
| 121 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
| 0 | ||||||
| 122 | if ((r = sshbuf_get_u32(b, &a->perm)) != 0)
| 0 | ||||||
| 123 | return r; never executed: return r; | 0 | ||||||
| 124 | } never executed: end of block | 0 | ||||||
| 125 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
| 0 | ||||||
| 126 | if ((r = sshbuf_get_u32(b, &a->atime)) != 0 ||
| 0 | ||||||
| 127 | (r = sshbuf_get_u32(b, &a->mtime)) != 0)
| 0 | ||||||
| 128 | return r; never executed: return r; | 0 | ||||||
| 129 | } never executed: end of block | 0 | ||||||
| 130 | /* vendor-specific extensions */ | - | ||||||
| 131 | if (a->flags & SSH2_FILEXFER_ATTR_EXTENDED) {
| 0 | ||||||
| 132 | char *type; | - | ||||||
| 133 | u_char *data; | - | ||||||
| 134 | size_t dlen; | - | ||||||
| 135 | u_int i, count; | - | ||||||
| 136 | - | |||||||
| 137 | if ((r = sshbuf_get_u32(b, &count)) != 0)
| 0 | ||||||
| 138 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); never executed: fatal("%s: buffer error: %s", __func__, ssh_err(r)); | 0 | ||||||
| 139 | for (i = 0; i < count; i++) {
| 0 | ||||||
| 140 | if ((r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
| 0 | ||||||
| 141 | (r = sshbuf_get_string(b, &data, &dlen)) != 0)
| 0 | ||||||
| 142 | return r; never executed: return r; | 0 | ||||||
| 143 | debug3("Got file attribute \"%.100s\" len %zu", | - | ||||||
| 144 | type, dlen); | - | ||||||
| 145 | free(type); | - | ||||||
| 146 | free(data); | - | ||||||
| 147 | } never executed: end of block | 0 | ||||||
| 148 | } never executed: end of block | 0 | ||||||
| 149 | return 0; never executed: return 0; | 0 | ||||||
| 150 | } | - | ||||||
| 151 | - | |||||||
| 152 | /* Encode attributes to buffer */ | - | ||||||
| 153 | int | - | ||||||
| 154 | encode_attrib(struct sshbuf *b, const Attrib *a) | - | ||||||
| 155 | { | - | ||||||
| 156 | int r; | - | ||||||
| 157 | - | |||||||
| 158 | if ((r = sshbuf_put_u32(b, a->flags)) != 0)
| 0 | ||||||
| 159 | return r; never executed: return r; | 0 | ||||||
| 160 | if (a->flags & SSH2_FILEXFER_ATTR_SIZE) {
| 0 | ||||||
| 161 | if ((r = sshbuf_put_u64(b, a->size)) != 0)
| 0 | ||||||
| 162 | return r; never executed: return r; | 0 | ||||||
| 163 | } never executed: end of block | 0 | ||||||
| 164 | if (a->flags & SSH2_FILEXFER_ATTR_UIDGID) {
| 0 | ||||||
| 165 | if ((r = sshbuf_put_u32(b, a->uid)) != 0 ||
| 0 | ||||||
| 166 | (r = sshbuf_put_u32(b, a->gid)) != 0)
| 0 | ||||||
| 167 | return r; never executed: return r; | 0 | ||||||
| 168 | } never executed: end of block | 0 | ||||||
| 169 | if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) {
| 0 | ||||||
| 170 | if ((r = sshbuf_put_u32(b, a->perm)) != 0)
| 0 | ||||||
| 171 | return r; never executed: return r; | 0 | ||||||
| 172 | } never executed: end of block | 0 | ||||||
| 173 | if (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
| 0 | ||||||
| 174 | if ((r = sshbuf_put_u32(b, a->atime)) != 0 ||
| 0 | ||||||
| 175 | (r = sshbuf_put_u32(b, a->mtime)) != 0)
| 0 | ||||||
| 176 | return r; never executed: return r; | 0 | ||||||
| 177 | } never executed: end of block | 0 | ||||||
| 178 | return 0; never executed: return 0; | 0 | ||||||
| 179 | } | - | ||||||
| 180 | - | |||||||
| 181 | /* Convert from SSH2_FX_ status to text error message */ | - | ||||||
| 182 | const char * | - | ||||||
| 183 | fx2txt(int status) | - | ||||||
| 184 | { | - | ||||||
| 185 | switch (status) { | - | ||||||
| 186 | case SSH2_FX_OK: never executed: case 0: | 0 | ||||||
| 187 | return("No error"); never executed: return("No error"); | 0 | ||||||
| 188 | case SSH2_FX_EOF: never executed: case 1: | 0 | ||||||
| 189 | return("End of file"); never executed: return("End of file"); | 0 | ||||||
| 190 | case SSH2_FX_NO_SUCH_FILE: never executed: case 2: | 0 | ||||||
| 191 | return("No such file or directory"); never executed: return("No such file or directory"); | 0 | ||||||
| 192 | case SSH2_FX_PERMISSION_DENIED: never executed: case 3: | 0 | ||||||
| 193 | return("Permission denied"); never executed: return("Permission denied"); | 0 | ||||||
| 194 | case SSH2_FX_FAILURE: never executed: case 4: | 0 | ||||||
| 195 | return("Failure"); never executed: return("Failure"); | 0 | ||||||
| 196 | case SSH2_FX_BAD_MESSAGE: never executed: case 5: | 0 | ||||||
| 197 | return("Bad message"); never executed: return("Bad message"); | 0 | ||||||
| 198 | case SSH2_FX_NO_CONNECTION: never executed: case 6: | 0 | ||||||
| 199 | return("No connection"); never executed: return("No connection"); | 0 | ||||||
| 200 | case SSH2_FX_CONNECTION_LOST: never executed: case 7: | 0 | ||||||
| 201 | return("Connection lost"); never executed: return("Connection lost"); | 0 | ||||||
| 202 | case SSH2_FX_OP_UNSUPPORTED: never executed: case 8: | 0 | ||||||
| 203 | return("Operation unsupported"); never executed: return("Operation unsupported"); | 0 | ||||||
| 204 | default: never executed: default: | 0 | ||||||
| 205 | return("Unknown status"); never executed: return("Unknown status"); | 0 | ||||||
| 206 | } | - | ||||||
| 207 | /* NOTREACHED */ | - | ||||||
| 208 | } | - | ||||||
| 209 | - | |||||||
| 210 | /* | - | ||||||
| 211 | * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh | - | ||||||
| 212 | */ | - | ||||||
| 213 | char * | - | ||||||
| 214 | ls_file(const char *name, const struct stat *st, int remote, int si_units) | - | ||||||
| 215 | { | - | ||||||
| 216 | int ulen, glen, sz = 0; | - | ||||||
| 217 | struct tm *ltime = localtime(&st->st_mtime); | - | ||||||
| 218 | const char *user, *group; | - | ||||||
| 219 | char buf[1024], lc[8], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; | - | ||||||
| 220 | char sbuf[FMT_SCALED_STRSIZE]; | - | ||||||
| 221 | time_t now; | - | ||||||
| 222 | - | |||||||
| 223 | strmode(st->st_mode, mode); | - | ||||||
| 224 | if (remote) {
| 0 | ||||||
| 225 | snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); | - | ||||||
| 226 | user = ubuf; | - | ||||||
| 227 | snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); | - | ||||||
| 228 | group = gbuf; | - | ||||||
| 229 | strlcpy(lc, "?", sizeof(lc)); | - | ||||||
| 230 | } else { never executed: end of block | 0 | ||||||
| 231 | user = user_from_uid(st->st_uid, 0); | - | ||||||
| 232 | group = group_from_gid(st->st_gid, 0); | - | ||||||
| 233 | snprintf(lc, sizeof(lc), "%u", (u_int)st->st_nlink); | - | ||||||
| 234 | } never executed: end of block | 0 | ||||||
| 235 | if (ltime != NULL) {
| 0 | ||||||
| 236 | now = time(NULL); | - | ||||||
| 237 | if (now - (365*24*60*60)/2 < st->st_mtime &&
| 0 | ||||||
| 238 | now >= st->st_mtime)
| 0 | ||||||
| 239 | sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); never executed: sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); | 0 | ||||||
| 240 | else | - | ||||||
| 241 | sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); never executed: sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); | 0 | ||||||
| 242 | } | - | ||||||
| 243 | if (sz == 0)
| 0 | ||||||
| 244 | tbuf[0] = '\0'; never executed: tbuf[0] = '\0'; | 0 | ||||||
| 245 | ulen = MAXIMUM(strlen(user), 8);
| 0 | ||||||
| 246 | glen = MAXIMUM(strlen(group), 8);
| 0 | ||||||
| 247 | if (si_units) {
| 0 | ||||||
| 248 | fmt_scaled((long long)st->st_size, sbuf); | - | ||||||
| 249 | snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8s %s %s", | - | ||||||
| 250 | mode, lc, ulen, user, glen, group, | - | ||||||
| 251 | sbuf, tbuf, name); | - | ||||||
| 252 | } else { never executed: end of block | 0 | ||||||
| 253 | snprintf(buf, sizeof buf, "%s %3s %-*s %-*s %8llu %s %s", | - | ||||||
| 254 | mode, lc, ulen, user, glen, group, | - | ||||||
| 255 | (unsigned long long)st->st_size, tbuf, name); | - | ||||||
| 256 | } never executed: end of block | 0 | ||||||
| 257 | return xstrdup(buf); never executed: return xstrdup(buf); | 0 | ||||||
| 258 | } | - | ||||||
| Source code | Switch to Preprocessed file |