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-2026 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 searchforlen = strlen(searchfor);
51 size_t replacewithlen = strlen(replacewith);
52 size_t templen = tosearchlen * replacewithlen;
53
54 if (tosearchlen == 0 || searchforlen == 0 || replacewithlen == 0)
55 {
56 return NULL;
57 }
58
59 bool is_potential_integer_overflow =
60 (templen == SIZE_MAX) || (templen / tosearchlen != replacewithlen);
61
62 if (is_potential_integer_overflow)
63 {
64 return NULL;
65 }
66
67 /* state: all parameters are valid */
68
69 const char *scratch = strstr(tosearch, searchfor);
70 if (!scratch)
71 {
72 return strdup(tosearch);
73 }
74
75 const char *searching = tosearch;
76 char temp[templen + 1];
77 memset(temp, 0, sizeof(temp));
78 char *target = temp;
79
80 while (scratch)
81 {
82 size_t skipped = (size_t)(scratch - searching);
83 memcpy(target, searching, skipped);
84 target += skipped;
85 memcpy(target, replacewith, replacewithlen);
86 target += replacewithlen;
87
88 searching = scratch + searchforlen;
89 scratch = strstr(searching, searchfor);
90 }
91 return strdup(temp);
92}
93
94const char *
95get_env(const char *name, const char *envp[])
96{
97 if (envp)
98 {
99 const size_t namelen = strlen(name);
100 for (int i = 0; envp[i]; ++i)
101 {
102 if (!strncmp(envp[i], name, namelen))
103 {
104 const char *cp = envp[i] + namelen;
105 if (*cp == '=')
106 {
107 return cp + 1;
108 }
109 }
110 }
111 }
112 return NULL;
113}
114
115int
116string_array_len(const char *array[])
117{
118 int i = 0;
119 if (array)
120 {
121 while (array[i])
122 {
123 ++i;
124 }
125 }
126 return i;
127}
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:116
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:95