util.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * $Id: util.c,v 1.10 2010/02/04 10:31:41 aland Exp $
  3. *
  4. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  5. *
  6. * Copyright (C) 1995,1996,1997 Lars Fenneberg
  7. *
  8. * Copyright 1992 Livingston Enterprises, Inc.
  9. *
  10. * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
  11. * and Merit Network, Inc. All Rights Reserved
  12. *
  13. * See the file COPYRIGHT for the respective terms and conditions.
  14. * If the file is missing contact me at lf@elemental.net
  15. * and I'll send you a copy.
  16. *
  17. */
  18. //#include <sys/time.h>
  19. #include <radius_config.h>
  20. #include <includes.h>
  21. #include <freeradius-client.h>
  22. #include "util.h"
  23. #define RC_BUFSIZ 1024
  24. static char const * months[] =
  25. {
  26. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  27. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  28. };
  29. /** Turns printable string into correct tm struct entries
  30. *
  31. * @param valstr the printable date in 'day month year' format.
  32. * @param tm the output struct.
  33. */
  34. void rc_str2tm (char const *valstr, struct tm *tm)
  35. {
  36. int i;
  37. /* Get the month */
  38. for (i = 0; i < 12; i++)
  39. {
  40. if (strncmp (months[i], valstr, 3) == 0)
  41. {
  42. tm->tm_mon = i;
  43. i = 13;
  44. }
  45. }
  46. /* Get the Day */
  47. tm->tm_mday = atoi (&valstr[4]);
  48. /* Now the year */
  49. tm->tm_year = atoi (&valstr[7]) - 1900;
  50. }
  51. /** Get the network interface name associated with this tty
  52. *
  53. * @param rh a handle to parsed configuration.
  54. * @param tty the name of the tty.
  55. * @return the network iface name.
  56. */
  57. char *rc_getifname(rc_handle *rh, char const *tty)
  58. {
  59. #if defined(BSD4_4) || defined(linux)
  60. int fd;
  61. if ((fd = open(tty, O_RDWR|O_NDELAY)) < 0) {
  62. rc_log(LOG_ERR, "rc_getifname: can't open %s: %s", tty, strerror(errno));
  63. return NULL;
  64. }
  65. #endif
  66. #ifdef BSD4_4
  67. strcpy(rh->ifname,ttyname(fd));
  68. if (strlen(rh->ifname) < 1) {
  69. rc_log(LOG_ERR, "rc_getifname: can't get attached interface of %s: %s", tty, strerror(errno));
  70. close(fd);
  71. return NULL;
  72. }
  73. #elif linux
  74. if (ioctl(fd, SIOCGIFNAME, rh->ifname) < 0) {
  75. rc_log(LOG_ERR, "rc_getifname: can't ioctl %s: %s", tty, strerror(errno));
  76. close(fd);
  77. return NULL;
  78. }
  79. #else
  80. return NULL;
  81. #endif
  82. #if defined(BSD4_4) || defined(linux)
  83. close(fd);
  84. return rh->ifname;
  85. #endif
  86. }
  87. /** Reads in a string from the user (with or witout echo)
  88. *
  89. * @param rh a handle to parsed configuration.
  90. * @param prompt the prompt to print.
  91. * @param do_echo whether to echo characters.
  92. * @return the data user typed or NULL.
  93. */
  94. #ifndef _MSC_VER
  95. char *rc_getstr (rc_handle *rh, char const *prompt, int do_echo)
  96. {
  97. #if 0
  98. int in, out;
  99. char *p;
  100. struct termios term_old, term_new;
  101. int is_term, flags, old_flags;
  102. char c;
  103. int flushed = 0;
  104. sigset_t newset;
  105. sigset_t oldset;
  106. in = fileno(stdin);
  107. out = fileno(stdout);
  108. (void) sigemptyset (&newset);
  109. (void) sigaddset (&newset, SIGINT);
  110. (void) sigaddset (&newset, SIGTSTP);
  111. (void) sigaddset (&newset, SIGQUIT);
  112. (void) sigprocmask (SIG_BLOCK, &newset, &oldset);
  113. if ((is_term = isatty(in)))
  114. {
  115. (void) tcgetattr (in, &term_old);
  116. term_new = term_old;
  117. if (do_echo)
  118. term_new.c_lflag |= ECHO;
  119. else
  120. term_new.c_lflag &= ~ECHO;
  121. if (tcsetattr (in, TCSAFLUSH, &term_new) == 0)
  122. flushed = 1;
  123. }
  124. else
  125. {
  126. is_term = 0;
  127. if ((flags = fcntl(in, F_GETFL, 0)) >= 0) {
  128. old_flags = flags;
  129. flags |= O_NONBLOCK;
  130. fcntl(in, F_SETFL, flags);
  131. while (read(in, &c, 1) > 0)
  132. /* nothing */;
  133. fcntl(in, F_SETFL, old_flags);
  134. flushed = 1;
  135. }
  136. }
  137. (void)write(out, prompt, strlen(prompt));
  138. /* well, this looks ugly, but it handles the following end of line
  139. markers: \r \r\0 \r\n \n \n\r, at least at a second pass */
  140. p = rh->buf;
  141. for (;;)
  142. {
  143. if (read(in, &c, 1) <= 0)
  144. return NULL;
  145. if (!flushed && ((c == '\0') || (c == '\r') || (c == '\n'))) {
  146. flushed = 1;
  147. continue;
  148. }
  149. if ((c == '\r') || (c == '\n'))
  150. break;
  151. flushed = 1;
  152. if (p < rh->buf + GETSTR_LENGTH)
  153. {
  154. if (do_echo && !is_term)
  155. (void)write(out, &c, 1);
  156. *p++ = c;
  157. }
  158. }
  159. *p = '\0';
  160. if (!do_echo || !is_term) (void)write(out, "\r\n", 2);
  161. if (is_term)
  162. tcsetattr (in, TCSAFLUSH, &term_old);
  163. else {
  164. if ((flags = fcntl(in, F_GETFL, 0)) >= 0) {
  165. old_flags = flags;
  166. flags |= O_NONBLOCK;
  167. fcntl(in, F_SETFL, flags);
  168. while (read(in, &c, 1) > 0)
  169. /* nothing */;
  170. fcntl(in, F_SETFL, old_flags);
  171. }
  172. }
  173. (void) sigprocmask (SIG_SETMASK, &oldset, NULL);
  174. #endif
  175. return rh->buf;
  176. }
  177. #endif
  178. void rc_mdelay(int msecs)
  179. {
  180. #if 0
  181. struct timeval tv;
  182. tv.tv_sec = (int) msecs / 1000;
  183. tv.tv_usec = (msecs % 1000) * 1000;
  184. select(0, NULL, NULL, NULL, &tv);
  185. #endif
  186. }
  187. /** Generate a quite unique string
  188. *
  189. * @note not that unique at all...
  190. *
  191. * @param rh a handle to parsed configuration.
  192. * @return unique string. Memory does not need to be freed.
  193. */
  194. char *rc_mksid (rc_handle *rh)
  195. {
  196. //snprintf (rh->buf1, sizeof(rh->buf1), "%08lX%04X", (unsigned long int) time (NULL), (unsigned int) getpid ());
  197. return rh->buf1;
  198. }
  199. /** Initialises new Radius Client handle
  200. *
  201. * @return a new rc_handle (free with rc_destroy).
  202. */
  203. rc_handle *rc_new(void)
  204. {
  205. rc_handle *rh;
  206. rh = malloc(sizeof(*rh));
  207. if (rh == NULL) {
  208. rc_log(LOG_CRIT, "rc_new: out of memory");
  209. return NULL;
  210. }
  211. memset(rh, 0, sizeof(*rh));
  212. return rh;
  213. }
  214. /** Destroys Radius Client handle reclaiming all memory
  215. *
  216. * @param rh The Radius client handle to free.
  217. */
  218. void rc_destroy(rc_handle *rh)
  219. {
  220. rc_map2id_free(rh);
  221. rc_dict_free(rh);
  222. rc_config_free(rh);
  223. free(rh);
  224. }
  225. /** Get next line from the stream.
  226. *
  227. * @param fp a %FILE pointer.
  228. * @param len output length.
  229. * @return the next line in an allocated buffer.
  230. */
  231. char *rc_fgetln(FILE *fp, size_t *len)
  232. {
  233. #if 0
  234. static char *buf = NULL;
  235. static size_t bufsiz = 0;
  236. char *ptr;
  237. if (buf == NULL) {
  238. bufsiz = RC_BUFSIZ;
  239. if ((buf = malloc(bufsiz)) == NULL)
  240. return NULL;
  241. }
  242. if (fgets(buf, (int)bufsiz, fp) == NULL)
  243. return NULL;
  244. *len = 0;
  245. while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
  246. size_t nbufsiz = bufsiz + RC_BUFSIZ;
  247. char *nbuf = realloc(buf, nbufsiz);
  248. if (nbuf == NULL) {
  249. int oerrno = errno;
  250. free(buf);
  251. errno = oerrno;
  252. buf = NULL;
  253. return NULL;
  254. } else
  255. buf = nbuf;
  256. *len = bufsiz;
  257. if (fgets(&buf[bufsiz], RC_BUFSIZ, fp) == NULL)
  258. return buf;
  259. bufsiz = nbufsiz;
  260. }
  261. *len = (ptr - buf) + 1;
  262. return buf;
  263. #endif
  264. }
  265. /** Returns the current time as a double.
  266. *
  267. * @return current time (seconds since epoch) expressed as
  268. * double-precision floating point number.
  269. */
  270. double rc_getctime(void)
  271. {
  272. #if 0
  273. struct timeval timev;
  274. if (gettimeofday(&timev, NULL) == -1)
  275. return -1;
  276. return timev.tv_sec + ((double)timev.tv_usec) / 1000000.0;
  277. #endif
  278. return RTC_GetUnixTime();
  279. }
  280. /*
  281. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  282. *
  283. * Permission to use, copy, modify, and distribute this software for any
  284. * purpose with or without fee is hereby granted, provided that the above
  285. * copyright notice and this permission notice appear in all copies.
  286. *
  287. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  288. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  289. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  290. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  291. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  292. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  293. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  294. *
  295. * Copyright 2006 The FreeRADIUS server project
  296. */
  297. #ifndef HAVE_STRLCPY
  298. /*
  299. * Copy src to string dst of size siz. At most siz-1 characters
  300. * will be copied. Always NUL terminates (unless siz == 0).
  301. * Returns strlen(src); if retval >= siz, truncation occurred.
  302. */
  303. size_t
  304. rc_strlcpy(char *dst, char const *src, size_t siz)
  305. {
  306. char *d = dst;
  307. char const *s = src;
  308. size_t n = siz;
  309. /* Copy as many bytes as will fit */
  310. if (n != 0 && --n != 0) {
  311. do {
  312. if ((*d++ = *s++) == 0)
  313. break;
  314. } while (--n != 0);
  315. }
  316. /* Not enough room in dst, add NUL and traverse rest of src */
  317. if (n == 0) {
  318. if (siz != 0)
  319. *d = '\0'; /* NUL-terminate dst */
  320. while (*s++)
  321. ;
  322. }
  323. return(s - src - 1); /* count does not include NUL */
  324. }
  325. #endif