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('-B', '--badpassword-frequency',
54 type='float', default=0.0,
55 help='frequency of connections with bad passwords')
56 parser.add_option('-K', '--prefer-kerberos',
58 help='prefer kerberos when authenticating test users')
59 parser.add_option('-I', '--instance-id', type='int', default=0,
60 help='Instance number, when running multiple instances')
61 parser.add_option('-t', '--timing-data',
62 help=('write individual message timing data here '
64 parser.add_option('--preserve-tempdir', default=False, action="store_true",
65 help='do not delete temporary files')
66 parser.add_option('-F', '--fixed-password',
67 type='string', default=None,
68 help=('Password used for the test users created. '
70 parser.add_option('-c', '--clean-up',
72 help='Clean up the generated groups and user accounts')
73 parser.add_option('--random-seed', type='int', default=None,
74 help='Use to keep randomness consistent across multiple runs')
75 parser.add_option('--stop-on-any-error',
77 help='abort the whole thing if a child fails')
78 model_group = optparse.OptionGroup(parser, 'Traffic Model Options',
79 'These options alter the traffic '
80 'generated by the model')
81 model_group.add_option('-S', '--scale-traffic', type='float', default=1.0,
82 help='Increase the number of conversations by '
84 parser.add_option('--old-scale',
86 help='emulate the old scale for traffic')
87 model_group.add_option('-D', '--duration', type='float', default=60.0,
88 help=('Run model for this long (approx). '
89 'Default 60s for models'))
90 model_group.add_option('--latency-timeout', type='float', default=None,
91 help=('Wait this long for last packet to finish'))
92 model_group.add_option('-r', '--replay-rate', type='float', default=1.0,
93 help='Replay the traffic faster by this factor')
94 model_group.add_option('--conversation-persistence', type='float',
96 help=('chance (0 to 1) that a conversation waits '
97 'when it would have died'))
98 model_group.add_option('--traffic-summary',
99 help=('Generate a traffic summary file and write '
100 'it here (- for stdout)'))
101 parser.add_option_group(model_group)
103 user_gen_group = optparse.OptionGroup(parser, 'Generate User Options',
104 "Add extra user/groups on the DC to "
105 "increase the DB size. These extra "
106 "users aren't used for traffic "
108 user_gen_group.add_option('-G', '--generate-users-only',
110 help='Generate the users, but do not replay '
112 user_gen_group.add_option('-n', '--number-of-users', type='int', default=0,
113 help='Total number of test users to create')
114 user_gen_group.add_option('--number-of-groups', type='int', default=0,
115 help='Create this many groups')
116 user_gen_group.add_option('--average-groups-per-user',
117 type='int', default=0,
118 help='Assign the test users to this '
119 'many groups on average')
120 user_gen_group.add_option('--group-memberships', type='int', default=0,
121 help='Total memberships to assign across all '
122 'test users and all groups')
123 user_gen_group.add_option('--max-members', type='int', default=None,
124 help='Max users to add to any one group')
125 parser.add_option_group(user_gen_group)
127 sambaopts = options.SambaOptions(parser)
128 parser.add_option_group(sambaopts)
129 parser.add_option_group(options.VersionOptions(parser))
130 credopts = options.CredentialsOptions(parser)
131 parser.add_option_group(credopts)
133 # the --no-password credential doesn't make sense for this tool
134 if parser.has_option('-N'):
135 parser.remove_option('-N')
137 opts, args = parser.parse_args()
139 # First ensure we have reasonable arguments
145 model_file, host = args
150 lp = sambaopts.get_loadparm()
151 debuglevel = get_debug_level()
152 logger = get_samba_logger(name=__name__,
153 verbose=debuglevel > 3,
154 quiet=debuglevel < 1)
156 traffic.DEBUG_LEVEL = debuglevel
157 # pass log level down to traffic module to make sure level is controlled
158 traffic.LOGGER.setLevel(logger.getEffectiveLevel())
161 logger.info("Removing user and machine accounts")
162 lp = sambaopts.get_loadparm()
163 creds = credopts.get_credentials(lp)
164 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
165 ldb = traffic.openLdb(host, creds, lp)
166 traffic.clean_up_accounts(ldb, opts.instance_id)
170 if not os.path.exists(model_file):
171 logger.error("Model file %s doesn't exist" % model_file)
173 # the model-file can be ommitted for --generate-users-only and
174 # --cleanup-up, but it should be specified in all other cases
175 elif not opts.generate_users_only:
176 logger.error("No model file specified to replay traffic from")
179 if not opts.fixed_password:
180 logger.error(("Please use --fixed-password to specify a password"
181 " for the users created as part of this test"))
184 if opts.random_seed is not None:
185 random.seed(opts.random_seed)
187 creds = credopts.get_credentials(lp)
188 creds.set_gensec_features(creds.get_gensec_features() | gensec.FEATURE_SEAL)
190 domain = creds.get_domain()
192 lp.set("workgroup", domain)
194 domain = lp.get("workgroup")
195 if domain == "WORKGROUP":
196 logger.error(("NETBIOS domain does not appear to be "
197 "specified, use the --workgroup option"))
200 if not opts.realm and not lp.get('realm'):
201 logger.error("Realm not specified, use the --realm option")
204 if opts.generate_users_only and not (opts.number_of_users or
205 opts.number_of_groups):
206 logger.error(("Please specify the number of users and/or groups "
210 if opts.group_memberships and opts.average_groups_per_user:
211 logger.error(("--group-memberships and --average-groups-per-user"
212 " are incompatible options - use one or the other"))
215 if not opts.number_of_groups and opts.average_groups_per_user:
216 logger.error(("--average-groups-per-user requires "
217 "--number-of-groups"))
220 if opts.number_of_groups and opts.average_groups_per_user:
221 if opts.number_of_groups < opts.average_groups_per_user:
222 logger.error(("--average-groups-per-user can not be more than "
223 "--number-of-groups"))
226 if not opts.number_of_groups and opts.group_memberships:
227 logger.error("--group-memberships requires --number-of-groups")
230 if opts.timing_data not in ('-', None):
232 open(opts.timing_data, 'w').close()
234 # exception info will be added to log automatically
235 logger.exception(("the supplied timing data destination "
236 "(%s) is not writable" % opts.timing_data))
239 if opts.traffic_summary not in ('-', None):
241 open(opts.traffic_summary, 'w').close()
243 # exception info will be added to log automatically
246 traceback.print_exc()
247 logger.exception(("the supplied traffic summary destination "
248 "(%s) is not writable" % opts.traffic_summary))
252 # we used to use a silly calculation based on the number
253 # of conversations; now we use the number of packets and
254 # scale traffic accurately. To roughly compare with older
255 # numbers you use --old-scale which approximates as follows:
256 opts.scale_traffic *= 0.55
259 if model_file and not opts.generate_users_only:
260 model = traffic.TrafficModel()
262 model.load(model_file)
266 traceback.print_exc()
267 logger.error(("Could not parse %s, which does not seem to be "
268 "a model generated by script/traffic_learner."
272 logger.info(("Using the specified model file to "
273 "generate conversations"))
276 model.generate_conversation_sequences(
280 opts.conversation_persistence)
284 if opts.number_of_users and opts.number_of_users < len(conversations):
285 logger.error(("--number-of-users (%d) is less than the "
286 "number of conversations to replay (%d)"
287 % (opts.number_of_users, len(conversations))))
290 number_of_users = max(opts.number_of_users, len(conversations))
291 max_memberships = number_of_users * opts.number_of_groups
293 if not opts.group_memberships and opts.average_groups_per_user:
294 opts.group_memberships = opts.average_groups_per_user * number_of_users
295 logger.info(("Using %d group-memberships based on %u average "
296 "memberships for %d users"
297 % (opts.group_memberships,
298 opts.average_groups_per_user, number_of_users)))
300 if opts.group_memberships > max_memberships:
301 logger.error(("The group memberships specified (%d) exceeds "
302 "the total users (%d) * total groups (%d)"
303 % (opts.group_memberships, number_of_users,
304 opts.number_of_groups)))
307 # Get an LDB connection.
309 # if we're only adding users, then it's OK to pass a sam.ldb filepath
310 # as the host, which creates the users much faster. In all other cases
311 # we should be connecting to a remote DC
312 if opts.generate_users_only and os.path.isfile(host):
313 ldb = SamDB(url="ldb://{0}".format(host),
314 session_info=system_session(), lp=lp)
316 ldb = traffic.openLdb(host, creds, lp)
318 logger.error(("\nInitial LDAP connection failed! Did you supply "
319 "a DNS host name and the correct credentials?"))
322 if opts.generate_users_only:
323 # generate computer accounts for added realism. Assume there will be
324 # some overhang with more computer accounts than users
325 computer_accounts = int(1.25 * number_of_users)
326 traffic.generate_users_and_groups(ldb,
329 opts.number_of_users,
330 opts.number_of_groups,
331 opts.group_memberships,
333 machine_accounts=computer_accounts,
334 traffic_accounts=False)
337 tempdir = tempfile.mkdtemp(prefix="samba_tg_")
338 logger.info("Using temp dir %s" % tempdir)
340 traffic.generate_users_and_groups(ldb,
344 opts.number_of_groups,
345 opts.group_memberships,
347 machine_accounts=len(conversations),
348 traffic_accounts=True)
350 accounts = traffic.generate_replay_accounts(ldb,
355 statsdir = traffic.mk_masked_dir(tempdir, 'stats')
357 if opts.traffic_summary:
358 if opts.traffic_summary == '-':
359 summary_dest = sys.stdout
361 summary_dest = open(opts.traffic_summary, 'w')
363 logger.info("Writing traffic summary")
365 for c in traffic.seq_to_conversations(conversations):
366 summaries += c.replay_as_summary_lines()
369 for (time, line) in summaries:
370 print(line, file=summary_dest)
374 traffic.replay(conversations,
379 dns_rate=opts.dns_rate,
380 duration=opts.duration,
381 latency_timeout=opts.latency_timeout,
382 badpassword_frequency=opts.badpassword_frequency,
383 prefer_kerberos=opts.prefer_kerberos,
386 base_dn=ldb.domain_dn(),
387 ou=traffic.ou_name(ldb, opts.instance_id),
389 stop_on_any_error=opts.stop_on_any_error,
390 domain_sid=ldb.get_domain_sid())
392 if opts.timing_data == '-':
393 timing_dest = sys.stdout
394 elif opts.timing_data is None:
397 timing_dest = open(opts.timing_data, 'w')
399 logger.info("Generating statistics")
400 traffic.generate_stats(statsdir, timing_dest)
402 if not opts.preserve_tempdir:
403 logger.info("Removing temporary directory")
404 shutil.rmtree(tempdir)
406 # delete the empty directories anyway. There are thousands of
407 # them and they're EMPTY.
408 for d in os.listdir(tempdir):
409 if d.startswith('conversation-'):
410 path = os.path.join(tempdir, d)
414 logger.info("not removing %s (%s)" % (path, e))