util.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * util.c: Miscellaneous stuffs
  43. *
  44. * All blame to Mike McCool
  45. */
  46. #include "libadmin/libadmin.h"
  47. #include "base/util.h"
  48. #include "private/pprio.h"
  49. #include <base/file.h>
  50. #ifdef XP_UNIX
  51. #include <dirent.h>
  52. #include <sys/types.h>
  53. #include <fcntl.h>
  54. #else
  55. #include <sys/stat.h>
  56. #endif /* WIN32? */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <sys/stat.h>
  61. #ifdef XP_UNIX
  62. SYS_FILE lf;
  63. #elif defined(XP_WIN32)
  64. HANDLE lf;
  65. #endif
  66. char *get_flock_path(void)
  67. {
  68. char *result="";
  69. char *port=getenv("SERVER_PORT");
  70. #ifdef XP_UNIX
  71. result=(char *) MALLOC(strlen("/tmp/lock.%%s.")+strlen(port)+4);
  72. sprintf(result, "/tmp/lock.%%s.%s", port);
  73. #endif
  74. return result;
  75. }
  76. /* Open a file with locking, close a file with unlocking. */
  77. FILE *fopen_l(char *path, char *mode)
  78. {
  79. FILE *f = fopen(path, mode);
  80. char *lockpath;
  81. char *sn="admserv";
  82. char *flp=get_flock_path();
  83. if(f == NULL) return NULL;
  84. lockpath=(char *) MALLOC(strlen(sn)+strlen(flp)+16);
  85. sprintf(lockpath, flp, sn);
  86. #ifdef XP_UNIX
  87. if( (lf=system_fopenRW(lockpath)) == SYS_ERROR_FD)
  88. report_error(FILE_ERROR, lockpath, "Could not open file.");
  89. if(system_flock(lf)==IO_ERROR)
  90. report_error(FILE_ERROR, lockpath, "Could not lock file.");
  91. #elif defined(XP_WIN32)
  92. /* Using mutexes because if the CGI program dies, the mutex will be
  93. * automatically released by the OS for another process to grab.
  94. * Semaphores do not have this property; and if the CGI program crashes,
  95. * the admin server would be effectively crippled.
  96. */
  97. if ( (lf = CreateMutex(NULL, 0, lockpath)) == NULL) {
  98. report_error(FILE_ERROR, lockpath, "Could not create admin mutex.");
  99. } else {
  100. if ( WaitForSingleObject(lf, 60*1000) == WAIT_FAILED) {
  101. report_error(FILE_ERROR, lockpath, "Unable to obtain mutex after 60 seconds.");
  102. }
  103. }
  104. #endif /* XP_UNIX */
  105. return f;
  106. }
  107. void fclose_l(FILE *f)
  108. {
  109. fclose(f);
  110. #ifdef XP_UNIX
  111. if(system_ulock(lf)==IO_ERROR)
  112. report_error(FILE_ERROR, NULL, "Could not unlock lock file.");
  113. system_fclose(lf);
  114. #elif defined(XP_WIN32)
  115. if (lf) {
  116. ReleaseMutex(lf);
  117. CloseHandle(lf);
  118. }
  119. #endif /* XP_UNIX */
  120. }
  121. char *alert_word_wrap(char *str, int width, char *linefeed)
  122. {
  123. char *ans = NULL;
  124. int counter=0;
  125. int lsc=0, lsa=0;
  126. register int strc=0, ansc=0;
  127. register int x=0;
  128. /* assume worst case */
  129. ans = (char *) MALLOC((strlen(str)*strlen(linefeed))+32);
  130. for(strc=0, ansc=0; str[strc]; /*none*/) {
  131. if(str[strc]=='\n') {
  132. counter=0;
  133. lsc=0, lsa=0;
  134. for(x=0; linefeed[x]; x++) {
  135. ans[ansc++]=linefeed[x];
  136. }
  137. strc++;
  138. } else if(str[strc]=='\r') {
  139. strc++;
  140. } else if(str[strc]=='\\') {
  141. ans[ansc++]='\\';
  142. ans[ansc++]=strc++;
  143. } else {
  144. if(counter==width) {
  145. if(lsc && lsa) {
  146. strc=lsc;
  147. ansc=lsa;
  148. counter=0;
  149. lsc=0, lsa=0;
  150. for(x=0; linefeed[x]; x++) {
  151. ans[ansc++]=linefeed[x];
  152. }
  153. strc++;
  154. } else {
  155. /* else, you're a loser, I'm breaking your big word anyway */
  156. counter=0;
  157. lsc=0, lsa=0;
  158. for(x=0; linefeed[x]; x++) {
  159. ans[ansc++]=linefeed[x];
  160. }
  161. strc++;
  162. }
  163. } else {
  164. if(str[strc] == ' ') {
  165. lsc=strc;
  166. lsa=ansc;
  167. }
  168. ans[ansc++]=str[strc++];
  169. counter++;
  170. }
  171. }
  172. }
  173. ans[ansc]='\0';
  174. return ans;
  175. }
  176. /***********************************************************************
  177. ** FUNCTION: cookieValue
  178. ** DESCRIPTION:
  179. ** Get the current value of the cookie variable
  180. ** INPUTS: var - the name of the cookie variable
  181. ** val - if non-NULL, set the in-memory copy of the var
  182. ** OUTPUTS: None
  183. ** RETURN: NULL if the var doesn't exist, else the value
  184. ** SIDE EFFECTS:
  185. ** Eats memory
  186. ** RESTRICTIONS:
  187. ** Don't screw around with the returned string, if anything else wants
  188. ** to use it.
  189. ** MEMORY: This is a memory leak, so only use it in CGIs
  190. ** ALGORITHM:
  191. ** If it's never been called, build a memory structure of the
  192. ** cookie variables.
  193. ** Look for the passed variable, and return its value, or NULL
  194. ***********************************************************************/
  195. NSAPI_PUBLIC char *
  196. cookieValue( char *var, char *val )
  197. {
  198. static char **vars = NULL;
  199. static char **vals = NULL;
  200. static int numVars = -1;
  201. int i;
  202. if ( numVars == -1 ) { /* first time, init the structure */
  203. char *cookie = getenv( "HTTP_COOKIE" );
  204. if ( cookie && *cookie ) {
  205. int len = strlen( cookie );
  206. int foundVal = 0;
  207. cookie = STRDUP( cookie );
  208. numVars = 0;
  209. vars = (char **)MALLOC( sizeof( char * ) );
  210. vals = (char **)MALLOC( sizeof( char * ) );
  211. vars[0] = cookie;
  212. for ( i = 0 ; i < len ; ++i ) {
  213. if ( ( ! foundVal ) && ( cookie[i] == '=' ) ) {
  214. vals[numVars++] = cookie + i + 1;
  215. cookie[i] = '\0';
  216. foundVal = 1;
  217. } else if ( ( cookie[i] == ';' ) && ( cookie[i+1] == ' ' ) ) {
  218. cookie[i] = '\0';
  219. vals = (char **) REALLOC( vals,
  220. sizeof( char * ) * ( numVars + 1 ) );
  221. vars = (char **) REALLOC( vars,
  222. sizeof( char * ) * ( numVars + 1 ) );
  223. vars[numVars] = cookie + i + 2;
  224. i += 2;
  225. foundVal = 0;
  226. }
  227. }
  228. } else { /* no cookie, no vars */
  229. numVars = 0;
  230. }
  231. }
  232. for ( i = 0 ; i < numVars ; ++i ) {
  233. if ( strcmp( vars[i], var ) == 0 ) {
  234. if ( val ) {
  235. vals[i] = STRDUP( val );
  236. } else {
  237. return vals[i];
  238. }
  239. }
  240. }
  241. return NULL;
  242. }
  243. static int adm_initialized=0;
  244. /* Initialize NSPR for all the base functions we use */
  245. NSAPI_PUBLIC int ADM_Init(void)
  246. {
  247. if(!adm_initialized) {
  248. NSPR_INIT("AdminPrograms");
  249. adm_initialized=1;
  250. }
  251. return 0;
  252. }