Changeset 4382
- Timestamp:
- 01/09/11 03:59:19 (13 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/globals.h
r4377 r4382 1237 1237 extern struct s_auth *find_user(char *); 1238 1238 extern int check_filled(uchar *value, int length); 1239 extern void *cs_malloc(void *result, size_t size, int quiterror); 1240 extern void *cs_realloc(void *result, size_t size, int quiterror); 1239 1241 #ifdef WEBIF 1240 1242 extern int x2i(int i); … … 1242 1244 extern char to_hex(char code); 1243 1245 extern char *urlencode(char *str); 1244 extern char *char_to_hex(const unsigned char* p_array, unsigned int p_array_len, char hex2ascii[256][2]);1246 extern void char_to_hex(const unsigned char* p_array, unsigned int p_array_len, unsigned char *result, char hex2ascii[256][2]); 1245 1247 extern void create_rand_str(char *dst, int size); 1246 1248 #endif -
trunk/oscam-http-helpers.c
r4331 r4382 17 17 if(result == NULL){ 18 18 if((*vars).varsalloc <= (*vars).varscnt){ 19 if(!cs_realloc(&(*vars).names, (*vars).varsalloc * 2 * sizeof(char**), -1)) return value; 20 if(!cs_realloc(&(*vars).values, (*vars).varsalloc * 2 * sizeof(char**), -1)) return value; 19 21 (*vars).varsalloc = (*vars).varscnt * 2; 20 (*vars).names = (char**) realloc ((*vars).names, (*vars).varsalloc * sizeof(char**)); 21 (*vars).values = (char**) realloc ((*vars).values, (*vars).varsalloc * sizeof(char**)); 22 } 23 tmp = (char *) malloc((strlen(name) + 1) * sizeof(char)); 22 } 23 if(!cs_malloc(&tmp,(strlen(name) + 1) * sizeof(char), -1)) return value; 24 24 strcpy(tmp, name); 25 25 (*vars).names[(*vars).varscnt] = tmp; 26 tmp = (char *) malloc((strlen(value) + 1) * sizeof(char)); 26 if(!cs_malloc(&tmp,(strlen(value) + 1) * sizeof(char), -1)){ 27 free((*vars).names[(*vars).varscnt]); 28 return value; 29 } 27 30 strcpy(tmp, value); 28 31 (*vars).values[(*vars).varscnt] = tmp; … … 32 35 if(append == 1){ 33 36 int oldlen = strlen((*vars).values[i]); 34 tmp = (char*) malloc ((oldlen + newlen + 1) * sizeof(char));37 if(!cs_malloc(&tmp, (oldlen + newlen + 1) * sizeof(char), -1)) return value; 35 38 memcpy(tmp, (*vars).values[i], oldlen); 36 39 strcpy(tmp + oldlen, value); 37 40 } else { 38 tmp = (char*) malloc ((newlen + 1) * sizeof(char));41 if(!cs_malloc(&tmp, (newlen + 1) * sizeof(char), -1)) return value; 39 42 strcpy(tmp, value); 40 43 } … … 49 52 it after having added the array here! */ 50 53 char *tpl_addTmp(struct templatevars *vars, char *value){ 51 if((*vars).tmpalloc <= (*vars).tmpcnt){ 54 if((*vars).tmpalloc <= (*vars).tmpcnt){ 55 if(!cs_realloc (&(*vars).tmp, (*vars).tmpalloc * 2 * sizeof(char**), -1)) return value; 52 56 (*vars).tmpalloc = (*vars).tmpcnt * 2; 53 (*vars).tmp = (char**) realloc ((*vars).tmp, (*vars).tmpalloc * sizeof(char**));54 57 } 55 58 (*vars).tmp[(*vars).tmpcnt] = value; 56 (*vars).tmpcnt = (*vars).tmpcnt + 1;59 (*vars).tmpcnt++; 57 60 return value; 58 61 } … … 70 73 va_end(argptr); 71 74 72 char *result = (char *) malloc((needed + 1) * sizeof(char)); 75 char *result; 76 if(!cs_malloc(&result, (needed + 1) * sizeof(char), -1)) return ""; 73 77 va_start(argptr,fmtstring); 74 78 vsnprintf(result, needed + 1, fmtstring, argptr); … … 98 102 } 99 103 100 /* Initializes all variables vor a templatevar-structure and returns a pointer to it. Make104 /* Initializes all variables for a templatevar-structure and returns a pointer to it. Make 101 105 sure to call tpl_clear() when you are finished or you'll run into a memory leak! */ 102 106 struct templatevars *tpl_create(){ 103 struct templatevars *vars = (struct templatevars *) malloc(sizeof(struct templatevars)); 104 (*vars).varsalloc = 16; 107 struct templatevars *vars; 108 if(!cs_malloc(&vars, sizeof(struct templatevars), -1)) return NULL; 109 (*vars).varsalloc = 64; 105 110 (*vars).varscnt = 0; 106 (*vars).tmpalloc = 16;111 (*vars).tmpalloc = 64; 107 112 (*vars).tmpcnt = 0; 108 (*vars).names = (char**) malloc ((*vars).varsalloc * sizeof(char**)); 109 (*vars).values = (char**) malloc ((*vars).varsalloc * sizeof(char**)); 110 (*vars).tmp = (char**) malloc ((*vars).tmpalloc * sizeof(char**)); 113 if(!cs_malloc(&(*vars).names, (*vars).varsalloc * sizeof(char**), -1)){ 114 free(vars); 115 return NULL; 116 } 117 if(!cs_malloc(&(*vars).values, (*vars).varsalloc * sizeof(char**), -1)){ 118 free((*vars).names); 119 free(vars); 120 return NULL; 121 }; 122 if(!cs_malloc(&(*vars).tmp, (*vars).tmpalloc * sizeof(char**), -1)){ 123 free((*vars).names); 124 free((*vars).values); 125 free(vars); 126 return NULL; 127 }; 111 128 return vars; 112 129 } … … 144 161 145 162 /* Returns an unparsed template either from disk or from internal templates. 146 Note: You must free() the result after using it !*/163 Note: You must free() the result after using it and you may get NULL if an error occured!*/ 147 164 char *tpl_getUnparsedTpl(const char* name){ 148 165 int i; … … 161 178 char buffer[1024]; 162 179 int read, allocated = 1025, size = 0; 163 result = (char *) malloc(allocated * sizeof(char));180 if(!cs_malloc(&result, allocated * sizeof(char), -1)) return NULL; 164 181 if((fp = fopen(path,"r"))!=NULL){ 165 182 while((read = fread(&buffer,sizeof(char),1024,fp)) > 0){ 166 183 if(allocated < size + read + 1) { 167 184 allocated += size + 1024; 168 result = (char *) realloc(result, allocated * sizeof(char));185 if(!cs_realloc(&result, allocated * sizeof(char), -1)) return NULL; 169 186 } 170 187 memcpy(result + size, buffer, read); … … 179 196 if(i >= 0 && i < tplmapcnt){ 180 197 int len = (strlen(tplmap[i])) + 1; 181 result = (char *) malloc(len * sizeof(char));198 if(!cs_malloc(&result, len * sizeof(char), -1)) return NULL; 182 199 memcpy(result, tplmap[i], len); 183 200 } else { 184 result = (char *) malloc(1 * sizeof(char));201 if(!cs_malloc(&result, 1 * sizeof(char), -1)) return NULL; 185 202 result[0] = '\0'; 186 203 } … … 189 206 190 207 /* Returns the specified template with all variables/other templates replaced or an 191 empty string if the template doesn't exist*/ 208 empty string if the template doesn't exist. Do not free the result yourself, it 209 will get automatically cleaned up! */ 192 210 char *tpl_getTpl(struct templatevars *vars, const char* name){ 193 211 char *tplorg = tpl_getUnparsedTpl(name); 212 if(!tplorg) return ""; 194 213 char *tplend = tplorg + strlen(tplorg); 195 214 char *pch, *pch2, *tpl=tplorg; … … 198 217 int tmp,respos = 0; 199 218 int allocated = 2 * strlen(tpl) + 1; 200 char *result = (char *) malloc(allocated * sizeof(char)); 219 char *result; 220 if(!cs_malloc(&result, allocated * sizeof(char), -1)) return ""; 201 221 202 222 while(tpl < tplend){ … … 216 236 if(tmp + respos + 2 >= allocated){ 217 237 allocated = tmp + respos + 256; 218 result = (char *) realloc(result, allocated * sizeof(char));238 if(!cs_realloc(&result, allocated * sizeof(char), -1)) return ""; 219 239 } 220 240 memcpy(result + respos, pch2, tmp); … … 225 245 if(respos + 2 >= allocated){ 226 246 allocated = respos + 256; 227 result = (char *) realloc(result, allocated * sizeof(char));247 if(!cs_realloc(&result, allocated * sizeof(char), -1)) return ""; 228 248 } 229 249 result[respos] = tpl[0]; … … 243 263 int tplmapcnt = sizeof(tplmap)/sizeof(char *); 244 264 int i, cnt = 0; 245 char tmp[2 00];265 char tmp[256]; 246 266 FILE *fp; 247 267 for(i = 0; i < tplcnt && i < tplmapcnt; ++i){ 248 if(strlen(tpl_getTplPath(tpl[i], path, tmp, 2 00)) > 0 && (fp = fopen(tmp,"w")) != NULL){268 if(strlen(tpl_getTplPath(tpl[i], path, tmp, 256)) > 0 && (fp = fopen(tmp,"w")) != NULL){ 249 269 fwrite(tplmap[i], sizeof(char), strlen(tplmap[i]), fp); 250 270 fclose (fp); … … 255 275 } 256 276 257 /* Parses a value in an authentication string by removing all quotes/whitespace. Note that the original array is modified */277 /* Parses a value in an authentication string by removing all quotes/whitespace. Note that the original array is modified. */ 258 278 char *parse_auth_value(char *value){ 259 279 char *pch = value; … … 270 290 } 271 291 272 /* Calculates the currently valid nonce value and copies it to result */273 void calculate_nonce(char *result , int resultlen){292 /* Calculates the currently valid nonce value and copies it to result. Please note that result needs to be at least (MD5_DIGEST_LENGTH * 2) + 1 large. */ 293 void calculate_nonce(char *result){ 274 294 char noncetmp[128]; 295 unsigned char md5tmp[MD5_DIGEST_LENGTH]; 275 296 sprintf(noncetmp, "%d:%s", (int)time(NULL)/AUTHNONCEVALIDSECS, noncekey); 276 char *expectednonce = char_to_hex(MD5((unsigned char*)noncetmp, strlen(noncetmp), NULL), MD5_DIGEST_LENGTH, hex2ascii); 277 cs_strncpy(result, expectednonce, resultlen); 278 free(expectednonce); 279 } 280 281 /* Checks if authentication is correct. Returns -1 if not correct, 1 if correct and 2 if nonce isn't valid anymore */ 297 char_to_hex(MD5((unsigned char*)noncetmp, strlen(noncetmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)result, hex2ascii); 298 } 299 300 /* Checks if authentication is correct. Returns -1 if not correct, 1 if correct and 2 if nonce isn't valid anymore. 301 Note that authstring will be modified. */ 282 302 int check_auth(char *authstring, char *method, char *path, char *expectednonce){ 283 303 int authok = 0, uriok = 0; … … 322 342 if(uriok == 1 && strcmp(username, cfg->http_user) == 0){ 323 343 char A1tmp[3 + strlen(username) + strlen(AUTHREALM) + strlen(expectedPassword)]; 344 char A1[(MD5_DIGEST_LENGTH * 2) + 1], A2[(MD5_DIGEST_LENGTH * 2) + 1], A3[(MD5_DIGEST_LENGTH * 2) + 1]; 345 unsigned char md5tmp[MD5_DIGEST_LENGTH]; 324 346 sprintf(A1tmp, "%s:%s:%s", username, AUTHREALM, expectedPassword); 325 char *A1 = char_to_hex(MD5((unsigned char*)A1tmp, strlen(A1tmp), NULL), MD5_DIGEST_LENGTH, hex2ascii);347 char_to_hex(MD5((unsigned char*)A1tmp, strlen(A1tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A1, hex2ascii); 326 348 327 349 char A2tmp[2 + strlen(method) + strlen(uri)]; 328 350 sprintf(A2tmp, "%s:%s", method, uri); 329 char *A2 = char_to_hex(MD5((unsigned char*)A2tmp, strlen(A2tmp), NULL), MD5_DIGEST_LENGTH, hex2ascii);351 char_to_hex(MD5((unsigned char*)A2tmp, strlen(A2tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A2, hex2ascii); 330 352 331 353 char A3tmp[10 + strlen(A1) + strlen(A2) + strlen(authnonce) + strlen(authnc) + strlen(authcnonce)]; 332 354 sprintf(A3tmp, "%s:%s:%s:%s:auth:%s", A1, authnonce, authnc, authcnonce, A2); 333 char *A3 = char_to_hex(MD5((unsigned char*)A3tmp, strlen(A3tmp), NULL), MD5_DIGEST_LENGTH, hex2ascii);355 char_to_hex(MD5((unsigned char*)A3tmp, strlen(A3tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char*)A3, hex2ascii); 334 356 335 357 if(strcmp(A3, authresponse) == 0) { … … 337 359 else authok = 2; 338 360 } 339 free(A1);340 free(A2);341 free(A3);342 361 } 343 362 return authok; … … 371 390 372 391 time_t now; 373 char timebuf[128]; 374 char buf[1024]; 375 376 sprintf(buf, "%s %d %s\r\n", PROTOCOL, status, title); 377 sprintf(buf+strlen(buf), "Server: %s\r\n", SERVER); 392 char timebuf[32]; 393 char buf[sizeof(PROTOCOL) + sizeof(SERVER) + strlen(title) + (extra == NULL?0:strlen(extra)+2) + (mime == NULL?0:strlen(mime)+2) + 256]; 394 char *pos = buf; 395 396 pos += sprintf(pos, "%s %d %s\r\n", PROTOCOL, status, title); 397 pos += sprintf(pos, "Server: %s\r\n", SERVER); 378 398 379 399 now = time(NULL); 380 400 strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now)); 381 sprintf(buf+strlen(buf), "Date: %s\r\n", timebuf);401 pos += sprintf(pos, "Date: %s\r\n", timebuf); 382 402 383 403 if (extra) 384 sprintf(buf+strlen(buf), "%s\r\n", extra);404 pos += sprintf(pos, "%s\r\n", extra); 385 405 386 406 if (mime) 387 sprintf(buf+strlen(buf), "Content-Type: %s\r\n", mime); 388 389 strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now)); 390 sprintf(buf+strlen(buf), "Cache-Control: no-store, no-cache, must-revalidate\r\n"); 391 sprintf(buf+strlen(buf), "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n"); 392 sprintf(buf+strlen(buf), "Last-Modified: %s\r\n", timebuf); 393 sprintf(buf+strlen(buf), "Connection: close\r\n"); 394 sprintf(buf+strlen(buf), "\r\n"); 407 pos += sprintf(pos, "Content-Type: %s\r\n", mime); 408 409 pos += sprintf(pos, "Cache-Control: no-store, no-cache, must-revalidate\r\n"); 410 pos += sprintf(pos, "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n"); 411 pos += sprintf(pos, "Last-Modified: %s\r\n", timebuf); 412 pos += sprintf(pos, "Connection: close\r\n"); 413 pos += sprintf(pos, "\r\n"); 395 414 webif_write(buf, f); 396 415 } 397 416 398 417 418 399 419 /* 400 * function for sending files. 1 = CSS, 2 = JS420 * function for sending files. 401 421 */ 402 void send_file(FILE *f, int fileno){ 403 404 char *filename; 405 406 if (fileno == 1) 422 void send_file(FILE *f, char *filename){ 423 int fileno = 0; 424 425 if (!strcmp(filename, "CSS")){ 407 426 filename = cfg->http_css; 408 else if (fileno == 2) 427 fileno = 1; 428 } else if (!strcmp(filename, "JS")){ 409 429 filename = cfg->http_jscript; 410 else411 return;430 fileno = 2; 431 } 412 432 413 433 if(strlen(filename) > 0 && file_exists(filename) == 1){ … … 428 448 else if (fileno == 2) 429 449 webif_write(JSCRIPT, f); 430 431 450 } 432 451 } … … 442 461 } 443 462 463 void send_error500(FILE *f){ 464 send_error(f, 500, "Internal Server Error", NULL, "An internal error has occured."); 465 } 466 444 467 char *getParam(struct uriparams *params, char *name){ 445 468 int i; … … 462 485 char *xml_encode(struct templatevars *vars, char *chartoencode) { 463 486 int i, pos = 0, len = strlen(chartoencode); 487 char *result; 464 488 /* In worst case, every character could get converted to 6 chars (we only support ASCII, for Unicode it would be 7)*/ 465 489 char encoded[len * 6 + 1], buffer[7]; … … 486 510 } 487 511 /* Allocate the needed memory size and store it in the templatevars */ 488 char *result = (char *)malloc(pos + 1);512 if(!cs_malloc(&result, pos + 1, -1)) return ""; 489 513 memcpy(result, encoded, pos); 490 514 result[pos] = '\0'; -
trunk/oscam-http.c
r4381 r4382 810 810 // Add new reader 811 811 struct s_reader *newrdr; 812 newrdr = malloc(sizeof(struct s_reader)); 812 if(!cs_malloc(&newrdr,sizeof(struct s_reader), -1)){ 813 send_error500(f); 814 return; 815 } 813 816 814 817 if (newrdr) { … … 1340 1343 ++i; 1341 1344 } 1342 if (! (account=malloc(sizeof(struct s_auth)))) {1343 cs_log("Error allocating memory (errno=%d)", errno);1345 if (!cs_malloc(&account, sizeof(struct s_auth), -1)) { 1346 send_error500(f); 1344 1347 return; 1345 1348 } … … 1774 1777 if (rcc && rcc->cards) { 1775 1778 pthread_mutex_lock(&rcc->cards_busy); 1776 char buf[4000];1777 1779 uint8 serbuf[8]; 1778 1780 … … 1810 1812 1811 1813 LL_ITER *pit = ll_iter_create(card->providers); 1812 char *p = buf;1813 *p = 0;1814 1814 struct cc_provider *prov; 1815 1815 … … 1825 1825 1826 1826 if (!apicall) { 1827 sprintf(p, "%s", provider);1828 p = strend(p);1829 //add SA:1830 1827 if (prov->sa[0] || prov->sa[1] || prov->sa[2] || prov->sa[3]) { 1831 sprintf(p, " SA:%02X%02X%02X%02X", prov->sa[0], prov->sa[1], prov->sa[2], prov->sa[3]); 1832 p = strend(p); 1828 tpl_printf(vars, 1, "PROVIDERS", "%s SA:%02X%02X%02X%02X<BR>\n", provider, prov->sa[0], prov->sa[1], prov->sa[2], prov->sa[3]); 1829 } else { 1830 tpl_printf(vars, 1, "PROVIDERS", "%s<BR>\n", provider); 1833 1831 } 1834 sprintf(p, "<BR>\n");1835 p = strend(p);1836 1837 1832 } else { 1838 1833 if (prov->sa[0] || prov->sa[1] || prov->sa[2] || prov->sa[3]) … … 1850 1845 } 1851 1846 1852 if (!apicall) tpl_addVar(vars, 1, "PROVIDERS", buf);1853 1854 1847 ll_iter_release(pit); 1855 1848 LL_ITER *nit = ll_iter_create(card->remote_nodes); 1856 p = buf;1857 *p = 0;1858 1849 uint8 *node; 1859 1850 … … 1864 1855 1865 1856 if (!apicall) { 1866 sprintf(p, "%02X%02X%02X%02X%02X%02X%02X%02X<BR>\n",1857 tpl_printf(vars, 1, "NODES", "%02X%02X%02X%02X%02X%02X%02X%02X<BR>\n", 1867 1858 node[0], node[1], node[2], node[3], node[4], node[5], node[6], node[7]); 1868 p = strend(p);1869 1870 1859 } else { 1871 1860 tpl_printf(vars, 0, "APINODE", "%02X%02X%02X%02X%02X%02X%02X%02X", node[0], node[1], node[2], node[3], node[4], node[5], node[6], node[7]); … … 1876 1865 tpl_printf(vars, 0, "APITOTALNODES", "%d", nodecount); 1877 1866 } 1878 1879 if (!apicall) tpl_addVar(vars, 0, "NODES", buf);1880 1867 1881 1868 ll_iter_release(nit); … … 2348 2335 void send_oscam_services_edit(struct templatevars *vars, FILE *f, struct uriparams *params, struct in_addr in) { 2349 2336 struct s_sidtab *sidtab,*ptr; 2350 char label[ 128];2337 char label[sizeof(cfg->sidtab->label)]; 2351 2338 int i; 2352 2339 2353 cs_strncpy(label, strtolower(getParam(params, "service")), sizeof(label) /sizeof(char));2340 cs_strncpy(label, strtolower(getParam(params, "service")), sizeof(label)); 2354 2341 2355 2342 for (sidtab = cfg->sidtab; sidtab != NULL && strcmp(label, sidtab->label) != 0; sidtab=sidtab->next); … … 2363 2350 ++i; 2364 2351 } 2365 if (! (sidtab=malloc(sizeof(struct s_sidtab)))) {2366 cs_log("Error allocating memory (errno=%d)", errno);2352 if (!cs_malloc(&sidtab, sizeof(struct s_sidtab), -1)) { 2353 send_error500(f); 2367 2354 return; 2368 2355 } … … 2967 2954 2968 2955 int authok = 0; 2969 char expectednonce[ 64];2956 char expectednonce[(MD5_DIGEST_LENGTH * 2) + 1]; 2970 2957 2971 2958 char *method, *path, *protocol; … … 3016 3003 return -1; 3017 3004 } 3018 3019 filebuf = realloc(filebuf, bufsize+n+1); 3020 3005 if(!cs_malloc(&filebuf, bufsize+n+1, -1)){ 3006 send_error500(f); 3007 return -1; 3008 } 3009 3021 3010 memcpy(filebuf+bufsize, buf2, n); 3022 3011 bufsize+=n; … … 3082 3071 3083 3072 if(strlen(cfg->http_user) == 0 || strlen(cfg->http_pwd) == 0) authok = 1; 3084 else calculate_nonce(expectednonce , sizeof(expectednonce)/sizeof(char));3073 else calculate_nonce(expectednonce); 3085 3074 3086 3075 char *str1, *saveptr=NULL; … … 3099 3088 3100 3089 if(authok != 1) { 3101 char temp[1024]; 3102 strcpy(temp, "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\""); 3103 strcat(temp, AUTHREALM); 3104 strcat(temp, "\", qop=\"auth\", opaque=\"\", nonce=\""); 3105 strcat(temp, expectednonce); 3106 strcat(temp, "\""); 3107 if(authok == 2) strcat(temp, ", stale=true"); 3090 char temp[sizeof(AUTHREALM) + sizeof(expectednonce) + 100]; 3091 snprintf(temp, sizeof(temp), "WWW-Authenticate: Digest algorithm=\"MD5\", realm=\"%s\", qop=\"auth\", opaque=\"\", nonce=\"%s\"", AUTHREALM, expectednonce); 3092 if(authok == 2) strncat(temp, ", stale=true", sizeof(temp)); 3108 3093 send_headers(f, 401, "Unauthorized", temp, "text/html"); 3109 3094 free(filebuf); … … 3114 3099 if(pgidx == 8) { 3115 3100 send_headers(f, 200, "OK", NULL, "text/css"); 3116 send_file(f, 1);3101 send_file(f, "CSS"); 3117 3102 } else if (pgidx == 17) { 3118 3103 send_headers(f, 200, "OK", NULL, "text/javascript"); 3119 send_file(f, 2);3104 send_file(f, "JS"); 3120 3105 } else { 3121 3106 if (pgidx == 18) … … 3125 3110 time_t t; 3126 3111 struct templatevars *vars = tpl_create(); 3112 if(vars == NULL){ 3113 send_error500(f); 3114 free(filebuf); 3115 return 0; 3116 } 3127 3117 struct tm lt, st; 3128 3118 time(&t); … … 3235 3225 3236 3226 if (cfg->http_cert[0]==0) 3237 s printf(path, "%s%s", cs_confdir, cs_cert);3227 snprintf(path, sizeof(path), "%s%s", cs_confdir, cs_cert); 3238 3228 else 3239 strcpy(path, cfg->http_cert);3229 cs_strncpy(path, cfg->http_cert, sizeof(path)); 3240 3230 3241 3231 if (!ctx) { … … 3273 3263 struct sockaddr_in remote; 3274 3264 socklen_t len = sizeof(remote); 3275 char *tmp;3276 3265 3277 3266 /* Prepare lookup array for conversion between ascii and hex */ 3278 tmp = malloc(3 * sizeof(char));3267 char tmp[3]; 3279 3268 for(i = 0; i < 256; i++) { 3280 snprintf(tmp, 3,"%02x", i);3269 snprintf(tmp, sizeof(tmp),"%02x", i); 3281 3270 memcpy(hex2ascii[i], tmp, 2); 3282 3271 } 3283 free(tmp); 3272 3284 3273 /* Create random string for nonce value generation */ 3285 3274 srand(time(NULL)); -
trunk/oscam-http.h
r4379 r4382 1655 1655 static char noncekey[33]; 1656 1656 1657 1658 char *tpl_addVar(struct templatevars *vars, int append, char *name, char *value);1659 char *tpl_addTmp(struct templatevars *vars, char *value);1660 char *tpl_printf(struct templatevars *vars, int append, char *varname, char *fmtstring, ...);1661 char *tpl_getVar(struct templatevars *vars, char *name);1662 struct templatevars *tpl_create();1663 void tpl_clear(struct templatevars *vars);1664 char *tpl_getUnparsedTpl(const char* name);1665 char *tpl_getTpl(struct templatevars *vars, const char* name);1666 char *parse_auth_value(char *value);1667 void calculate_nonce(char *result, int resultlen);1668 int check_auth(char *authstring, char *method, char *path, char *expectednonce);1669 void send_headers(FILE *f, int status, char *title, char *extra, char *mime);1670 void send_css(FILE *f);1671 void send_js(FILE *f);1672 char *getParam(struct uriparams *params, char *name);1673 int tpl_saveIncludedTpls(const char *path);1674 1657 int cv(){return 91789605==crc32(0L,(unsigned char*)ICMAI,strlen(ICMAI))/2?1:0;} 1675 1658 -
trunk/oscam-log.c
r4364 r4382 32 32 if( stlog.st_size >= cfg->max_log_size*1024 && *f != NULL) { 33 33 int rc; 34 char prev_log[ 128];35 s printf(prev_log, "%s-prev", file);34 char prev_log[strlen(file) + 6]; 35 snprintf(prev_log, sizeof(prev_log), "%s-prev", file); 36 36 if ( pthread_mutex_trylock(&switching_log) == 0) { //I got the lock so I am the first thread detecting a switchlog is needed 37 37 fprintf(*f, "switch log file\n"); … … 154 154 switch(flag) { 155 155 case -1: 156 s printf(log_buf,"[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s\n",156 snprintf(log_buf, sizeof(log_buf), "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s\n", 157 157 lt.tm_year+1900, lt.tm_mon+1, lt.tm_mday, 158 158 lt.tm_hour, lt.tm_min, lt.tm_sec, txt); 159 159 break; 160 160 case 1: 161 s printf(log_buf, "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s",161 snprintf(log_buf, sizeof(log_buf), "[LOG000]%4d/%02d/%02d %2d:%02d:%02d %s", 162 162 lt.tm_year+1900, lt.tm_mon+1, lt.tm_mday, 163 163 lt.tm_hour, lt.tm_min, lt.tm_sec, txt); 164 164 break; 165 165 default: 166 s printf(log_buf, "[LOG000]%s", txt);166 snprintf(log_buf, sizeof(log_buf), "[LOG000]%s", txt); 167 167 } 168 168 … … 200 200 continue; 201 201 } 202 s printf(sbuf, "%03d", cl->logcounter);202 snprintf(sbuf, sizeof(sbuf), "%03d", cl->logcounter); 203 203 cl->logcounter = (cl->logcounter+1) % 1000; 204 204 memcpy(log_buf + 4, sbuf, 3); … … 214 214 va_list params; 215 215 va_start(params, fmt); 216 vs printf(log_txt+11, fmt, params);216 vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params); 217 217 va_end(params); 218 218 write_to_log(-1, log_txt); … … 234 234 va_list params; 235 235 va_start(params, fmt); 236 vs printf(log_txt+11, fmt, params);236 vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params); 237 237 va_end(params); 238 238 write_to_log(-1, log_txt); … … 250 250 va_list params; 251 251 va_start(params, fmt); 252 vs printf(log_txt+11, fmt, params);252 vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params); 253 253 va_end(params); 254 254 write_to_log(-1, log_txt); … … 259 259 { 260 260 get_log_header(0, log_txt); 261 s printf(log_txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));261 snprintf(log_txt+11, sizeof(log_txt) - 11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i)); 262 262 write_to_log(-1, log_txt); 263 263 } … … 274 274 va_list params; 275 275 va_start(params, fmt); 276 vs printf(log_txt+11, fmt, params);276 vsnprintf(log_txt+11, sizeof(log_txt) - 11, fmt, params); 277 277 va_end(params); 278 278 write_to_log(-1, log_txt); … … 284 284 { 285 285 get_log_header(0, log_txt); 286 s printf(log_txt+11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i));286 snprintf(log_txt+11, sizeof(log_txt) - 11, "%s", cs_hexdump(1, buf+i, (n-i>16) ? 16 : n-i)); 287 287 write_to_log(-1, log_txt); 288 288 } … … 348 348 * so we can use the same Pipe as Log 349 349 */ 350 s printf(buf, "s%02d.%02d.%02d %02d:%02d:%02d %3.1f %s %s %d %d %d %d %d %d %d %ld %ld %02d:%02d:%02d %s %04X:%04X %s\n",350 snprintf(buf, sizeof(buf), "s%02d.%02d.%02d %02d:%02d:%02d %3.1f %s %s %d %d %d %d %d %d %d %ld %ld %02d:%02d:%02d %s %04X:%04X %s\n", 351 351 lt.tm_mday, lt.tm_mon+1, lt.tm_year%100, 352 352 lt.tm_hour, lt.tm_min, lt.tm_sec, cwps, -
trunk/oscam-simples.c
r4344 r4382 574 574 } 575 575 576 /* This function encapsulates malloc. It automatically adds an error message to the log if it failed and calls cs_exit(quiterror) if quiterror > -1. 577 result will be automatically filled with the new memory position or NULL on failure. */ 578 void *cs_malloc(void *result, size_t size, int quiterror){ 579 void **tmp = (void *)result; 580 *tmp = malloc (size); 581 if(*tmp == NULL){ 582 cs_log("Couldn't allocate memory (errno=%d)!", errno); 583 if(quiterror > -1) cs_exit(quiterror); 584 } 585 return *tmp; 586 } 587 588 /* This function encapsulates realloc. It automatically adds an error message to the log if it failed and calls cs_exit(quiterror) if quiterror > -1. 589 result will be automatically filled with the new memory position or NULL on failure. If a failure occured, the existing memory in result will be freed. */ 590 void *cs_realloc(void *result, size_t size, int quiterror){ 591 void **tmp = (void *)result, **tmp2 = (void *)result; 592 *tmp = realloc (*tmp, size); 593 //printf("reallocating\n"); 594 //fflush(stdout); 595 if(*tmp == NULL){ 596 cs_log("Couldn't allocate memory (errno=%d)!", errno); 597 free(*tmp2); 598 if(quiterror > -1) cs_exit(quiterror); 599 } 600 return *tmp; 601 } 602 576 603 #ifdef WEBIF 577 604 /* Helper function for urldecode.*/ … … 633 660 /* Converts a char array to a char array with hex values (needed for example for md5). The hex2ascii 634 661 array is a lookup table with the corresponding hex string on the array position of the integer representation 635 of the ascii value. Note that you need to "free" the resulting array after usage or you'll get a memory leak!*/ 636 char *char_to_hex(const unsigned char* p_array, unsigned int p_array_len, char hex2ascii[256][2]) { 637 unsigned char* str = (unsigned char*)malloc(p_array_len*2+1); 638 str[p_array_len*2] = '\0'; 662 of the ascii value. Note that result needs to be at least (p_array_len * 2) + 1 large. */ 663 void char_to_hex(const unsigned char* p_array, unsigned int p_array_len, unsigned char *result, char hex2ascii[256][2]) { 664 result[p_array_len*2] = '\0'; 639 665 const unsigned char* p_end = p_array + p_array_len; 640 666 size_t pos=0; 641 667 const unsigned char* p; 642 668 for( p = p_array; p != p_end; p++, pos+=2 ) { 643 str[pos] = hex2ascii[*p][0]; 644 str[pos+1] = hex2ascii[*p][1]; 645 } 646 return (char*)str; 669 result[pos] = hex2ascii[*p][0]; 670 result[pos+1] = hex2ascii[*p][1]; 671 } 647 672 } 648 673
Note:
See TracChangeset
for help on using the changeset viewer.