summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/messenger_test.c2
-rw-r--r--core/Messenger.c12
-rw-r--r--core/Messenger.h16
-rw-r--r--testing/nTox.c2
-rw-r--r--testing/toxic/chat.c2
-rw-r--r--testing/toxic/configdir.c108
6 files changed, 88 insertions, 54 deletions
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 ebde5a78..1c81163c 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -267,10 +267,18 @@ int setname(Messenger *m, uint8_t * name, uint16_t length)
267 put it in name 267 put it in name
268 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 268 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
269 return the length of the name */ 269 return the length of the name */
270uint16_t getself_name(Messenger *m, uint8_t *name) 270uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
271{ 271{
272 uint16_t len;
273
274 if (name == NULL || nlen == 0) {
275 return 0;
276 }
277
278 len = MIN(nlen, m->name_length);
272 memcpy(name, m->name, m->name_length); 279 memcpy(name, m->name, m->name_length);
273 return m->name_length; 280
281 return len;
274} 282}
275 283
276/* get name of friendnumber 284/* 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 */
197int setname(Messenger *m, uint8_t *name, uint16_t length); 197int 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 *
202uint16_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 */
210uint16_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 */
43char *get_user_config_dir(void) 45char *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/*