OpenVPN
utils.c
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single TCP/UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 /*
25  * OpenVPN plugin module to do PAM authentication using a split
26  * privilege model.
27  */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 
33 #include <string.h>
34 #include <ctype.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <stdint.h>
39 
40 #include "utils.h"
41 
42 char *
43 searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
44 {
45  if (!tosearch || !searchfor || !replacewith)
46  {
47  return NULL;
48  }
49 
50  size_t tosearchlen = strlen(tosearch);
51  size_t replacewithlen = strlen(replacewith);
52  size_t templen = tosearchlen * replacewithlen;
53 
54  if (tosearchlen == 0 || strlen(searchfor) == 0 || replacewithlen == 0)
55  {
56  return NULL;
57  }
58 
59  bool is_potential_integer_overflow = (templen == SIZE_MAX) || (templen / tosearchlen != replacewithlen);
60 
61  if (is_potential_integer_overflow)
62  {
63  return NULL;
64  }
65 
66  /* state: all parameters are valid */
67 
68  const char *searching = tosearch;
69  char *scratch;
70 
71  char temp[templen+1];
72  temp[0] = 0;
73 
74  scratch = strstr(searching, searchfor);
75  if (!scratch)
76  {
77  return strdup(tosearch);
78  }
79 
80  while (scratch)
81  {
82  strncat(temp, searching, scratch-searching);
83  strcat(temp, replacewith);
84 
85  searching = scratch+strlen(searchfor);
86  scratch = strstr(searching, searchfor);
87  }
88  return strdup(temp);
89 }
90 
91 const char *
92 get_env(const char *name, const char *envp[])
93 {
94  if (envp)
95  {
96  int i;
97  const int namelen = strlen(name);
98  for (i = 0; envp[i]; ++i)
99  {
100  if (!strncmp(envp[i], name, namelen))
101  {
102  const char *cp = envp[i] + namelen;
103  if (*cp == '=')
104  {
105  return cp + 1;
106  }
107  }
108  }
109  }
110  return NULL;
111 }
112 
113 int
114 string_array_len(const char *array[])
115 {
116  int i = 0;
117  if (array)
118  {
119  while (array[i])
120  {
121  ++i;
122  }
123  }
124  return i;
125 }
string_array_len
int string_array_len(const char *array[])
Return the length of a string array.
Definition: utils.c:114
get_env
const char * get_env(const char *name, const char *envp[])
Given an environmental variable name, search the envp array for its value.
Definition: utils.c:92
utils.h
searchandreplace
char * searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
Read 'tosearch', replace all occurrences of 'searchfor' with 'replacewith' and return a pointer to th...
Definition: utils.c:43
config.h