Esempio n. 1
0
static char *get_real_username(char *usname)
{
	if(usname[0] && turn_params.use_auth_secret_with_timestamp) {
		char *col=strchr(usname,turn_params.rest_api_separator);
		if(col) {
			if(col == usname) {
				usname +=1;
			} else {
				char *ptr = usname;
				int found_non_figure = 0;
				while(ptr < col) {
					if(!(ptr[0]>='0' && ptr[0]<='9')) {
						found_non_figure=1;
						break;
					}
					++ptr;
				}
				if(!found_non_figure) {
					usname = col+1;
				} else {
					*col=0;
					usname = turn_strdup(usname);
					*col=turn_params.rest_api_separator;
					return usname;
				}
			}
		}
	}

	return turn_strdup(usname);
}
Esempio n. 2
0
static void set_log_file_name_func(char *base, char *f, size_t fsz)
{
	if(simple_log) {
	  strncpy(f,base,fsz);
	  return;
	}

	char logdate[125];
	char *tail=turn_strdup(".log");

	get_date(logdate,sizeof(logdate));

	char *base1=turn_strdup(base);

	int len=(int)strlen(base1);

	--len;

	while(len>=0) {
		if((base1[len]==' ')||(base1[len]=='\t')) {
			base1[len]='_';
		}
		--len;
	}

	len=(int)strlen(base1);

	while(len>=0) {
		if(base1[len]=='/')
			break;
		else if(base1[len]=='.') {
			turn_free(tail,strlen(tail)+1);
			tail=turn_strdup(base1+len);
			base1[len]=0;
			if(strlen(tail)<2) {
				turn_free(tail,strlen(tail)+1);
				tail = turn_strdup(".log");
			}
			break;
		}
		--len;
	}

	len=(int)strlen(base1);
	if(len>0 && (base1[len-1]!='/') && (base1[len-1]!='-') && (base1[len-1]!='_')) {
	  snprintf(f, FILE_STR_LEN, "%s_%s%s", base1,logdate,tail);
	} else {
	  snprintf(f, FILE_STR_LEN, "%s%s%s", base1,logdate,tail);
	}

	turn_free(base1,strlen(base1)+1);
	turn_free(tail,strlen(tail)+1);
}
Esempio n. 3
0
void add_to_secrets_list(secrets_list_t *sl, const char* elem)
{
	if(sl && elem) {
	  sl->secrets = (char**)turn_realloc(sl->secrets,0,(sizeof(char*)*(sl->sz+1)));
	  sl->secrets[sl->sz] = turn_strdup(elem);
	  sl->sz += 1;
	}
}
Esempio n. 4
0
int get_realm_options_by_origin(char *origin, realm_options_t* ro)
{
	ur_string_map_value_type value = 0;
	TURN_MUTEX_LOCK(&o_to_realm_mutex);
	if (ur_string_map_get(o_to_realm, (ur_string_map_key_type) origin, &value) && value) {
		char *realm = turn_strdup((char*)value);
		TURN_MUTEX_UNLOCK(&o_to_realm_mutex);
		realm_params_t rp;
		get_realm_data(realm, &rp);
		ns_bcopy(&(rp.options),ro,sizeof(realm_options_t));
		turn_free(realm,strlen(realm)+1);
		return 1;
	} else {
		TURN_MUTEX_UNLOCK(&o_to_realm_mutex);
		get_default_realm_options(ro);
		return 0;
	}
}
Esempio n. 5
0
int is_secure_username(const u08bits *username)
{
	int ret = 0;
	if(username) {
		unsigned char *s0 = (unsigned char*)turn_strdup((const char*)username);
		unsigned char *s = s0;
		while(*s) {
			*s = (unsigned char)tolower((int)*s);
			++s;
		}
		s = s0;
		if(strstr((char*)s," ")||strstr((char*)s,"\t")||strstr((char*)s,"'")||strstr((char*)s,"\"")||strstr((char*)s,"\n")||strstr((char*)s,"\r")||strstr((char*)s,"\\")) {
			;
		} else if(strstr((char*)s,"union")&&strstr((char*)s,"select")) {
			;
		} else {
			ret = 1;
		}
		turn_free(s,strlen((char*)s));
	}
	return ret;
}
Esempio n. 6
0
static void pgsql_reread_realms(secrets_list_t * realms_list) {
	PGconn * pqc = get_pqdb_connection();
	if(pqc) {
		char statement[TURN_LONG_STRING_SIZE];

		{
			snprintf(statement,sizeof(statement),"select origin,realm from turn_origin_to_realm");
			PGresult *res = PQexec(pqc, statement);

			if(res && (PQresultStatus(res) == PGRES_TUPLES_OK)) {

				ur_string_map *o_to_realm_new = ur_string_map_create(turn_free_simple);

				int i = 0;
				for(i=0;i<PQntuples(res);i++) {
					char *oval = PQgetvalue(res,i,0);
					if(oval) {
						char *rval = PQgetvalue(res,i,1);
						if(rval) {
							get_realm(rval);
							ur_string_map_value_type value = turn_strdup(rval);
							ur_string_map_put(o_to_realm_new, (const ur_string_map_key_type) oval, value);
						}
					}
				}

        update_o_to_realm(o_to_realm_new);
			}

			if(res) {
				PQclear(res);
			}
		}

		{
			{
				size_t i = 0;
				size_t rlsz = 0;

				lock_realms();
				rlsz = realms_list->sz;
				unlock_realms();

				for (i = 0; i<rlsz; ++i) {

					char *realm = realms_list->secrets[i];

					realm_params_t* rp = get_realm(realm);

					lock_realms();
					rp->options.perf_options.max_bps = turn_params.max_bps;
					unlock_realms();

					lock_realms();
					rp->options.perf_options.total_quota = turn_params.total_quota;
					unlock_realms();

					lock_realms();
					rp->options.perf_options.user_quota = turn_params.user_quota;
					unlock_realms();

				}
			}

			snprintf(statement,sizeof(statement),"select realm,opt,value from turn_realm_option");
			PGresult *res = PQexec(pqc, statement);

			if(res && (PQresultStatus(res) == PGRES_TUPLES_OK)) {

				int i = 0;
				for(i=0;i<PQntuples(res);i++) {
					char *rval = PQgetvalue(res,i,0);
					char *oval = PQgetvalue(res,i,1);
					char *vval = PQgetvalue(res,i,2);
					if(rval && oval && vval) {
						realm_params_t* rp = get_realm(rval);
						if(!strcmp(oval,"max-bps"))
							rp->options.perf_options.max_bps = (band_limit_t)strtoul(vval,NULL,10);
						else if(!strcmp(oval,"total-quota"))
							rp->options.perf_options.total_quota = (vint)atoi(vval);
						else if(!strcmp(oval,"user-quota"))
							rp->options.perf_options.user_quota = (vint)atoi(vval);
						else {
							TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Unknown realm option: %s\n", oval);
						}
					}
				}
			}

			if(res) {
				PQclear(res);
			}
		}
	}
}
Esempio n. 7
0
static void mongo_reread_realms(secrets_list_t * realms_list) {

	UNUSED_ARG(realms_list);

	mongoc_collection_t * collection = mongo_get_collection("realm");

	if (!collection)
		return;

	bson_t query;
	bson_init(&query);

	bson_t fields;
	bson_init(&fields);
	BSON_APPEND_INT32(&fields, "realm", 1);
	BSON_APPEND_INT32(&fields, "origin", 1);
	BSON_APPEND_INT32(&fields, "options", 1);

	mongoc_cursor_t * cursor;
	cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0,
			&query, &fields, NULL);

	if (!cursor) {
		TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
				"Error querying MongoDB collection 'realm'\n");
	} else {
		ur_string_map *o_to_realm_new = ur_string_map_create(turn_free_simple);

		const bson_t * item;
		uint32_t length;
		bson_iter_t iter;

		while (mongoc_cursor_next(cursor, &item)) {

			if (bson_iter_init(&iter, item) && bson_iter_find(&iter, "realm")
					&& BSON_ITER_HOLDS_UTF8(&iter)) {

				char * _realm = turn_strdup(bson_iter_utf8(&iter, &length));

				get_realm(_realm);

				if (bson_iter_init(&iter, item) && bson_iter_find(&iter,
						"origin") && BSON_ITER_HOLDS_ARRAY(&iter)) {
					const uint8_t *docbuf = NULL;
					uint32_t doclen = 0;
					bson_t origin_array;
					bson_iter_t origin_iter;

					bson_iter_array(&iter, &doclen, &docbuf);
					bson_init_static(&origin_array, docbuf, doclen);

					if (bson_iter_init(&origin_iter, &origin_array)) {
						while (bson_iter_next(&origin_iter)) {
							if (BSON_ITER_HOLDS_UTF8(&origin_iter)) {
								char* _origin =	turn_strdup(bson_iter_utf8(&origin_iter, &length));
								char *rval = turn_strdup(_realm);
								ur_string_map_value_type value =
										(ur_string_map_value_type) (rval);
								ur_string_map_put(o_to_realm_new,
										(const ur_string_map_key_type) _origin,
										value);
								turn_free(_origin,strlen(_origin)+1);
							}
						}
					}
				}

				realm_params_t* rp = get_realm(_realm);
				lock_realms();
				rp->options.perf_options.max_bps = turn_params.max_bps;
				rp->options.perf_options.total_quota = turn_params.total_quota;
				rp->options.perf_options.user_quota = turn_params.user_quota;
				unlock_realms();

				if (bson_iter_init(&iter, item) && bson_iter_find(&iter,
						"options") && BSON_ITER_HOLDS_DOCUMENT(&iter)) {
					const uint8_t *docbuf = NULL;
					uint32_t doclen = 0;
					bson_t options;
					bson_iter_t options_iter;

					bson_iter_document(&iter, &doclen, &docbuf);
					bson_init_static(&options, docbuf, doclen);

					if (bson_iter_init(&options_iter, &options)) {
						while (bson_iter_next(&options_iter)) {
							const char * _k = bson_iter_key(&options_iter);
							uint64_t _v = 0;
							if (BSON_ITER_HOLDS_DOUBLE(&options_iter)) {
								_v = (uint64_t) bson_iter_double(&options_iter);
							} else if (BSON_ITER_HOLDS_INT32(&options_iter)) {
								_v = (uint64_t)bson_iter_int32(&options_iter);
							} else if (BSON_ITER_HOLDS_INT64(&options_iter)) {
								_v = (uint64_t) bson_iter_int64(&options_iter);
							}
							if (_v) {
								if (!strcmp(_k, "max-bps"))
									rp->options.perf_options.max_bps = (band_limit_t) _v;
								else if (!strcmp(_k, "total-quota"))
									rp->options.perf_options.total_quota = (vint) _v;
								else if (!strcmp(_k, "user-quota"))
									rp->options.perf_options.user_quota = (vint) _v;
								else {
									TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,
											"Unknown realm option: %s\n", _k);
								}
							}
						}
					}
				}
				turn_free(_realm,strlen(_realm)+1);
			}
		}
		update_o_to_realm(o_to_realm_new);
		mongoc_cursor_destroy(cursor);
	}
	mongoc_collection_destroy(collection);
	bson_destroy(&query);
	bson_destroy(&fields);
}