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