Sun, 09 May 2004 04:06:58 +0000
[gaim-migrate @ 9680]
a patch from wing to fix some grammer and puctuation (but not my spelling),
and some white space
committer: Luke Schierer <lschiere@pidgin.im>
| 8849 | 1 | /* |
| 2 | ||
| 3 | silcgaim_util.c | |
| 4 | ||
| 5 | Author: Pekka Riikonen <priikone@silcnet.org> | |
| 6 | ||
| 7 | Copyright (C) 2004 Pekka Riikonen | |
| 8 | ||
| 9 | This program is free software; you can redistribute it and/or modify | |
| 10 | it under the terms of the GNU General Public License as published by | |
| 11 | the Free Software Foundation; version 2 of the License. | |
| 12 | ||
| 13 | This program is distributed in the hope that it will be useful, | |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | GNU General Public License for more details. | |
| 17 | ||
| 18 | */ | |
| 19 | ||
| 20 | #include "silcincludes.h" | |
| 21 | #include "silcclient.h" | |
| 22 | #include "silcgaim.h" | |
| 23 | ||
| 24 | /**************************** Utility Routines *******************************/ | |
| 25 | ||
| 26 | static char str[256], str2[256]; | |
| 27 | ||
| 28 | const char *silcgaim_silcdir(void) | |
| 29 | { | |
| 30 | const char *hd = gaim_home_dir(); | |
| 31 | memset(str, 0, sizeof(str)); | |
| 32 | g_snprintf(str, sizeof(str) - 1, "%s" G_DIR_SEPARATOR_S ".silc", hd ? hd : "/tmp"); | |
| 33 | return (const char *)str; | |
| 34 | } | |
| 35 | ||
| 36 | const char *silcgaim_session_file(const char *account) | |
| 37 | { | |
| 38 | memset(str2, 0, sizeof(str2)); | |
| 39 | g_snprintf(str2, sizeof(str2) - 1, "%s" G_DIR_SEPARATOR_S "%s_session", | |
| 40 | silcgaim_silcdir(), account); | |
| 41 | return (const char *)str2; | |
| 42 | } | |
| 43 | ||
| 44 | gboolean silcgaim_ip_is_private(const char *ip) | |
| 45 | { | |
| 46 | if (silc_net_is_ip4(ip)) { | |
| 47 | if (!strncmp(ip, "10.", 3)) { | |
| 48 | return TRUE; | |
| 49 | } else if (!strncmp(ip, "172.", 4) && strlen(ip) > 6) { | |
| 50 | char tmp[3]; | |
| 8910 | 51 | int s; |
| 8849 | 52 | memset(tmp, 0, sizeof(tmp)); |
| 53 | strncpy(tmp, ip + 4, 2); | |
| 8910 | 54 | s = atoi(tmp); |
| 8849 | 55 | if (s >= 16 && s <= 31) |
| 56 | return TRUE; | |
| 57 | } else if (!strncmp(ip, "192.168.", 8)) { | |
| 58 | return TRUE; | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | return FALSE; | |
| 63 | } | |
| 64 | ||
| 65 | /* This checks stats for various SILC files and directories. First it | |
| 66 | checks if ~/.silc directory exist and is owned by the correct user. If | |
| 67 | it doesn't exist, it will create the directory. After that it checks if | |
| 68 | user's Public and Private key files exists and creates them if needed. */ | |
| 69 | ||
| 70 | gboolean silcgaim_check_silc_dir(GaimConnection *gc) | |
| 71 | { | |
| 72 | char filename[256], file_public_key[256], file_private_key[256]; | |
| 73 | char servfilename[256], clientfilename[256], friendsfilename[256]; | |
| 74 | struct stat st; | |
| 75 | struct passwd *pw; | |
| 76 | ||
| 77 | pw = getpwuid(getuid()); | |
| 78 | if (!pw) { | |
| 79 | fprintf(stderr, "silc: %s\n", strerror(errno)); | |
| 80 | return FALSE; | |
| 81 | } | |
| 82 | ||
| 83 | g_snprintf(filename, sizeof(filename) - 1, "%s" G_DIR_SEPARATOR_S, silcgaim_silcdir()); | |
| 84 | g_snprintf(servfilename, sizeof(servfilename) - 1, "%s" G_DIR_SEPARATOR_S "serverkeys", | |
| 85 | silcgaim_silcdir()); | |
| 86 | g_snprintf(clientfilename, sizeof(clientfilename) - 1, "%s" G_DIR_SEPARATOR_S "clientkeys", | |
| 87 | silcgaim_silcdir()); | |
| 88 | g_snprintf(friendsfilename, sizeof(friendsfilename) - 1, "%s" G_DIR_SEPARATOR_S "friends", | |
| 89 | silcgaim_silcdir()); | |
| 90 | ||
| 91 | /* | |
| 92 | * Check ~/.silc directory | |
| 93 | */ | |
| 94 | if ((stat(filename, &st)) == -1) { | |
| 95 | /* If dir doesn't exist */ | |
| 96 | if (errno == ENOENT) { | |
| 97 | if (pw->pw_uid == geteuid()) { | |
| 98 | if ((mkdir(filename, 0755)) == -1) { | |
| 99 | fprintf(stderr, "Couldn't create `%s' directory\n", filename); | |
| 100 | return FALSE; | |
| 101 | } | |
| 102 | } else { | |
| 103 | fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n", | |
| 104 | filename); | |
| 105 | return FALSE; | |
| 106 | } | |
| 107 | } else { | |
| 108 | fprintf(stderr, "%s\n", strerror(errno)); | |
| 109 | return FALSE; | |
| 110 | } | |
| 111 | } else { | |
| 112 | /* Check the owner of the dir */ | |
| 113 | if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { | |
| 114 | fprintf(stderr, "You don't seem to own `%s' directory\n", | |
| 115 | filename); | |
| 116 | return FALSE; | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | /* | |
| 121 | * Check ~./silc/serverkeys directory | |
| 122 | */ | |
| 123 | if ((stat(servfilename, &st)) == -1) { | |
| 124 | /* If dir doesn't exist */ | |
| 125 | if (errno == ENOENT) { | |
| 126 | if (pw->pw_uid == geteuid()) { | |
| 127 | if ((mkdir(servfilename, 0755)) == -1) { | |
| 128 | fprintf(stderr, "Couldn't create `%s' directory\n", servfilename); | |
| 129 | return FALSE; | |
| 130 | } | |
| 131 | } else { | |
| 132 | fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n", | |
| 133 | servfilename); | |
| 134 | return FALSE; | |
| 135 | } | |
| 136 | } else { | |
| 137 | fprintf(stderr, "%s\n", strerror(errno)); | |
| 138 | return FALSE; | |
| 139 | } | |
| 140 | } | |
| 141 | ||
| 142 | /* | |
| 143 | * Check ~./silc/clientkeys directory | |
| 144 | */ | |
| 145 | if ((stat(clientfilename, &st)) == -1) { | |
| 146 | /* If dir doesn't exist */ | |
| 147 | if (errno == ENOENT) { | |
| 148 | if (pw->pw_uid == geteuid()) { | |
| 149 | if ((mkdir(clientfilename, 0755)) == -1) { | |
| 150 | fprintf(stderr, "Couldn't create `%s' directory\n", clientfilename); | |
| 151 | return FALSE; | |
| 152 | } | |
| 153 | } else { | |
| 154 | fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n", | |
| 155 | clientfilename); | |
| 156 | return FALSE; | |
| 157 | } | |
| 158 | } else { | |
| 159 | fprintf(stderr, "%s\n", strerror(errno)); | |
| 160 | return FALSE; | |
| 161 | } | |
| 162 | } | |
| 163 | ||
| 164 | /* | |
| 165 | * Check ~./silc/friends directory | |
| 166 | */ | |
| 167 | if ((stat(friendsfilename, &st)) == -1) { | |
| 168 | /* If dir doesn't exist */ | |
| 169 | if (errno == ENOENT) { | |
| 170 | if (pw->pw_uid == geteuid()) { | |
| 171 | if ((mkdir(friendsfilename, 0755)) == -1) { | |
| 172 | fprintf(stderr, "Couldn't create `%s' directory\n", friendsfilename); | |
| 173 | return FALSE; | |
| 174 | } | |
| 175 | } else { | |
| 176 | fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n", | |
| 177 | friendsfilename); | |
| 178 | return FALSE; | |
| 179 | } | |
| 180 | } else { | |
| 181 | fprintf(stderr, "%s\n", strerror(errno)); | |
| 182 | return FALSE; | |
| 183 | } | |
| 184 | } | |
| 185 | ||
| 186 | /* | |
| 187 | * Check Public and Private keys | |
| 188 | */ | |
| 189 | g_snprintf(file_public_key, sizeof(file_public_key) - 1, "%s", | |
| 190 | gaim_prefs_get_string("/plugins/prpl/silc/pubkey")); | |
| 191 | g_snprintf(file_private_key, sizeof(file_public_key) - 1, "%s", | |
| 192 | gaim_prefs_get_string("/plugins/prpl/silc/privkey")); | |
| 193 | ||
| 194 | if ((stat(file_public_key, &st)) == -1) { | |
| 195 | /* If file doesn't exist */ | |
| 196 | if (errno == ENOENT) { | |
| 197 | gaim_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5); | |
| 198 | silc_create_key_pair(SILCGAIM_DEF_PKCS, | |
| 199 | SILCGAIM_DEF_PKCS_LEN, | |
| 200 | file_public_key, file_private_key, NULL, | |
| 201 | "", NULL, NULL, NULL, FALSE); | |
| 202 | } else { | |
| 203 | fprintf(stderr, "%s\n", strerror(errno)); | |
| 204 | return FALSE; | |
| 205 | } | |
| 206 | } | |
| 207 | ||
| 208 | /* Check the owner of the public key */ | |
| 209 | if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { | |
| 210 | fprintf(stderr, "You don't seem to own your public key!?\n"); | |
| 211 | return FALSE; | |
| 212 | } | |
| 213 | ||
| 214 | if ((stat(file_private_key, &st)) == -1) { | |
| 215 | /* If file doesn't exist */ | |
| 216 | if (errno == ENOENT) { | |
| 217 | gaim_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5); | |
| 218 | silc_create_key_pair(SILCGAIM_DEF_PKCS, | |
| 219 | SILCGAIM_DEF_PKCS_LEN, | |
| 220 | file_public_key, file_private_key, NULL, | |
| 221 | "", NULL, NULL, NULL, FALSE); | |
| 222 | } else { | |
| 223 | fprintf(stderr, "%s\n", strerror(errno)); | |
| 224 | return FALSE; | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | /* Check the owner of the private key */ | |
| 229 | if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { | |
| 230 | fprintf(stderr, "You don't seem to own your private key!?\n"); | |
| 231 | return FALSE; | |
| 232 | } | |
| 233 | ||
| 234 | /* Check the permissions for the private key */ | |
| 235 | if ((st.st_mode & 0777) != 0600) { | |
| 236 | fprintf(stderr, "Wrong permissions in your private key file `%s'!\n" | |
| 237 | "Trying to change them ... ", file_private_key); | |
| 238 | if ((chmod(file_private_key, 0600)) == -1) { | |
| 239 | fprintf(stderr, | |
| 240 | "Failed to change permissions for private key file!\n" | |
| 241 | "Permissions for your private key file must be 0600.\n"); | |
| 242 | return FALSE; | |
| 243 | } | |
| 244 | fprintf(stderr, "Done.\n\n"); | |
| 245 | } | |
| 246 | ||
| 247 | return TRUE; | |
| 248 | } | |
| 249 | ||
| 250 | void silcgaim_show_public_key(SilcGaim sg, | |
| 251 | const char *name, SilcPublicKey public_key, | |
| 252 | GCallback callback, void *context) | |
| 253 | { | |
| 254 | SilcPublicKeyIdentifier ident; | |
| 255 | SilcPKCS pkcs; | |
| 256 | char *fingerprint, *babbleprint; | |
| 257 | unsigned char *pk; | |
| 258 | SilcUInt32 pk_len, key_len = 0; | |
| 259 | GString *s; | |
| 260 | char *buf; | |
| 261 | ||
| 262 | ident = silc_pkcs_decode_identifier(public_key->identifier); | |
| 263 | if (!ident) | |
| 264 | return; | |
| 265 | ||
| 266 | pk = silc_pkcs_public_key_encode(public_key, &pk_len); | |
| 267 | fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); | |
| 268 | babbleprint = silc_hash_babbleprint(NULL, pk, pk_len); | |
| 269 | ||
| 270 | if (silc_pkcs_alloc(public_key->name, &pkcs)) { | |
| 271 | key_len = silc_pkcs_public_key_set(pkcs, public_key); | |
| 272 | silc_pkcs_free(pkcs); | |
| 273 | } | |
| 274 | ||
| 275 | s = g_string_new(""); | |
| 276 | if (ident->realname) | |
| 277 | g_string_append_printf(s, "Real Name: \t%s\n", ident->realname); | |
| 278 | if (ident->username) | |
| 279 | g_string_append_printf(s, "User Name: \t%s\n", ident->username); | |
| 280 | if (ident->email) | |
| 281 | g_string_append_printf(s, "EMail: \t\t%s\n", ident->email); | |
| 282 | if (ident->host) | |
| 283 | g_string_append_printf(s, "Host Name: \t%s\n", ident->host); | |
| 284 | if (ident->org) | |
| 285 | g_string_append_printf(s, "Organization: \t%s\n", ident->org); | |
| 286 | if (ident->country) | |
| 287 | g_string_append_printf(s, "Country: \t%s\n", ident->country); | |
| 288 | g_string_append_printf(s, "Algorithm: \t\t%s\n", public_key->name); | |
| 289 | g_string_append_printf(s, "Key Length: \t%d bits\n", (int)key_len); | |
| 290 | g_string_append_printf(s, "\n"); | |
| 291 | g_string_append_printf(s, "Public Key Fingerprint:\n%s\n\n", fingerprint); | |
| 292 | g_string_append_printf(s, "Public Key Babbleprint:\n%s", babbleprint); | |
| 293 | ||
| 294 | buf = g_string_free(s, FALSE); | |
| 295 | ||
| 296 | gaim_request_action(NULL, _("Public Key Information"), | |
| 297 | _("Public Key Information"), | |
| 298 | buf, 0, context, 1, | |
| 299 | _("Close"), callback); | |
| 300 | ||
| 301 | g_free(buf); | |
| 302 | silc_free(fingerprint); | |
| 303 | silc_free(babbleprint); | |
| 304 | silc_free(pk); | |
| 305 | silc_pkcs_free_identifier(ident); | |
| 306 | } | |
| 307 | ||
| 308 | SilcAttributePayload | |
| 309 | silcgaim_get_attr(SilcDList attrs, SilcAttribute attribute) | |
| 310 | { | |
| 311 | SilcAttributePayload attr = NULL; | |
| 312 | ||
| 313 | if (!attrs) | |
| 314 | return NULL; | |
| 315 | ||
| 316 | silc_dlist_start(attrs); | |
| 317 | while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) | |
| 318 | if (attribute == silc_attribute_get_attribute(attr)) | |
| 319 | break; | |
| 320 | ||
| 321 | return attr; | |
| 322 | } | |
| 323 | ||
| 324 | void silcgaim_get_umode_string(SilcUInt32 mode, char *buf, | |
| 325 | SilcUInt32 buf_size) | |
| 326 | { | |
| 327 | memset(buf, 0, buf_size); | |
| 328 | if ((mode & SILC_UMODE_SERVER_OPERATOR) || | |
| 329 | (mode & SILC_UMODE_ROUTER_OPERATOR)) { | |
| 330 | strcat(buf, (mode & SILC_UMODE_SERVER_OPERATOR) ? | |
| 331 | "[server operator] " : | |
| 332 | (mode & SILC_UMODE_ROUTER_OPERATOR) ? | |
| 333 | "[SILC operator] " : "[unknown mode] "); | |
| 334 | } | |
| 335 | if (mode & SILC_UMODE_GONE) | |
| 336 | strcat(buf, "[away] "); | |
| 337 | if (mode & SILC_UMODE_INDISPOSED) | |
| 338 | strcat(buf, "[indisposed] "); | |
| 339 | if (mode & SILC_UMODE_BUSY) | |
| 340 | strcat(buf, "[busy] "); | |
| 341 | if (mode & SILC_UMODE_PAGE) | |
| 342 | strcat(buf, "[wake me up] "); | |
| 343 | if (mode & SILC_UMODE_HYPER) | |
| 344 | strcat(buf, "[hyperactive] "); | |
| 345 | if (mode & SILC_UMODE_ROBOT) | |
| 346 | strcat(buf, "[robot] "); | |
| 347 | if (mode & SILC_UMODE_ANONYMOUS) | |
| 348 | strcat(buf, "[anonymous] "); | |
| 349 | if (mode & SILC_UMODE_BLOCK_PRIVMSG) | |
| 350 | strcat(buf, "[blocks private messages] "); | |
| 351 | if (mode & SILC_UMODE_DETACHED) | |
| 352 | strcat(buf, "[detached] "); | |
| 353 | if (mode & SILC_UMODE_REJECT_WATCHING) | |
| 354 | strcat(buf, "[rejects watching] "); | |
| 355 | if (mode & SILC_UMODE_BLOCK_INVITE) | |
| 356 | strcat(buf, "[blocks invites] "); | |
| 357 | } | |
| 358 | ||
| 359 | void silcgaim_get_chmode_string(SilcUInt32 mode, char *buf, | |
| 360 | SilcUInt32 buf_size) | |
| 361 | { | |
| 362 | memset(buf, 0, buf_size); | |
| 363 | if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) | |
| 364 | strcat(buf, "[permanent] "); | |
| 365 | if (mode & SILC_CHANNEL_MODE_PRIVATE) | |
| 366 | strcat(buf, "[private] "); | |
| 367 | if (mode & SILC_CHANNEL_MODE_SECRET) | |
| 368 | strcat(buf, "[secret] "); | |
| 369 | if (mode & SILC_CHANNEL_MODE_SECRET) | |
| 370 | strcat(buf, "[secret] "); | |
| 371 | if (mode & SILC_CHANNEL_MODE_PRIVKEY) | |
| 372 | strcat(buf, "[private key] "); | |
| 373 | if (mode & SILC_CHANNEL_MODE_INVITE) | |
| 374 | strcat(buf, "[invite only] "); | |
| 375 | if (mode & SILC_CHANNEL_MODE_TOPIC) | |
| 376 | strcat(buf, "[topic restricted] "); | |
| 377 | if (mode & SILC_CHANNEL_MODE_ULIMIT) | |
| 378 | strcat(buf, "[user count limit] "); | |
| 379 | if (mode & SILC_CHANNEL_MODE_PASSPHRASE) | |
| 380 | strcat(buf, "[passphrase auth] "); | |
| 381 | if (mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) | |
| 382 | strcat(buf, "[public key auth] "); | |
| 383 | if (mode & SILC_CHANNEL_MODE_SILENCE_USERS) | |
| 384 | strcat(buf, "[users silenced] "); | |
| 385 | if (mode & SILC_CHANNEL_MODE_SILENCE_OPERS) | |
| 386 | strcat(buf, "[operators silenced] "); | |
| 387 | } | |
| 388 | ||
| 389 | void silcgaim_get_chumode_string(SilcUInt32 mode, char *buf, | |
| 390 | SilcUInt32 buf_size) | |
| 391 | { | |
| 392 | memset(buf, 0, buf_size); | |
| 393 | if (mode & SILC_CHANNEL_UMODE_CHANFO) | |
| 394 | strcat(buf, "[founder] "); | |
| 395 | if (mode & SILC_CHANNEL_UMODE_CHANOP) | |
| 396 | strcat(buf, "[operator] "); | |
| 397 | if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) | |
| 398 | strcat(buf, "[blocks messages] "); | |
| 399 | if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) | |
| 400 | strcat(buf, "[blocks user messages] "); | |
| 401 | if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) | |
| 402 | strcat(buf, "[blocks robot messages] "); | |
| 403 | if (mode & SILC_CHANNEL_UMODE_QUIET) | |
| 404 | strcat(buf, "[quieted] "); | |
| 405 | } |