summaryrefslogtreecommitdiff
path: root/other/guess-who/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'other/guess-who/main.cc')
-rw-r--r--other/guess-who/main.cc166
1 files changed, 166 insertions, 0 deletions
diff --git a/other/guess-who/main.cc b/other/guess-who/main.cc
new file mode 100644
index 0000000..84c2422
--- /dev/null
+++ b/other/guess-who/main.cc
@@ -0,0 +1,166 @@
1/*
2 * Copyright (C) 2002,2003 Sebastian Krahmer.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Sebastian Krahmer.
16 * 4. The name Sebastian Krahmer may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32#include <stdio.h>
33#include <unistd.h>
34#include <iostream>
35#include <pthread.h>
36#include <map>
37#include <vector>
38
39extern "C" {
40#include <openssl/crypto.h>
41}
42#include "thread.h"
43#include "misc.h"
44#include "ssh.h"
45
46using namespace std;
47
48pthread_mutex_t map_lock = PTHREAD_MUTEX_INITIALIZER;
49map<int, pthread_mutex_t *> n_locks;
50
51
52unsigned long my_id()
53{
54 return (unsigned long)pthread_self();
55}
56
57
58void my_locking(int mode, int n, const char *file, int line)
59{
60 pthread_mutex_t *l = NULL;
61
62 if (mode & CRYPTO_LOCK) {
63 pthread_mutex_lock(&map_lock);
64 if (n_locks.find(n) == n_locks.end()) {
65 l = new pthread_mutex_t;
66 pthread_mutex_init(l, NULL);
67 n_locks[n] = l;
68 } else
69 l = n_locks[n];
70 pthread_mutex_lock(l);
71 pthread_mutex_unlock(&map_lock);
72 } else {
73 pthread_mutex_lock(&map_lock);
74 pthread_mutex_unlock(n_locks[n]);
75 pthread_mutex_unlock(&map_lock);
76 }
77}
78
79
80void usage()
81{
82 printf("\nguess-who SSH2 parallel passwd bruter (C) 2002 by "
83 "krahmer@cs.uni-potsdam.de\n\n"
84 "Usage: ./a.out <-l login> <-h host> [-p port] <-1|-2> "
85 "[-N nthreads] [-n ntries]\n"
86 "Use -1 for producer/consumer thread model, -2 for dumb "
87 "parallelism. Passwds go on stdin. :)\n\n");
88 exit(1);
89}
90
91
92int main(int argc, char **argv)
93{
94
95 int c;
96 int mode = 0, nthreads = 10;
97 thread_data td;
98
99 td.port = 22;
100 td.tries = 6;
101
102 while ((c = getopt(argc, argv, "l:h:p:12N:n:")) != -1) {
103 switch (c) {
104 case 'n':
105 td.tries = atoi(optarg);
106 break;
107 case 'l':
108 td.login = optarg;
109 break;
110 case 'h':
111 td.host = optarg;
112 break;
113 case 'p':
114 td.port = atoi(optarg);
115 break;
116 case '1':
117 mode = 1;
118 break;
119 case '2':
120 mode = 2;
121 break;
122 case 'N':
123 nthreads = atoi(optarg);
124 break;
125 default:
126 usage();
127 }
128 }
129
130 if (td.login.size() == 0 || td.host.size() == 0 || mode == 0 ||
131 (mode == 2 && nthreads == 0))
132 usage();
133
134 CRYPTO_set_locking_callback(my_locking);
135 CRYPTO_set_id_callback(my_id);
136
137 now = time(NULL);
138
139 if (mode == 1) {
140 pthread_t tid1, tid2;
141
142 pthread_create(&tid1, NULL, producer_thread, &td);
143 pthread_create(&tid2, NULL, consumer_thread, &td);
144
145 void *vp;
146 pthread_join(tid1, &vp);
147 pthread_join(tid2, &vp);
148 } else {
149 vector<pthread_t *> tids;
150 tids.resize(nthreads);
151
152 for (int i = 0; i < nthreads; ++i) {
153 pthread_t *tid = new pthread_t;
154 pthread_create(tid, NULL, single_try, &td);
155 tids[i] = tid;
156 }
157 void *vp;
158 for (int i = 0; i < nthreads; ++i) {
159 pthread_join(*tids[i], &vp);
160 delete tids[i];
161 }
162 }
163
164 return 0;
165}
166