]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/var.c
6d02e10794537917c21282157c4b56e2cdbe1c07
2 * CGI form variable and array functions for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2005 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
19 #include "cgi-private.h"
20 #include <cups/http.h>
21 #include <cups/md5-private.h>
28 #define CUPS_SID "org.cups.sid"
32 * Data structure to hold all the CGI form variables and arrays...
35 typedef struct /**** Form variable structure ****/
37 const char *name
; /* Name of variable */
38 int nvalues
, /* Number of values */
39 avalues
; /* Number of values allocated */
40 const char **values
; /* Value(s) of variable */
48 static int num_cookies
= 0;/* Number of cookies */
49 static cups_option_t
*cookies
= NULL
;/* Cookies */
50 static int form_count
= 0, /* Form variable count */
51 form_alloc
= 0; /* Number of variables allocated */
52 static _cgi_var_t
*form_vars
= NULL
;
54 static cgi_file_t
*form_file
= NULL
;
62 static void cgi_add_variable(const char *name
, int element
,
64 static int cgi_compare_variables(const _cgi_var_t
*v1
,
65 const _cgi_var_t
*v2
);
66 static _cgi_var_t
*cgi_find_variable(const char *name
);
67 static void cgi_initialize_cookies(void);
68 static int cgi_initialize_get(void);
69 static int cgi_initialize_multipart(const char *boundary
);
70 static int cgi_initialize_post(void);
71 static int cgi_initialize_string(const char *data
);
72 static const char *cgi_passwd(const char *prompt
);
73 static const char *cgi_set_sid(void);
74 static void cgi_sort_variables(void);
75 static void cgi_unlink_file(void);
79 * 'cgiCheckVariables()' - Check for the presence of "required" variables.
81 * Names may be separated by spaces and/or commas.
84 int /* O - 1 if all variables present, 0 otherwise */
85 cgiCheckVariables(const char *names
) /* I - Variables to look for */
87 char name
[255], /* Current variable name */
88 *s
; /* Pointer in string */
89 const char *val
; /* Value of variable */
90 int element
; /* Array element number */
96 while (*names
!= '\0')
98 while (*names
== ' ' || *names
== ',')
101 for (s
= name
; *names
!= '\0' && *names
!= ' ' && *names
!= ','; s
++, names
++)
108 if ((s
= strrchr(name
, '-')) != NULL
)
111 element
= atoi(s
+ 1) - 1;
112 val
= cgiGetArray(name
, element
);
115 val
= cgiGetVariable(name
);
121 return (0); /* Can't be blank, either! */
129 * 'cgiClearVariables()' - Clear all form variables.
133 cgiClearVariables(void)
135 int i
, j
; /* Looping vars */
136 _cgi_var_t
*v
; /* Current variable */
139 fputs("DEBUG: cgiClearVariables called.\n", stderr
);
141 for (v
= form_vars
, i
= form_count
; i
> 0; v
++, i
--)
143 _cupsStrFree(v
->name
);
144 for (j
= 0; j
< v
->nvalues
; j
++)
146 _cupsStrFree(v
->values
[j
]);
156 * 'cgiGetArray()' - Get an element from a form array.
159 const char * /* O - Element value or NULL */
160 cgiGetArray(const char *name
, /* I - Name of array variable */
161 int element
) /* I - Element number (0 to N) */
163 _cgi_var_t
*var
; /* Pointer to variable */
166 if ((var
= cgi_find_variable(name
)) == NULL
)
169 if (element
< 0 || element
>= var
->nvalues
)
172 return (_cupsStrRetain(var
->values
[element
]));
177 * 'cgiGetCookie()' - Get a cookie value.
180 const char * /* O - Value or NULL */
181 cgiGetCookie(const char *name
) /* I - Name of cookie */
183 return (cupsGetOption(name
, num_cookies
, cookies
));
188 * 'cgiGetFile()' - Get the file (if any) that was submitted in the form.
191 const cgi_file_t
* /* O - Attached file or NULL */
199 * 'cgiGetSize()' - Get the size of a form array value.
202 int /* O - Number of elements */
203 cgiGetSize(const char *name
) /* I - Name of variable */
205 _cgi_var_t
*var
; /* Pointer to variable */
208 if ((var
= cgi_find_variable(name
)) == NULL
)
211 return (var
->nvalues
);
216 * 'cgiGetVariable()' - Get a CGI variable from the database.
218 * Returns NULL if the variable doesn't exist. If the variable is an
219 * array of values, returns the last element.
222 const char * /* O - Value of variable */
223 cgiGetVariable(const char *name
) /* I - Name of variable */
225 const _cgi_var_t
*var
; /* Returned variable */
228 var
= cgi_find_variable(name
);
232 DEBUG_printf(("cgiGetVariable(\"%s\") is returning NULL...\n", name
));
234 DEBUG_printf(("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name
,
235 var
->values
[var
->nvalues
- 1]));
238 return ((var
== NULL
) ? NULL
: _cupsStrRetain(var
->values
[var
->nvalues
- 1]));
243 * 'cgiInitialize()' - Initialize the CGI variable "database".
246 int /* O - Non-zero if there was form data */
249 const char *method
, /* Form posting method */
250 *content_type
, /* Content-Type of post data */
251 *cups_sid_cookie
, /* SID cookie */
252 *cups_sid_form
; /* SID form variable */
256 * Setup a password callback for authentication...
259 cupsSetPasswordCB(cgi_passwd
);
262 * Set the locale so that times, etc. are formatted properly...
265 setlocale(LC_ALL
, "");
269 * Disable output buffering to find bugs...
272 setbuf(stdout
, NULL
);
279 cgi_initialize_cookies();
281 if ((cups_sid_cookie
= cgiGetCookie(CUPS_SID
)) == NULL
)
283 fputs("DEBUG: " CUPS_SID
" cookie not found, initializing!\n", stderr
);
284 cups_sid_cookie
= cgi_set_sid();
287 fprintf(stderr
, "DEBUG: " CUPS_SID
" cookie is \"%s\"\n", cups_sid_cookie
);
290 * Get the request method (GET or POST)...
293 method
= getenv("REQUEST_METHOD");
294 content_type
= getenv("CONTENT_TYPE");
299 * Grab form data from the corresponding location...
302 if (!_cups_strcasecmp(method
, "GET"))
303 return (cgi_initialize_get());
304 else if (!_cups_strcasecmp(method
, "POST") && content_type
)
306 const char *boundary
= strstr(content_type
, "boundary=");
311 if (content_type
&& !strncmp(content_type
, "multipart/form-data; ", 21))
313 if (!cgi_initialize_multipart(boundary
))
316 else if (!cgi_initialize_post())
319 if ((cups_sid_form
= cgiGetVariable(CUPS_SID
)) == NULL
||
320 strcmp(cups_sid_cookie
, cups_sid_form
))
323 fprintf(stderr
, "DEBUG: " CUPS_SID
" form variable is \"%s\"\n",
326 fputs("DEBUG: " CUPS_SID
" form variable is not present.\n", stderr
);
340 * 'cgiIsPOST()' - Determine whether this page was POSTed.
343 int /* O - 1 if POST, 0 if GET */
346 const char *method
; /* REQUEST_METHOD environment variable */
349 if ((method
= getenv("REQUEST_METHOD")) == NULL
)
352 return (!strcmp(method
, "POST"));
357 * 'cgiSetArray()' - Set array element N to the specified string.
359 * If the variable array is smaller than (element + 1), the intervening
360 * elements are set to NULL.
364 cgiSetArray(const char *name
, /* I - Name of variable */
365 int element
, /* I - Element number (0 to N) */
366 const char *value
) /* I - Value of variable */
368 int i
; /* Looping var */
369 _cgi_var_t
*var
; /* Returned variable */
372 if (name
== NULL
|| value
== NULL
|| element
< 0 || element
> 100000)
375 fprintf(stderr
, "DEBUG: cgiSetArray: %s[%d]=\"%s\"\n", name
, element
, value
);
377 if ((var
= cgi_find_variable(name
)) == NULL
)
379 cgi_add_variable(name
, element
, value
);
380 cgi_sort_variables();
384 if (element
>= var
->avalues
)
386 const char **temp
; /* Temporary pointer */
388 temp
= (const char **)realloc((void *)(var
->values
),
389 sizeof(char *) * (size_t)(element
+ 16));
393 var
->avalues
= element
+ 16;
397 if (element
>= var
->nvalues
)
399 for (i
= var
->nvalues
; i
< element
; i
++)
400 var
->values
[i
] = NULL
;
402 var
->nvalues
= element
+ 1;
404 else if (var
->values
[element
])
405 _cupsStrFree((char *)var
->values
[element
]);
407 var
->values
[element
] = _cupsStrAlloc(value
);
413 * 'cgiSetCookie()' - Set a cookie value.
417 cgiSetCookie(const char *name
, /* I - Name */
418 const char *value
, /* I - Value */
419 const char *path
, /* I - Path (typically "/") */
420 const char *domain
, /* I - Domain name */
421 time_t expires
, /* I - Expiration date (0 for session) */
422 int secure
) /* I - Require SSL */
424 num_cookies
= cupsAddOption(name
, value
, num_cookies
, &cookies
);
426 printf("Set-Cookie: %s=%s;", name
, value
);
428 printf(" path=%s;", path
);
430 printf(" domain=%s;", domain
);
433 char date
[256]; /* Date string */
435 printf(" expires=%s;", httpGetDateString2(expires
, date
, sizeof(date
)));
438 puts(" httponly; secure;");
445 * 'cgiSetSize()' - Set the array size.
449 cgiSetSize(const char *name
, /* I - Name of variable */
450 int size
) /* I - Number of elements (0 to N) */
452 int i
; /* Looping var */
453 _cgi_var_t
*var
; /* Returned variable */
456 if (name
== NULL
|| size
< 0 || size
> 100000)
459 if ((var
= cgi_find_variable(name
)) == NULL
)
462 if (size
>= var
->avalues
)
464 const char **temp
; /* Temporary pointer */
466 temp
= (const char **)realloc((void *)(var
->values
),
467 sizeof(char *) * (size_t)(size
+ 16));
471 var
->avalues
= size
+ 16;
475 if (size
> var
->nvalues
)
477 for (i
= var
->nvalues
; i
< size
; i
++)
478 var
->values
[i
] = NULL
;
480 else if (size
< var
->nvalues
)
482 for (i
= size
; i
< var
->nvalues
; i
++)
484 _cupsStrFree((void *)(var
->values
[i
]));
492 * 'cgiSetVariable()' - Set a CGI variable in the database.
494 * If the variable is an array, this truncates the array to a single element.
498 cgiSetVariable(const char *name
, /* I - Name of variable */
499 const char *value
) /* I - Value of variable */
501 int i
; /* Looping var */
502 _cgi_var_t
*var
; /* Returned variable */
505 if (name
== NULL
|| value
== NULL
)
508 fprintf(stderr
, "cgiSetVariable: %s=\"%s\"\n", name
, value
);
510 if ((var
= cgi_find_variable(name
)) == NULL
)
512 cgi_add_variable(name
, 0, value
);
513 cgi_sort_variables();
517 for (i
= 0; i
< var
->nvalues
; i
++)
519 _cupsStrFree((char *)var
->values
[i
]);
521 var
->values
[0] = _cupsStrAlloc(value
);
528 * 'cgi_add_variable()' - Add a form variable.
532 cgi_add_variable(const char *name
, /* I - Variable name */
533 int element
, /* I - Array element number */
534 const char *value
) /* I - Variable value */
536 _cgi_var_t
*var
; /* New variable */
539 if (name
== NULL
|| value
== NULL
|| element
< 0 || element
> 100000)
542 DEBUG_printf(("cgi_add_variable: Adding variable \'%s\' with value "
543 "\'%s\'...\n", name
, value
));
545 if (form_count
>= form_alloc
)
547 _cgi_var_t
*temp_vars
; /* Temporary form pointer */
551 temp_vars
= malloc(sizeof(_cgi_var_t
) * 16);
553 temp_vars
= realloc(form_vars
, (size_t)(form_alloc
+ 16) * sizeof(_cgi_var_t
));
558 form_vars
= temp_vars
;
562 var
= form_vars
+ form_count
;
564 if ((var
->values
= calloc((size_t)element
+ 1, sizeof(char *))) == NULL
)
567 var
->name
= _cupsStrAlloc(name
);
568 var
->nvalues
= element
+ 1;
569 var
->avalues
= element
+ 1;
570 var
->values
[element
] = _cupsStrAlloc(value
);
577 * 'cgi_compare_variables()' - Compare two variables.
580 static int /* O - Result of comparison */
581 cgi_compare_variables(
582 const _cgi_var_t
*v1
, /* I - First variable */
583 const _cgi_var_t
*v2
) /* I - Second variable */
585 return (_cups_strcasecmp(v1
->name
, v2
->name
));
590 * 'cgi_find_variable()' - Find a variable.
593 static _cgi_var_t
* /* O - Variable pointer or NULL */
594 cgi_find_variable(const char *name
) /* I - Name of variable */
596 _cgi_var_t key
; /* Search key */
599 if (form_count
< 1 || name
== NULL
)
604 return ((_cgi_var_t
*)bsearch(&key
, form_vars
, (size_t)form_count
, sizeof(_cgi_var_t
),
605 (int (*)(const void *, const void *))cgi_compare_variables
));
610 * 'cgi_initialize_cookies()' - Initialize cookies.
614 cgi_initialize_cookies(void)
616 const char *cookie
; /* HTTP_COOKIE environment variable */
617 char name
[128], /* Name string */
618 value
[512], /* Value string */
619 *ptr
; /* Pointer into name/value */
622 if ((cookie
= getenv("HTTP_COOKIE")) == NULL
)
627 int skip
= 0; /* Skip this cookie? */
630 * Skip leading whitespace...
633 while (isspace(*cookie
& 255))
642 for (ptr
= name
; *cookie
&& *cookie
!= '=';)
643 if (ptr
< (name
+ sizeof(name
) - 1))
665 for (cookie
++, ptr
= value
; *cookie
&& *cookie
!= '\"';)
666 if (ptr
< (value
+ sizeof(value
) - 1))
683 for (ptr
= value
; *cookie
&& *cookie
!= ';';)
684 if (ptr
< (value
+ sizeof(value
) - 1))
703 * Then add the cookie to an array as long as the name doesn't start with
707 if (name
[0] != '$' && !skip
)
708 num_cookies
= cupsAddOption(name
, value
, num_cookies
, &cookies
);
714 * 'cgi_initialize_get()' - Initialize form variables using the GET method.
717 static int /* O - 1 if form data read */
718 cgi_initialize_get(void)
720 char *data
; /* Pointer to form data string */
723 DEBUG_puts("cgi_initialize_get: Initializing variables using GET method...");
726 * Check to see if there is anything for us to read...
729 data
= getenv("QUERY_STRING");
730 if (data
== NULL
|| strlen(data
) == 0)
734 * Parse it out and return...
737 return (cgi_initialize_string(data
));
742 * 'cgi_initialize_multipart()' - Initialize variables and file using the POST
745 * TODO: Update to support files > 2GB.
748 static int /* O - 1 if form data was read */
749 cgi_initialize_multipart(
750 const char *boundary
) /* I - Boundary string */
752 char line
[10240], /* MIME header line */
753 name
[1024], /* Form variable name */
754 filename
[1024], /* Form filename */
755 mimetype
[1024], /* MIME media type */
756 bstring
[256], /* Boundary string to look for */
757 *ptr
, /* Pointer into name/filename */
758 *end
; /* End of buffer */
759 int ch
, /* Character from file */
760 fd
; /* Temporary file descriptor */
761 size_t blen
; /* Length of boundary string */
764 DEBUG_printf(("cgi_initialize_multipart(boundary=\"%s\")\n", boundary
));
767 * Read multipart form data until we run out...
774 snprintf(bstring
, sizeof(bstring
), "\r\n--%s", boundary
);
775 blen
= strlen(bstring
);
777 while (fgets(line
, sizeof(line
), stdin
))
779 if (!strcmp(line
, "\r\n"))
782 * End of headers, grab value...
788 * Read an embedded file...
794 * Remove previous file...
801 * Allocate memory for the new file...
804 if ((form_file
= calloc(1, sizeof(cgi_file_t
))) == NULL
)
807 form_file
->name
= strdup(name
);
808 form_file
->filename
= strdup(filename
);
809 form_file
->mimetype
= strdup(mimetype
);
811 fd
= cupsTempFd(form_file
->tempfile
, sizeof(form_file
->tempfile
));
816 atexit(cgi_unlink_file
);
819 * Copy file data to the temp file...
824 while ((ch
= getchar()) != EOF
)
828 if ((size_t)(ptr
- line
) >= blen
&& !memcmp(ptr
- blen
, bstring
, blen
))
834 if ((ptr
- line
- (int)blen
) >= 8192)
837 * Write out the first 8k of the buffer...
840 write(fd
, line
, 8192);
841 memmove(line
, line
+ 8192, (size_t)(ptr
- line
- 8192));
847 * Write the rest of the data and close the temp file...
851 write(fd
, line
, (size_t)(ptr
- line
));
858 * Just get a form variable; the current code only handles
859 * form values up to 10k in size...
863 end
= line
+ sizeof(line
) - 1;
865 while ((ch
= getchar()) != EOF
)
870 if ((size_t)(ptr
- line
) >= blen
&& !memcmp(ptr
- blen
, bstring
, blen
))
880 * Set the form variable...
883 if ((ptr
= strrchr(name
, '-')) != NULL
&& isdigit(ptr
[1] & 255))
886 * Set a specific index in the array...
891 cgiSetArray(name
, atoi(ptr
) - 1, line
);
893 else if (cgiGetVariable(name
))
896 * Add another element in the array...
899 cgiSetArray(name
, cgiGetSize(name
), line
);
904 * Just set the line...
907 cgiSetVariable(name
, line
);
912 * Read the rest of the current line...
915 fgets(line
, sizeof(line
), stdin
);
918 * Clear the state vars...
925 else if (!_cups_strncasecmp(line
, "Content-Disposition:", 20))
927 if ((ptr
= strstr(line
+ 20, " name=\"")) != NULL
)
929 strlcpy(name
, ptr
+ 7, sizeof(name
));
931 if ((ptr
= strchr(name
, '\"')) != NULL
)
935 if ((ptr
= strstr(line
+ 20, " filename=\"")) != NULL
)
937 strlcpy(filename
, ptr
+ 11, sizeof(filename
));
939 if ((ptr
= strchr(filename
, '\"')) != NULL
)
943 else if (!_cups_strncasecmp(line
, "Content-Type:", 13))
945 for (ptr
= line
+ 13; isspace(*ptr
& 255); ptr
++);
947 strlcpy(mimetype
, ptr
, sizeof(mimetype
));
949 for (ptr
= mimetype
+ strlen(mimetype
) - 1;
950 ptr
> mimetype
&& isspace(*ptr
& 255);
956 * Return 1 for "form data found"...
964 * 'cgi_initialize_post()' - Initialize variables using the POST method.
967 static int /* O - 1 if form data was read */
968 cgi_initialize_post(void)
970 char *content_length
, /* Length of input data (string) */
971 *data
; /* Pointer to form data string */
972 size_t length
, /* Length of input data */
973 tbytes
; /* Total number of bytes read */
974 ssize_t nbytes
; /* Number of bytes read this read() */
975 int status
; /* Return status */
978 DEBUG_puts("cgi_initialize_post: Initializing variables using POST method...");
981 * Check to see if there is anything for us to read...
984 content_length
= getenv("CONTENT_LENGTH");
985 if (content_length
== NULL
|| atoi(content_length
) <= 0)
989 * Get the length of the input stream and allocate a buffer for it...
992 length
= (size_t)strtol(content_length
, NULL
, 10);
993 data
= malloc(length
+ 1);
999 * Read the data into the buffer...
1002 for (tbytes
= 0; tbytes
< length
; tbytes
+= (size_t)nbytes
)
1003 if ((nbytes
= read(0, data
+ tbytes
, (size_t)(length
- tbytes
))) < 0)
1005 if (errno
!= EAGAIN
)
1013 else if (nbytes
== 0)
1016 * CUPS STR #3176: OpenBSD: Early end-of-file on POST data causes 100% CPU
1018 * This should never happen, but does on OpenBSD. If we see early end-of-
1019 * file, treat this as an error and process no data.
1026 data
[length
] = '\0';
1032 status
= cgi_initialize_string(data
);
1035 * Free the data and return...
1045 * 'cgi_initialize_string()' - Initialize form variables from a string.
1048 static int /* O - 1 if form data was processed */
1049 cgi_initialize_string(const char *data
) /* I - Form data string */
1051 int done
; /* True if we're done reading a form variable */
1052 char *s
, /* Pointer to current form string */
1053 ch
, /* Temporary character */
1054 name
[255], /* Name of form variable */
1055 value
[65536]; /* Variable value */
1066 * Loop until we've read all the form data...
1069 while (*data
!= '\0')
1072 * Get the variable name...
1075 for (s
= name
; *data
!= '\0'; data
++)
1078 else if (*data
>= ' ' && s
< (name
+ sizeof(name
) - 1))
1088 * Read the variable value...
1091 for (s
= value
, done
= 0; !done
&& *data
!= '\0'; data
++)
1094 case '&' : /* End of data... */
1098 case '+' : /* Escaped space character */
1099 if (s
< (value
+ sizeof(value
) - 1))
1103 case '%' : /* Escaped control character */
1105 * Read the hex code...
1108 if (!isxdigit(data
[1] & 255) || !isxdigit(data
[2] & 255))
1111 if (s
< (value
+ sizeof(value
) - 1))
1117 *s
= (char)(ch
<< 4);
1129 default : /* Other characters come straight through */
1130 if (*data
>= ' ' && s
< (value
+ sizeof(value
) - 1))
1135 *s
= '\0'; /* nul terminate the string */
1138 * Remove trailing whitespace...
1144 while (s
>= value
&& isspace(*s
& 255))
1148 * Add the string to the variable "database"...
1151 if ((s
= strrchr(name
, '-')) != NULL
&& isdigit(s
[1] & 255))
1155 cgiSetArray(name
, atoi(s
) - 1, value
);
1157 else if (cgiGetVariable(name
) != NULL
)
1158 cgiSetArray(name
, cgiGetSize(name
), value
);
1160 cgiSetVariable(name
, value
);
1168 * 'cgi_passwd()' - Catch authentication requests and notify the server.
1170 * This function sends a Status header and exits, forcing authentication
1174 static const char * /* O - NULL (no return) */
1175 cgi_passwd(const char *prompt
) /* I - Prompt (not used) */
1179 fprintf(stderr
, "DEBUG: cgi_passwd(prompt=\"%s\") called!\n",
1180 prompt
? prompt
: "(null)");
1183 * Send a 401 (unauthorized) status to the server, so it can notify
1184 * the client that authentication is required.
1187 puts("Status: 401\n");
1191 * This code is never executed, but is present to satisfy the compiler.
1199 * 'cgi_set_sid()' - Set the CUPS session ID.
1202 static const char * /* O - New session ID */
1205 char buffer
[512], /* SID data */
1206 sid
[33]; /* SID string */
1207 _cups_md5_state_t md5
; /* MD5 state */
1208 unsigned char sum
[16]; /* MD5 sum */
1209 const char *remote_addr
, /* REMOTE_ADDR */
1210 *server_name
, /* SERVER_NAME */
1211 *server_port
; /* SERVER_PORT */
1214 if ((remote_addr
= getenv("REMOTE_ADDR")) == NULL
)
1215 remote_addr
= "REMOTE_ADDR";
1216 if ((server_name
= getenv("SERVER_NAME")) == NULL
)
1217 server_name
= "SERVER_NAME";
1218 if ((server_port
= getenv("SERVER_PORT")) == NULL
)
1219 server_port
= "SERVER_PORT";
1221 CUPS_SRAND(time(NULL
));
1222 snprintf(buffer
, sizeof(buffer
), "%s:%s:%s:%02X%02X%02X%02X%02X%02X%02X%02X",
1223 remote_addr
, server_name
, server_port
,
1224 (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255,
1225 (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255,
1226 (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255,
1227 (unsigned)CUPS_RAND() & 255, (unsigned)CUPS_RAND() & 255);
1229 _cupsMD5Append(&md5
, (unsigned char *)buffer
, (int)strlen(buffer
));
1230 _cupsMD5Finish(&md5
, sum
);
1232 cgiSetCookie(CUPS_SID
, httpMD5String(sum
, sid
), "/", NULL
, 0, 0);
1234 return (cupsGetOption(CUPS_SID
, num_cookies
, cookies
));
1239 * 'cgi_sort_variables()' - Sort all form variables for faster lookup.
1243 cgi_sort_variables(void)
1249 DEBUG_puts("cgi_sort_variables: Sorting variables...");
1255 qsort(form_vars
, (size_t)form_count
, sizeof(_cgi_var_t
),
1256 (int (*)(const void *, const void *))cgi_compare_variables
);
1259 DEBUG_puts("cgi_sort_variables: Sorted variable list is:");
1260 for (i
= 0; i
< form_count
; i
++)
1261 DEBUG_printf(("cgi_sort_variables: %d: %s (%d) = \"%s\" ...\n", i
,
1262 form_vars
[i
].name
, form_vars
[i
].nvalues
,
1263 form_vars
[i
].values
[0]));
1269 * 'cgi_unlink_file()' - Remove the uploaded form.
1273 cgi_unlink_file(void)
1278 * Remove the temporary file...
1281 unlink(form_file
->tempfile
);
1284 * Free memory used...
1287 free(form_file
->name
);
1288 free(form_file
->filename
);
1289 free(form_file
->mimetype
);