util.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2021 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /*
  13. * util.c: Miscellaneous stuffs
  14. *
  15. * All blame to Mike McCool
  16. */
  17. #include "libadmin/libadmin.h"
  18. #include "base/util.h"
  19. #include "private/pprio.h"
  20. #include <base/file.h>
  21. #include <dirent.h>
  22. #include <sys/types.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. SYS_FILE lf;
  29. char *
  30. get_flock_path(void)
  31. {
  32. char *result = "";
  33. char *port = getenv("SERVER_PORT");
  34. if (port == NULL) {
  35. port = "";
  36. }
  37. result = (char *)MALLOC(strlen("/tmp/lock.%%s.") + strlen(port) + 4);
  38. if (result != NULL) {
  39. sprintf(result, "/tmp/lock.%%s.%s", port);
  40. }
  41. return result;
  42. }
  43. char *
  44. alert_word_wrap(char *str, int width, char *linefeed)
  45. {
  46. char *ans = NULL;
  47. int counter = 0;
  48. int lsc = 0, lsa = 0;
  49. int strc = 0, ansc = 0;
  50. int x = 0;
  51. /* assume worst case */
  52. ans = (char *)MALLOC((strlen(str) * strlen(linefeed)) + 32);
  53. if (ans == NULL) {
  54. return NULL;
  55. }
  56. for (strc = 0, ansc = 0; str[strc]; /*none*/) {
  57. if (str[strc] == '\n') {
  58. counter = 0;
  59. lsc = 0, lsa = 0;
  60. for (x = 0; linefeed[x]; x++) {
  61. ans[ansc++] = linefeed[x];
  62. }
  63. strc++;
  64. } else if (str[strc] == '\r') {
  65. strc++;
  66. } else if (str[strc] == '\\') {
  67. ans[ansc++] = '\\';
  68. ans[ansc++] = strc++;
  69. } else {
  70. if (counter == width) {
  71. if (lsc && lsa) {
  72. strc = lsc;
  73. ansc = lsa;
  74. counter = 0;
  75. lsc = 0, lsa = 0;
  76. for (x = 0; linefeed[x]; x++) {
  77. ans[ansc++] = linefeed[x];
  78. }
  79. strc++;
  80. } else {
  81. /* else, you're a loser, I'm breaking your big word anyway */
  82. counter = 0;
  83. lsc = 0, lsa = 0;
  84. for (x = 0; linefeed[x]; x++) {
  85. ans[ansc++] = linefeed[x];
  86. }
  87. strc++;
  88. }
  89. } else {
  90. if (str[strc] == ' ') {
  91. lsc = strc;
  92. lsa = ansc;
  93. }
  94. ans[ansc++] = str[strc++];
  95. counter++;
  96. }
  97. }
  98. }
  99. ans[ansc] = '\0';
  100. return ans;
  101. }
  102. /***********************************************************************
  103. ** FUNCTION: cookieValue
  104. ** DESCRIPTION:
  105. ** Get the current value of the cookie variable
  106. ** INPUTS: var - the name of the cookie variable
  107. ** val - if non-NULL, set the in-memory copy of the var
  108. ** OUTPUTS: None
  109. ** RETURN: NULL if the var doesn't exist, else the value
  110. ** SIDE EFFECTS:
  111. ** Eats memory
  112. ** RESTRICTIONS:
  113. ** Don't screw around with the returned string, if anything else wants
  114. ** to use it.
  115. ** MEMORY: This is a memory leak, so only use it in CGIs
  116. ** ALGORITHM:
  117. ** If it's never been called, build a memory structure of the
  118. ** cookie variables.
  119. ** Look for the passed variable, and return its value, or NULL
  120. ***********************************************************************/
  121. NSAPI_PUBLIC char *
  122. cookieValue(char *var, char *val)
  123. {
  124. static char **vars = NULL;
  125. static char **vals = NULL;
  126. static int numVars = -1;
  127. int i;
  128. if (numVars == -1) { /* first time, init the structure */
  129. char *cookie = getenv("HTTP_COOKIE");
  130. if (cookie && *cookie) {
  131. int len = strlen(cookie);
  132. int foundVal = 0;
  133. cookie = STRDUP(cookie);
  134. if (cookie == NULL) {
  135. return NULL;
  136. }
  137. numVars = 0;
  138. vars = (char **)MALLOC(sizeof(char *));
  139. if (vars == NULL) {
  140. FREE(cookie);
  141. return NULL;
  142. }
  143. vals = (char **)MALLOC(sizeof(char *));
  144. if (vals == NULL) {
  145. FREE(cookie);
  146. FREE(vars);
  147. return NULL;
  148. }
  149. vars[0] = cookie;
  150. for (i = 0; i < len; ++i) {
  151. if ((!foundVal) && (cookie[i] == '=')) {
  152. vals[numVars++] = cookie + i + 1;
  153. cookie[i] = '\0';
  154. foundVal = 1;
  155. } else if ((cookie[i] == ';') && (cookie[i + 1] == ' ')) {
  156. cookie[i] = '\0';
  157. vals = (char **)REALLOC(vals,
  158. sizeof(char *) * (numVars + 1));
  159. vars = (char **)REALLOC(vars,
  160. sizeof(char *) * (numVars + 1));
  161. vars[numVars] = cookie + i + 2;
  162. i += 2;
  163. foundVal = 0;
  164. }
  165. }
  166. } else { /* no cookie, no vars */
  167. numVars = 0;
  168. }
  169. }
  170. for (i = 0; i < numVars; ++i) {
  171. if (strcmp(vars[i], var) == 0) {
  172. if (val) {
  173. vals[i] = STRDUP(val);
  174. } else {
  175. return vals[i];
  176. }
  177. }
  178. }
  179. return NULL;
  180. }
  181. static int adm_initialized = 0;
  182. /* Initialize NSPR for all the base functions we use */
  183. NSAPI_PUBLIC int
  184. ADM_Init(void)
  185. {
  186. if (!adm_initialized) {
  187. NSPR_INIT("AdminPrograms");
  188. adm_initialized = 1;
  189. }
  190. return 0;
  191. }