]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/LPdir_unix.c
Fix intermittent sslapitest early data related failures
[thirdparty/openssl.git] / crypto / LPdir_unix.c
CommitLineData
2039c421 1/*
da1c088f 2 * Copyright 2004-2023 The OpenSSL Project Authors. All Rights Reserved.
2039c421 3 *
0e9725bc 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
a2400fca 10/*
8d1598b0 11 * This file is dual-licensed and is also available under the following
0c3d0d4a
RS
12 * terms:
13 *
a5829ae2 14 * Copyright (c) 2004, 2018, Richard Levitte <richard@levitte.org>
a2400fca
RL
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
0f113f3e 25 *
bb09fd2b
RL
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a2400fca
RL
37 */
38
39#include <stddef.h>
40#include <stdlib.h>
f1bdf1d5 41#include <limits.h>
a2400fca
RL
42#include <string.h>
43#include <sys/types.h>
44#include <dirent.h>
45#include <errno.h>
46#ifndef LPDIR_H
0f113f3e 47# include "LPdir.h"
a2400fca 48#endif
a5829ae2
RL
49#ifdef __VMS
50# include <ctype.h>
51#endif
a2400fca 52
0f113f3e 53/*
46d08509 54 * The POSIX macro for the maximum number of characters in a file path is
0f113f3e
MC
55 * NAME_MAX. However, some operating systems use PATH_MAX instead.
56 * Therefore, it seems natural to first check for PATH_MAX and use that, and
57 * if it doesn't exist, use NAME_MAX.
58 */
bb09fd2b
RL
59#if defined(PATH_MAX)
60# define LP_ENTRY_SIZE PATH_MAX
61#elif defined(NAME_MAX)
62# define LP_ENTRY_SIZE NAME_MAX
63#endif
64
0f113f3e
MC
65/*
66 * Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
67 * exist. It's also possible that NAME_MAX exists but is define to a very
68 * small value (HP-UX offers 14), so we need to check if we got a result, and
69 * if it meets a minimum standard, and create or change it if not.
70 */
bb09fd2b
RL
71#if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
72# undef LP_ENTRY_SIZE
73# define LP_ENTRY_SIZE 255
f1bdf1d5
AP
74#endif
75
0f113f3e
MC
76struct LP_dir_context_st {
77 DIR *dir;
78 char entry_name[LP_ENTRY_SIZE + 1];
a5829ae2
RL
79#ifdef __VMS
80 int expect_file_generations;
81 char previous_entry_name[LP_ENTRY_SIZE + 1];
82#endif
a2400fca
RL
83};
84
85const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
86{
0f113f3e 87 struct dirent *direntry = NULL;
a2400fca 88
0f113f3e
MC
89 if (ctx == NULL || directory == NULL) {
90 errno = EINVAL;
91 return 0;
a2400fca
RL
92 }
93
0f113f3e
MC
94 errno = 0;
95 if (*ctx == NULL) {
b4faea50 96 *ctx = malloc(sizeof(**ctx));
0f113f3e
MC
97 if (*ctx == NULL) {
98 errno = ENOMEM;
99 return 0;
100 }
16f8d4eb 101 memset(*ctx, 0, sizeof(**ctx));
a2400fca 102
a5829ae2
RL
103#ifdef __VMS
104 {
105 char c = directory[strlen(directory) - 1];
106
107 if (c == ']' || c == '>' || c == ':')
108 (*ctx)->expect_file_generations = 1;
109 }
110#endif
111
0f113f3e
MC
112 (*ctx)->dir = opendir(directory);
113 if ((*ctx)->dir == NULL) {
114 int save_errno = errno; /* Probably not needed, but I'm paranoid */
115 free(*ctx);
116 *ctx = NULL;
117 errno = save_errno;
118 return 0;
119 }
a2400fca
RL
120 }
121
a5829ae2
RL
122#ifdef __VMS
123 strncpy((*ctx)->previous_entry_name, (*ctx)->entry_name,
124 sizeof((*ctx)->previous_entry_name));
125
126 again:
127#endif
128
0f113f3e
MC
129 direntry = readdir((*ctx)->dir);
130 if (direntry == NULL) {
131 return 0;
a2400fca
RL
132 }
133
db5cf865
BE
134 OPENSSL_strlcpy((*ctx)->entry_name, direntry->d_name,
135 sizeof((*ctx)->entry_name));
a5829ae2
RL
136#ifdef __VMS
137 if ((*ctx)->expect_file_generations) {
138 char *p = (*ctx)->entry_name + strlen((*ctx)->entry_name);
139
82298744 140 while (p > (*ctx)->entry_name && isdigit((unsigned char)p[-1]))
a5829ae2
RL
141 p--;
142 if (p > (*ctx)->entry_name && p[-1] == ';')
143 p[-1] = '\0';
fba140c7
DB
144 if (OPENSSL_strcasecmp((*ctx)->entry_name,
145 (*ctx)->previous_entry_name) == 0)
a5829ae2
RL
146 goto again;
147 }
148#endif
0f113f3e 149 return (*ctx)->entry_name;
a2400fca
RL
150}
151
152int LP_find_file_end(LP_DIR_CTX **ctx)
153{
0f113f3e
MC
154 if (ctx != NULL && *ctx != NULL) {
155 int ret = closedir((*ctx)->dir);
a2400fca 156
0f113f3e
MC
157 free(*ctx);
158 switch (ret) {
159 case 0:
160 return 1;
161 case -1:
162 return 0;
163 default:
164 break;
165 }
a2400fca 166 }
0f113f3e
MC
167 errno = EINVAL;
168 return 0;
a2400fca 169}