xref: /openssl/crypto/LPdir_unix.c (revision da1c088f)
1 /*
2  * Copyright 2004-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
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 
10 /*
11  * This file is dual-licensed and is also available under the following
12  * terms:
13  *
14  * Copyright (c) 2004, 2018, Richard Levitte <richard@levitte.org>
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.
25  *
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.
37  */
38 
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <limits.h>
42 #include <string.h>
43 #include <sys/types.h>
44 #include <dirent.h>
45 #include <errno.h>
46 #ifndef LPDIR_H
47 # include "LPdir.h"
48 #endif
49 #ifdef __VMS
50 # include <ctype.h>
51 #endif
52 
53 /*
54  * The POSIX macro for the maximum number of characters in a file path is
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  */
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 
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  */
71 #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
72 # undef LP_ENTRY_SIZE
73 # define LP_ENTRY_SIZE 255
74 #endif
75 
76 struct LP_dir_context_st {
77     DIR *dir;
78     char entry_name[LP_ENTRY_SIZE + 1];
79 #ifdef __VMS
80     int expect_file_generations;
81     char previous_entry_name[LP_ENTRY_SIZE + 1];
82 #endif
83 };
84 
LP_find_file(LP_DIR_CTX ** ctx,const char * directory)85 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
86 {
87     struct dirent *direntry = NULL;
88 
89     if (ctx == NULL || directory == NULL) {
90         errno = EINVAL;
91         return 0;
92     }
93 
94     errno = 0;
95     if (*ctx == NULL) {
96         *ctx = malloc(sizeof(**ctx));
97         if (*ctx == NULL) {
98             errno = ENOMEM;
99             return 0;
100         }
101         memset(*ctx, 0, sizeof(**ctx));
102 
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 
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         }
120     }
121 
122 #ifdef __VMS
123     strncpy((*ctx)->previous_entry_name, (*ctx)->entry_name,
124             sizeof((*ctx)->previous_entry_name));
125 
126  again:
127 #endif
128 
129     direntry = readdir((*ctx)->dir);
130     if (direntry == NULL) {
131         return 0;
132     }
133 
134     OPENSSL_strlcpy((*ctx)->entry_name, direntry->d_name,
135                     sizeof((*ctx)->entry_name));
136 #ifdef __VMS
137     if ((*ctx)->expect_file_generations) {
138         char *p = (*ctx)->entry_name + strlen((*ctx)->entry_name);
139 
140         while (p > (*ctx)->entry_name && isdigit((unsigned char)p[-1]))
141             p--;
142         if (p > (*ctx)->entry_name && p[-1] == ';')
143             p[-1] = '\0';
144         if (OPENSSL_strcasecmp((*ctx)->entry_name,
145                                (*ctx)->previous_entry_name) == 0)
146             goto again;
147     }
148 #endif
149     return (*ctx)->entry_name;
150 }
151 
LP_find_file_end(LP_DIR_CTX ** ctx)152 int LP_find_file_end(LP_DIR_CTX **ctx)
153 {
154     if (ctx != NULL && *ctx != NULL) {
155         int ret = closedir((*ctx)->dir);
156 
157         free(*ctx);
158         switch (ret) {
159         case 0:
160             return 1;
161         case -1:
162             return 0;
163         default:
164             break;
165         }
166     }
167     errno = EINVAL;
168     return 0;
169 }
170