allocate an extension when creating the subscriber
diff --git a/openbsc/include/openbsc/gsm_subscriber.h b/openbsc/include/openbsc/gsm_subscriber.h
index 0abf17a..5a86ec8 100644
--- a/openbsc/include/openbsc/gsm_subscriber.h
+++ b/openbsc/include/openbsc/gsm_subscriber.h
@@ -11,6 +11,9 @@
 #define GSM_NAME_LENGTH 128
 #define GSM_EXTENSION_LENGTH 128
 
+#define GSM_MIN_EXTEN 20000
+#define GSM_MAX_EXTEN 49999
+
 #define GSM_SUBSCRIBER_FIRST_CONTACT	0x00000001
 
 struct gsm_equipment {
diff --git a/openbsc/src/db.c b/openbsc/src/db.c
index 6017260..f610bfc 100644
--- a/openbsc/src/db.c
+++ b/openbsc/src/db.c
@@ -248,6 +248,7 @@
 	strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
 	dbi_result_free(result);
 	printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
+	db_subscriber_alloc_exten(subscr);
 	return subscr;
 }
 
@@ -419,6 +420,34 @@
 	return 0;
 }
 
+int db_subscriber_alloc_exten(struct gsm_subscriber* subscriber) {
+	dbi_result result=NULL;
+	u_int32_t try;
+	for (;;) {
+		try = (rand()%(GSM_MAX_EXTEN+1)+GSM_MIN_EXTEN)%(GSM_MAX_EXTEN+1);
+		result = dbi_conn_queryf(conn,
+			"SELECT * FROM Subscriber "
+			"WHERE extension = %llu",
+			try
+		);
+		if (result==NULL) {
+			printf("DB: Failed to query Subscriber while allocating new extension.\n");
+			return 1;
+		}
+		if (dbi_result_get_numrows(result)){
+			dbi_result_free(result);
+			continue;
+		}
+		if (!dbi_result_next_row(result)) {
+			dbi_result_free(result);
+			break;
+		}
+		dbi_result_free(result);
+	}
+	sprintf(subscriber->extension, "%i", try);
+	printf("DB: Allocated extension %i for IMSI %s.\n", try, subscriber->imsi);
+	return db_sync_subscriber(subscriber);
+}
 /*
  * try to allocate a new unique token for this subscriber and return it
  * via a parameter. if the subscriber already has a token, return