2 # Generates samba network traffic
4 # Copyright (C) Catalyst IT Ltd. 2017
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from __future__ import print_function
27 sys.path.insert(0, "bin/python")
29 from samba import gensec, get_debug_level
30 from samba.emulate import traffic
31 import samba.getopt as options
32 from samba.logger import get_samba_logger
33 from samba.samdb import SamDB
34 from samba.auth import system_session
37 def print_err(*args, **kwargs):
38 print(*args, file=sys.stderr, **kwargs)
43 desc = ("Generates network traffic 'conversations' based on a model generated"
44 " by script/traffic_learner. This traffic is sent to <dns-hostname>,"
45 " which is the full DNS hostname of the DC being tested.")
47 parser = optparse.OptionParser(
48 "%prog [--help|options] <model-file> <dns-hostname>",
51 parser.add_option('--dns-rate', type='float', default=0,
52 help='fire extra DNS packets at this rate')
53 parser.add_option('--dns-query-file', dest="dns_query_file",
54 help='A file contains DNS query list')
55 parser.add_option('-B', '--badpassword-frequency',
56 type='float', default=0.0,
57 help='frequency of connections with bad passwords')
58 parser.add_option('-K', '--prefer-kerberos',
60 help='prefer kerberos when authenticating test users')
61 parser.add_option('-I', '--instance-id', type='int', default=0,
62 help='Instance number, when running multiple instances')
63 parser.add_option('-t', '--timing-data',
64 help=('write individual message timing data here '
66 parser.add_option('--preserve-tempdir', default=False, action="store_true",
67 help='do not delete temporary files')
68 parser.add_option('-F', '--fixed-password',
69 type='string', default=None,
70 help=('Password used for the test users created. '
72 parser.add_option('-c', '--clean-up',
74 help='Clean up the generated groups and user accounts')
75 parser.add_option('--random-seed', type='int', default=None,
76 help='Use to keep randomness consistent across multiple runs')
77 parser.add_option('--stop-on-any-error',
79 help='abort the whole thing if a child fails')
80 model_group = optparse.OptionGroup(parser, 'Traffic Model Options',
81 'These options alter the traffic '
82 'generated by the model')
83 model_group.add_option('-S', '--scale-traffic', type='float',
84 help=('Increase the number of conversations by '
85 'this factor (or use -T)'))
86 parser.add_option('-T', '--packets-per-second', type=float,
87 help=('attempt this many packets per second '
88 '(alternative to -S)'))
89 parser.add_option('--old-scale',
91 help='emulate the old scale for traffic')
92 model_group.add_option('-D', '--duration', type='float', default=60.0,
93 help=('Run model for this long (approx). '
94 'Default 60s for models'))
95 model_group.add_option('--latency-timeout', type='float', default=None,
96 help=('Wait this long for last packet to finish'))
97 model_group.add_option('-r', '--replay-rate', type='float', default=1.0,
98 help='Replay the traffic faster by this factor')
99 model_group.add_option('--conversation-persistence', type='float',
101 help=('chance (0 to 1) that a conversation waits '
102 'when it would have died'))
103 model_group.add_option('--traffic-summary',
104 help=('Generate a traffic summary file and write '
105 'it here (- for stdout)'))
106 parser.add_option_group(model_group)
108 user_gen_group = optparse.OptionGroup(parser, 'Generate User Options',
109 "Add extra user/groups on the DC to "
110 "increase the DB size. These extra "
111 "users aren't used for traffic "
113 user_gen_group.add_option('-G', '--generate-users-only',
115 help='Generate the users, but do not replay '
117 user_gen_group.add_option('-n', '--number-of-users', type='int', default=0,
118 help='Total number of test users to create')
119 user_gen_group.add_option('--number-of-groups', type='int', default=None,
120 help='Create this many groups')
121 user_gen_group.add_option('--average-groups-per-user',
122 type='int', default=0,
123 help='Assign the test users to this '
124 'many groups on average')
125 user_gen_group.add_option('--group-memberships', type='int', default=0,
126 help='Total memberships to assign across all '
127 'test users and all groups')
128 user_gen_group.add_option('--max-members', type='int', default=None,
129 help='Max users to add to any one group')
130 parser.add_option_group(user_gen_group)
132 sambaopts = options.SambaOptions(parser)
133 parser.add_option_group(sambaopts)
134 parser.add_option_group(options.VersionOptions(parser))
135 credopts = options.CredentialsOptions(parser)
136 parser.add_option_group(credopts)
138 # the --no-password credential doesn't make sense for this tool
139 if parser.has_option('-N'):
140 parser.remove_option('-N')
142 opts, args = parser.parse_args()
144 # First ensure we have reasonable arguments
150 model_file, host = args
155 lp = sambaopts.get_loadparm()
156 debuglevel = get_debug_level()
157 logger = get_samba_logger(name=__name__,
158 verbose=debuglevel > 3,
159 quiet=debuglevel < 1)
161 traffic.DEBUG_LEVEL = debuglevel
162 # pass log level down to traffic module to make sure level is controlled
163 traffic.LOGGER.setLevel(logger.getEffectiveLevel())
166 logger.info("Removing user and machine accounts")
167 lp = sambaopts.get_loadparm()
168 creds = credopts.get_credentials(lp)
169 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
170 ldb = traffic.openLdb(host, creds, lp)
171 traffic.clean_up_accounts(ldb, opts.instance_id)
175 if not os.path.exists(model_file):
176 logger.error("Model file %s doesn't exist" % model_file)
178 # the model-file can be ommitted for --generate-users-only and
179 # --cleanup-up, but it should be specified in all other cases
180 elif not opts.generate_users_only:
181 logger.error("No model file specified to replay traffic from")
184 if not opts.fixed_password:
185 logger.error(("Please use --fixed-password to specify a password"
186 " for the users created as part of this test"))
189 if opts.random_seed is not None:
190 random.seed(opts.random_seed)
192 creds = credopts.get_credentials(lp)
193 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
195 domain = creds.get_domain()
197 lp.set("workgroup", domain)
199 domain = lp.get("workgroup")
200 if domain == "WORKGROUP":
201 logger.error(("NETBIOS domain does not appear to be "
202 "specified, use the --workgroup option"))
205 if not opts.realm and not lp.get('realm'):
206 logger.error("Realm not specified, use the --realm option")
209 if opts.generate_users_only and not (opts.number_of_users or
210 opts.number_of_groups):
211 logger.error(("Please specify the number of users and/or groups "
215 if opts.group_memberships and opts.average_groups_per_user:
216 logger.error(("--group-memberships and --average-groups-per-user"
217 " are incompatible options - use one or the other"))
220 if not opts.number_of_groups and opts.average_groups_per_user:
221 logger.error(("--average-groups-per-user requires "
222 "--number-of-groups"))
225 if opts.number_of_groups and opts.average_groups_per_user:
226 if opts.number_of_groups < opts.average_groups_per_user:
227 logger.error(("--average-groups-per-user can not be more than "
228 "--number-of-groups"))
231 if not opts.number_of_groups and opts.group_memberships:
232 logger.error("--group-memberships requires --number-of-groups")
235 if opts.scale_traffic is not None and opts.packets_per_second is not None:
236 logger.error("--scale-traffic and --packets-per-second "
237 "are incompatible. Use one or the other.")
240 if not opts.scale_traffic and not opts.packets_per_second:
241 logger.info("No packet rate specified. Using --scale-traffic=1.0")
242 opts.scale_traffic = 1.0
244 if opts.timing_data not in ('-', None):
246 open(opts.timing_data, 'w').close()
248 # exception info will be added to log automatically
249 logger.exception(("the supplied timing data destination "
250 "(%s) is not writable" % opts.timing_data))
253 if opts.traffic_summary not in ('-', None):
255 open(opts.traffic_summary, 'w').close()
257 # exception info will be added to log automatically
260 traceback.print_exc()
261 logger.exception(("the supplied traffic summary destination "
262 "(%s) is not writable" % opts.traffic_summary))
266 # we used to use a silly calculation based on the number
267 # of conversations; now we use the number of packets and
268 # scale traffic accurately. To roughly compare with older
269 # numbers you use --old-scale which approximates as follows:
270 opts.scale_traffic *= 0.55
273 if model_file and not opts.generate_users_only:
274 model = traffic.TrafficModel()
276 model.load(model_file)
280 traceback.print_exc()
281 logger.error(("Could not parse %s, which does not seem to be "
282 "a model generated by script/traffic_learner."
286 logger.info(("Using the specified model file to "
287 "generate conversations"))
289 if opts.scale_traffic:
290 packets_per_second = model.scale_to_packet_rate(opts.scale_traffic)
292 packets_per_second = opts.packets_per_second
295 model.generate_conversation_sequences(
299 opts.conversation_persistence)
303 if opts.number_of_users and opts.number_of_users < len(conversations):
304 logger.error(("--number-of-users (%d) is less than the "
305 "number of conversations to replay (%d)"
306 % (opts.number_of_users, len(conversations))))
309 number_of_users = max(opts.number_of_users, len(conversations))
311 if opts.number_of_groups is None:
312 opts.number_of_groups = max(int(number_of_users / 10), 1)
314 max_memberships = number_of_users * opts.number_of_groups
316 if not opts.group_memberships and opts.average_groups_per_user:
317 opts.group_memberships = opts.average_groups_per_user * number_of_users
318 logger.info(("Using %d group-memberships based on %u average "
319 "memberships for %d users"
320 % (opts.group_memberships,
321 opts.average_groups_per_user, number_of_users)))
323 if opts.group_memberships > max_memberships:
324 logger.error(("The group memberships specified (%d) exceeds "
325 "the total users (%d) * total groups (%d)"
326 % (opts.group_memberships, number_of_users,
327 opts.number_of_groups)))
330 # if no groups were specified by the user, then make sure we create some
331 # group memberships (otherwise it's not really a fair test)
332 if not opts.group_memberships and not opts.average_groups_per_user:
333 opts.group_memberships = min(number_of_users * 5, max_memberships)
335 # Get an LDB connection.
337 # if we're only adding users, then it's OK to pass a sam.ldb filepath
338 # as the host, which creates the users much faster. In all other cases
339 # we should be connecting to a remote DC
340 if opts.generate_users_only and os.path.isfile(host):
341 ldb = SamDB(url="ldb://{0}".format(host),
342 session_info=system_session(), lp=lp)
344 ldb = traffic.openLdb(host, creds, lp)
346 logger.error(("\nInitial LDAP connection failed! Did you supply "
347 "a DNS host name and the correct credentials?"))
350 if opts.generate_users_only:
351 # generate computer accounts for added realism. Assume there will be
352 # some overhang with more computer accounts than users
353 computer_accounts = int(1.25 * number_of_users)
354 traffic.generate_users_and_groups(ldb,
357 opts.number_of_users,
358 opts.number_of_groups,
359 opts.group_memberships,
361 machine_accounts=computer_accounts,
362 traffic_accounts=False)
365 tempdir = tempfile.mkdtemp(prefix="samba_tg_")
366 logger.info("Using temp dir %s" % tempdir)
368 traffic.generate_users_and_groups(ldb,
372 opts.number_of_groups,
373 opts.group_memberships,
375 machine_accounts=len(conversations),
376 traffic_accounts=True)
378 accounts = traffic.generate_replay_accounts(ldb,
383 statsdir = traffic.mk_masked_dir(tempdir, 'stats')
385 if opts.traffic_summary:
386 if opts.traffic_summary == '-':
387 summary_dest = sys.stdout
389 summary_dest = open(opts.traffic_summary, 'w')
391 logger.info("Writing traffic summary")
393 for c in traffic.seq_to_conversations(conversations):
394 summaries += c.replay_as_summary_lines()
397 for (time, line) in summaries:
398 print(line, file=summary_dest)
402 traffic.replay(conversations,
407 dns_rate=opts.dns_rate,
408 dns_query_file=opts.dns_query_file,
409 duration=opts.duration,
410 latency_timeout=opts.latency_timeout,
411 badpassword_frequency=opts.badpassword_frequency,
412 prefer_kerberos=opts.prefer_kerberos,
415 base_dn=ldb.domain_dn(),
416 ou=traffic.ou_name(ldb, opts.instance_id),
418 stop_on_any_error=opts.stop_on_any_error,
419 domain_sid=ldb.get_domain_sid(),
420 instance_id=opts.instance_id)
422 if opts.timing_data == '-':
423 timing_dest = sys.stdout
424 elif opts.timing_data is None:
427 timing_dest = open(opts.timing_data, 'w')
429 logger.info("Generating statistics")
430 traffic.generate_stats(statsdir, timing_dest)
432 if not opts.preserve_tempdir:
433 logger.info("Removing temporary directory")
434 shutil.rmtree(tempdir)
436 # delete the empty directories anyway. There are thousands of
437 # them and they're EMPTY.
438 for d in os.listdir(tempdir):
439 if d.startswith('conversation-'):
440 path = os.path.join(tempdir, d)
444 logger.info("not removing %s (%s)" % (path, e))