Changeset c595308
- Timestamp:
- 2008-02-11T21:20:35Z (17 years ago)
- Branches:
- master
- Children:
- 8bd697c
- Parents:
- d6fdf44
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
irc.c
rd6fdf44 rc595308 131 131 132 132 irc->otr_us = otrl_userstate_create(); 133 irc->otr_mutex = g_mutex_new();133 g_static_rec_mutex_init(&irc->otr_mutex); 134 134 135 135 return( irc ); … … 284 284 285 285 otrl_userstate_free(irc->otr_us); 286 g_ mutex_free(irc->otr_mutex);286 g_static_rec_mutex_free(&irc->otr_mutex); 287 287 288 288 g_free(irc); -
irc.h
rd6fdf44 rc595308 99 99 100 100 OtrlUserState otr_us; 101 G Mutex *otr_mutex; /* for locking otr during keygen */101 GStaticRecMutex otr_mutex; /* for locking otr during keygen */ 102 102 } irc_t; 103 103 -
otr.c
rd6fdf44 rc595308 54 54 void cmd_otr_info(irc_t *irc, char **args); 55 55 void cmd_otr_keygen(irc_t *irc, char **args); 56 /* void cmd_otr_forget(irc_t *irc, char **args); */ 56 void cmd_otr_forget(irc_t *irc, char **args); 57 57 58 58 const command_t otr_commands[] = { … … 63 63 { "info", 0, &cmd_otr_info, 0 }, 64 64 { "keygen", 1, &cmd_otr_keygen, 0 }, 65 /* 66 { "forget", 1, &cmd_otr_forget, 0 }, 67 */ 65 { "forget", 2, &cmd_otr_forget, 0 }, 68 66 { NULL } 69 67 }; … … 74 72 /* start background thread to generate a (new) key for a given account */ 75 73 void otr_keygen(irc_t *irc, const char *handle, const char *protocol); 74 76 75 /* keygen thread main func */ 77 76 gpointer otr_keygen_thread_func(gpointer data); 77 78 78 /* mainloop handler for when keygen thread finishes */ 79 79 gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond); 80 80 81 /* data to be passed to otr_keygen_thread_func */ 81 82 struct kgdata { … … 89 90 }; 90 91 91 /* yes/no handlers for "generate key now?"*/92 /* some yes/no handlers */ 92 93 void yes_keygen(gpointer w, void *data); 93 void no_keygen(gpointer w, void *data); 94 void yes_forget_fingerprint(gpointer w, void *data); 95 void yes_forget_context(gpointer w, void *data); 94 96 95 97 /* helper to make sure accountname and protocol match the incoming "opdata" */ … … 124 126 /* show the list of fingerprints associated with a given context */ 125 127 void show_fingerprints(irc_t *irc, ConnContext *ctx); 128 129 /* find a fingerprint by prefix (given as any number of hex strings) */ 130 Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args); 126 131 127 132 /* to log out accounts during keygen */ … … 259 264 char *colormsg; 260 265 261 if(!g_ mutex_trylock(ic->irc->otr_mutex)) {266 if(!g_static_rec_mutex_trylock(&ic->irc->otr_mutex)) { 262 267 irc_usermsg(ic->irc, "otr keygen in progress - msg from %s dropped", 263 268 peernick(ic->irc, handle, ic->acc->prpl->name)); … … 273 278 if(ignore_msg) { 274 279 /* this was an internal OTR protocol message */ 275 g_ mutex_unlock(ic->irc->otr_mutex);280 g_static_rec_mutex_unlock(&ic->irc->otr_mutex); 276 281 return NULL; 277 282 } else if(!newmsg) { 278 283 /* this was a non-OTR message */ 279 g_ mutex_unlock(ic->irc->otr_mutex);284 g_static_rec_mutex_unlock(&ic->irc->otr_mutex); 280 285 return g_strdup(msg); 281 286 } else { … … 296 301 } 297 302 otrl_message_free(newmsg); 298 g_ mutex_unlock(ic->irc->otr_mutex);303 g_static_rec_mutex_unlock(&ic->irc->otr_mutex); 299 304 return colormsg; 300 305 } … … 307 312 ConnContext *ctx = NULL; 308 313 309 if(!g_ mutex_trylock(ic->irc->otr_mutex)) {314 if(!g_static_rec_mutex_trylock(&ic->irc->otr_mutex)) { 310 315 irc_usermsg(ic->irc, "otr keygen in progress - msg to %s not sent", 311 316 peernick(ic->irc, handle, ic->acc->prpl->name)); … … 317 322 msg, NULL, &otrmsg, NULL, NULL); 318 323 if(st) { 319 g_ mutex_unlock(ic->irc->otr_mutex);324 g_static_rec_mutex_unlock(&ic->irc->otr_mutex); 320 325 return st; 321 326 } … … 328 333 if(!ctx) { 329 334 otrl_message_free(otrmsg); 330 g_ mutex_unlock(ic->irc->otr_mutex);335 g_static_rec_mutex_unlock(&ic->irc->otr_mutex); 331 336 return 1; 332 337 } … … 339 344 } 340 345 341 g_ mutex_unlock(ic->irc->otr_mutex);346 g_static_rec_mutex_unlock(&ic->irc->otr_mutex); 342 347 return st; 343 348 } … … 359 364 360 365 if(!cmd->command) { 361 irc_usermsg(irc, "%s %s: unknown subcommand, see \x02help otr\x02",366 irc_usermsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02", 362 367 args[0], args[1]); 363 368 return; … … 392 397 s = g_strdup_printf("oops, no otr privkey for %s/%s - generate one now?", 393 398 accountname, protocol); 394 query_add(ic->irc, ic, s, yes_keygen, no_keygen, ic->acc);399 query_add(ic->irc, ic, s, yes_keygen, NULL, ic->acc); 395 400 } 396 401 … … 476 481 check_imc(opdata, context->accountname, context->protocol); 477 482 user_t *u; 483 const char *trust; 478 484 479 485 log_message(LOGLVL_DEBUG, "op_gone_secure '%s' '%s' '%s'", context->accountname, context->protocol, context->username); … … 486 492 return; 487 493 } 488 if(context->active_fingerprint->trust[0]) 494 495 trust = context->active_fingerprint->trust; 496 if(trust && trust[0]) 489 497 u->encrypted = 2; 490 498 else … … 579 587 otrl_message_disconnect(irc->otr_us, &global.otr_ops, 580 588 u->ic, u->ic->acc->user, u->ic->acc->prpl->name, u->handle); 589 590 /* for some reason, libotr (3.1.0) doesn't do this itself: */ 591 if(u->encrypted) { 592 ConnContext *ctx; 593 ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, 594 u->ic->acc->prpl->name, 0, NULL, NULL, NULL); 595 if(ctx) 596 op_gone_insecure(u->ic, ctx); 597 else /* huh? */ 598 u->encrypted = 0; 599 } 581 600 } 582 601 … … 719 738 *(protocol++) = '\0'; 720 739 myhandle = strchr(protocol, '/'); 721 if(!myhandle) {722 /* TODO: try to find a unique account for this context */723 }724 740 } 725 741 if(protocol && myhandle) { … … 728 744 ctx = otrl_context_find(irc->otr_us, handle, myhandle, protocol, 0, NULL, NULL, NULL); 729 745 if(!ctx) { 730 irc_usermsg(irc, "no such context (%s %s %s)", handle, protocol, myhandle);746 irc_usermsg(irc, "no such context"); 731 747 g_free(arg); 732 748 return; … … 778 794 if(otrl_privkey_find(irc->otr_us, a->user, a->prpl->name)) { 779 795 char *s = g_strdup_printf("account %d already has a key, replace it?", n); 780 query_add(irc, a->ic, s, yes_keygen, no_keygen, a);796 query_add(irc, a->ic, s, yes_keygen, NULL, a); 781 797 } else { 782 798 otr_keygen(irc, a->user, a->prpl->name); 799 } 800 } 801 802 void yes_forget_fingerprint(gpointer w, void *data) 803 { 804 struct im_connection *ic = (struct im_connection *)w; 805 Fingerprint *fp = (Fingerprint *)data; 806 807 if(fp == fp->context->active_fingerprint) { 808 irc_usermsg(ic->irc, "that fingerprint is active, terminate otr connection first"); 809 return; 810 } 811 812 otrl_context_forget_fingerprint(fp, 0); 813 } 814 815 void yes_forget_context(gpointer w, void *data) 816 { 817 struct im_connection *ic = (struct im_connection *)w; 818 ConnContext *ctx = (ConnContext *)data; 819 820 if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { 821 irc_usermsg(ic->irc, "active otr connection with %s, terminate it first", 822 peernick(ic->irc, ctx->username, ctx->protocol)); 823 return; 824 } 825 826 if(ctx->msgstate == OTRL_MSGSTATE_FINISHED) 827 otrl_context_force_plaintext(ctx); 828 otrl_context_forget(ctx); 829 } 830 831 void cmd_otr_forget(irc_t *irc, char **args) 832 { 833 if(!strcmp(args[1], "fingerprint")) 834 { 835 user_t *u; 836 ConnContext *ctx; 837 Fingerprint *fp; 838 char human[54]; 839 char *s; 840 841 if(!args[3]) { 842 irc_usermsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]); 843 return; 844 } 845 846 u = user_find(irc, args[2]); 847 if(!u || !u->ic) { 848 irc_usermsg(irc, "%s: unknown user", args[2]); 849 return; 850 } 851 852 ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, 853 u->ic->acc->prpl->name, 0, NULL, NULL, NULL); 854 if(!ctx) { 855 irc_usermsg(irc, "no otr context with %s", args[2]); 856 return; 857 } 858 859 fp = match_fingerprint(irc, ctx, ((const char **)args)+3); 860 if(!fp) { 861 /* match_fingerprint does error messages */ 862 return; 863 } 864 865 if(fp == ctx->active_fingerprint) { 866 irc_usermsg(irc, "that fingerprint is active, terminate otr connection first"); 867 return; 868 } 869 870 otrl_privkey_hash_to_human(human, fp->fingerprint); 871 s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human); 872 query_add(irc, u->ic, s, yes_forget_fingerprint, NULL, fp); 873 } 874 875 else if(!strcmp(args[1], "context")) 876 { 877 user_t *u; 878 ConnContext *ctx; 879 char *s; 880 881 u = user_find(irc, args[2]); 882 if(!u || !u->ic) { 883 irc_usermsg(irc, "%s: unknown user", args[2]); 884 return; 885 } 886 887 ctx = otrl_context_find(irc->otr_us, u->handle, u->ic->acc->user, 888 u->ic->acc->prpl->name, 0, NULL, NULL, NULL); 889 if(!ctx) { 890 irc_usermsg(irc, "no otr context with %s", args[2]); 891 return; 892 } 893 894 if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { 895 irc_usermsg(irc, "active otr connection with %s, terminate it first", args[2]); 896 return; 897 } 898 899 s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]); 900 query_add(irc, u->ic, s, yes_forget_context, NULL, ctx); 901 } 902 903 else if(!strcmp(args[1], "key")) 904 { 905 irc_usermsg(irc, "n/a: TODO"); 906 } 907 908 else 909 { 910 irc_usermsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02", 911 args[0], args[1]); 783 912 } 784 913 } … … 956 1085 int trusted = u->encrypted > 1; 957 1086 char flags[7]; 958 int nflags ;1087 int nflags=0; 959 1088 char *p = flags; 960 1089 char *from; … … 1036 1165 } 1037 1166 1167 Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args) 1168 { 1169 Fingerprint *fp, *fp2; 1170 char human[45]; 1171 char prefix[45], *p; 1172 int n; 1173 int i,j; 1174 1175 /* assemble the args into a prefix in standard "human" form */ 1176 n=0; 1177 p=prefix; 1178 for(i=0; args[i]; i++) { 1179 for(j=0; args[i][j]; j++) { 1180 char c = toupper(args[i][j]); 1181 1182 if(n>=40) { 1183 irc_usermsg(irc, "too many fingerprint digits given, expected at most 40"); 1184 return NULL; 1185 } 1186 1187 if( (c>='A' && c<='F') || (c>='0' && c<='9') ) { 1188 *(p++) = c; 1189 } else { 1190 irc_usermsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1); 1191 return NULL; 1192 } 1193 1194 n++; 1195 if(n%8 == 0) 1196 *(p++) = ' '; 1197 } 1198 } 1199 *p = '\0'; 1200 log_message(LOGLVL_DEBUG, "match_fingerprint '%s'", prefix); 1201 log_message(LOGLVL_DEBUG, "n=%d strlen(prefix)=%d", n, strlen(prefix)); 1202 1203 /* find first fingerprint with the given prefix */ 1204 n = strlen(prefix); 1205 for(fp=&ctx->fingerprint_root; fp; fp=fp->next) { 1206 if(!fp->fingerprint) 1207 continue; 1208 otrl_privkey_hash_to_human(human, fp->fingerprint); 1209 if(!strncmp(prefix, human, n)) 1210 break; 1211 } 1212 if(!fp) { 1213 irc_usermsg(irc, "%s: no match", prefix); 1214 return NULL; 1215 } 1216 1217 /* make sure the match, if any, is unique */ 1218 for(fp2=fp->next; fp2; fp2=fp2->next) { 1219 if(!fp2->fingerprint) 1220 continue; 1221 otrl_privkey_hash_to_human(human, fp2->fingerprint); 1222 if(!strncmp(prefix, human, n)) 1223 break; 1224 } 1225 if(fp2) { 1226 irc_usermsg(irc, "%s: multiple matches", prefix); 1227 return NULL; 1228 } 1229 1230 return fp; 1231 } 1232 1038 1233 void show_general_otr_info(irc_t *irc) 1039 1234 { … … 1080 1275 1081 1276 if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { 1082 otrl_privkey_hash_to_human(human, ctx->active_fingerprint->fingerprint);1083 1277 irc_usermsg(irc, " \x02%s\x02", userstring); 1084 irc_usermsg(irc, " %s", human);1085 1278 } else { 1086 1279 irc_usermsg(irc, " %s", userstring); … … 1124 1317 } 1125 1318 1126 irc_usermsg(irc, " knownfingerprints: (bold=active)");1319 irc_usermsg(irc, " fingerprints: (bold=active)"); 1127 1320 show_fingerprints(irc, ctx); 1128 1321 } … … 1159 1352 return; 1160 1353 } 1161 kg->done = FALSE;1354 kg->done = 0; 1162 1355 1163 1356 /* Poll for completion of the thread periodically. I would have preferred … … 1195 1388 1196 1389 /* lock OTR subsystem and do the work */ 1197 g_ mutex_lock(kg->irc->otr_mutex);1390 g_static_rec_mutex_lock(&kg->irc->otr_mutex); 1198 1391 kg->result = otrl_privkey_generate(kg->irc->otr_us, kg->keyfile, kg->handle, 1199 1392 kg->protocol); 1200 g_ mutex_unlock(kg->irc->otr_mutex);1393 g_static_rec_mutex_unlock(&kg->irc->otr_mutex); 1201 1394 /* OTR enabled again */ 1202 1395 1203 1396 /* notify mainloop */ 1204 1397 g_mutex_lock(kg->mutex); 1205 kg->done = TRUE;1398 kg->done = 1; 1206 1399 g_mutex_unlock(kg->mutex); 1207 1400 … … 1239 1432 } 1240 1433 1241 void no_keygen(gpointer w, void *data)1242 {1243 account_t *acc = (account_t *)data;1244 1245 irc_usermsg(acc->irc, "keygen cancelled for %s/%s",1246 acc->user, acc->prpl->name);1247 }1248 1249 1434 1250 1435 #else /* WITH_OTR undefined */ -
root_commands.c
rd6fdf44 rc595308 86 86 return; 87 87 88 if(!g_ mutex_trylock(irc->otr_mutex)) {88 if(!g_static_rec_mutex_trylock(&irc->otr_mutex)) { 89 89 irc_usermsg(irc, "keygen in progress, bitlbee comatose - please wait"); 90 90 return; … … 97 97 { 98 98 irc_usermsg( irc, "Not enough parameters given (need %d)", commands[i].required_parameters ); 99 g_ mutex_unlock(irc->otr_mutex);99 g_static_rec_mutex_unlock(&irc->otr_mutex); 100 100 return; 101 101 } 102 102 commands[i].execute( irc, cmd ); 103 g_ mutex_unlock(irc->otr_mutex);103 g_static_rec_mutex_unlock(&irc->otr_mutex); 104 104 return; 105 105 } 106 106 107 107 irc_usermsg( irc, "Unknown command: %s. Please use \x02help commands\x02 to get a list of available commands.", cmd[0] ); 108 g_ mutex_unlock(irc->otr_mutex);108 g_static_rec_mutex_unlock(&irc->otr_mutex); 109 109 } 110 110
Note: See TracChangeset
for help on using the changeset viewer.