Radcli library 1.4.0
A simple radius library
Loading...
Searching...
No Matches
util.c
1/*
2 * Copyright (C) 1995,1996,1997 Lars Fenneberg
3 *
4 * Copyright 1992 Livingston Enterprises, Inc.
5 *
6 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
7 * and Merit Network, Inc. All Rights Reserved
8 *
9 * See the file COPYRIGHT for the respective terms and conditions.
10 * If the file is missing contact me at lf@elemental.net
11 * and I'll send you a copy.
12 *
13 */
14
15#define _GNU_SOURCE
16
17#include <sys/time.h>
18
19#include <config.h>
20#include <includes.h>
21#include <radcli/radcli.h>
22#include "util.h"
23
24#define RC_BUFSIZ 1024
25
26#ifdef __linux__
27#include <sched.h>
28#define NSNET_SZ (128)
29#endif
30
31
32static char const * months[] =
33 {
34 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
36 };
37
38/*- Turns printable string into correct tm struct entries
39 *
40 * @param valstr the printable date in 'day month year' format.
41 * @param tm the output struct.
42 -*/
43void rc_str2tm (char const *valstr, struct tm *tm)
44{
45 int i;
46
47 /* Get the month */
48 for (i = 0; i < 12; i++)
49 {
50 if (strncmp (months[i], valstr, 3) == 0)
51 {
52 tm->tm_mon = i;
53 i = 13;
54 }
55 }
56
57 /* Get the Day */
58 tm->tm_mday = atoi (&valstr[4]);
59
60 /* Now the year */
61 tm->tm_year = atoi (&valstr[7]) - 1900;
62}
63
64/*- Returns the current monotonic time as a double
65 *
66 * @return Get monotonic time seconds since some fixed start) expressed as
67 * double-precision floating point number.
68 -*/
69double rc_getmtime(void)
70{
71#ifdef HAVE_CLOCK_GETTIME
72 struct timespec timespec;
73
74 if (clock_gettime(CLOCK_MONOTONIC, &timespec) != 0)
75 return -1;
76
77 return timespec.tv_sec + ((double)timespec.tv_nsec) / 1000000000.0;
78#else
79 struct timeval timev;
80
81 if (gettimeofday(&timev, NULL) == -1)
82 return -1;
83
84 return timev.tv_sec + ((double)timev.tv_usec) / 1000000.0;
85#endif
86}
87
93char *
94rc_mksid (void)
95{
96 static char buf[15];
97 static unsigned short int cnt = 0;
98 snprintf (buf, sizeof(buf), "%08lX%04X%02hX",
99 (unsigned long int) time (NULL),
100 (unsigned int) getpid (),
101 cnt & 0xFF);
102 cnt++;
103 return buf;
104}
105/*
106 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
107 *
108 * Permission to use, copy, modify, and distribute this software for any
109 * purpose with or without fee is hereby granted, provided that the above
110 * copyright notice and this permission notice appear in all copies.
111 *
112 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
113 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
115 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
116 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
117 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
118 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
119 *
120 * Copyright 2006 The FreeRADIUS server project
121 */
122
123#ifndef HAVE_STRLCPY
124
125/*-
126 * Copy src to string dst of size siz. At most siz-1 characters
127 * will be copied. Always NUL terminates (unless siz == 0).
128 * Returns strlen(src); if retval >= siz, truncation occurred.
129 -*/
130size_t
131rc_strlcpy(char *dst, char const *src, size_t siz)
132{
133 char *d = dst;
134 char const *s = src;
135 size_t n = siz;
136
137 /* Copy as many bytes as will fit */
138 if (n != 0 && --n != 0) {
139 do {
140 if ((*d++ = *s++) == 0)
141 break;
142 } while (--n != 0);
143 }
144
145 /* Not enough room in dst, add NUL and traverse rest of src */
146 if (n == 0) {
147 if (siz != 0)
148 *d = '\0'; /* NUL-terminate dst */
149 while (*s++)
150 ;
151 }
152
153 return(s - src - 1); /* count does not include NUL */
154}
155
156#endif
157
165int rc_set_netns(const char *net_namespace, int *prev_ns_handle)
166{
167 int rc = 0;
168#ifdef __linux__
169 rc = -1;
170 static const char* crt_nsnet = "/proc/self/ns/net";
171 int sock_ns_fd = -1;
172 char sock_nsnet[NSNET_SZ];
173
174 if (NULL == net_namespace) {
175 rc_log(LOG_ERR, "Namespace not provided");
176 return rc;
177 }
178 if (NULL == prev_ns_handle) {
179 rc_log(LOG_ERR, "NULL NS handle for: %s", net_namespace);
180 return rc;
181 }
182 *prev_ns_handle = -1;
183 snprintf(sock_nsnet, NSNET_SZ, "/var/run/netns/%s", net_namespace);
184 do {
185 *prev_ns_handle = open(crt_nsnet, O_RDONLY);
186 if (*prev_ns_handle < 0) {
187 rc_log(LOG_ERR, "Cannot open %s errno=%s(%d)", crt_nsnet, strerror (errno), errno);
188 break;
189 }
190 sock_ns_fd = open(sock_nsnet, O_RDONLY);
191 if (sock_ns_fd < 0) {
192 rc_log(LOG_ERR, "Cannot open %s errno=%s(%d)", sock_nsnet, strerror (errno), errno);
193 break;
194 }
195 if (setns(sock_ns_fd, CLONE_NEWNET) < 0) {
196 rc_log(LOG_ERR, "'setns' set failed for %s errno=%s(%d)", sock_nsnet,
197 strerror(errno), errno);
198 break;
199 }
200 rc = 0;
201 }while (0);
202 if (sock_ns_fd >= 0) {
203 (void)close(sock_ns_fd);
204 }
205 if (rc != 0) {
206 if (*prev_ns_handle >=0 ) {
207 (void)close(*prev_ns_handle);
208 *prev_ns_handle = -1;
209 }
210 }
211#else
212 rc_log(LOG_ERR, "Not a Linux system. No operation performed");
213#endif
214 return rc;
215}
216
224int rc_reset_netns(int *prev_ns_handle)
225{
226 int rc = 0;
227#ifdef __linux__
228 if (NULL == prev_ns_handle) {
229 rc_log(LOG_ERR, "NULL NS handle");
230 return -1;
231 }
232 if (setns(*prev_ns_handle, CLONE_NEWNET) < 0) {
233 rc_log(LOG_ERR, "'setns' - reset failed errno=%s(%d)", strerror (errno), errno);
234 }
235 if (close(*prev_ns_handle) != 0) {
236 rc_log(LOG_ERR, "Close error fd=%d errno=%s(%d)", *prev_ns_handle, strerror (errno), errno);
237 rc = -1;
238 }
239 *prev_ns_handle = -1;
240#else
241 rc_log(LOG_ERR, "Not a Linux system. No operation performed");
242#endif
243 return rc;
244}