Coverage Report

Created: 2026-01-06 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libssh/src/misc.c
Line
Count
Source
1
/*
2
 * misc.c - useful client functions
3
 *
4
 * This file is part of the SSH Library
5
 *
6
 * Copyright (c) 2003-2009 by Aris Adamantiadis
7
 * Copyright (c) 2008-2009 by Andreas Schneider <asn@cryptomilk.org>
8
 *
9
 * The SSH Library is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU Lesser General Public License as published by
11
 * the Free Software Foundation; either version 2.1 of the License, or (at your
12
 * option) any later version.
13
 *
14
 * The SSH Library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17
 * License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with the SSH Library; see the file COPYING.  If not, write to
21
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
 * MA 02111-1307, USA.
23
 */
24
25
#include "config.h"
26
27
#ifndef _WIN32
28
/* This is needed for a standard getpwuid_r on opensolaris */
29
#define _POSIX_PTHREAD_SEMANTICS
30
#include <arpa/inet.h>
31
#include <net/if.h>
32
#include <netinet/in.h>
33
#include <pwd.h>
34
#include <sys/socket.h>
35
#include <sys/types.h>
36
37
#endif /* _WIN32 */
38
39
#include <errno.h>
40
#include <limits.h>
41
#include <stdio.h>
42
#include <string.h>
43
#include <stdlib.h>
44
#include <sys/stat.h>
45
#include <sys/types.h>
46
#include <ctype.h>
47
#include <time.h>
48
#ifdef HAVE_SYS_TIME_H
49
#include <sys/time.h>
50
#endif /* HAVE_SYS_TIME_H */
51
52
53
#ifdef _WIN32
54
55
#ifndef _WIN32_IE
56
# define _WIN32_IE 0x0501 // SHGetSpecialFolderPath
57
#endif
58
59
#include <winsock2.h> // Must be the first to include
60
#include <ws2tcpip.h>
61
#include <shlobj.h>
62
#include <direct.h>
63
#include <netioapi.h>
64
65
#ifdef HAVE_IO_H
66
#include <io.h>
67
#endif /* HAVE_IO_H */
68
69
#endif /* _WIN32 */
70
71
#include "libssh/priv.h"
72
#include "libssh/misc.h"
73
#include "libssh/session.h"
74
75
#ifdef HAVE_LIBGCRYPT
76
#define GCRYPT_STRING "/gcrypt"
77
#else
78
#define GCRYPT_STRING ""
79
#endif
80
81
#ifdef HAVE_LIBCRYPTO
82
#define CRYPTO_STRING "/openssl"
83
#else
84
#define CRYPTO_STRING ""
85
#endif
86
87
#ifdef HAVE_LIBMBEDCRYPTO
88
#define MBED_STRING "/mbedtls"
89
#else
90
#define MBED_STRING ""
91
#endif
92
93
#ifdef WITH_ZLIB
94
0
#define ZLIB_STRING "/zlib"
95
#else
96
#define ZLIB_STRING ""
97
#endif
98
99
0
#define ARPA_DOMAIN_MAX_LEN 63
100
101
/**
102
 * @defgroup libssh_misc The SSH helper functions
103
 * @ingroup libssh
104
 *
105
 * Different helper functions used in the SSH Library.
106
 *
107
 * @{
108
 */
109
110
#ifdef _WIN32
111
char *ssh_get_user_home_dir(void)
112
{
113
  char tmp[PATH_MAX] = {0};
114
  char *szPath = NULL;
115
116
  if (SHGetSpecialFolderPathA(NULL, tmp, CSIDL_PROFILE, TRUE)) {
117
    szPath = malloc(strlen(tmp) + 1);
118
    if (szPath == NULL) {
119
      return NULL;
120
    }
121
122
    strcpy(szPath, tmp);
123
    return szPath;
124
  }
125
126
  return NULL;
127
}
128
129
/* we have read access on file */
130
int ssh_file_readaccess_ok(const char *file)
131
{
132
    if (_access(file, 4) < 0) {
133
        return 0;
134
    }
135
136
    return 1;
137
}
138
139
/**
140
 * @brief Check if the given path is an existing directory and that is
141
 * accessible for writing.
142
 *
143
 * @param[in] path Path to the directory to be checked
144
 *
145
 * @return Return 1 if the directory exists and is accessible; 0 otherwise
146
 * */
147
int ssh_dir_writeable(const char *path)
148
{
149
    struct _stat buffer;
150
    int rc;
151
152
    rc = _stat(path, &buffer);
153
    if (rc < 0) {
154
        return 0;
155
    }
156
157
    if ((buffer.st_mode & _S_IFDIR) && (buffer.st_mode & _S_IWRITE)) {
158
        return 1;
159
    }
160
161
    return 0;
162
}
163
164
#define SSH_USEC_IN_SEC         1000000LL
165
#define SSH_SECONDS_SINCE_1601  11644473600LL
166
167
int ssh_gettimeofday(struct timeval *__p, void *__t)
168
{
169
  union {
170
    unsigned long long ns100; /* time since 1 Jan 1601 in 100ns units */
171
    FILETIME ft;
172
  } now;
173
174
  GetSystemTimeAsFileTime (&now.ft);
175
  __p->tv_usec = (long) ((now.ns100 / 10LL) % SSH_USEC_IN_SEC);
176
  __p->tv_sec  = (long)(((now.ns100 / 10LL ) / SSH_USEC_IN_SEC) - SSH_SECONDS_SINCE_1601);
177
178
  return (0);
179
}
180
181
/**
182
 * @internal
183
 *
184
 * @brief Convert time in seconds since the Epoch to broken-down local time
185
 *
186
 * This is a helper used to provide localtime_r() like function interface
187
 * on Windows.
188
 *
189
 * @param timer    Pointer to a location storing the time_t which
190
 *                 represents the time in seconds since the Epoch.
191
 *
192
 * @param result   Pointer to a location where the broken-down time
193
 *                 (expressed as local time) should be stored.
194
 *
195
 * @returns        A pointer to the structure pointed to by the parameter
196
 *                 <tt>result</tt> on success, NULL on error with the errno
197
 *                 set to indicate the error.
198
 */
199
struct tm *ssh_localtime(const time_t *timer, struct tm *result)
200
{
201
    errno_t rc;
202
    rc = localtime_s(result, timer);
203
    if (rc != 0) {
204
        return NULL;
205
    }
206
207
    return result;
208
}
209
210
char *ssh_get_local_username(void)
211
{
212
    DWORD size = 0;
213
    char *user = NULL;
214
    int rc;
215
216
    /* get the size */
217
    GetUserName(NULL, &size);
218
219
    user = (char *)malloc(size);
220
    if (user == NULL) {
221
        return NULL;
222
    }
223
224
    if (GetUserName(user, &size)) {
225
        rc = ssh_check_username_syntax(user);
226
        if (rc == SSH_OK) {
227
            return user;
228
        }
229
    }
230
231
    free(user);
232
233
    return NULL;
234
}
235
236
int ssh_is_ipaddr_v4(const char *str)
237
{
238
    struct sockaddr_storage ss;
239
    int sslen = sizeof(ss);
240
    int rc = SOCKET_ERROR;
241
242
    /* WSAStringToAddressA thinks that 0.0.0 is a valid IP */
243
    if (strlen(str) < 7) {
244
        return 0;
245
    }
246
247
    rc = WSAStringToAddressA((LPSTR) str,
248
                             AF_INET,
249
                             NULL,
250
                             (struct sockaddr*)&ss,
251
                             &sslen);
252
    if (rc == 0) {
253
        return 1;
254
    }
255
256
    return 0;
257
}
258
259
int ssh_is_ipaddr(const char *str)
260
{
261
    int rc = SOCKET_ERROR;
262
    char *s = strdup(str);
263
264
    if (s == NULL) {
265
        return -1;
266
    }
267
    if (strchr(s, ':')) {
268
        struct sockaddr_storage ss;
269
        int sslen = sizeof(ss);
270
        char *network_interface = strchr(s, '%');
271
272
        /* link-local (IP:v6:addr%ifname). */
273
        if (network_interface != NULL) {
274
            rc = if_nametoindex(network_interface + 1);
275
            if (rc == 0) {
276
                free(s);
277
                return 0;
278
            }
279
            *network_interface = '\0';
280
        }
281
        rc = WSAStringToAddressA((LPSTR) s,
282
                                 AF_INET6,
283
                                 NULL,
284
                                 (struct sockaddr*)&ss,
285
                                 &sslen);
286
        if (rc == 0) {
287
            free(s);
288
            return 1;
289
        }
290
    }
291
292
    free(s);
293
    return ssh_is_ipaddr_v4(str);
294
}
295
#else /* _WIN32 */
296
297
#ifndef NSS_BUFLEN_PASSWD
298
#define NSS_BUFLEN_PASSWD 4096
299
#endif /* NSS_BUFLEN_PASSWD */
300
301
char *ssh_get_user_home_dir(void)
302
0
{
303
0
    char *szPath = NULL;
304
0
    struct passwd pwd;
305
0
    struct passwd *pwdbuf = NULL;
306
0
    char buf[NSS_BUFLEN_PASSWD] = {0};
307
0
    int rc;
308
309
0
    rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
310
0
    if (rc != 0 || pwdbuf == NULL ) {
311
0
        szPath = getenv("HOME");
312
0
        if (szPath == NULL) {
313
0
            return NULL;
314
0
        }
315
0
        snprintf(buf, sizeof(buf), "%s", szPath);
316
317
0
        return strdup(buf);
318
0
    }
319
320
0
    szPath = strdup(pwd.pw_dir);
321
322
0
    return szPath;
323
0
}
324
325
/* we have read access on file */
326
int ssh_file_readaccess_ok(const char *file)
327
0
{
328
0
    if (access(file, R_OK) < 0) {
329
0
        return 0;
330
0
    }
331
332
0
    return 1;
333
0
}
334
335
/**
336
 * @brief Check if the given path is an existing directory and that is
337
 * accessible for writing.
338
 *
339
 * @param[in] path Path to the directory to be checked
340
 *
341
 * @return Return 1 if the directory exists and is accessible; 0 otherwise
342
 * */
343
int ssh_dir_writeable(const char *path)
344
0
{
345
0
    struct stat buffer;
346
0
    int rc;
347
348
0
    rc = stat(path, &buffer);
349
0
    if (rc < 0) {
350
0
        return 0;
351
0
    }
352
353
0
    if (S_ISDIR(buffer.st_mode) && (buffer.st_mode & S_IWRITE)) {
354
0
        return 1;
355
0
    }
356
357
0
    return 0;
358
0
}
359
360
char *ssh_get_local_username(void)
361
0
{
362
0
    struct passwd pwd;
363
0
    struct passwd *pwdbuf = NULL;
364
0
    char buf[NSS_BUFLEN_PASSWD];
365
0
    char *name = NULL;
366
0
    int rc;
367
368
0
    rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
369
0
    if (rc != 0 || pwdbuf == NULL) {
370
0
        return NULL;
371
0
    }
372
373
0
    name = strdup(pwd.pw_name);
374
0
    rc = ssh_check_username_syntax(name);
375
376
0
    if (rc != SSH_OK) {
377
0
        free(name);
378
0
        return NULL;
379
0
    }
380
381
0
    return name;
382
0
}
383
384
int ssh_is_ipaddr_v4(const char *str)
385
0
{
386
0
    int rc = -1;
387
0
    struct in_addr dest;
388
389
0
    rc = inet_pton(AF_INET, str, &dest);
390
0
    if (rc > 0) {
391
0
        return 1;
392
0
    }
393
394
0
    return 0;
395
0
}
396
397
int ssh_is_ipaddr(const char *str)
398
0
{
399
0
    int rc = -1;
400
0
    char *s = strdup(str);
401
402
0
    if (s == NULL) {
403
0
        return -1;
404
0
    }
405
0
    if (strchr(s, ':')) {
406
0
        struct in6_addr dest6;
407
0
        char *network_interface = strchr(s, '%');
408
409
        /* link-local (IP:v6:addr%ifname). */
410
0
        if (network_interface != NULL) {
411
0
            rc = if_nametoindex(network_interface + 1);
412
0
            if (rc == 0) {
413
0
                free(s);
414
0
                return 0;
415
0
            }
416
0
            *network_interface = '\0';
417
0
        }
418
0
        rc = inet_pton(AF_INET6, s, &dest6);
419
0
        if (rc > 0) {
420
0
            free(s);
421
0
            return 1;
422
0
        }
423
0
    }
424
425
0
    free(s);
426
0
    return ssh_is_ipaddr_v4(str);
427
0
}
428
429
#endif /* _WIN32 */
430
431
char *ssh_lowercase(const char* str)
432
0
{
433
0
    char *new = NULL, *p = NULL;
434
435
0
    if (str == NULL) {
436
0
        return NULL;
437
0
    }
438
439
0
    new = strdup(str);
440
0
    if (new == NULL) {
441
0
        return NULL;
442
0
    }
443
444
0
    for (p = new; *p; p++) {
445
0
        *p = tolower(*p);
446
0
    }
447
448
0
    return new;
449
0
}
450
451
char *ssh_hostport(const char *host, int port)
452
0
{
453
0
    char *dest = NULL;
454
0
    size_t len;
455
456
0
    if (host == NULL) {
457
0
        return NULL;
458
0
    }
459
460
    /* 3 for []:, 5 for 65536 and 1 for nul */
461
0
    len = strlen(host) + 3 + 5 + 1;
462
0
    dest = malloc(len);
463
0
    if (dest == NULL) {
464
0
        return NULL;
465
0
    }
466
0
    snprintf(dest, len, "[%s]:%d", host, port);
467
468
0
    return dest;
469
0
}
470
471
/**
472
 * @brief Convert a buffer into a colon separated hex string.
473
 * The caller has to free the memory.
474
 *
475
 * @param[in]  what         What should be converted to a hex string.
476
 *
477
 * @param[in]  len          Length of the buffer to convert.
478
 *
479
 * @return                  The hex string or NULL on error. The memory needs
480
 *                          to be freed using ssh_string_free_char().
481
 *
482
 * @see ssh_string_free_char()
483
 */
484
char *ssh_get_hexa(const unsigned char *what, size_t len)
485
0
{
486
0
    const char h[] = "0123456789abcdef";
487
0
    char *hexa = NULL;
488
0
    size_t i;
489
0
    size_t hlen = len * 3;
490
491
0
    if (len > (UINT_MAX - 1) / 3) {
492
0
        return NULL;
493
0
    }
494
495
0
    hexa = malloc(hlen + 1);
496
0
    if (hexa == NULL) {
497
0
        return NULL;
498
0
    }
499
500
0
    for (i = 0; i < len; i++) {
501
0
        hexa[i * 3] = h[(what[i] >> 4) & 0xF];
502
0
        hexa[i * 3 + 1] = h[what[i] & 0xF];
503
0
        hexa[i * 3 + 2] = ':';
504
0
    }
505
0
    hexa[hlen - 1] = '\0';
506
507
0
    return hexa;
508
0
}
509
510
/**
511
 * @deprecated          Please use ssh_print_hash() instead
512
 */
513
void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len)
514
0
{
515
0
    char *hexa = ssh_get_hexa(what, len);
516
517
0
    if (hexa == NULL) {
518
0
      return;
519
0
    }
520
0
    fprintf(stderr, "%s: %s\n", descr, hexa);
521
522
0
    free(hexa);
523
0
}
524
525
/**
526
 * @brief Log the content of a buffer in hexadecimal format, similar to the
527
 * output of 'hexdump -C' command.
528
 *
529
 * The first logged line is the given description followed by the length.
530
 * Then the content of the buffer is logged 16 bytes per line in the following
531
 * format:
532
 *
533
 * (offset) (first 8 bytes) (last 8 bytes) (the 16 bytes as ASCII char values)
534
 *
535
 * The output for a 16 bytes array containing values from 0x00 to 0x0f would be:
536
 *
537
 * "Example (16 bytes):"
538
 * "  00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  ................"
539
 *
540
 * The value for each byte as corresponding ASCII character is printed at the
541
 * end if the value is printable. Otherwise, it is replaced with '.'.
542
 *
543
 * @param[in] descr A description for the content to be logged
544
 * @param[in] what  The buffer to be logged
545
 * @param[in] len   The length of the buffer given in what
546
 *
547
 * @note If a too long description is provided (which would result in a first
548
 * line longer than 80 bytes), the function will fail.
549
 */
550
void ssh_log_hexdump(const char *descr, const unsigned char *what, size_t len)
551
0
{
552
0
    size_t i;
553
0
    char ascii[17];
554
0
    const unsigned char *pc = NULL;
555
0
    size_t count = 0;
556
0
    ssize_t printed = 0;
557
558
    /* The required buffer size is calculated from:
559
     *
560
     *  2 bytes for spaces at the beginning
561
     *  8 bytes for the offset
562
     *  2 bytes for spaces
563
     * 24 bytes to print the first 8 bytes + spaces
564
     *  1 byte for an extra space
565
     * 24 bytes to print next 8 bytes + spaces
566
     *  2 bytes for extra spaces
567
     * 16 bytes for the content as ASCII characters at the end
568
     *  1 byte for the ending '\0'
569
     *
570
     * Resulting in 80 bytes.
571
     *
572
     * Except for the first line (description + size), all lines have fixed
573
     * length. If a too long description is used, the function will fail.
574
     * */
575
0
    char buffer[80];
576
577
    /* Print description */
578
0
    if (descr != NULL) {
579
0
        printed = snprintf(buffer, sizeof(buffer), "%s ", descr);
580
0
        if (printed < 0) {
581
0
            goto error;
582
0
        }
583
0
        count += printed;
584
0
    } else {
585
0
        printed = snprintf(buffer, sizeof(buffer), "(NULL description) ");
586
0
        if (printed < 0) {
587
0
            goto error;
588
0
        }
589
0
        count += printed;
590
0
    }
591
592
0
    if (len == 0) {
593
0
        printed = snprintf(buffer + count, sizeof(buffer) - count,
594
0
                           "(zero length):");
595
0
        if (printed < 0) {
596
0
            goto error;
597
0
        }
598
0
        SSH_LOG(SSH_LOG_DEBUG, "%s", buffer);
599
0
        return;
600
0
    } else {
601
0
        printed = snprintf(buffer + count, sizeof(buffer) - count,
602
0
                           "(%zu bytes):", len);
603
0
        if (printed < 0) {
604
0
            goto error;
605
0
        }
606
0
        count += printed;
607
0
    }
608
609
0
    if (what == NULL) {
610
0
        printed = snprintf(buffer + count, sizeof(buffer) - count,
611
0
                           "(NULL)");
612
0
        if (printed < 0) {
613
0
            goto error;
614
0
        }
615
0
        SSH_LOG(SSH_LOG_DEBUG, "%s", buffer);
616
0
        return;
617
0
    }
618
619
0
    SSH_LOG(SSH_LOG_DEBUG, "%s", buffer);
620
621
    /* Reset state */
622
0
    count = 0;
623
0
    pc = what;
624
625
0
    for (i = 0; i < len; i++) {
626
        /* Add one space after printing 8 bytes */
627
0
        if ((i % 8) == 0) {
628
0
            if (i != 0) {
629
0
                printed = snprintf(buffer + count, sizeof(buffer) - count, " ");
630
0
                if (printed < 0) {
631
0
                    goto error;
632
0
                }
633
0
                count += printed;
634
0
            }
635
0
        }
636
637
        /* Log previous line and reset state for new line */
638
0
        if ((i % 16) == 0) {
639
0
            if (i != 0) {
640
0
                printed = snprintf(buffer + count, sizeof(buffer) - count,
641
0
                                   "  %s", ascii);
642
0
                if (printed < 0) {
643
0
                    goto error;
644
0
                }
645
0
                SSH_LOG(SSH_LOG_DEBUG, "%s", buffer);
646
0
                count = 0;
647
0
            }
648
649
            /* Start a new line with the offset */
650
0
            printed = snprintf(buffer, sizeof(buffer),
651
0
                               "  %08zx ", i);
652
0
            if (printed < 0) {
653
0
                goto error;
654
0
            }
655
0
            count += printed;
656
0
        }
657
658
        /* Print the current byte hexadecimal representation */
659
0
        printed = snprintf(buffer + count, sizeof(buffer) - count,
660
0
                           " %02x", pc[i]);
661
0
        if (printed < 0) {
662
0
            goto error;
663
0
        }
664
0
        count += printed;
665
666
        /* If printable, store the ASCII character */
667
0
        if (isprint(pc[i])) {
668
0
            ascii[i % 16] = pc[i];
669
0
        } else {
670
0
            ascii[i % 16] = '.';
671
0
        }
672
0
        ascii[(i % 16) + 1] = '\0';
673
0
    }
674
675
    /* Add padding if not exactly 16 characters */
676
0
    while ((i % 16) != 0) {
677
        /* Add one space after printing 8 bytes */
678
0
        if ((i % 8) == 0) {
679
0
            if (i != 0) {
680
0
                printed = snprintf(buffer + count, sizeof(buffer) - count, " ");
681
0
                if (printed < 0) {
682
0
                    goto error;
683
0
                }
684
0
                count += printed;
685
0
            }
686
0
        }
687
688
0
        printed = snprintf(buffer + count, sizeof(buffer) - count, "   ");
689
0
        if (printed < 0) {
690
0
            goto error;
691
0
        }
692
0
        count += printed;
693
0
        i++;
694
0
    }
695
696
    /* Print the last printable part */
697
0
    printed = snprintf(buffer + count, sizeof(buffer) - count,
698
0
                       "   %s", ascii);
699
0
    if (printed < 0) {
700
0
        goto error;
701
0
    }
702
703
0
    SSH_LOG(SSH_LOG_DEBUG, "%s", buffer);
704
705
0
    return;
706
707
0
error:
708
0
    SSH_LOG(SSH_LOG_DEBUG, "Could not print to buffer");
709
0
    return;
710
0
}
711
712
/**
713
 * @brief Check if libssh is the required version or get the version
714
 * string.
715
 *
716
 * @param[in]  req_version The version required.
717
 *
718
 * @return              If the version of libssh is newer than the version
719
 *                      required it will return a version string.
720
 *                      NULL if the version is older.
721
 *
722
 * Example:
723
 *
724
 * @code
725
 *  if (ssh_version(SSH_VERSION_INT(0,2,1)) == NULL) {
726
 *    fprintf(stderr, "libssh version is too old!\n");
727
 *    exit(1);
728
 *  }
729
 *
730
 *  if (debug) {
731
 *    printf("libssh %s\n", ssh_version(0));
732
 *  }
733
 * @endcode
734
 */
735
const char *ssh_version(int req_version)
736
0
{
737
0
    if (req_version <= LIBSSH_VERSION_INT) {
738
0
        return SSH_STRINGIFY(LIBSSH_VERSION) GCRYPT_STRING CRYPTO_STRING
739
0
               MBED_STRING ZLIB_STRING;
740
0
    }
741
742
0
    return NULL;
743
0
}
744
745
struct ssh_list *ssh_list_new(void)
746
0
{
747
0
    struct ssh_list *ret = malloc(sizeof(struct ssh_list));
748
0
    if (ret == NULL) {
749
0
        return NULL;
750
0
    }
751
0
    ret->root = ret->end = NULL;
752
0
    return ret;
753
0
}
754
755
void ssh_list_free(struct ssh_list *list)
756
0
{
757
0
    struct ssh_iterator *ptr = NULL, *next = NULL;
758
0
    if (!list)
759
0
        return;
760
0
    ptr = list->root;
761
0
    while (ptr) {
762
0
        next = ptr->next;
763
0
        SAFE_FREE(ptr);
764
0
        ptr = next;
765
0
    }
766
0
    SAFE_FREE(list);
767
0
}
768
769
struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list)
770
0
{
771
0
    if (!list)
772
0
        return NULL;
773
0
    return list->root;
774
0
}
775
776
struct ssh_iterator *ssh_list_find(const struct ssh_list *list, void *value)
777
0
{
778
0
    struct ssh_iterator *it = NULL;
779
780
0
    for (it = ssh_list_get_iterator(list); it != NULL ; it = it->next)
781
0
        if (it->data == value)
782
0
            return it;
783
0
    return NULL;
784
0
}
785
786
/**
787
 * @brief Get the number of elements in the list
788
 *
789
 * @param[in]  list     The list to count.
790
 *
791
 * @return The number of elements in the list.
792
 */
793
size_t ssh_list_count(const struct ssh_list *list)
794
0
{
795
0
  struct ssh_iterator *it = NULL;
796
0
  size_t count = 0;
797
798
0
  for (it = ssh_list_get_iterator(list); it != NULL ; it = it->next) {
799
0
      count++;
800
0
  }
801
802
0
  return count;
803
0
}
804
805
static struct ssh_iterator *ssh_iterator_new(const void *data)
806
0
{
807
0
    struct ssh_iterator *iterator = malloc(sizeof(struct ssh_iterator));
808
809
0
    if (iterator == NULL) {
810
0
        return NULL;
811
0
    }
812
0
    iterator->next = NULL;
813
0
    iterator->data = data;
814
0
    return iterator;
815
0
}
816
817
int ssh_list_append(struct ssh_list *list,const void *data)
818
0
{
819
0
  struct ssh_iterator *iterator = NULL;
820
821
0
  if (list == NULL) {
822
0
      return SSH_ERROR;
823
0
  }
824
825
0
  iterator = ssh_iterator_new(data);
826
0
  if (iterator == NULL) {
827
0
      return SSH_ERROR;
828
0
  }
829
830
0
  if(!list->end){
831
    /* list is empty */
832
0
    list->root=list->end=iterator;
833
0
  } else {
834
    /* put it on end of list */
835
0
    list->end->next=iterator;
836
0
    list->end=iterator;
837
0
  }
838
0
  return SSH_OK;
839
0
}
840
841
int ssh_list_prepend(struct ssh_list *list, const void *data)
842
0
{
843
0
  struct ssh_iterator *it = NULL;
844
845
0
  if (list == NULL) {
846
0
      return SSH_ERROR;
847
0
  }
848
849
0
  it = ssh_iterator_new(data);
850
0
  if (it == NULL) {
851
0
    return SSH_ERROR;
852
0
  }
853
854
0
  if (list->end == NULL) {
855
    /* list is empty */
856
0
    list->root = list->end = it;
857
0
  } else {
858
    /* set as new root */
859
0
    it->next = list->root;
860
0
    list->root = it;
861
0
  }
862
863
0
  return SSH_OK;
864
0
}
865
866
void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator)
867
0
{
868
0
    struct ssh_iterator *ptr = NULL, *prev = NULL;
869
870
0
    if (list == NULL) {
871
0
        return;
872
0
    }
873
874
0
    prev = NULL;
875
0
    ptr = list->root;
876
0
    while (ptr && ptr != iterator) {
877
0
        prev = ptr;
878
0
        ptr = ptr->next;
879
0
    }
880
0
    if (!ptr) {
881
        /* we did not find the element */
882
0
        return;
883
0
    }
884
    /* unlink it */
885
0
    if (prev)
886
0
        prev->next = ptr->next;
887
    /* if iterator was the head */
888
0
    if (list->root == iterator)
889
0
        list->root = iterator->next;
890
    /* if iterator was the tail */
891
0
    if (list->end == iterator)
892
0
        list->end = prev;
893
0
    SAFE_FREE(iterator);
894
0
}
895
896
/**
897
 * @internal
898
 *
899
 * @brief Removes the top element of the list and returns the data value
900
 * attached to it.
901
 *
902
 * @param[in]  list     The ssh_list to remove the element.
903
 *
904
 * @returns             A pointer to the element being stored in head, or NULL
905
 *                      if the list is empty.
906
 */
907
const void *_ssh_list_pop_head(struct ssh_list *list)
908
0
{
909
0
  struct ssh_iterator *iterator = NULL;
910
0
  const void *data = NULL;
911
912
0
  if (list == NULL) {
913
0
      return NULL;
914
0
  }
915
916
0
  iterator = list->root;
917
0
  if (iterator == NULL) {
918
0
      return NULL;
919
0
  }
920
0
  data=iterator->data;
921
0
  list->root=iterator->next;
922
0
  if(list->end==iterator)
923
0
    list->end=NULL;
924
0
  SAFE_FREE(iterator);
925
0
  return data;
926
0
}
927
928
/**
929
 * @brief Parse directory component.
930
 *
931
 * dirname breaks a null-terminated pathname string into a directory component.
932
 * In the usual case, ssh_dirname() returns the string up to, but not including,
933
 * the final '/'. Trailing '/' characters are  not  counted as part of the
934
 * pathname. The caller must free the memory using ssh_string_free_char().
935
 *
936
 * @param[in]  path     The path to parse.
937
 *
938
 * @return              The dirname of path or NULL if we can't allocate memory.
939
 *                      If path does not contain a slash, c_dirname() returns
940
 *                      the string ".".  If path is a string "/", it returns
941
 *                      the string "/". If path is NULL or an empty string,
942
 *                      "." is returned. The memory needs to be freed using
943
 *                      ssh_string_free_char().
944
 *
945
 * @see ssh_string_free_char()
946
 */
947
char *ssh_dirname (const char *path)
948
0
{
949
0
  char *new = NULL;
950
0
  size_t len;
951
952
0
  if (path == NULL || *path == '\0') {
953
0
    return strdup(".");
954
0
  }
955
956
0
  len = strlen(path);
957
958
  /* Remove trailing slashes */
959
0
  while(len > 0 && path[len - 1] == '/') --len;
960
961
  /* We have only slashes */
962
0
  if (len == 0) {
963
0
    return strdup("/");
964
0
  }
965
966
  /* goto next slash */
967
0
  while(len > 0 && path[len - 1] != '/') --len;
968
969
0
  if (len == 0) {
970
0
    return strdup(".");
971
0
  } else if (len == 1) {
972
0
    return strdup("/");
973
0
  }
974
975
  /* Remove slashes again */
976
0
  while(len > 0 && path[len - 1] == '/') --len;
977
978
0
  new = malloc(len + 1);
979
0
  if (new == NULL) {
980
0
    return NULL;
981
0
  }
982
983
0
  strncpy(new, path, len);
984
0
  new[len] = '\0';
985
986
0
  return new;
987
0
}
988
989
/**
990
 * @brief basename - parse filename component.
991
 *
992
 * basename breaks a null-terminated pathname string into a filename component.
993
 * ssh_basename() returns the component following the final '/'.  Trailing '/'
994
 * characters are not counted as part of the pathname.
995
 *
996
 * @param[in]  path     The path to parse.
997
 *
998
 * @return              The filename of path or NULL if we can't allocate
999
 *                      memory. If path is the string "/", basename returns
1000
 *                      the string "/". If path is NULL or an empty string,
1001
 *                      "." is returned. The caller needs to free this memory
1002
 *                      ssh_string_free_char().
1003
 *
1004
 * @see ssh_string_free_char()
1005
 */
1006
char *ssh_basename (const char *path)
1007
0
{
1008
0
  char *new = NULL;
1009
0
  const char *s = NULL;
1010
0
  size_t len;
1011
1012
0
  if (path == NULL || *path == '\0') {
1013
0
    return strdup(".");
1014
0
  }
1015
1016
0
  len = strlen(path);
1017
  /* Remove trailing slashes */
1018
0
  while(len > 0 && path[len - 1] == '/') --len;
1019
1020
  /* We have only slashes */
1021
0
  if (len == 0) {
1022
0
    return strdup("/");
1023
0
  }
1024
1025
0
  while(len > 0 && path[len - 1] != '/') --len;
1026
1027
0
  if (len > 0) {
1028
0
    s = path + len;
1029
0
    len = strlen(s);
1030
1031
0
    while(len > 0 && s[len - 1] == '/') --len;
1032
0
  } else {
1033
0
    return strdup(path);
1034
0
  }
1035
1036
0
  new = malloc(len + 1);
1037
0
  if (new == NULL) {
1038
0
    return NULL;
1039
0
  }
1040
1041
0
  strncpy(new, s, len);
1042
0
  new[len] = '\0';
1043
1044
0
  return new;
1045
0
}
1046
1047
/**
1048
 * @brief Attempts to create a directory with the given pathname.
1049
 *
1050
 * This is the portable version of mkdir, mode is ignored on Windows systems.
1051
 *
1052
 * @param[in]  pathname The path name to create the directory.
1053
 *
1054
 * @param[in]  mode     The permissions to use.
1055
 *
1056
 * @return              0 on success, < 0 on error with errno set.
1057
 */
1058
int ssh_mkdir(const char *pathname, mode_t mode)
1059
0
{
1060
0
    int r;
1061
#ifdef _WIN32
1062
    r = _mkdir(pathname);
1063
#else
1064
0
    r = mkdir(pathname, mode);
1065
0
#endif
1066
1067
0
    return r;
1068
0
}
1069
1070
/**
1071
 * @brief Attempts to create a directory with the given pathname. The missing
1072
 * directories in the given pathname are created recursively.
1073
 *
1074
 * @param[in]  pathname The path name to create the directory.
1075
 *
1076
 * @param[in]  mode     The permissions to use.
1077
 *
1078
 * @return              0 on success, < 0 on error with errno set.
1079
 *
1080
 * @note mode is ignored on Windows systems.
1081
 */
1082
int ssh_mkdirs(const char *pathname, mode_t mode)
1083
0
{
1084
0
    int rc = 0;
1085
0
    char *parent = NULL;
1086
1087
0
    if (pathname == NULL ||
1088
0
        pathname[0] == '\0' ||
1089
0
        !strcmp(pathname, "/") ||
1090
0
        !strcmp(pathname, "."))
1091
0
    {
1092
0
        errno = EINVAL;
1093
0
        return -1;
1094
0
    }
1095
1096
0
    errno = 0;
1097
1098
#ifdef _WIN32
1099
    rc = _mkdir(pathname);
1100
#else
1101
0
    rc = mkdir(pathname, mode);
1102
0
#endif
1103
1104
0
    if (rc < 0) {
1105
        /* If a directory was missing, try to create the parent */
1106
0
        if (errno == ENOENT) {
1107
0
            parent = ssh_dirname(pathname);
1108
0
            if (parent == NULL) {
1109
0
                errno = ENOMEM;
1110
0
                return -1;
1111
0
            }
1112
1113
0
            rc = ssh_mkdirs(parent, mode);
1114
0
            if (rc < 0) {
1115
                /* We could not create the parent */
1116
0
                SAFE_FREE(parent);
1117
0
                return -1;
1118
0
            }
1119
1120
0
            SAFE_FREE(parent);
1121
1122
            /* Try again */
1123
0
            errno = 0;
1124
#ifdef _WIN32
1125
            rc = _mkdir(pathname);
1126
#else
1127
0
            rc = mkdir(pathname, mode);
1128
0
#endif
1129
0
        }
1130
0
    }
1131
1132
0
    return rc;
1133
0
}
1134
1135
/**
1136
 * @brief Expand a directory starting with a tilde '~'
1137
 *
1138
 * @param[in]  d        The directory to expand.
1139
 *
1140
 * @return              The expanded directory, NULL on error. The caller
1141
 *                      needs to free the memory using ssh_string_free_char().
1142
 *
1143
 * @see ssh_string_free_char()
1144
 */
1145
char *ssh_path_expand_tilde(const char *d)
1146
0
{
1147
0
    char *h = NULL, *r = NULL;
1148
0
    const char *p = NULL;
1149
0
    size_t ld;
1150
0
    size_t lh = 0;
1151
1152
0
    if (d[0] != '~') {
1153
0
        return strdup(d);
1154
0
    }
1155
0
    d++;
1156
1157
    /* handle ~user/path */
1158
0
    p = strchr(d, '/');
1159
0
    if (p != NULL && p > d) {
1160
#ifdef _WIN32
1161
        return strdup(d);
1162
#else
1163
0
        struct passwd *pw = NULL;
1164
0
        size_t s = p - d;
1165
0
        char u[128];
1166
1167
0
        if (s >= sizeof(u)) {
1168
0
            return NULL;
1169
0
        }
1170
0
        memcpy(u, d, s);
1171
0
        u[s] = '\0';
1172
0
        pw = getpwnam(u);
1173
0
        if (pw == NULL) {
1174
0
            return NULL;
1175
0
        }
1176
0
        ld = strlen(p);
1177
0
        h = strdup(pw->pw_dir);
1178
0
#endif
1179
0
    } else {
1180
0
        ld = strlen(d);
1181
0
        p = (char *) d;
1182
0
        h = ssh_get_user_home_dir();
1183
0
    }
1184
0
    if (h == NULL) {
1185
0
        return NULL;
1186
0
    }
1187
0
    lh = strlen(h);
1188
1189
0
    r = malloc(ld + lh + 1);
1190
0
    if (r == NULL) {
1191
0
        SAFE_FREE(h);
1192
0
        return NULL;
1193
0
    }
1194
1195
0
    if (lh > 0) {
1196
0
        memcpy(r, h, lh);
1197
0
    }
1198
0
    SAFE_FREE(h);
1199
0
    memcpy(r + lh, p, ld + 1);
1200
1201
0
    return r;
1202
0
}
1203
1204
/** @internal
1205
 * @brief expands a string in function of session options
1206
 * @param[in] s Format string to expand. Known parameters:
1207
 *              %d SSH configuration directory (~/.ssh)
1208
 *              %h target host name
1209
 *              %u local username
1210
 *              %l local hostname
1211
 *              %r remote username
1212
 *              %p remote port
1213
 * @returns Expanded string. The caller needs to free the memory using
1214
 *          ssh_string_free_char().
1215
 *
1216
 * @see ssh_string_free_char()
1217
 */
1218
char *ssh_path_expand_escape(ssh_session session, const char *s)
1219
0
{
1220
0
    char host[NI_MAXHOST] = {0};
1221
0
    char *buf = NULL;
1222
0
    char *r = NULL;
1223
0
    char *x = NULL;
1224
0
    const char *p = NULL;
1225
0
    size_t i, l;
1226
1227
0
    r = ssh_path_expand_tilde(s);
1228
0
    if (r == NULL) {
1229
0
        ssh_set_error_oom(session);
1230
0
        return NULL;
1231
0
    }
1232
1233
0
    if (strlen(r) > MAX_BUF_SIZE) {
1234
0
        ssh_set_error(session, SSH_FATAL, "string to expand too long");
1235
0
        free(r);
1236
0
        return NULL;
1237
0
    }
1238
1239
0
    buf = malloc(MAX_BUF_SIZE);
1240
0
    if (buf == NULL) {
1241
0
        ssh_set_error_oom(session);
1242
0
        free(r);
1243
0
        return NULL;
1244
0
    }
1245
1246
0
    p = r;
1247
0
    buf[0] = '\0';
1248
1249
0
    for (i = 0; *p != '\0'; p++) {
1250
0
        if (*p != '%') {
1251
0
        escape:
1252
0
            buf[i] = *p;
1253
0
            i++;
1254
0
            if (i >= MAX_BUF_SIZE) {
1255
0
                free(buf);
1256
0
                free(r);
1257
0
                return NULL;
1258
0
            }
1259
0
            buf[i] = '\0';
1260
0
            continue;
1261
0
        }
1262
1263
0
        p++;
1264
0
        if (*p == '\0') {
1265
0
            break;
1266
0
        }
1267
1268
0
        switch (*p) {
1269
0
            case '%':
1270
0
                goto escape;
1271
0
            case 'd':
1272
0
                if (session->opts.sshdir) {
1273
0
                    x = strdup(session->opts.sshdir);
1274
0
                } else {
1275
0
                    ssh_set_error(session, SSH_FATAL,
1276
0
                            "Cannot expand sshdir");
1277
0
                    free(buf);
1278
0
                    free(r);
1279
0
                    return NULL;
1280
0
                }
1281
0
                break;
1282
0
            case 'u':
1283
0
                x = ssh_get_local_username();
1284
0
                break;
1285
0
            case 'l':
1286
0
                if (gethostname(host, sizeof(host) == 0)) {
1287
0
                    x = strdup(host);
1288
0
                }
1289
0
                break;
1290
0
            case 'h':
1291
0
                if (session->opts.host) {
1292
0
                    x = strdup(session->opts.host);
1293
0
                } else {
1294
0
                    ssh_set_error(session, SSH_FATAL,
1295
0
                            "Cannot expand host");
1296
0
                    free(buf);
1297
0
                    free(r);
1298
0
                    return NULL;
1299
0
                }
1300
0
                break;
1301
0
            case 'r':
1302
0
                if (session->opts.username) {
1303
0
                    x = strdup(session->opts.username);
1304
0
                } else {
1305
0
                    ssh_set_error(session, SSH_FATAL,
1306
0
                            "Cannot expand username");
1307
0
                    free(buf);
1308
0
                    free(r);
1309
0
                    return NULL;
1310
0
                }
1311
0
                break;
1312
0
            case 'p':
1313
0
                {
1314
0
                  char tmp[6];
1315
1316
0
                  snprintf(tmp, sizeof(tmp), "%hu",
1317
0
                           (uint16_t)(session->opts.port > 0 ? session->opts.port
1318
0
                                                             : 22));
1319
0
                  x = strdup(tmp);
1320
0
                }
1321
0
                break;
1322
0
            default:
1323
0
                ssh_set_error(session, SSH_FATAL,
1324
0
                        "Wrong escape sequence detected");
1325
0
                free(buf);
1326
0
                free(r);
1327
0
                return NULL;
1328
0
        }
1329
1330
0
        if (x == NULL) {
1331
0
            ssh_set_error_oom(session);
1332
0
            free(buf);
1333
0
            free(r);
1334
0
            return NULL;
1335
0
        }
1336
1337
0
        i += strlen(x);
1338
0
        if (i >= MAX_BUF_SIZE) {
1339
0
            ssh_set_error(session, SSH_FATAL,
1340
0
                    "String too long");
1341
0
            free(buf);
1342
0
            free(x);
1343
0
            free(r);
1344
0
            return NULL;
1345
0
        }
1346
0
        l = strlen(buf);
1347
0
        strncpy(buf + l, x, MAX_BUF_SIZE - l - 1);
1348
0
        buf[i] = '\0';
1349
0
        SAFE_FREE(x);
1350
0
    }
1351
1352
0
    free(r);
1353
1354
    /* strip the unused space by realloc */
1355
0
    x = realloc(buf, strlen(buf) + 1);
1356
0
    if (x == NULL) {
1357
0
        ssh_set_error_oom(session);
1358
0
        free(buf);
1359
0
    }
1360
0
    return x;
1361
0
}
1362
1363
/**
1364
 * @internal
1365
 *
1366
 * @brief Analyze the SSH banner to extract version information.
1367
 *
1368
 * @param  session      The session to analyze the banner from.
1369
 * @param  server       0 means we are a client, 1 a server.
1370
 *
1371
 * @return 0 on success, < 0 on error.
1372
 *
1373
 * @see ssh_get_issue_banner()
1374
 */
1375
int ssh_analyze_banner(ssh_session session, int server)
1376
0
{
1377
0
    const char *banner = NULL;
1378
0
    const char *openssh = NULL;
1379
0
    const char *ios = NULL;
1380
1381
0
    if (server) {
1382
0
        banner = session->clientbanner;
1383
0
    } else {
1384
0
        banner = session->serverbanner;
1385
0
    }
1386
1387
0
    if (banner == NULL) {
1388
0
        ssh_set_error(session, SSH_FATAL, "Invalid banner");
1389
0
        return -1;
1390
0
    }
1391
1392
    /*
1393
     * Typical banners e.g. are:
1394
     *
1395
     * SSH-1.5-openSSH_5.4
1396
     * SSH-1.99-openSSH_3.0
1397
     *
1398
     * SSH-2.0-something
1399
     * 012345678901234567890
1400
     */
1401
0
    if (strlen(banner) < 6 ||
1402
0
        strncmp(banner, "SSH-", 4) != 0) {
1403
0
          ssh_set_error(session, SSH_FATAL, "Protocol mismatch: %s", banner);
1404
0
          return -1;
1405
0
    }
1406
1407
0
    SSH_LOG(SSH_LOG_DEBUG, "Analyzing banner: %s", banner);
1408
1409
0
    switch (banner[4]) {
1410
0
        case '2':
1411
0
            break;
1412
0
        case '1':
1413
0
            if (strlen(banner) > 6) {
1414
0
                if (banner[6] == '9') {
1415
0
                    break;
1416
0
                }
1417
0
            }
1418
0
            FALL_THROUGH;
1419
0
        default:
1420
0
            ssh_set_error(session, SSH_FATAL, "Protocol mismatch: %s", banner);
1421
0
            return -1;
1422
0
    }
1423
1424
    /* Make a best-effort to extract OpenSSH version numbers. */
1425
0
    openssh = strstr(banner, "OpenSSH");
1426
0
    if (openssh != NULL) {
1427
0
        char *tmp = NULL;
1428
0
        unsigned long int major = 0UL;
1429
0
        unsigned long int minor = 0UL;
1430
0
        int off = 0;
1431
1432
        /*
1433
         * The banner is typical:
1434
         * OpenSSH_5.4
1435
         * 012345678901234567890
1436
         */
1437
0
        if (strlen(openssh) > 9) {
1438
0
            errno = 0;
1439
0
            major = strtoul(openssh + 8, &tmp, 10);
1440
0
            if ((tmp == (openssh + 8)) ||
1441
0
                ((errno == ERANGE) && (major == ULONG_MAX)) ||
1442
0
                ((errno != 0) && (major == 0)) ||
1443
0
                ((major < 1) || (major > 100))) {
1444
                /* invalid major */
1445
0
                errno = 0;
1446
0
                goto done;
1447
0
            }
1448
1449
0
            errno = 0;
1450
0
            off = major >= 10 ? 11 : 10;
1451
0
            minor = strtoul(openssh + off, &tmp, 10);
1452
0
            if ((tmp == (openssh + off)) ||
1453
0
                ((errno == ERANGE) && (major == ULONG_MAX)) ||
1454
0
                ((errno != 0) && (major == 0)) ||
1455
0
                (minor > 100)) {
1456
                /* invalid minor */
1457
0
                errno = 0;
1458
0
                goto done;
1459
0
            }
1460
1461
0
            session->openssh = SSH_VERSION_INT(((int) major), ((int) minor), 0);
1462
1463
0
            SSH_LOG(SSH_LOG_DEBUG,
1464
0
                    "We are talking to an OpenSSH %s version: %lu.%lu (%x)",
1465
0
                    server ? "client" : "server",
1466
0
                    major, minor, session->openssh);
1467
0
        }
1468
0
    }
1469
    /* Cisco devices have odd scp implementation which breaks */
1470
0
    ios = strstr(banner, "Cisco");
1471
0
    if (ios != NULL) {
1472
0
        session->flags |= SSH_SESSION_FLAG_SCP_QUOTING_BROKEN;
1473
0
    }
1474
1475
0
done:
1476
0
    return 0;
1477
0
}
1478
1479
/* try the Monotonic clock if possible for perfs reasons */
1480
#ifdef _POSIX_MONOTONIC_CLOCK
1481
0
#define CLOCK CLOCK_MONOTONIC
1482
#else
1483
#define CLOCK CLOCK_REALTIME
1484
#endif
1485
1486
/**
1487
 * @internal
1488
 * @brief initializes a timestamp to the current time
1489
 * @param[out] ts pointer to an allocated ssh_timestamp structure
1490
 */
1491
void ssh_timestamp_init(struct ssh_timestamp *ts)
1492
0
{
1493
0
#ifdef HAVE_CLOCK_GETTIME
1494
0
  struct timespec tp;
1495
0
  clock_gettime(CLOCK, &tp);
1496
0
  ts->useconds = tp.tv_nsec / 1000;
1497
#else
1498
  struct timeval tp;
1499
  gettimeofday(&tp, NULL);
1500
  ts->useconds = tp.tv_usec;
1501
#endif
1502
0
  ts->seconds = tp.tv_sec;
1503
0
}
1504
1505
#undef CLOCK
1506
1507
/**
1508
 * @internal
1509
 * @brief gets the time difference between two timestamps in ms
1510
 * @param[in] old older value
1511
 * @param[in] new newer value
1512
 * @returns difference in milliseconds
1513
 */
1514
1515
static int
1516
ssh_timestamp_difference(struct ssh_timestamp *old, struct ssh_timestamp *new)
1517
0
{
1518
0
    long seconds, usecs, msecs;
1519
0
    seconds = new->seconds - old->seconds;
1520
0
    usecs = new->useconds - old->useconds;
1521
0
    if (usecs < 0){
1522
0
        seconds--;
1523
0
        usecs += 1000000;
1524
0
    }
1525
0
    msecs = seconds * 1000 + usecs/1000;
1526
0
    return msecs;
1527
0
}
1528
1529
/**
1530
 * @internal
1531
 * @brief turn seconds and microseconds pair (as provided by user-set options)
1532
 * into millisecond value
1533
 * @param[in] sec number of seconds
1534
 * @param[in] usec number of microseconds
1535
 * @returns milliseconds, or 10000 if user supplied values are equal to zero
1536
 */
1537
int ssh_make_milliseconds(unsigned long sec, unsigned long usec)
1538
0
{
1539
0
  unsigned long res = usec ? (usec / 1000) : 0;
1540
0
  res += (sec * 1000);
1541
0
  if (res == 0) {
1542
0
    res = 10 * 1000; /* use a reasonable default value in case
1543
        * SSH_OPTIONS_TIMEOUT is not set in options. */
1544
0
  }
1545
1546
0
    if (res > INT_MAX) {
1547
0
        return SSH_TIMEOUT_INFINITE;
1548
0
    } else {
1549
0
        return (int)res;
1550
0
    }
1551
0
}
1552
1553
/**
1554
 * @internal
1555
 * @brief Checks if a timeout is elapsed, in function of a previous
1556
 * timestamp and an assigned timeout
1557
 * @param[in] ts pointer to an existing timestamp
1558
 * @param[in] timeout timeout in milliseconds. Negative values mean infinite
1559
 *                   timeout
1560
 * @returns 1 if timeout is elapsed
1561
 *          0 otherwise
1562
 */
1563
int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout)
1564
0
{
1565
0
    struct ssh_timestamp now;
1566
1567
0
    switch(timeout) {
1568
0
        case -2: /*
1569
                  * -2 means user-defined timeout as available in
1570
                  * session->timeout, session->timeout_usec.
1571
                  */
1572
0
            SSH_LOG(SSH_LOG_DEBUG, "ssh_timeout_elapsed called with -2. this needs to "
1573
0
                            "be fixed. please set a breakpoint on misc.c:%d and "
1574
0
                            "fix the caller\n", __LINE__);
1575
0
            return 0;
1576
0
        case -1: /* -1 means infinite timeout */
1577
0
            return 0;
1578
0
        case 0: /* 0 means no timeout */
1579
0
            return 1;
1580
0
        default:
1581
0
            break;
1582
0
    }
1583
1584
0
    ssh_timestamp_init(&now);
1585
1586
0
    return (ssh_timestamp_difference(ts,&now) >= timeout);
1587
0
}
1588
1589
/**
1590
 * @brief updates a timeout value so it reflects the remaining time
1591
 * @param[in] ts pointer to an existing timestamp
1592
 * @param[in] timeout timeout in milliseconds. Negative values mean infinite
1593
 *             timeout
1594
 * @returns   remaining time in milliseconds, 0 if elapsed, -1 if never.
1595
 */
1596
int ssh_timeout_update(struct ssh_timestamp *ts, int timeout)
1597
0
{
1598
0
  struct ssh_timestamp now;
1599
0
  int ms, ret;
1600
0
  if (timeout <= 0) {
1601
0
      return timeout;
1602
0
  }
1603
0
  ssh_timestamp_init(&now);
1604
0
  ms = ssh_timestamp_difference(ts,&now);
1605
0
  if(ms < 0)
1606
0
    ms = 0;
1607
0
  ret = timeout - ms;
1608
0
  return ret >= 0 ? ret: 0;
1609
0
}
1610
1611
/**
1612
 * @brief Securely free memory by overwriting it before deallocation
1613
 *
1614
 * Overwrites the memory region with zeros before calling free() to prevent
1615
 * sensitive data from remaining in memory after deallocation.
1616
 *
1617
 * @param[in] ptr Pointer to the memory region to securely free.
1618
 *                Can be NULL (no operation performed).
1619
 * @param[in] len Length of the memory region in bytes.
1620
 *
1621
 */
1622
void burn_free(void *ptr, size_t len)
1623
0
{
1624
0
    if (ptr == NULL || len == 0) {
1625
0
        return;
1626
0
    }
1627
1628
0
    ssh_burn(ptr, len);
1629
0
    free(ptr);
1630
0
}
1631
1632
#if !defined(HAVE_STRNDUP)
1633
char *strndup(const char *s, size_t n)
1634
{
1635
    char *x = NULL;
1636
1637
    if (n + 1 < n) {
1638
        return NULL;
1639
    }
1640
1641
    x = malloc(n + 1);
1642
    if (x == NULL) {
1643
        return NULL;
1644
    }
1645
1646
    memcpy(x, s, n);
1647
    x[n] = '\0';
1648
1649
    return x;
1650
}
1651
#endif /* ! HAVE_STRNDUP */
1652
1653
/* Increment 64b integer in network byte order */
1654
void
1655
uint64_inc(unsigned char *counter)
1656
0
{
1657
0
    int i;
1658
1659
0
    for (i = 7; i >= 0; i--) {
1660
0
        counter[i]++;
1661
0
        if (counter[i])
1662
0
          return;
1663
0
    }
1664
0
}
1665
1666
/**
1667
 * @internal
1668
 *
1669
 * @brief Quote file name to be used on shell.
1670
 *
1671
 * Try to put the given file name between single quotes. There are special
1672
 * cases:
1673
 *
1674
 * - When the '\'' char is found in the file name, it is double quoted
1675
 *   - example:
1676
 *     input: a'b
1677
 *     output: 'a'"'"'b'
1678
 * - When the '!' char is found in the file name, it is replaced by an unquoted
1679
 *   verbatim char "\!"
1680
 *   - example:
1681
 *     input: a!b
1682
 *     output 'a'\!'b'
1683
 *
1684
 * @param[in]   file_name  File name string to be quoted before used on shell
1685
 * @param[out]  buf       Buffer to receive the final quoted file name.  Must
1686
 *                        have room for the final quoted string.  The maximum
1687
 *                        output length would be (3 * strlen(file_name) + 1)
1688
 *                        since in the worst case each character would be
1689
 *                        replaced by 3 characters, plus the terminating '\0'.
1690
 * @param[in]   buf_len   The size of the provided output buffer
1691
 *
1692
 * @returns SSH_ERROR on error; length of the resulting string not counting the
1693
 * string terminator '\0'
1694
 * */
1695
int ssh_quote_file_name(const char *file_name, char *buf, size_t buf_len)
1696
0
{
1697
0
    const char *src = NULL;
1698
0
    char *dst = NULL;
1699
0
    size_t required_buf_len;
1700
1701
0
    enum ssh_quote_state_e state = NO_QUOTE;
1702
1703
0
    if (file_name == NULL || buf == NULL || buf_len == 0) {
1704
0
        SSH_LOG(SSH_LOG_TRACE, "Invalid parameter");
1705
0
        return SSH_ERROR;
1706
0
    }
1707
1708
    /* Only allow file names smaller than 32kb. */
1709
0
    if (strlen(file_name) > 32 * 1024) {
1710
0
        SSH_LOG(SSH_LOG_TRACE, "File name too long");
1711
0
        return SSH_ERROR;
1712
0
    }
1713
1714
    /* Paranoia check */
1715
0
    required_buf_len = (size_t)3 * strlen(file_name) + 1;
1716
0
    if (required_buf_len > buf_len) {
1717
0
        SSH_LOG(SSH_LOG_TRACE, "Buffer too small");
1718
0
        return SSH_ERROR;
1719
0
    }
1720
1721
0
    src = file_name;
1722
0
    dst = buf;
1723
1724
0
    while ((*src != '\0')) {
1725
0
        switch (*src) {
1726
1727
        /* The '\'' char is double quoted */
1728
1729
0
        case '\'':
1730
0
            switch (state) {
1731
0
            case NO_QUOTE:
1732
                /* Start a new double quoted string. The '\'' char will be
1733
                 * copied to the beginning of it at the end of the loop. */
1734
0
                *dst++ = '"';
1735
0
                break;
1736
0
            case SINGLE_QUOTE:
1737
                /* Close the current single quoted string and start a new double
1738
                 * quoted string. The '\'' char will be copied to the beginning
1739
                 * of it at the end of the loop. */
1740
0
                *dst++ = '\'';
1741
0
                *dst++ = '"';
1742
0
                break;
1743
0
            case DOUBLE_QUOTE:
1744
                /* If already in the double quoted string, keep copying the
1745
                 * sequence of chars. */
1746
0
                break;
1747
0
            default:
1748
                /* Should never be reached */
1749
0
                goto error;
1750
0
            }
1751
1752
            /* When the '\'' char is found, the resulting state will be
1753
             * DOUBLE_QUOTE in any case*/
1754
0
            state = DOUBLE_QUOTE;
1755
0
            break;
1756
1757
        /* The '!' char is replaced by unquoted "\!" */
1758
1759
0
        case '!':
1760
0
            switch (state) {
1761
0
            case NO_QUOTE:
1762
                /* The '!' char is interpreted in some shells (e.g. CSH) even
1763
                 * when is quoted with single quotes.  Replace it with unquoted
1764
                 * "\!" which is correctly interpreted as the '!' character. */
1765
0
                *dst++ = '\\';
1766
0
                break;
1767
0
            case SINGLE_QUOTE:
1768
                /* Close the currently quoted string and replace '!' for unquoted
1769
                 * "\!" */
1770
0
                *dst++ = '\'';
1771
0
                *dst++ = '\\';
1772
0
                break;
1773
0
            case DOUBLE_QUOTE:
1774
                /* Close currently quoted string and replace  "!" for unquoted
1775
                 * "\!" */
1776
0
                *dst++ = '"';
1777
0
                *dst++ = '\\';
1778
0
                break;
1779
0
            default:
1780
                /* Should never be reached */
1781
0
                goto error;
1782
0
            }
1783
1784
            /* When the '!' char is found, the resulting state will be NO_QUOTE
1785
             * in any case*/
1786
0
            state = NO_QUOTE;
1787
0
            break;
1788
1789
        /* Ordinary chars are single quoted */
1790
1791
0
        default:
1792
0
            switch (state) {
1793
0
            case NO_QUOTE:
1794
                /* Start a new single quoted string */
1795
0
                *dst++ = '\'';
1796
0
                break;
1797
0
            case SINGLE_QUOTE:
1798
                /* If already in the single quoted string, keep copying the
1799
                 * sequence of chars. */
1800
0
                break;
1801
0
            case DOUBLE_QUOTE:
1802
                /* Close current double quoted string and start a new single
1803
                 * quoted string. */
1804
0
                *dst++ = '"';
1805
0
                *dst++ = '\'';
1806
0
                break;
1807
0
            default:
1808
                /* Should never be reached */
1809
0
                goto error;
1810
0
            }
1811
1812
            /* When an ordinary char is found, the resulting state will be
1813
             * SINGLE_QUOTE in any case*/
1814
0
            state = SINGLE_QUOTE;
1815
0
            break;
1816
0
        }
1817
1818
        /* Copy the current char to output */
1819
0
        *dst++ = *src++;
1820
0
    }
1821
1822
    /* Close the quoted string when necessary */
1823
1824
0
    switch (state) {
1825
0
    case NO_QUOTE:
1826
        /* No open string */
1827
0
        break;
1828
0
    case SINGLE_QUOTE:
1829
        /* Close current single quoted string */
1830
0
        *dst++ = '\'';
1831
0
        break;
1832
0
    case DOUBLE_QUOTE:
1833
        /* Close current double quoted string */
1834
0
        *dst++ = '"';
1835
0
        break;
1836
0
    default:
1837
        /* Should never be reached */
1838
0
        goto error;
1839
0
    }
1840
1841
    /* Put the string terminator */
1842
0
    *dst = '\0';
1843
1844
0
    return (int)(dst - buf);
1845
1846
0
error:
1847
0
    return SSH_ERROR;
1848
0
}
1849
1850
/**
1851
 * @internal
1852
 *
1853
 * @brief Given a string, encode existing newlines as the string "\\n"
1854
 *
1855
 * @param[in]  string   Input string
1856
 * @param[out] buf      Output buffer. This buffer must be at least (2 *
1857
 *                      strlen(string)) + 1 long.  In the worst case,
1858
 *                      each character can be encoded as 2 characters plus the
1859
 *                      terminating '\0'.
1860
 * @param[in]  buf_len  Size of the provided output buffer
1861
 *
1862
 * @returns SSH_ERROR on error; length of the resulting string not counting the
1863
 * terminating '\0' otherwise
1864
 */
1865
int ssh_newline_vis(const char *string, char *buf, size_t buf_len)
1866
0
{
1867
0
    const char *in = NULL;
1868
0
    char *out = NULL;
1869
1870
0
    if (string == NULL || buf == NULL || buf_len == 0) {
1871
0
        return SSH_ERROR;
1872
0
    }
1873
1874
0
    if ((2 * strlen(string) + 1) > buf_len) {
1875
0
        SSH_LOG(SSH_LOG_TRACE, "Buffer too small");
1876
0
        return SSH_ERROR;
1877
0
    }
1878
1879
0
    out = buf;
1880
0
    for (in = string; *in != '\0'; in++) {
1881
0
        if (*in == '\n') {
1882
0
            *out++ = '\\';
1883
0
            *out++ = 'n';
1884
0
        } else {
1885
0
            *out++ = *in;
1886
0
        }
1887
0
    }
1888
0
    *out = '\0';
1889
1890
0
    return (int)(out - buf);
1891
0
}
1892
1893
/**
1894
 * @internal
1895
 *
1896
 * @brief Replaces the last 6 characters of a string from 'X' to 6 random hexdigits.
1897
 *
1898
 * @param[in,out]  name   Any input string with last 6 characters as 'X'.
1899
 * @returns -1 as error when the last 6 characters of the input to be replaced are not 'X'
1900
 * 0 otherwise.
1901
 */
1902
int ssh_tmpname(char *name)
1903
0
{
1904
0
    char *tmp = NULL;
1905
0
    size_t i = 0;
1906
0
    int rc = 0;
1907
0
    uint8_t random[6];
1908
1909
0
    if (name == NULL) {
1910
0
        goto err;
1911
0
    }
1912
1913
0
    tmp = name + strlen(name) - 6;
1914
0
    if (tmp < name) {
1915
0
        goto err;
1916
0
    }
1917
1918
0
    for (i = 0; i < 6; i++) {
1919
0
        if (tmp[i] != 'X') {
1920
0
            SSH_LOG(SSH_LOG_WARNING,
1921
0
                    "Invalid input. Last six characters of the input must be \'X\'");
1922
0
            goto err;
1923
0
        }
1924
0
    }
1925
1926
0
    rc = ssh_get_random(random, 6, 0);
1927
0
    if (!rc) {
1928
0
        SSH_LOG(SSH_LOG_WARNING,
1929
0
                "Could not generate random data\n");
1930
0
        goto err;
1931
0
    }
1932
1933
0
    for (i = 0; i < 6; i++) {
1934
        /* Limit the random[i] < 32 */
1935
0
        random[i] &= 0x1f;
1936
        /* For values from 0 to 9 use numbers, otherwise use letters */
1937
0
        tmp[i] = random[i] > 9 ? random[i] + 'a' - 10 : random[i] + '0';
1938
0
    }
1939
1940
0
    return 0;
1941
1942
0
err:
1943
0
    errno = EINVAL;
1944
0
    return -1;
1945
0
}
1946
1947
/**
1948
 * @internal
1949
 *
1950
 * @brief Finds the first occurrence of a pattern in a string and replaces it.
1951
 *
1952
 * @param[in]  src          Source string containing the pattern to be replaced.
1953
 * @param[in]  pattern      Pattern to be replaced in the source string.
1954
 *                          Note: this function replaces the first occurrence of
1955
 *                          pattern only.
1956
 * @param[in]  replace      String to be replaced is stored in replace.
1957
 *
1958
 * @returns  src_replaced a pointer that points to the replaced string.
1959
 * NULL if allocation fails or if src is NULL. The returned memory needs to be
1960
 * freed using ssh_string_free_char().
1961
 *
1962
 * @see ssh_string_free_char()
1963
 */
1964
char *ssh_strreplace(const char *src, const char *pattern, const char *replace)
1965
0
{
1966
0
    char *p = NULL;
1967
0
    char *src_replaced = NULL;
1968
1969
0
    if (src == NULL) {
1970
0
        return NULL;
1971
0
    }
1972
1973
0
    if (pattern == NULL || replace == NULL) {
1974
0
        return strdup(src);
1975
0
    }
1976
1977
0
    p = strstr(src, pattern);
1978
1979
0
    if (p != NULL) {
1980
0
        size_t offset = p - src;
1981
0
        size_t pattern_len = strlen(pattern);
1982
0
        size_t replace_len = strlen(replace);
1983
0
        size_t len  = strlen(src);
1984
0
        size_t len_replaced = len + replace_len - pattern_len + 1;
1985
1986
0
        src_replaced = (char *)malloc(len_replaced);
1987
1988
0
        if (src_replaced == NULL) {
1989
0
            return NULL;
1990
0
        }
1991
1992
0
        memset(src_replaced, 0, len_replaced);
1993
0
        memcpy(src_replaced, src, offset);
1994
0
        memcpy(src_replaced + offset, replace, replace_len);
1995
0
        memcpy(src_replaced + offset + replace_len, src + offset + pattern_len, len - offset - pattern_len);
1996
0
        return src_replaced; /* free in the caller */
1997
0
    } else {
1998
0
        return strdup(src);
1999
0
    }
2000
0
}
2001
2002
/**
2003
 * @internal
2004
 *
2005
 * @brief Processes errno into error string
2006
 *
2007
 * @param[in] err_num The errno value
2008
 * @param[out] buf Pointer to a place where the string could be saved
2009
 * @param[in] buflen The allocated size of buf
2010
 *
2011
 * @return error string
2012
 */
2013
char *ssh_strerror(int err_num, char *buf, size_t buflen)
2014
0
{
2015
#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__CYGWIN__)) && defined(_GNU_SOURCE)
2016
    /* GNU extension on Linux */
2017
    return strerror_r(err_num, buf, buflen);
2018
#else
2019
0
    int rv;
2020
2021
#if defined(_WIN32)
2022
    rv = strerror_s(buf, buflen, err_num);
2023
#else
2024
    /* POSIX version available for example on FreeBSD or in musl libc */
2025
0
    rv = strerror_r(err_num, buf, buflen);
2026
0
#endif /* _WIN32 */
2027
2028
    /* make sure the buffer is initialized and terminated with NULL */
2029
0
    if (-rv == ERANGE) {
2030
0
        buf[0] = '\0';
2031
0
    }
2032
0
    return buf;
2033
0
#endif /* ((defined(__linux__) && defined(__GLIBC__)) || defined(__CYGWIN__)) && defined(_GNU_SOURCE) */
2034
0
}
2035
2036
/**
2037
 * @brief Read the requested number of bytes from a local file.
2038
 *
2039
 * A call to read() may perform a short read even when sufficient data is
2040
 * present in the file. This function can be used to avoid such short reads.
2041
 *
2042
 * This function tries to read the requested number of bytes from the file
2043
 * until one of the following occurs :
2044
 *     - Requested number of bytes are read.
2045
 *     - EOF is encountered before reading the requested number of bytes.
2046
 *     - An error occurs.
2047
 *
2048
 * On encountering an error due to an interrupt, this function ignores that
2049
 * error and continues trying to read the data.
2050
 *
2051
 * @param[in] fd          The file descriptor of the local file to read from.
2052
 *
2053
 * @param[out] buf        Pointer to a buffer in which read data will be
2054
 *                        stored.
2055
 *
2056
 * @param[in] nbytes      Number of bytes to read.
2057
 *
2058
 * @returns               Number of bytes read on success,
2059
 *                        SSH_ERROR on error with errno set to indicate the
2060
 *                        error.
2061
 */
2062
ssize_t ssh_readn(int fd, void *buf, size_t nbytes)
2063
0
{
2064
0
    size_t total_bytes_read = 0;
2065
0
    ssize_t bytes_read;
2066
2067
0
    if (fd < 0 || buf == NULL || nbytes == 0) {
2068
0
        errno = EINVAL;
2069
0
        return SSH_ERROR;
2070
0
    }
2071
2072
0
    do {
2073
0
        bytes_read = read(fd,
2074
0
                          ((char *)buf) + total_bytes_read,
2075
0
                          nbytes - total_bytes_read);
2076
0
        if (bytes_read == -1) {
2077
0
            if (errno == EINTR) {
2078
                /* Ignoring errors due to signal interrupts */
2079
0
                continue;
2080
0
            }
2081
2082
0
            return SSH_ERROR;
2083
0
        }
2084
2085
0
        if (bytes_read == 0) {
2086
            /* EOF encountered on the local file before reading nbytes */
2087
0
            break;
2088
0
        }
2089
2090
0
        total_bytes_read += (size_t)bytes_read;
2091
0
    } while (total_bytes_read < nbytes);
2092
2093
0
    return total_bytes_read;
2094
0
}
2095
2096
/**
2097
 * @brief Write the requested number of bytes to a local file.
2098
 *
2099
 * A call to write() may perform a short write on a local file. This function
2100
 * can be used to avoid short writes.
2101
 *
2102
 * This function tries to write the requested number of bytes until those many
2103
 * bytes are written or some error occurs.
2104
 *
2105
 * On encountering an error due to an interrupt, this function ignores that
2106
 * error and continues trying to write the data.
2107
 *
2108
 * @param[in] fd          The file descriptor of the local file to write to.
2109
 *
2110
 * @param[in] buf         Pointer to a buffer in which data to write is stored.
2111
 *
2112
 * @param[in] nbytes      Number of bytes to write.
2113
 *
2114
 * @returns               Number of bytes written on success,
2115
 *                        SSH_ERROR on error with errno set to indicate the
2116
 *                        error.
2117
 */
2118
ssize_t ssh_writen(int fd, const void *buf, size_t nbytes)
2119
0
{
2120
0
    size_t total_bytes_written = 0;
2121
0
    ssize_t bytes_written;
2122
2123
0
    if (fd < 0 || buf == NULL || nbytes == 0) {
2124
0
        errno = EINVAL;
2125
0
        return SSH_ERROR;
2126
0
    }
2127
2128
0
    do {
2129
0
        bytes_written = write(fd,
2130
0
                              ((const char *)buf) + total_bytes_written,
2131
0
                              nbytes - total_bytes_written);
2132
0
        if (bytes_written == -1) {
2133
0
            if (errno == EINTR) {
2134
                /* Ignoring errors due to signal interrupts */
2135
0
                continue;
2136
0
            }
2137
2138
0
            return SSH_ERROR;
2139
0
        }
2140
2141
0
        total_bytes_written += (size_t)bytes_written;
2142
0
    } while (total_bytes_written < nbytes);
2143
2144
0
    return total_bytes_written;
2145
0
}
2146
2147
/**
2148
 * @brief Checks syntax of a domain name
2149
 *
2150
 * The check is made based on the RFC1035 section 2.3.1
2151
 * Allowed characters are: hyphen, period, digits (0-9) and letters (a-zA-Z)
2152
 *
2153
 * The label should be no longer than 63 characters
2154
 * The label should start with a letter and end with a letter or number
2155
 * The label in this implementation can start with a number to allow virtual
2156
 * URLs to pass. Note that this will make IPv4 addresses to pass
2157
 * this check too.
2158
 *
2159
 * @param hostname The domain name to be checked, has to be null terminated
2160
 *
2161
 * @return SSH_OK if the hostname passes syntax check
2162
 *         SSH_ERROR otherwise or if hostname is NULL or empty string
2163
 */
2164
int ssh_check_hostname_syntax(const char *hostname)
2165
0
{
2166
0
    char *it = NULL, *s = NULL, *buf = NULL;
2167
0
    size_t it_len;
2168
0
    char c;
2169
2170
0
    if (hostname == NULL || strlen(hostname) == 0) {
2171
0
        return SSH_ERROR;
2172
0
    }
2173
2174
    /* strtok_r writes into the string, keep the input clean */
2175
0
    s = strdup(hostname);
2176
0
    if (s == NULL) {
2177
0
        return SSH_ERROR;
2178
0
    }
2179
2180
0
    it = strtok_r(s, ".", &buf);
2181
    /* if the token has 0 length */
2182
0
    if (it == NULL) {
2183
0
        free(s);
2184
0
        return SSH_ERROR;
2185
0
    }
2186
0
    do {
2187
0
        it_len = strlen(it);
2188
0
        if (it_len > ARPA_DOMAIN_MAX_LEN ||
2189
            /* the first char must be a letter, but some virtual urls start
2190
             * with a number */
2191
0
            isalnum(it[0]) == 0 ||
2192
0
            isalnum(it[it_len - 1]) == 0) {
2193
0
            free(s);
2194
0
            return SSH_ERROR;
2195
0
        }
2196
0
        while (*it != '\0') {
2197
0
            c = *it;
2198
            /* the "." is allowed too, but tokenization removes it from the
2199
             * string */
2200
0
            if (isalnum(c) == 0 && c != '-') {
2201
0
                free(s);
2202
0
                return SSH_ERROR;
2203
0
            }
2204
0
            it++;
2205
0
        }
2206
0
    } while ((it = strtok_r(NULL, ".", &buf)) != NULL);
2207
2208
0
    free(s);
2209
2210
0
    return SSH_OK;
2211
0
}
2212
2213
/**
2214
 * @brief Checks syntax of a username
2215
 *
2216
 * This check disallows metacharacters in the username
2217
 *
2218
 * @param username The username to be checked, has to be null terminated
2219
 *
2220
 * @return SSH_OK if the username passes syntax check
2221
 *         SSH_ERROR otherwise or if username is NULL or empty string
2222
 */
2223
int ssh_check_username_syntax(const char *username)
2224
0
{
2225
0
    size_t username_len;
2226
2227
0
    if (username == NULL || *username == '-') {
2228
0
        return SSH_ERROR;
2229
0
    }
2230
2231
0
    username_len = strlen(username);
2232
0
    if (username_len == 0 || username[username_len - 1] == '\\' ||
2233
0
        strpbrk(username, "'`\";&<>|(){}") != NULL) {
2234
0
        return SSH_ERROR;
2235
0
    }
2236
0
    for (size_t i = 0; i < username_len; i++) {
2237
0
        if (isspace(username[i]) != 0 && username[i + 1] == '-') {
2238
0
            return SSH_ERROR;
2239
0
        }
2240
0
    }
2241
2242
0
    return SSH_OK;
2243
0
}
2244
2245
/**
2246
 * @brief Free proxy jump list
2247
 *
2248
 * Frees everything in a proxy jump list, but doesn't free the ssh_list
2249
 *
2250
 * @param proxy_jump_list
2251
 *
2252
 */
2253
void
2254
ssh_proxyjumps_free(struct ssh_list *proxy_jump_list)
2255
0
{
2256
0
    struct ssh_jump_info_struct *jump = NULL;
2257
2258
0
    for (jump =
2259
0
             ssh_list_pop_head(struct ssh_jump_info_struct *, proxy_jump_list);
2260
0
         jump != NULL;
2261
0
         jump = ssh_list_pop_head(struct ssh_jump_info_struct *,
2262
0
                                  proxy_jump_list)) {
2263
0
        SAFE_FREE(jump->hostname);
2264
0
        SAFE_FREE(jump->username);
2265
0
        SAFE_FREE(jump);
2266
0
    }
2267
0
}
2268
2269
/**
2270
 * @brief Check if libssh proxy jumps is enabled
2271
 *
2272
 * If env variable OPENSSH_PROXYJUMP is set to 1 then proxyjump will be
2273
 * through the OpenSSH binary.
2274
 *
2275
 * @return false if OPENSSH_PROXYJUMP=1
2276
 *         true otherwise
2277
 */
2278
bool
2279
ssh_libssh_proxy_jumps(void)
2280
0
{
2281
0
    const char *t = getenv("OPENSSH_PROXYJUMP");
2282
2283
0
    return !(t != NULL && t[0] == '1');
2284
0
}
2285
2286
/** @} */