diff options
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | auto_tests/messenger_test.c | 2 | ||||
-rw-r--r-- | core/Messenger.c | 12 | ||||
-rw-r--r-- | core/Messenger.h | 16 | ||||
-rw-r--r-- | testing/nTox.c | 2 | ||||
-rw-r--r-- | testing/toxic/chat.c | 2 | ||||
-rw-r--r-- | testing/toxic/configdir.c | 108 |
7 files changed, 92 insertions, 58 deletions
@@ -49,9 +49,9 @@ configure for the normal user or suffer from being way too centralized. | |||
49 | 49 | ||
50 | - [Installation](/INSTALL.md) | 50 | - [Installation](/INSTALL.md) |
51 | - [Commands](/docs/commands.rst) | 51 | - [Commands](/docs/commands.rst) |
52 | - [DHT Protocol](https://github.com/irungentoo/ProjectTox-Core/wiki/DHT)<br /> | 52 | - [DHT Protocol](http://wiki.tox.im/index.php/DHT)<br /> |
53 | - [Lossless UDP Protocol](https://github.com/irungentoo/ProjectTox-Core/wiki/Lossless-UDP)<br /> | 53 | - [Lossless UDP Protocol](http://wiki.tox.im/index.php/Lossless_UDP)<br /> |
54 | - [Crypto](https://github.com/irungentoo/ProjectTox-Core/wiki/Crypto)<br /> | 54 | - [Crypto](http://wiki.tox.im/index.php/Crypto)<br /> |
55 | - [Ideas](https://github.com/irungentoo/ProjectTox-Core/wiki/Ideas) | 55 | - [Ideas](http://wiki.tox.im/index.php/Ideas) |
56 | 56 | ||
57 | [String]: https://en.wikipedia.org/wiki/String_(computer_science) | 57 | [String]: https://en.wikipedia.org/wiki/String_(computer_science) |
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 64b44d5f..28747899 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c | |||
@@ -169,7 +169,7 @@ START_TEST(test_getself_name) | |||
169 | char nick_check[len]; | 169 | char nick_check[len]; |
170 | 170 | ||
171 | setname(m, (uint8_t *)nickname, len); | 171 | setname(m, (uint8_t *)nickname, len); |
172 | getself_name(m, (uint8_t *)nick_check); | 172 | getself_name(m, (uint8_t *)nick_check, len); |
173 | 173 | ||
174 | ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)), | 174 | ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)), |
175 | "getself_name failed to return the known name!\n" | 175 | "getself_name failed to return the known name!\n" |
diff --git a/core/Messenger.c b/core/Messenger.c index eb151a6a..af102406 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -269,10 +269,18 @@ int setname(Messenger *m, uint8_t * name, uint16_t length) | |||
269 | put it in name | 269 | put it in name |
270 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 270 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
271 | return the length of the name */ | 271 | return the length of the name */ |
272 | uint16_t getself_name(Messenger *m, uint8_t *name) | 272 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) |
273 | { | 273 | { |
274 | uint16_t len; | ||
275 | |||
276 | if (name == NULL || nlen == 0) { | ||
277 | return 0; | ||
278 | } | ||
279 | |||
280 | len = MIN(nlen, m->name_length); | ||
274 | memcpy(name, m->name, m->name_length); | 281 | memcpy(name, m->name, m->name_length); |
275 | return m->name_length; | 282 | |
283 | return len; | ||
276 | } | 284 | } |
277 | 285 | ||
278 | /* get name of friendnumber | 286 | /* get name of friendnumber |
diff --git a/core/Messenger.h b/core/Messenger.h index fa69d104..aa9611a4 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -196,10 +196,18 @@ int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t lengt | |||
196 | return -1 if failure */ | 196 | return -1 if failure */ |
197 | int setname(Messenger *m, uint8_t *name, uint16_t length); | 197 | int setname(Messenger *m, uint8_t *name, uint16_t length); |
198 | 198 | ||
199 | /* get our nickname | 199 | /** |
200 | put it in name | 200 | * @brief Get your nickname. |
201 | return the length of the name*/ | 201 | * |
202 | uint16_t getself_name(Messenger *m, uint8_t *name); | 202 | * @param[in] m The messanger context to use. |
203 | * | ||
204 | * @param[inout] name Pointer to a string for the name. | ||
205 | * | ||
206 | * @param[in] nlen The length of the string buffer. | ||
207 | * | ||
208 | * @return Return the length of the name, 0 on error. | ||
209 | */ | ||
210 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); | ||
203 | 211 | ||
204 | /* get name of friendnumber | 212 | /* get name of friendnumber |
205 | put it in name | 213 | put it in name |
diff --git a/testing/nTox.c b/testing/nTox.c index 59d1cbf6..74db2ae2 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -113,7 +113,7 @@ char *format_message(Messenger *m, char *message, int friendnum) | |||
113 | if (friendnum != -1) { | 113 | if (friendnum != -1) { |
114 | getname(m, friendnum, (uint8_t*)name); | 114 | getname(m, friendnum, (uint8_t*)name); |
115 | } else { | 115 | } else { |
116 | getself_name(m, (uint8_t*)name); | 116 | getself_name(m, (uint8_t*)name, sizeof(name)); |
117 | } | 117 | } |
118 | char *msg = malloc(100+strlen(message)+strlen(name)+1); | 118 | char *msg = malloc(100+strlen(message)+strlen(name)+1); |
119 | 119 | ||
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 112b20b7..59b13492 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -210,7 +210,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct | |||
210 | wattroff(ctx->history, COLOR_PAIR(2)); | 210 | wattroff(ctx->history, COLOR_PAIR(2)); |
211 | 211 | ||
212 | uint8_t selfname[MAX_NAME_LENGTH]; | 212 | uint8_t selfname[MAX_NAME_LENGTH]; |
213 | int len = getself_name(m, selfname); | 213 | int len = getself_name(m, selfname, sizeof(selfname)); |
214 | char msg[MAX_STR_SIZE-len-4]; | 214 | char msg[MAX_STR_SIZE-len-4]; |
215 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); | 215 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); |
216 | 216 | ||
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c index 6cbb06bc..18e211ce 100644 --- a/testing/toxic/configdir.c +++ b/testing/toxic/configdir.c | |||
@@ -28,65 +28,83 @@ | |||
28 | #ifdef WIN32 | 28 | #ifdef WIN32 |
29 | #include <shlobj.h> | 29 | #include <shlobj.h> |
30 | #include <direct.h> | 30 | #include <direct.h> |
31 | #endif | 31 | #else /* WIN32 */ |
32 | |||
33 | #ifdef __APPLE__ | ||
34 | #include <unistd.h> | 32 | #include <unistd.h> |
35 | #include <pwd.h> | 33 | #include <pwd.h> |
36 | #endif | 34 | #endif /* WIN32 */ |
37 | 35 | ||
38 | #include "configdir.h" | 36 | #include "configdir.h" |
39 | 37 | ||
40 | /* | 38 | /** |
41 | * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash | 39 | * @brief Get the users config directory. |
40 | * | ||
41 | * This is without a trailing slash. | ||
42 | * | ||
43 | * @return The users config dir or NULL on error. | ||
42 | */ | 44 | */ |
43 | char *get_user_config_dir(void) | 45 | char *get_user_config_dir(void) |
44 | { | 46 | { |
45 | char *user_config_dir; | 47 | char *user_config_dir; |
48 | #ifdef WIN32 | ||
49 | char appdata[MAX_PATH]; | ||
50 | BOOL ok; | ||
46 | 51 | ||
47 | #ifdef WIN32 | 52 | ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE); |
53 | if (!ok) { | ||
54 | return NULL; | ||
55 | } | ||
48 | 56 | ||
49 | char appdata[MAX_PATH]; | 57 | user_config_dir = strdup(appdata); |
50 | HRESULT result = SHGetFolderPath( | 58 | |
51 | NULL, | 59 | return user_config_dir; |
52 | CSIDL_APPDATA, | 60 | |
53 | NULL, | 61 | #else /* WIN32 */ |
54 | SHGFP_TYPE_CURRENT, | 62 | |
55 | appdata | 63 | #ifndef NSS_BUFLEN_PASSWD |
56 | ) | 64 | #define NSS_BUFLEN_PASSWD 4096 |
57 | if (!result) return NULL; | 65 | #endif /* NSS_BUFLEN_PASSWD */ |
58 | 66 | ||
59 | user_config_dir = strdup(appdata); | 67 | struct passwd pwd; |
60 | 68 | struct passwd *pwdbuf; | |
61 | return user_config_dir; | 69 | const char *home; |
62 | 70 | char buf[NSS_BUFLEN_PASSWD]; | |
63 | #elif defined __APPLE__ | 71 | size_t len; |
64 | 72 | int rc; | |
65 | struct passwd *pass = getpwuid(getuid()); | 73 | |
66 | if (!pass) return NULL; | 74 | rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); |
67 | char *home = pass->pw_dir; | 75 | if (rc == 0) { |
68 | user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1); | 76 | home = pwd.pw_dir; |
69 | 77 | } else { | |
70 | if(user_config_dir) { | 78 | home = getenv("HOME"); |
71 | strcpy(user_config_dir, home); | 79 | if (home == NULL) { |
72 | strcat(user_config_dir, "/Library/Application Support"); | 80 | return NULL; |
73 | } | 81 | } |
74 | return user_config_dir; | 82 | /* env variables can be tainted */ |
83 | snprintf(buf, sizeof(buf), "%s", home); | ||
84 | home = buf; | ||
85 | } | ||
75 | 86 | ||
76 | #else | 87 | # if defined(__APPLE__) |
88 | len = strlen(home) + strlen("/Library/Application Support") + 1; | ||
89 | user_config_dir = malloc(len); | ||
90 | if (user_config_dir == NULL) { | ||
91 | return NULL; | ||
92 | } | ||
77 | 93 | ||
78 | if (getenv("XDG_CONFIG_HOME")) { | 94 | snprintf(user_config_dir, len, "%s/Library/Application Support", home); |
79 | user_config_dir = strdup(getenv("XDG_CONFIG_HOME")); | 95 | # else /* __APPLE__ */ |
80 | } else { | 96 | len = strlen(home) + strlen("/.config") + 1; |
81 | user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); | 97 | user_config_dir = malloc(len); |
82 | if (user_config_dir) { | 98 | if (user_config_dir == NULL) { |
83 | strcpy(user_config_dir, getenv("HOME")); | 99 | return NULL; |
84 | strcat(user_config_dir, "/.config"); | ||
85 | } | 100 | } |
86 | } | ||
87 | return user_config_dir; | ||
88 | 101 | ||
89 | #endif | 102 | snprintf(user_config_dir, len, "%s/.config", home); |
103 | # endif /* __APPLE__ */ | ||
104 | |||
105 | return user_config_dir; | ||
106 | #undef NSS_BUFLEN_PASSWD | ||
107 | #endif /* WIN32 */ | ||
90 | } | 108 | } |
91 | 109 | ||
92 | /* | 110 | /* |