• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythongamewindowsbathyscaphephpguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

密猟オンラインサーバープログラム


Commit MetaInfo

Revision8 (tree)
Time2016-01-29 18:27:15
Authormanjihq

Log Message

暗号化ルーチンを追加
ユーザーデータ保存時にパスワード部分を暗号化するようにした
暗号化キーを設定ファイルに追加

Change Summary

Incremental Difference

--- trunk/mjcrypt.c (nonexistent)
+++ trunk/mjcrypt.c (revision 8)
@@ -0,0 +1,111 @@
1+#include "port.h"
2+#include <stdio.h>
3+#include <stdlib.h>
4+#include <string.h>
5+#include "mjcrypt.h"
6+
7+/* byte to manji */
8+static unsigned
9+byte2manji (MANJIKEY mkey, unsigned char c)
10+{
11+ c = (unsigned char) (((unsigned short) c + mkey->key[mkey->npos++]) & 0xff);
12+ if (mkey->npos >= mkey->nlen)
13+ {
14+ mkey->npos = 0;
15+ }
16+ c ^= mkey->key[mkey->npos++];
17+ if (mkey->npos >= mkey->nlen)
18+ {
19+ mkey->npos = 0;
20+ }
21+ return c;
22+}
23+
24+/* maji to byte */
25+static unsigned char
26+manji2byte (MANJIKEY mkey, unsigned char c)
27+{
28+ unsigned char nbase = mkey->key[mkey->npos++];
29+ if (mkey->npos >= mkey->nlen)
30+ {
31+ mkey->npos = 0;
32+ }
33+ c ^= mkey->key[mkey->npos++];
34+ if (mkey->npos >= mkey->nlen)
35+ {
36+ mkey->npos = 0;
37+ }
38+ c = (unsigned char) (((unsigned short) c - nbase) & 0xff);
39+ return c;
40+}
41+
42+/* Manji crypt */
43+int
44+manjiCrypt (MANJIKEY mkey,
45+ unsigned char *pi, int nin_len, unsigned char *po, int nout_len)
46+{
47+ int i;
48+ int nout = 0;
49+ if (mkey->nlen < 1)
50+ {
51+ return -1;
52+ }
53+ for (i = 0; i < nin_len; i++)
54+ {
55+ if (nout + 1 > nout_len)
56+ {
57+ /* overflow */
58+ nout = -1;
59+ break;
60+ }
61+ *po++ = byte2manji (mkey, *pi++);
62+ nout++;
63+ }
64+ return nout;
65+}
66+
67+/* Manji decrypt */
68+int
69+manjiDecrypt (MANJIKEY mkey,
70+ unsigned char *pi, int nin_len, unsigned char *po, int nout_len)
71+{
72+ int i;
73+ int nout = 0;
74+ if (mkey->nlen < 1)
75+ {
76+ return -1;
77+ }
78+ for (i = 0; i < nin_len; i++)
79+ {
80+ if (nout + 1 > nout_len)
81+ {
82+ /* overflow */
83+ nout = -1;
84+ break;
85+ }
86+ *po++ = manji2byte (mkey, *pi++);
87+ nout++;
88+ }
89+ return nout;
90+}
91+
92+/* Allocate manji key */
93+MANJIKEY
94+manjiKeyAllocate (char *pkey)
95+{
96+ MANJIKEY p = malloc (sizeof (struct manjikey_t));
97+ p->nlen = strlen (pkey);
98+ p->npos = 0;
99+ p->key = (unsigned char *) strdup (pkey);
100+ return p;
101+}
102+
103+/* Deallocate manji key */
104+void
105+manjiKeyDeallocate (MANJIKEY mkey)
106+{
107+ free (mkey->key);
108+ free (mkey);
109+}
110+
111+/* EOF */
--- trunk/Makefile (revision 7)
+++ trunk/Makefile (revision 8)
@@ -4,7 +4,8 @@
44 #CFLAGS=-Wall -g $(DEFINES)
55 CFLAGS=-Wall -O $(DEFINES)
66
7-SRCS=huntserv.c ctlsock.c pktio.c strtable.c hunt.h pltio.h ctlsock.h strtable.h port.h
7+SRCS=huntserv.c ctlsock.c pktio.c strtable.c mjcrypt.c \
8+ hunt.h pltio.h ctlsock.h strtable.h mjcrypt.h port.h
89
910 all: huntserv
1011
@@ -15,8 +16,8 @@
1516 -rm *~ core *.o
1617 -rm h-user h-bbs h-map h-mail.* huntserv
1718
18-huntserv : huntserv.o ctlsock.o pktio.o strtable.o
19- $(CC) $(CFLAGS) -o huntserv huntserv.o ctlsock.o pktio.o strtable.o
19+huntserv : huntserv.o ctlsock.o pktio.o strtable.o mjcrypt.o
20+ $(CC) $(CFLAGS) -o huntserv huntserv.o ctlsock.o pktio.o strtable.o mjcrypt.o
2021
2122
2223 ctlsock.o : ctlsock.c ctlsock.h port.h
@@ -25,8 +26,10 @@
2526
2627 strtable.o : strtable.c strtable.h port.h
2728
28-huntserv.o : huntserv.c hunt.h pktio.h ctlsock.h port.h
29+mjcrypt.o : mjcrypt.c mjcrypt.h port.h
2930
31+huntserv.o : huntserv.c hunt.h pktio.h ctlsock.h strtable.h mjcrypt.h port.h
3032
3133
34+
3235 #EOF
--- trunk/mjcrypt.h (nonexistent)
+++ trunk/mjcrypt.h (revision 8)
@@ -0,0 +1,23 @@
1+/* Manji crypt */
2+
3+struct manjikey_t
4+{
5+ int nlen;
6+ unsigned char *key;
7+ int npos;
8+};
9+typedef struct manjikey_t *MANJIKEY;
10+
11+#define RESET_MANJIKEY(mkey) ((mkey)->npos = 0)
12+
13+extern int
14+manjiCrypt (MANJIKEY mkey,
15+ unsigned char *pi, int nin_len, unsigned char *po, int nout_len);
16+extern int
17+manjiDecrypt (MANJIKEY mkey,
18+ unsigned char *pi, int nin_len, unsigned char *po,
19+ int nout_len);
20+extern MANJIKEY manjiKeyAllocate (char *pkey);
21+extern void manjiKeyDeallocate (MANJIKEY mkey);
22+
23+/* EOF */
--- trunk/huntserv.c (revision 7)
+++ trunk/huntserv.c (revision 8)
@@ -17,6 +17,7 @@
1717 #include "hunt.h"
1818 #include "pktio.h"
1919 #include "strtable.h"
20+#include "mjcrypt.h"
2021
2122 /* Game feature */
2223 #define USE_SAVEMAP
@@ -551,6 +552,8 @@
551552 static int HireableNpcType = HIREABLE_NPC_TYPE;
552553 static char makeUserKey[PASSWORD_LEN] = "HUNTER1234567890";
553554 static char rootPassword[PASSWORD_LEN] = "HunterMasterCode";
555+static char manjiPassword[PASSWORD_LEN] = "MANJICRYPTKEY123";
556+static MANJIKEY pmanjiKey = NULL;
554557
555558 /* map data */
556559 struct map_block
@@ -2174,7 +2177,7 @@
21742177 {
21752178 break;
21762179 }
2177- if (c == ' ' || c == ',' || iscntrl ((int) c))
2180+ if (c == ' ' || c == ',' || c == '>' || c == '<' || iscntrl ((int) c))
21782181 {
21792182 c = '_';
21802183 }
@@ -2797,6 +2800,7 @@
27972800 if (iptr != NULL)
27982801 {
27992802 char work[BUFFER_SIZE];
2803+ RESET_MANJIKEY (pmanjiKey);
28002804 if (fgets (work, sizeof (work), iptr) != NULL)
28012805 {
28022806 int ver = 0;
@@ -2808,6 +2812,10 @@
28082812 {
28092813 ver = 6;
28102814 }
2815+ else if (!memcmp (work, "#H0110", 6))
2816+ {
2817+ ver = 10;
2818+ }
28112819 if (ver == 0)
28122820 {
28132821 /* mismatch version */
@@ -2838,7 +2846,8 @@
28382846 {
28392847 break;
28402848 }
2841- for (j = 0; work[j] != '\0' && j < USERNAME_LEN; j++)
2849+ for (j = 0; work[j] != '\n' && work[j] != '\0' &&
2850+ j < USERNAME_LEN; j++)
28422851 {
28432852 User[i].name[j] = work[j];
28442853 }
@@ -2856,13 +2865,51 @@
28562865 {
28572866 break;
28582867 }
2859- for (j = 0; work[j] != '\0' && j < PASSWORD_LEN; j++)
2868+ if (ver == 10)
28602869 {
2861- User[i].password[j] = work[j];
2870+ char cnvbuffer[PASSWORD_LEN];
2871+ char wkhex[3];
2872+ wkhex[2] = '\0';
2873+ for (j = 0; j < PASSWORD_LEN; j++)
2874+ {
2875+ wkhex[0] = work[j * 2];
2876+ if (wkhex[0] == '\n' || wkhex[0] == '\0')
2877+ {
2878+ break;
2879+ }
2880+ wkhex[1] = work[j * 2 + 1];
2881+ if (wkhex[1] == '\n' || wkhex[1] == '\0')
2882+ {
2883+ break;
2884+ }
2885+ cnvbuffer[j] =
2886+ (char) (strtol (wkhex, NULL, 16) & 0xff);
2887+ }
2888+ j = manjiDecrypt (pmanjiKey,
2889+ (unsigned char *) cnvbuffer, j,
2890+ (unsigned char *)
2891+ User[i].password, PASSWORD_LEN);
2892+ if (j == -1)
2893+ {
2894+ /* error ? */
2895+ j = 0;
2896+ }
2897+ for (; j < PASSWORD_LEN; j++)
2898+ {
2899+ User[i].password[j] = ' ';
2900+ }
28622901 }
2863- for (; j < PASSWORD_LEN; j++)
2902+ else
28642903 {
2865- User[i].password[j] = ' ';
2904+ for (j = 0; work[j] != '\n' && work[j] != '\0' &&
2905+ j < PASSWORD_LEN; j++)
2906+ {
2907+ User[i].password[j] = work[j];
2908+ }
2909+ for (; j < PASSWORD_LEN; j++)
2910+ {
2911+ User[i].password[j] = ' ';
2912+ }
28662913 }
28672914 if (fgets (work, sizeof (work), iptr) == NULL)
28682915 {
@@ -2998,7 +3045,8 @@
29983045 {
29993046 int i;
30003047 int nusermax = 0;
3001- fputs ("#H0106 Hunter-Online USER DATA FILE - Do not modify!\n", optr);
3048+ RESET_MANJIKEY (pmanjiKey);
3049+ fputs ("#H0110 Hunter-Online USER DATA FILE - Do not modify!\n", optr);
30023050 for (i = 1; i < UserLimit; i++)
30033051 {
30043052 if (User[i].name[0] != '\0')
@@ -3012,13 +3060,23 @@
30123060 if (User[i].name[0] != '\0')
30133061 {
30143062 int j;
3063+ int nresult;
30153064 char work[BUFFER_SIZE];
30163065 memcpy (work, User[i].name, USERNAME_LEN);
30173066 work[USERNAME_LEN] = '\0';
30183067 fprintf (optr, "%-*s\n", USERNAME_LEN, work);
3019- memcpy (work, User[i].password, PASSWORD_LEN);
3020- work[PASSWORD_LEN] = '\0';
3021- fprintf (optr, "%-*s\n", PASSWORD_LEN, work);
3068+ nresult = manjiCrypt (pmanjiKey,
3069+ (unsigned char *) User[i].password,
3070+ PASSWORD_LEN, (unsigned char *) work,
3071+ PASSWORD_LEN);
3072+ if (nresult > 0)
3073+ {
3074+ for (j = 0; j < nresult; j++)
3075+ {
3076+ fprintf (optr, "%02X", (unsigned) work[j] & 0xff);
3077+ }
3078+ }
3079+ fputs ("\n", optr);
30223080 fprintf (optr, "%lu\n", User[i].score);
30233081 fprintf (optr, "%d,%d,%d,%d,%d,%d,%d\n", User[i].c_type,
30243082 User[i].direction, User[i].pos.x, User[i].pos.y,
@@ -6371,7 +6429,8 @@
63716429 for (i = 0; i < USERNAME_LEN && *p != '\0'; i++, p++)
63726430 {
63736431 char c = *p;
6374- if (c == ' ' || c == ',' || iscntrl ((int) c))
6432+ if (c == ' ' || c == ',' || c == '>' || c == '<' ||
6433+ iscntrl ((int) c))
63756434 {
63766435 c = '_';
63776436 }
@@ -10827,6 +10886,13 @@
1082710886 copy_string_nolf (rootPassword, lbuf + 1, PASSWORD_LEN);
1082810887 continue;
1082910888 }
10889+ if (lbuf[0] == '$')
10890+ {
10891+ /* define manji crypt-key */
10892+ /* $KEY */
10893+ copy_string_nolf (manjiPassword, lbuf + 1, PASSWORD_LEN);
10894+ continue;
10895+ }
1083010896 if (sscanf (lbuf, "%[^,],%lu", namebuf, &nbuf) != 2)
1083110897 {
1083210898 continue;
@@ -11182,6 +11248,11 @@
1118211248 kill_all_send_message_queue_thread ();
1118311249 #endif
1118411250 #endif /* MAKEWIN32 */
11251+ if (pmanjiKey != NULL)
11252+ {
11253+ manjiKeyDeallocate (pmanjiKey);
11254+ pmanjiKey = NULL;
11255+ }
1118511256 puts ("complete.");
1118611257 return;
1118711258 }
@@ -11360,6 +11431,7 @@
1136011431 puts ("Loading datas...");
1136111432 sprintf (work, "%s/%s", DATA_DIR, INIT_DATA_FILE);
1136211433 LoadSetup (work, 0);
11434+ pmanjiKey = manjiKeyAllocate (manjiPassword);
1136311435 User = calloc (sizeof (USERINFO), UserLimit);
1136411436 Animal = calloc (sizeof (ANIMALINFO), AnimalLimit);
1136511437 Npc = calloc (sizeof (NPCINFO), NpcLimit);