Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 1 | /* |
Rich Salz | aa6bb13 | 2016-05-17 15:38:09 -0400 | [diff] [blame] | 2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
Richard Levitte | 0e9725b | 2018-12-06 14:03:01 +0100 | [diff] [blame] | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
Rich Salz | aa6bb13 | 2016-05-17 15:38:09 -0400 | [diff] [blame] | 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 | /* |
Rich Salz | 8d1598b | 2017-06-19 22:48:25 -0400 | [diff] [blame] | 11 | * This file is dual-licensed and is also available under the following |
Rich Salz | 0c3d0d4 | 2017-06-15 13:15:26 -0400 | [diff] [blame] | 12 | * terms: |
| 13 | * |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 14 | * Copyright (c) 2004, 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. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 25 | * |
Richard Levitte | bb09fd2 | 2004-09-23 22:11:39 +0000 | [diff] [blame] | 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. |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | #include <stddef.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <errno.h> |
| 43 | #include <descrip.h> |
| 44 | #include <namdef.h> |
| 45 | #include <rmsdef.h> |
| 46 | #include <libfildef.h> |
| 47 | #include <lib$routines.h> |
| 48 | #include <strdef.h> |
| 49 | #include <str$routines.h> |
| 50 | #include <stsdef.h> |
| 51 | #ifndef LPDIR_H |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 52 | # include "LPdir.h" |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 53 | #endif |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 54 | #include "vms_rms.h" |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 55 | |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 56 | /* Some compiler options hide EVMSERR. */ |
Richard Levitte | b084134 | 2004-07-11 20:21:19 +0000 | [diff] [blame] | 57 | #ifndef EVMSERR |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 58 | # define EVMSERR 65535 /* error for non-translatable VMS errors */ |
Richard Levitte | b084134 | 2004-07-11 20:21:19 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | struct LP_dir_context_st { |
| 62 | unsigned long VMS_context; |
| 63 | char filespec[NAMX_MAXRSS + 1]; |
| 64 | char result[NAMX_MAXRSS + 1]; |
| 65 | struct dsc$descriptor_d filespec_dsc; |
| 66 | struct dsc$descriptor_d result_dsc; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory) |
| 70 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 71 | int status; |
| 72 | char *p, *r; |
| 73 | size_t l; |
| 74 | unsigned long flags = 0; |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 75 | |
| 76 | /* Arrange 32-bit pointer to (copied) string storage, if needed. */ |
| 77 | #if __INITIAL_POINTER_SIZE == 64 |
| 78 | # pragma pointer_size save |
| 79 | # pragma pointer_size 32 |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 80 | char *ctx_filespec_32p; |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 81 | # pragma pointer_size restore |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 82 | char ctx_filespec_32[NAMX_MAXRSS + 1]; |
| 83 | #endif /* __INITIAL_POINTER_SIZE == 64 */ |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 84 | |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 85 | #ifdef NAML$C_MAXRSS |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 86 | flags |= LIB$M_FIL_LONG_NAMES; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 87 | #endif |
| 88 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 89 | if (ctx == NULL || directory == NULL) { |
| 90 | errno = EINVAL; |
| 91 | return 0; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 94 | errno = 0; |
| 95 | if (*ctx == NULL) { |
| 96 | size_t filespeclen = strlen(directory); |
| 97 | char *filespec = NULL; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 98 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 99 | if (filespeclen == 0) { |
| 100 | errno = ENOENT; |
| 101 | return 0; |
| 102 | } |
Richard Levitte | 360928b | 2014-08-15 01:24:34 +0200 | [diff] [blame] | 103 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 104 | /* MUST be a VMS directory specification! Let's estimate if it is. */ |
| 105 | if (directory[filespeclen - 1] != ']' |
| 106 | && directory[filespeclen - 1] != '>' |
| 107 | && directory[filespeclen - 1] != ':') { |
| 108 | errno = EINVAL; |
| 109 | return 0; |
| 110 | } |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 111 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 112 | filespeclen += 4; /* "*.*;" */ |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 113 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 114 | if (filespeclen > NAMX_MAXRSS) { |
| 115 | errno = ENAMETOOLONG; |
| 116 | return 0; |
| 117 | } |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 118 | |
Rich Salz | b4faea5 | 2015-05-01 23:10:31 -0400 | [diff] [blame] | 119 | *ctx = malloc(sizeof(**ctx)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 120 | if (*ctx == NULL) { |
| 121 | errno = ENOMEM; |
| 122 | return 0; |
| 123 | } |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 124 | memset(*ctx, 0, sizeof(**ctx)); |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 125 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 126 | strcpy((*ctx)->filespec, directory); |
| 127 | strcat((*ctx)->filespec, "*.*;"); |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 128 | |
| 129 | /* Arrange 32-bit pointer to (copied) string storage, if needed. */ |
| 130 | #if __INITIAL_POINTER_SIZE == 64 |
| 131 | # define CTX_FILESPEC ctx_filespec_32p |
| 132 | /* Copy the file name to storage with a 32-bit pointer. */ |
| 133 | ctx_filespec_32p = ctx_filespec_32; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 134 | strcpy(ctx_filespec_32p, (*ctx)->filespec); |
| 135 | #else /* __INITIAL_POINTER_SIZE == 64 */ |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 136 | # define CTX_FILESPEC (*ctx)->filespec |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | #endif /* __INITIAL_POINTER_SIZE == 64 [else] */ |
Richard Levitte | 537c982 | 2011-03-19 10:58:14 +0000 | [diff] [blame] | 138 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 139 | (*ctx)->filespec_dsc.dsc$w_length = filespeclen; |
| 140 | (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T; |
| 141 | (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S; |
| 142 | (*ctx)->filespec_dsc.dsc$a_pointer = CTX_FILESPEC; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 145 | (*ctx)->result_dsc.dsc$w_length = 0; |
| 146 | (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T; |
| 147 | (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D; |
| 148 | (*ctx)->result_dsc.dsc$a_pointer = 0; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 149 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 150 | status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc, |
| 151 | &(*ctx)->VMS_context, 0, 0, 0, &flags); |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 152 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 153 | if (status == RMS$_NMF) { |
| 154 | errno = 0; |
| 155 | vaxc$errno = status; |
| 156 | return NULL; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 159 | if (!$VMS_STATUS_SUCCESS(status)) { |
| 160 | errno = EVMSERR; |
| 161 | vaxc$errno = status; |
| 162 | return NULL; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 165 | /* |
| 166 | * Quick, cheap and dirty way to discard any device and directory, since |
| 167 | * we only want file names |
| 168 | */ |
| 169 | l = (*ctx)->result_dsc.dsc$w_length; |
| 170 | p = (*ctx)->result_dsc.dsc$a_pointer; |
| 171 | r = p; |
| 172 | for (; *p; p++) { |
| 173 | if (*p == '^' && p[1] != '\0') { /* Take care of ODS-5 escapes */ |
| 174 | p++; |
| 175 | } else if (*p == ':' || *p == '>' || *p == ']') { |
| 176 | l -= p + 1 - r; |
| 177 | r = p + 1; |
| 178 | } else if (*p == ';') { |
| 179 | l = p - r; |
| 180 | break; |
| 181 | } |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 184 | strncpy((*ctx)->result, r, l); |
| 185 | (*ctx)->result[l] = '\0'; |
| 186 | str$free1_dx(&(*ctx)->result_dsc); |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 187 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 188 | return (*ctx)->result; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int LP_find_file_end(LP_DIR_CTX **ctx) |
| 192 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 193 | if (ctx != NULL && *ctx != NULL) { |
| 194 | int status = lib$find_file_end(&(*ctx)->VMS_context); |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 195 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 196 | free(*ctx); |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 197 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 198 | if (!$VMS_STATUS_SUCCESS(status)) { |
| 199 | errno = EVMSERR; |
| 200 | vaxc$errno = status; |
| 201 | return 0; |
| 202 | } |
| 203 | return 1; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 204 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 205 | errno = EINVAL; |
| 206 | return 0; |
Richard Levitte | a2400fc | 2004-07-10 13:16:02 +0000 | [diff] [blame] | 207 | } |