util.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /*
  39. * util.c: Miscellaneous stuffs
  40. *
  41. * All blame to Mike McCool
  42. */
  43. #include "libadmin/libadmin.h"
  44. #include "base/util.h"
  45. #include "private/pprio.h"
  46. #include <base/file.h>
  47. #ifdef XP_UNIX
  48. #include <dirent.h>
  49. #include <sys/types.h>
  50. #include <fcntl.h>
  51. #else
  52. #include <sys/stat.h>
  53. #endif /* WIN32? */
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <sys/stat.h>
  58. #ifdef XP_UNIX
  59. SYS_FILE lf;
  60. #elif defined(XP_WIN32)
  61. HANDLE lf;
  62. #endif
  63. char *get_flock_path(void)
  64. {
  65. char *result="";
  66. char *port=getenv("SERVER_PORT");
  67. #ifdef XP_UNIX
  68. result=(char *) MALLOC(strlen("/tmp/lock.%%s.")+strlen(port)+4);
  69. sprintf(result, "/tmp/lock.%%s.%s", port);
  70. #endif
  71. return result;
  72. }
  73. /* Open a file with locking, close a file with unlocking. */
  74. FILE *fopen_l(char *path, char *mode)
  75. {
  76. FILE *f = fopen(path, mode);
  77. char *lockpath;
  78. char *sn="admserv";
  79. char *flp=get_flock_path();
  80. if(f == NULL) return NULL;
  81. lockpath=(char *) MALLOC(strlen(sn)+strlen(flp)+16);
  82. sprintf(lockpath, flp, sn);
  83. #ifdef XP_UNIX
  84. if( (lf=system_fopenRW(lockpath)) == SYS_ERROR_FD)
  85. report_error(FILE_ERROR, lockpath, "Could not open file.");
  86. if(system_flock(lf)==IO_ERROR)
  87. report_error(FILE_ERROR, lockpath, "Could not lock file.");
  88. #elif defined(XP_WIN32)
  89. /* Using mutexes because if the CGI program dies, the mutex will be
  90. * automatically released by the OS for another process to grab.
  91. * Semaphores do not have this property; and if the CGI program crashes,
  92. * the admin server would be effectively crippled.
  93. */
  94. if ( (lf = CreateMutex(NULL, 0, lockpath)) == NULL) {
  95. report_error(FILE_ERROR, lockpath, "Could not create admin mutex.");
  96. } else {
  97. if ( WaitForSingleObject(lf, 60*1000) == WAIT_FAILED) {
  98. report_error(FILE_ERROR, lockpath, "Unable to obtain mutex after 60 seconds.");
  99. }
  100. }
  101. #endif /* XP_UNIX */
  102. return f;
  103. }
  104. void fclose_l(FILE *f)
  105. {
  106. fclose(f);
  107. #ifdef XP_UNIX
  108. if(system_ulock(lf)==IO_ERROR)
  109. report_error(FILE_ERROR, NULL, "Could not unlock lock file.");
  110. system_fclose(lf);
  111. #elif defined(XP_WIN32)
  112. if (lf) {
  113. ReleaseMutex(lf);
  114. CloseHandle(lf);
  115. }
  116. #endif /* XP_UNIX */
  117. }
  118. char *alert_word_wrap(char *str, int width, char *linefeed)
  119. {
  120. char *ans = NULL;
  121. int counter=0;
  122. int lsc=0, lsa=0;
  123. register int strc=0, ansc=0;
  124. register int x=0;
  125. /* assume worst case */
  126. ans = (char *) MALLOC((strlen(str)*strlen(linefeed))+32);
  127. for(strc=0, ansc=0; str[strc]; /*none*/) {
  128. if(str[strc]=='\n') {
  129. counter=0;
  130. lsc=0, lsa=0;
  131. for(x=0; linefeed[x]; x++) {
  132. ans[ansc++]=linefeed[x];
  133. }
  134. strc++;
  135. } else if(str[strc]=='\r') {
  136. strc++;
  137. } else if(str[strc]=='\\') {
  138. ans[ansc++]='\\';
  139. ans[ansc++]=strc++;
  140. } else {
  141. if(counter==width) {
  142. if(lsc && lsa) {
  143. strc=lsc;
  144. ansc=lsa;
  145. counter=0;
  146. lsc=0, lsa=0;
  147. for(x=0; linefeed[x]; x++) {
  148. ans[ansc++]=linefeed[x];
  149. }
  150. strc++;
  151. } else {
  152. /* else, you're a loser, I'm breaking your big word anyway */
  153. counter=0;
  154. lsc=0, lsa=0;
  155. for(x=0; linefeed[x]; x++) {
  156. ans[ansc++]=linefeed[x];
  157. }
  158. strc++;
  159. }
  160. } else {
  161. if(str[strc] == ' ') {
  162. lsc=strc;
  163. lsa=ansc;
  164. }
  165. ans[ansc++]=str[strc++];
  166. counter++;
  167. }
  168. }
  169. }
  170. ans[ansc]='\0';
  171. return ans;
  172. }
  173. /***********************************************************************
  174. ** FUNCTION: cookieValue
  175. ** DESCRIPTION:
  176. ** Get the current value of the cookie variable
  177. ** INPUTS: var - the name of the cookie variable
  178. ** val - if non-NULL, set the in-memory copy of the var
  179. ** OUTPUTS: None
  180. ** RETURN: NULL if the var doesn't exist, else the value
  181. ** SIDE EFFECTS:
  182. ** Eats memory
  183. ** RESTRICTIONS:
  184. ** Don't screw around with the returned string, if anything else wants
  185. ** to use it.
  186. ** MEMORY: This is a memory leak, so only use it in CGIs
  187. ** ALGORITHM:
  188. ** If it's never been called, build a memory structure of the
  189. ** cookie variables.
  190. ** Look for the passed variable, and return its value, or NULL
  191. ***********************************************************************/
  192. NSAPI_PUBLIC char *
  193. cookieValue( char *var, char *val )
  194. {
  195. static char **vars = NULL;
  196. static char **vals = NULL;
  197. static int numVars = -1;
  198. int i;
  199. if ( numVars == -1 ) { /* first time, init the structure */
  200. char *cookie = getenv( "HTTP_COOKIE" );
  201. if ( cookie && *cookie ) {
  202. int len = strlen( cookie );
  203. int foundVal = 0;
  204. cookie = STRDUP( cookie );
  205. numVars = 0;
  206. vars = (char **)MALLOC( sizeof( char * ) );
  207. vals = (char **)MALLOC( sizeof( char * ) );
  208. vars[0] = cookie;
  209. for ( i = 0 ; i < len ; ++i ) {
  210. if ( ( ! foundVal ) && ( cookie[i] == '=' ) ) {
  211. vals[numVars++] = cookie + i + 1;
  212. cookie[i] = '\0';
  213. foundVal = 1;
  214. } else if ( ( cookie[i] == ';' ) && ( cookie[i+1] == ' ' ) ) {
  215. cookie[i] = '\0';
  216. vals = (char **) REALLOC( vals,
  217. sizeof( char * ) * ( numVars + 1 ) );
  218. vars = (char **) REALLOC( vars,
  219. sizeof( char * ) * ( numVars + 1 ) );
  220. vars[numVars] = cookie + i + 2;
  221. i += 2;
  222. foundVal = 0;
  223. }
  224. }
  225. } else { /* no cookie, no vars */
  226. numVars = 0;
  227. }
  228. }
  229. for ( i = 0 ; i < numVars ; ++i ) {
  230. if ( strcmp( vars[i], var ) == 0 ) {
  231. if ( val ) {
  232. vals[i] = STRDUP( val );
  233. } else {
  234. return vals[i];
  235. }
  236. }
  237. }
  238. return NULL;
  239. }
  240. static int adm_initialized=0;
  241. /* Initialize NSPR for all the base functions we use */
  242. NSAPI_PUBLIC int ADM_Init(void)
  243. {
  244. if(!adm_initialized) {
  245. NSPR_INIT("AdminPrograms");
  246. adm_initialized=1;
  247. }
  248. return 0;
  249. }