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-2025 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, see <https://www.gnu.org/licenses/>.
21 */
22
23/*
24 * OpenVPN plugin module to do PAM authentication using a split
25 * privilege model.
26 */
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31
32#include <string.h>
33#include <ctype.h>
34#include <stdbool.h>
35#include <stdlib.h>
36#include <sys/types.h>
37#include <stdint.h>
38
39#include "utils.h"
40
41char *
42searchandreplace(const char *tosearch, const char *searchfor, const char *replacewith)
43{
44 if (!tosearch || !searchfor || !replacewith)
45 {
46 return NULL;
47 }
48
49 size_t tosearchlen = strlen(tosearch);
50 size_t replacewithlen = strlen(replacewith);
51 size_t templen = tosearchlen * replacewithlen;
52
53 if (tosearchlen == 0 || strlen(searchfor) == 0 || replacewithlen == 0)
54 {
55 return NULL;
56 }
57
58 bool is_potential_integer_overflow =
59 (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, (size_t)(scratch - searching));
83 strcat(temp, replacewith);
84
85 searching = scratch + strlen(searchfor);
86 scratch = strstr(searching, searchfor);
87 }
88 return strdup(temp);
89}
90
91const char *
92get_env(const char *name, const char *envp[])
93{
94 if (envp)
95 {
96 const size_t namelen = strlen(name);
97 for (int i = 0; envp[i]; ++i)
98 {
99 if (!strncmp(envp[i], name, namelen))
100 {
101 const char *cp = envp[i] + namelen;
102 if (*cp == '=')
103 {
104 return cp + 1;
105 }
106 }
107 }
108 }
109 return NULL;
110}
111
112int
113string_array_len(const char *array[])
114{
115 int i = 0;
116 if (array)
117 {
118 while (array[i])
119 {
120 ++i;
121 }
122 }
123 return i;
124}
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:42
int string_array_len(const char *array[])
Return the length of a string array.
Definition utils.c:113
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