1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >SAMBA Developers Guide</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.77"></HEAD
19 NAME="SAMBA-DEVELOPERS-GUIDE"
26 NAME="SAMBA-DEVELOPERS-GUIDE"
28 >SAMBA Developers Guide</H1
48 > : Mon Sep 30 15:23:53 CDT 2002</P
50 >This book is a collection of documents that might be useful for
51 people developing samba or those interested in doing so.
52 It's nothing more than a collection of documents written by samba developers about
53 the internals of various parts of samba and the SMB protocol. It's still incomplete.
54 The most recent version of this document
56 HREF="http://devel.samba.org/"
58 >http://devel.samba.org/</A
60 Please send updates to <A
61 HREF="mailto:jelmer@samba.org"
66 >This documentation is distributed under the GNU General Public License (GPL)
67 version 2. A copy of the license is included with the Samba source
68 distribution. A copy can be found on-line at <A
69 HREF="http://www.fsf.org/licenses/gpl.txt"
71 >http://www.fsf.org/licenses/gpl.txt</A
83 >Definition of NetBIOS Protocol and Name Resolution Modes</A
107 >Samba Architecture</A
119 >Multithreading and Samba</A
141 >The samba DEBUG system</A
148 >New Output Syntax</A
153 >The DEBUG() Macro</A
158 >The DEBUGADD() Macro</A
163 >The DEBUGLVL() Macro</A
185 >format_debug_text()</A
193 HREF="#CODINGSUGGESTIONS"
194 >Coding Suggestions</A
206 >Character Handling</A
211 >The new functions</A
216 >Macros in byteorder.h</A
233 >SCVAL(buf,pos,val)</A
258 >SSVAL(buf,pos,val)</A
263 >SIVAL(buf,pos,val)</A
268 >SSVALS(buf,pos,val)</A
273 >SIVALS(buf,pos,val)</A
288 >RSSVAL(buf,pos,val)</A
293 >RSIVAL(buf,pos,val)</A
300 >LAN Manager Samba API</A
319 >Code character table</A
326 >The smb.conf file</A
340 >Handling of Whitespace</A
345 >Handling of Line Continuation</A
350 >Line Continuation Quirks</A
373 >NetBIOS in a Unix World</A
420 >Protocol Complexity</A
427 >Tracing samba system calls</A
458 >Notes and Structures</A
482 >MSRPC over Transact Named Pipe</A
504 >RPC Bind / Bind Ack</A
509 >NTLSA Transact Named Pipe</A
519 >LSA Query Info Policy</A
524 >LSA Enumerate Trusted Domains</A
551 >NETLOGON rpc Transact Named Pipe</A
558 >LSA Request Challenge</A
563 >LSA Authenticate 2</A
568 >LSA Server Password Set</A
585 >\\MAILSLOT\NET\NTLOGON</A
604 >SRVSVC Transact Named Pipe</A
616 >Net Server Get Info</A
623 >Cryptographic side of NT Domain Authentication</A
668 >Samba Printing Internals</A
680 >Printing Interface to Various Back ends</A
685 >Print Queue TDB's</A
690 >ChangeID & Client Caching of Printer Information</A
695 >Windows NT/2K Printer Change Notify</A
702 >Samba WINS Internals</A
716 >The Upcoming SAM System</A
723 >Security in the 'new SAM'</A
728 >Standalone from UNIX</A
733 >Handles and Races in the new SAM</A
769 >Special Module: sam_passdb</A
781 >Memory Management</A
793 >LanMan and NT Password Encryption</A
805 >How does it work?</A
811 NAME="SMBPASSWDFILEFORMAT"
813 >The smbpasswd file</A
825 >Chapter 1. Definition of NetBIOS Protocol and Name Resolution Modes</H1
835 >NetBIOS runs over the following tranports: TCP/IP; NetBEUI and IPX/SPX.
836 Samba only uses NetBIOS over TCP/IP. For details on the TCP/IP NetBIOS
837 Session Service NetBIOS Datagram Service, and NetBIOS Names, see
838 rfc1001.txt and rfc1002.txt.</P
841 NetBEUI is a raw NetBIOS frame protocol implementation that allows NetBIOS
842 datagrams to be sent out over the 'wire' embedded within LLC frames.
843 NetBEUI is not required when using NetBIOS over TCP/IP protocols and it
844 is preferable NOT to install NetBEUI if it can be avoided.</P
847 IPX/SPX is also not required when using NetBIOS over TCP/IP, and it is
848 preferable NOT to install the IPX/SPX transport unless you are using Novell
849 servers. At the very least, it is recommended that you do not install
850 'NetBIOS over IPX/SPX'.</P
852 >[When installing Windows 95, you will find that NetBEUI and IPX/SPX are
853 installed as the default protocols. This is because they are the simplest
854 to manage: no Windows 95 user-configuration is required].</P
857 NetBIOS applications (such as samba) offer their services (for example,
858 SMB file and print sharing) on a NetBIOS name. They must claim this name
859 on the network before doing so. The NetBIOS session service will then
860 accept connections on the application's behalf (on the NetBIOS name
861 claimed by the application). A NetBIOS session between the application
862 and the client can then commence.</P
865 NetBIOS names consist of 15 characters plus a 'type' character. This is
866 similar, in concept, to an IP address and a TCP port number, respectively.
867 A NetBIOS-aware application on a host will offer different services under
868 different NetBIOS name types, just as a host will offer different TCP/IP
869 services on different port numbers.</P
872 NetBIOS names must be claimed on a network, and must be defended. The use
873 of NetBIOS names is most suitable on a single subnet; a Local Area Network
874 or a Wide Area Network.</P
877 NetBIOS names are either UNIQUE or GROUP. Only one application can claim a
878 UNIQUE NetBIOS name on a network.</P
880 >There are two kinds of NetBIOS Name resolution: Broadcast and Point-to-Point.</P
889 >1.2. BROADCAST NetBIOS</H2
892 Clients can claim names, and therefore offer services on successfully claimed
893 names, on their broadcast-isolated subnet. One way to get NetBIOS services
894 (such as browsing: see ftp.microsoft.com/drg/developr/CIFS/browdiff.txt; and
895 SMB file/print sharing: see cifs4.txt) working on a LAN or WAN is to make
896 your routers forward all broadcast packets from TCP/IP ports 137, 138 and 139.</P
899 This, however, is not recommended. If you have a large LAN or WAN, you will
900 find that some of your hosts spend 95 percent of their time dealing with
901 broadcast traffic. [If you have IPX/SPX on your LAN or WAN, you will find
902 that this is already happening: a packet analyzer will show, roughly
903 every twelve minutes, great swathes of broadcast traffic!].</P
912 >1.3. NBNS NetBIOS</H2
914 >rfc1001.txt describes, amongst other things, the implementation and use
915 of, a 'NetBIOS Name Service'. NT/AS offers 'Windows Internet Name Service'
916 which is fully rfc1001/2 compliant, but has had to take specific action
917 with certain NetBIOS names in order to make it useful. (for example, it
918 deals with the registration of <1c> <1d> <1e> names all in different ways.
919 I recommend the reading of the Microsoft WINS Server Help files for full
923 The use of a WINS server cuts down on broadcast network traffic for
924 NetBIOS name resolution. It has the effect of pulling all the broadcast
925 isolated subnets together into a single NetBIOS scope, across your LAN
926 or WAN, while avoiding the use of TCP/IP broadcast packets.</P
928 >When you have a WINS server on your LAN, WINS clients will be able to
929 contact the WINS server to resolve NetBIOS names. Note that only those
930 WINS clients that have registered with the same WINS server will be
931 visible. The WINS server _can_ have static NetBIOS entries added to its
932 database (usually for security reasons you might want to consider putting
933 your domain controllers or other important servers as static entries,
934 but you should not rely on this as your sole means of security), but for
935 the most part, NetBIOS names are registered dynamically.</P
937 >This provides some confusion for lots of people, and is worth mentioning
938 here: a Browse Server is NOT a WINS Server, even if these services are
939 implemented in the same application. A Browse Server _needs_ a WINS server
940 because a Browse Server is a WINS client, which is _not_ the same thing].</P
942 >Clients can claim names, and therefore offer services on successfully claimed
943 names, on their broadcast-isolated subnet. One way to get NetBIOS services
944 (such as browsing: see ftp.microsoft.com/drg/developr/CIFS/browdiff.txt; and
945 SMB file/print sharing: see cifs6.txt) working on a LAN or WAN is to make
946 your routers forward all broadcast packets from TCP/IP ports 137, 138 and 139.
947 You will find, however, if you do this on a large LAN or a WAN, that your
948 network is completely swamped by NetBIOS and browsing packets, which is why
949 WINS was developed to minimise the necessity of broadcast traffic.</P
952 WINS Clients therefore claim names from the WINS server. If the WINS
953 server allows them to register a name, the client's NetBIOS session service
954 can then offer services on this name. Other WINS clients will then
955 contact the WINS server to resolve a NetBIOS name.</P
964 >Chapter 2. Samba Architecture</H1
972 >2.1. Introduction</H2
974 >This document gives a general overview of how Samba works
975 internally. The Samba Team has tried to come up with a model which is
976 the best possible compromise between elegance, portability, security
977 and the constraints imposed by the very messy SMB and CIFS
980 >It also tries to answer some of the frequently asked questions such as:</P
987 > Is Samba secure when running on Unix? The xyz platform?
988 What about the root priveliges issue?</P
992 >Pros and cons of multithreading in various parts of Samba</P
996 >Why not have a separate process for name resolution, WINS, and browsing?</P
1007 >2.2. Multithreading and Samba</H2
1009 >People sometimes tout threads as a uniformly good thing. They are very
1010 nice in their place but are quite inappropriate for smbd. nmbd is
1011 another matter, and multi-threading it would be very nice. </P
1013 >The short version is that smbd is not multithreaded, and alternative
1014 servers that take this approach under Unix (such as Syntax, at the
1015 time of writing) suffer tremendous performance penalties and are less
1016 robust. nmbd is not threaded either, but this is because it is not
1017 possible to do it while keeping code consistent and portable across 35
1018 or more platforms. (This drawback also applies to threading smbd.)</P
1020 >The longer versions is that there are very good reasons for not making
1021 smbd multi-threaded. Multi-threading would actually make Samba much
1022 slower, less scalable, less portable and much less robust. The fact
1023 that we use a separate process for each connection is one of Samba's
1024 biggest advantages.</P
1033 >2.3. Threading smbd</H2
1035 >A few problems that would arise from a threaded smbd are:</P
1042 > It's not only to create threads instead of processes, but you
1043 must care about all variables if they have to be thread specific
1044 (currently they would be global).</P
1048 > if one thread dies (eg. a seg fault) then all threads die. We can
1049 immediately throw robustness out the window.</P
1053 > many of the system calls we make are blocking. Non-blocking
1054 equivalents of many calls are either not available or are awkward (and
1055 slow) to use. So while we block in one thread all clients are
1056 waiting. Imagine if one share is a slow NFS filesystem and the others
1057 are fast, we will end up slowing all clients to the speed of NFS.</P
1061 > you can't run as a different uid in different threads. This means
1062 we would have to switch uid/gid on _every_ SMB packet. It would be
1063 horrendously slow.</P
1067 > the per process file descriptor limit would mean that we could only
1068 support a limited number of clients.</P
1072 > we couldn't use the system locking calls as the locking context of
1073 fcntl() is a process, not a thread.</P
1084 >2.4. Threading nmbd</H2
1086 >This would be ideal, but gets sunk by portability requirements.</P
1088 >Andrew tried to write a test threads library for nmbd that used only
1089 ansi-C constructs (using setjmp and longjmp). Unfortunately some OSes
1090 defeat this by restricting longjmp to calling addresses that are
1091 shallower than the current address on the stack (apparently AIX does
1092 this). This makes a truly portable threads library impossible. So to
1093 support all our current platforms we would have to code nmbd both with
1094 and without threads, and as the real aim of threads is to make the
1095 code clearer we would not have gained anything. (it is a myth that
1096 threads make things faster. threading is like recursion, it can make
1097 things clear but the same thing can always be done faster by some
1100 >Chris tried to spec out a general design that would abstract threading
1101 vs separate processes (vs other methods?) and make them accessible
1102 through some general API. This doesn't work because of the data
1103 sharing requirements of the protocol (packets in the future depending
1104 on packets now, etc.) At least, the code would work but would be very
1105 clumsy, and besides the fork() type model would never work on Unix. (Is there an OS that it would work on, for nmbd?)</P
1107 >A fork() is cheap, but not nearly cheap enough to do on every UDP
1108 packet that arrives. Having a pool of processes is possible but is
1109 nasty to program cleanly due to the enormous amount of shared data (in
1110 complex structures) between the processes. We can't rely on each
1111 platform having a shared memory system.</P
1120 >2.5. nbmd Design</H2
1122 >Originally Andrew used recursion to simulate a multi-threaded
1123 environment, which use the stack enormously and made for really
1124 confusing debugging sessions. Luke Leighton rewrote it to use a
1125 queuing system that keeps state information on each packet. The
1126 first version used a single structure which was used by all the
1127 pending states. As the initialisation of this structure was
1128 done by adding arguments, as the functionality developed, it got
1129 pretty messy. So, it was replaced with a higher-order function
1130 and a pointer to a user-defined memory block. This suddenly
1131 made things much simpler: large numbers of functions could be
1132 made static, and modularised. This is the same principle as used
1133 in NT's kernel, and achieves the same effect as threads, but in
1134 a single process.</P
1136 >Then Jeremy rewrote nmbd. The packet data in nmbd isn't what's on the
1137 wire. It's a nice format that is very amenable to processing but still
1138 keeps the idea of a distinct packet. See "struct packet_struct" in
1139 nameserv.h. It has all the detail but none of the on-the-wire
1140 mess. This makes it ideal for using in disk or memory-based databases
1141 for browsing and WINS support. </P
1150 >Chapter 3. The samba DEBUG system</H1
1158 >3.1. New Output Syntax</H2
1160 > The syntax of a debugging log file is represented as:</P
1163 CLASS="PROGRAMLISTING"
1164 > >debugfile< :== { >debugmsg< }
1166 >debugmsg< :== >debughdr< '\n' >debugtext<
1168 >debughdr< :== '[' TIME ',' LEVEL ']' FILE ':' [FUNCTION] '(' LINE ')'
1170 >debugtext< :== { >debugline< }
1172 >debugline< :== TEXT '\n'</PRE
1175 >TEXT is a string of characters excluding the newline character.</P
1177 >LEVEL is the DEBUG level of the message (an integer in the range
1180 >TIME is a timestamp.</P
1182 >FILE is the name of the file from which the debug message was
1185 >FUNCTION is the function from which the debug message was generated.</P
1187 >LINE is the line number of the debug statement that generated the
1190 >Basically, what that all means is:</P
1197 >A debugging log file is made up of debug messages.</P
1201 >Each debug message is made up of a header and text. The header is
1202 separated from the text by a newline.</P
1206 >The header begins with the timestamp and debug level of the
1207 message enclosed in brackets. The filename, function, and line
1208 number at which the message was generated follow. The filename is
1209 terminated by a colon, and the function name is terminated by the
1210 parenthesis which contain the line number. Depending upon the
1211 compiler, the function name may be missing (it is generated by the
1212 __FUNCTION__ macro, which is not universally implemented, dangit).</P
1216 >The message text is made up of zero or more lines, each terminated
1221 >Here's some example output:</P
1224 CLASS="PROGRAMLISTING"
1225 > [1998/08/03 12:55:25, 1] nmbd.c:(659)
1226 Netbios nameserver version 1.9.19-prealpha started.
1227 Copyright Andrew Tridgell 1994-1997
1228 [1998/08/03 12:55:25, 3] loadparm.c:(763)
1229 Initializing global parameters</PRE
1232 >Note that in the above example the function names are not listed on
1233 the header line. That's because the example above was generated on an
1234 SGI Indy, and the SGI compiler doesn't support the __FUNCTION__ macro.</P
1243 >3.2. The DEBUG() Macro</H2
1245 >Use of the DEBUG() macro is unchanged. DEBUG() takes two parameters.
1246 The first is the message level, the second is the body of a function
1247 call to the Debug1() function.</P
1249 >That's confusing.</P
1251 >Here's an example which may help a bit. If you would write</P
1254 CLASS="PROGRAMLISTING"
1255 >printf( "This is a %s message.\n", "debug" );</PRE
1258 >to send the output to stdout, then you would write</P
1261 CLASS="PROGRAMLISTING"
1262 >DEBUG( 0, ( "This is a %s message.\n", "debug" ) );</PRE
1265 >to send the output to the debug file. All of the normal printf()
1266 formatting escapes work.</P
1268 >Note that in the above example the DEBUG message level is set to 0.
1269 Messages at level 0 always print. Basically, if the message level is
1270 less than or equal to the global value DEBUGLEVEL, then the DEBUG
1271 statement is processed.</P
1273 >The output of the above example would be something like:</P
1276 CLASS="PROGRAMLISTING"
1277 > [1998/07/30 16:00:51, 0] file.c:function(128)
1278 This is a debug message.</PRE
1281 >Each call to DEBUG() creates a new header *unless* the output produced
1282 by the previous call to DEBUG() did not end with a '\n'. Output to the
1283 debug file is passed through a formatting buffer which is flushed
1284 every time a newline is encountered. If the buffer is not empty when
1285 DEBUG() is called, the new input is simply appended.</P
1287 >...but that's really just a Kludge. It was put in place because
1288 DEBUG() has been used to write partial lines. Here's a simple (dumb)
1289 example of the kind of thing I'm talking about:</P
1292 CLASS="PROGRAMLISTING"
1293 > DEBUG( 0, ("The test returned " ) );
1295 DEBUG(0, ("True") );
1297 DEBUG(0, ("False") );
1298 DEBUG(0, (".\n") );</PRE
1301 >Without the format buffer, the output (assuming test() returned true)
1302 would look like this:</P
1305 CLASS="PROGRAMLISTING"
1306 > [1998/07/30 16:00:51, 0] file.c:function(256)
1308 [1998/07/30 16:00:51, 0] file.c:function(258)
1310 [1998/07/30 16:00:51, 0] file.c:function(261)
1314 >Which isn't much use. The format buffer kludge fixes this problem.</P
1323 >3.3. The DEBUGADD() Macro</H2
1325 >In addition to the kludgey solution to the broken line problem
1326 described above, there is a clean solution. The DEBUGADD() macro never
1327 generates a header. It will append new text to the current debug
1328 message even if the format buffer is empty. The syntax of the
1329 DEBUGADD() macro is the same as that of the DEBUG() macro.</P
1332 CLASS="PROGRAMLISTING"
1333 > DEBUG( 0, ("This is the first line.\n" ) );
1334 DEBUGADD( 0, ("This is the second line.\nThis is the third line.\n" ) );</PRE
1340 CLASS="PROGRAMLISTING"
1341 > [1998/07/30 16:00:51, 0] file.c:function(512)
1342 This is the first line.
1343 This is the second line.
1344 This is the third line.</PRE
1354 >3.4. The DEBUGLVL() Macro</H2
1356 >One of the problems with the DEBUG() macro was that DEBUG() lines
1357 tended to get a bit long. Consider this example from
1358 nmbd_sendannounce.c:</P
1361 CLASS="PROGRAMLISTING"
1362 > DEBUG(3,("send_local_master_announcement: type %x for name %s on subnet %s for workgroup %s\n",
1363 type, global_myname, subrec->subnet_name, work->work_group));</PRE
1366 >One solution to this is to break it down using DEBUG() and DEBUGADD(),
1370 CLASS="PROGRAMLISTING"
1371 > DEBUG( 3, ( "send_local_master_announcement: " ) );
1372 DEBUGADD( 3, ( "type %x for name %s ", type, global_myname ) );
1373 DEBUGADD( 3, ( "on subnet %s ", subrec->subnet_name ) );
1374 DEBUGADD( 3, ( "for workgroup %s\n", work->work_group ) );</PRE
1377 >A similar, but arguably nicer approach is to use the DEBUGLVL() macro.
1378 This macro returns True if the message level is less than or equal to
1379 the global DEBUGLEVEL value, so:</P
1382 CLASS="PROGRAMLISTING"
1383 > if( DEBUGLVL( 3 ) )
1385 dbgtext( "send_local_master_announcement: " );
1386 dbgtext( "type %x for name %s ", type, global_myname );
1387 dbgtext( "on subnet %s ", subrec->subnet_name );
1388 dbgtext( "for workgroup %s\n", work->work_group );
1392 >(The dbgtext() function is explained below.)</P
1394 >There are a few advantages to this scheme:</P
1401 >The test is performed only once.</P
1405 >You can allocate variables off of the stack that will only be used
1406 within the DEBUGLVL() block.</P
1410 >Processing that is only relevant to debug output can be contained
1411 within the DEBUGLVL() block.</P
1422 >3.5. New Functions</H2
1430 >3.5.1. dbgtext()</H3
1432 >This function prints debug message text to the debug file (and
1433 possibly to syslog) via the format buffer. The function uses a
1434 variable argument list just like printf() or Debug1(). The
1435 input is printed into a buffer using the vslprintf() function,
1436 and then passed to format_debug_text().
1438 If you use DEBUGLVL() you will probably print the body of the
1439 message using dbgtext(). </P
1448 >3.5.2. dbghdr()</H3
1450 >This is the function that writes a debug message header.
1451 Headers are not processed via the format buffer. Also note that
1452 if the format buffer is not empty, a call to dbghdr() will not
1453 produce any output. See the comments in dbghdr() for more info.</P
1455 >It is not likely that this function will be called directly. It
1456 is used by DEBUG() and DEBUGADD().</P
1465 >3.5.3. format_debug_text()</H3
1467 >This is a static function in debug.c. It stores the output text
1468 for the body of the message in a buffer until it encounters a
1469 newline. When the newline character is found, the buffer is
1470 written to the debug file via the Debug1() function, and the
1471 buffer is reset. This allows us to add the indentation at the
1472 beginning of each line of the message body, and also ensures
1473 that the output is written a line at a time (which cleans up
1482 NAME="CODINGSUGGESTIONS"
1484 >Chapter 4. Coding Suggestions</H1
1486 >So you want to add code to Samba ...</P
1488 >One of the daunting tasks facing a programmer attempting to write code for
1489 Samba is understanding the various coding conventions used by those most
1490 active in the project. These conventions were mostly unwritten and helped
1491 improve either the portability, stability or consistency of the code. This
1492 document will attempt to document a few of the more important coding
1493 practices used at this time on the Samba project. The coding practices are
1494 expected to change slightly over time, and even to grow as more is learned
1495 about obscure portability considerations. Two existing documents
1498 >samba/source/internals.doc</TT
1502 >samba/source/architecture.doc</TT
1504 additional information.</P
1506 >The loosely related question of coding style is very personal and this
1507 document does not attempt to address that subject, except to say that I
1508 have observed that eight character tabs seem to be preferred in Samba
1509 source. If you are interested in the topic of coding style, two oft-quoted
1513 HREF="http://lxr.linux.no/source/Documentation/CodingStyle"
1515 >http://lxr.linux.no/source/Documentation/CodingStyle</A
1519 HREF="http://www.fsf.org/prep/standards_toc.html"
1521 >http://www.fsf.org/prep/standards_toc.html</A
1524 >But note that coding style in Samba varies due to the many different
1525 programmers who have contributed.</P
1527 >Following are some considerations you should use when adding new code to
1528 Samba. First and foremost remember that:</P
1530 >Portability is a primary consideration in adding function, as is network
1531 compatability with de facto, existing, real world CIFS/SMB implementations.
1532 There are lots of platforms that Samba builds on so use caution when adding
1533 a call to a library function that is not invoked in existing Samba code.
1534 Also note that there are many quite different SMB/CIFS clients that Samba
1535 tries to support, not all of which follow the SNIA CIFS Technical Reference
1536 (or the earlier Microsoft reference documents or the X/Open book on the SMB
1537 Standard) perfectly.</P
1539 >Here are some other suggestions:</P
1546 > use d_printf instead of printf for display text
1547 reason: enable auto-substitution of translated language text </P
1551 > use SAFE_FREE instead of free
1552 reason: reduce traps due to null pointers</P
1556 > don't use bzero use memset, or ZERO_STRUCT and ZERO_STRUCTP macros
1557 reason: not POSIX</P
1561 > don't use strcpy and strlen (use safe_* equivalents)
1562 reason: to avoid traps due to buffer overruns</P
1566 > don't use getopt_long, use popt functions instead
1567 reason: portability</P
1571 > explicitly add const qualifiers on parm passing in functions where parm
1572 is input only (somewhat controversial but const can be #defined away)</P
1576 > when passing a va_list as an arg, or assigning one to another
1577 please use the VA_COPY() macro
1578 reason: on some platforms, va_list is a struct that must be
1579 initialized in each function...can SEGV if you don't.</P
1583 > discourage use of threads
1584 reason: portability (also see architecture.doc)</P
1588 > don't explicitly include new header files in C files - new h files
1589 should be included by adding them once to includes.h
1590 reason: consistency</P
1594 > don't explicitly extern functions (they are autogenerated by
1595 "make proto" into proto.h)
1596 reason: consistency</P
1600 > use endian safe macros when unpacking SMBs (see byteorder.h and
1602 reason: not everyone uses Intel</P
1606 > Note Unicode implications of charset handling (see internals.doc). See
1607 pull_* and push_* and convert_string functions.
1608 reason: Internationalization</P
1612 > Don't assume English only
1613 reason: See above</P
1617 > Try to avoid using in/out parameters (functions that return data which
1618 overwrites input parameters)
1619 reason: Can cause stability problems</P
1623 > Ensure copyright notices are correct, don't append Tridge's name to code
1624 that he didn't write. If you did not write the code, make sure that it
1625 can coexist with the rest of the Samba GPLed code.</P
1629 > Consider usage of DATA_BLOBs for length specified byte-data.
1630 reason: stability</P
1634 > Take advantage of tdbs for database like function
1635 reason: consistency</P
1639 > Don't access the SAM_ACCOUNT structure directly, they should be accessed
1640 via pdb_get...() and pdb_set...() functions.
1641 reason: stability, consistency</P
1645 > Don't check a password directly against the passdb, always use the
1646 check_password() interface.
1647 reason: long term pluggability</P
1651 > Try to use asprintf rather than pstrings and fstrings where possible</P
1655 > Use normal C comments / * instead of C++ comments // like
1656 this. Although the C++ comment format is part of the C99
1657 standard, some older vendor C compilers do not accept it.</P
1661 > Try to write documentation for API functions and structures
1662 explaining the point of the code, the way it should be used, and
1663 any special conditions or results. Mark these with a double-star
1664 comment start / ** so that they can be picked up by Doxygen, as in
1669 > Keep the scope narrow. This means making functions/variables
1670 static whenever possible. We don't want our namespace
1671 polluted. Each module should have a minimal number of externally
1672 visible functions or variables.</P
1676 > Use function pointers to keep knowledge about particular pieces of
1677 code isolated in one place. We don't want a particular piece of
1678 functionality to be spread out across lots of places - that makes
1679 for fragile, hand to maintain code. Instead, design an interface
1680 and use tables containing function pointers to implement specific
1681 functionality. This is particularly important for command
1686 > Think carefully about what it will be like for someone else to add
1687 to and maintain your code. If it would be hard for someone else to
1688 maintain then do it another way. </P
1692 >The suggestions above are simply that, suggestions, but the information may
1693 help in reducing the routine rework done on new code. The preceeding list
1694 is expected to change routinely as new support routines and macros are
1703 >Chapter 5. Samba Internals</H1
1711 >5.1. Character Handling</H2
1713 >This section describes character set handling in Samba, as implemented in
1714 Samba 3.0 and above</P
1716 >In the past Samba had very ad-hoc character set handling. Scattered
1717 throughout the code were numerous calls which converted particular
1718 strings to/from DOS codepages. The problem is that there was no way of
1719 telling if a particular char* is in dos codepage or unix
1720 codepage. This led to a nightmare of code that tried to cope with
1721 particular cases without handlingt the general case.</P
1730 >5.2. The new functions</H2
1732 >The new system works like this:</P
1739 > all char* strings inside Samba are "unix" strings. These are
1740 multi-byte strings that are in the charset defined by the "unix
1741 charset" option in smb.conf. </P
1745 > there is no single fixed character set for unix strings, but any
1746 character set that is used does need the following properties:
1754 > must not contain NULLs except for termination
1759 > must be 7-bit compatible with C strings, so that a constant
1760 string or character in C will be byte-for-byte identical to the
1761 equivalent string in the chosen character set.
1766 > when you uppercase or lowercase a string it does not become
1767 longer than the original string
1772 > must be able to correctly hold all characters that your client
1778 > For example, UTF-8 is fine, and most multi-byte asian character sets
1779 are fine, but UCS2 could not be used for unix strings as they
1785 > when you need to put a string into a buffer that will be sent on the
1786 wire, or you need a string in a character set format that is
1787 compatible with the clients character set then you need to use a
1788 pull_ or push_ function. The pull_ functions pull a string from a
1789 wire buffer into a (multi-byte) unix string. The push_ functions
1790 push a string out to a wire buffer. </P
1794 > the two main pull_ and push_ functions you need to understand are
1795 pull_string and push_string. These functions take a base pointer
1796 that should point at the start of the SMB packet that the string is
1797 in. The functions will check the flags field in this packet to
1798 automatically determine if the packet is marked as a unicode packet,
1799 and they will choose whether to use unicode for this string based on
1800 that flag. You may also force this decision using the STR_UNICODE or
1801 STR_ASCII flags. For use in smbd/ and libsmb/ there are wrapper
1802 functions clistr_ and srvstr_ that call the pull_/push_ functions
1803 with the appropriate first argument.
1806 > You may also call the pull_ascii/pull_ucs2 or push_ascii/push_ucs2
1807 functions if you know that a particular string is ascii or
1808 unicode. There are also a number of other convenience functions in
1809 charcnv.c that call the pull_/push_ functions with particularly
1810 common arguments, such as pull_ascii_pstring()
1815 > The biggest thing to remember is that internal (unix) strings in Samba
1816 may now contain multi-byte characters. This means you cannot assume
1817 that characters are always 1 byte long. Often this means that you will
1818 have to convert strings to ucs2 and back again in order to do some
1819 (seemingly) simple task. For examples of how to do this see functions
1820 like strchr_m(). I know this is very slow, and we will eventually
1821 speed it up but right now we want this stuff correct not fast.</P
1825 > all lp_ functions now return unix strings. The magic "DOS" flag on
1826 parameters is gone.</P
1830 > all vfs functions take unix strings. Don't convert when passing to them</P
1841 >5.3. Macros in byteorder.h</H2
1843 >This section describes the macros defined in byteorder.h. These macros
1844 are used extensively in the Samba code.</P
1852 >5.3.1. CVAL(buf,pos)</H3
1854 >returns the byte at offset pos within buffer buf as an unsigned character.</P
1863 >5.3.2. PVAL(buf,pos)</H3
1865 >returns the value of CVAL(buf,pos) cast to type unsigned integer.</P
1874 >5.3.3. SCVAL(buf,pos,val)</H3
1876 >sets the byte at offset pos within buffer buf to value val.</P
1885 >5.3.4. SVAL(buf,pos)</H3
1887 > returns the value of the unsigned short (16 bit) little-endian integer at
1888 offset pos within buffer buf. An integer of this type is sometimes
1889 refered to as "USHORT".</P
1898 >5.3.5. IVAL(buf,pos)</H3
1900 >returns the value of the unsigned 32 bit little-endian integer at offset
1901 pos within buffer buf.</P
1910 >5.3.6. SVALS(buf,pos)</H3
1912 >returns the value of the signed short (16 bit) little-endian integer at
1913 offset pos within buffer buf.</P
1922 >5.3.7. IVALS(buf,pos)</H3
1924 >returns the value of the signed 32 bit little-endian integer at offset pos
1925 within buffer buf.</P
1934 >5.3.8. SSVAL(buf,pos,val)</H3
1936 >sets the unsigned short (16 bit) little-endian integer at offset pos within
1937 buffer buf to value val.</P
1946 >5.3.9. SIVAL(buf,pos,val)</H3
1948 >sets the unsigned 32 bit little-endian integer at offset pos within buffer
1949 buf to the value val.</P
1958 >5.3.10. SSVALS(buf,pos,val)</H3
1960 >sets the short (16 bit) signed little-endian integer at offset pos within
1961 buffer buf to the value val.</P
1970 >5.3.11. SIVALS(buf,pos,val)</H3
1972 >sets the signed 32 bit little-endian integer at offset pos withing buffer
1973 buf to the value val.</P
1982 >5.3.12. RSVAL(buf,pos)</H3
1984 >returns the value of the unsigned short (16 bit) big-endian integer at
1985 offset pos within buffer buf.</P
1994 >5.3.13. RIVAL(buf,pos)</H3
1996 >returns the value of the unsigned 32 bit big-endian integer at offset
1997 pos within buffer buf.</P
2006 >5.3.14. RSSVAL(buf,pos,val)</H3
2008 >sets the value of the unsigned short (16 bit) big-endian integer at
2009 offset pos within buffer buf to value val.
2010 refered to as "USHORT".</P
2019 >5.3.15. RSIVAL(buf,pos,val)</H3
2021 >sets the value of the unsigned 32 bit big-endian integer at offset
2022 pos within buffer buf to value val.</P
2032 >5.4. LAN Manager Samba API</H2
2034 >This section describes the functions need to make a LAN Manager RPC call.
2035 This information had been obtained by examining the Samba code and the LAN
2036 Manager 2.0 API documentation. It should not be considered entirely
2040 CLASS="PROGRAMLISTING"
2041 >call_api(int prcnt, int drcnt, int mprcnt, int mdrcnt,
2042 char *param, char *data, char **rparam, char **rdata);</PRE
2045 >This function is defined in client.c. It uses an SMB transaction to call a
2054 >5.4.1. Parameters</H3
2056 >The parameters are as follows:</P
2063 > prcnt: the number of bytes of parameters begin sent.</P
2067 > drcnt: the number of bytes of data begin sent.</P
2071 > mprcnt: the maximum number of bytes of parameters which should be returned</P
2075 > mdrcnt: the maximum number of bytes of data which should be returned</P
2079 > param: a pointer to the parameters to be sent.</P
2083 > data: a pointer to the data to be sent.</P
2087 > rparam: a pointer to a pointer which will be set to point to the returned
2088 paramters. The caller of call_api() must deallocate this memory.</P
2092 > rdata: a pointer to a pointer which will be set to point to the returned
2093 data. The caller of call_api() must deallocate this memory.</P
2097 >These are the parameters which you ought to send, in the order of their
2098 appearance in the parameter block:</P
2105 >An unsigned 16 bit integer API number. You should set this value with
2106 SSVAL(). I do not know where these numbers are described.</P
2110 >An ASCIIZ string describing the parameters to the API function as defined
2111 in the LAN Manager documentation. The first parameter, which is the server
2112 name, is ommited. This string is based uppon the API function as described
2113 in the manual, not the data which is actually passed.</P
2117 >An ASCIIZ string describing the data structure which ought to be returned.</P
2121 >Any parameters which appear in the function call, as defined in the LAN
2122 Manager API documentation, after the "Server" and up to and including the
2123 "uLevel" parameters.</P
2127 >An unsigned 16 bit integer which gives the size in bytes of the buffer we
2128 will use to receive the returned array of data structures. Presumably this
2129 should be the same as mdrcnt. This value should be set with SSVAL().</P
2133 >An ASCIIZ string describing substructures which should be returned. If no
2134 substructures apply, this string is of zero length.</P
2138 >The code in client.c always calls call_api() with no data. It is unclear
2139 when a non-zero length data buffer would be sent.</P
2148 >5.4.2. Return value</H3
2150 >The returned parameters (pointed to by rparam), in their order of appearance
2158 >An unsigned 16 bit integer which contains the API function's return code.
2159 This value should be read with SVAL().</P
2163 >An adjustment which tells the amount by which pointers in the returned
2164 data should be adjusted. This value should be read with SVAL(). Basically,
2165 the address of the start of the returned data buffer should have the returned
2166 pointer value added to it and then have this value subtracted from it in
2167 order to obtain the currect offset into the returned data buffer.</P
2171 >A count of the number of elements in the array of structures returned.
2172 It is also possible that this may sometimes be the number of bytes returned.</P
2176 >When call_api() returns, rparam points to the returned parameters. The
2177 first if these is the result code. It will be zero if the API call
2178 suceeded. This value by be read with "SVAL(rparam,0)".</P
2180 >The second parameter may be read as "SVAL(rparam,2)". It is a 16 bit offset
2181 which indicates what the base address of the returned data buffer was when
2182 it was built on the server. It should be used to correct pointer before
2185 >The returned data buffer contains the array of returned data structures.
2186 Note that all pointers must be adjusted before use. The function
2187 fix_char_ptr() in client.c can be used for this purpose.</P
2189 >The third parameter (which may be read as "SVAL(rparam,4)") has something to
2190 do with indicating the amount of data returned or possibly the amount of
2191 data which can be returned if enough buffer space is allowed.</P
2201 >5.5. Code character table</H2
2203 >Certain data structures are described by means of ASCIIz strings containing
2204 code characters. These are the code characters:</P
2211 >W a type byte little-endian unsigned integer</P
2215 >N a count of substructures which follow</P
2219 >D a four byte little-endian unsigned integer</P
2223 >B a byte (with optional count expressed as trailing ASCII digits)</P
2227 >z a four byte offset to a NULL terminated string</P
2231 >l a four byte offset to non-string user data</P
2235 >b an offset to data (with count expressed as trailing ASCII digits)</P
2239 >r pointer to returned data buffer???</P
2243 >L length in bytes of returned data buffer???</P
2247 >h number of bytes of information available???</P
2258 >Chapter 6. The smb.conf file</H1
2266 >6.1. Lexical Analysis</H2
2268 >Basically, the file is processed on a line by line basis. There are
2269 four types of lines that are recognized by the lexical analyzer
2277 >Blank lines - Lines containing only whitespace.</P
2281 >Comment lines - Lines beginning with either a semi-colon or a
2282 pound sign (';' or '#').</P
2286 >Section header lines - Lines beginning with an open square bracket ('[').</P
2290 >Parameter lines - Lines beginning with any other character.
2291 (The default line type.)</P
2295 >The first two are handled exclusively by the lexical analyzer, which
2296 ignores them. The latter two line types are scanned for</P
2303 > - Section names</P
2307 > - Parameter names</P
2311 > - Parameter values</P
2315 >These are the only tokens passed to the parameter loader
2316 (loadparm.c). Parameter names and values are divided from one
2317 another by an equal sign: '='.</P
2325 >6.1.1. Handling of Whitespace</H3
2327 >Whitespace is defined as all characters recognized by the isspace()
2328 function (see ctype(3C)) except for the newline character ('\n')
2329 The newline is excluded because it identifies the end of the line.</P
2336 >The lexical analyzer scans past white space at the beginning of a line.</P
2340 >Section and parameter names may contain internal white space. All
2341 whitespace within a name is compressed to a single space character. </P
2345 >Internal whitespace within a parameter value is kept verbatim with
2346 the exception of carriage return characters ('\r'), all of which
2351 >Leading and trailing whitespace is removed from names and values.</P
2362 >6.1.2. Handling of Line Continuation</H3
2364 >Long section header and parameter lines may be extended across
2365 multiple lines by use of the backslash character ('\\'). Line
2366 continuation is ignored for blank and comment lines.</P
2368 >If the last (non-whitespace) character within a section header or on
2369 a parameter line is a backslash, then the next line will be
2370 (logically) concatonated with the current line by the lexical
2371 analyzer. For example:</P
2374 CLASS="PROGRAMLISTING"
2375 > param name = parameter value string \
2376 with line continuation.</PRE
2379 >Would be read as</P
2382 CLASS="PROGRAMLISTING"
2383 > param name = parameter value string with line continuation.</PRE
2386 >Note that there are five spaces following the word 'string',
2387 representing the one space between 'string' and '\\' in the top
2388 line, plus the four preceeding the word 'with' in the second line.
2389 (Yes, I'm counting the indentation.)</P
2391 >Line continuation characters are ignored on blank lines and at the end
2392 of comments. They are *only* recognized within section and parameter
2402 >6.1.3. Line Continuation Quirks</H3
2404 >Note the following example:</P
2407 CLASS="PROGRAMLISTING"
2408 > param name = parameter value string \
2410 with line continuation.</PRE
2413 >The middle line is *not* parsed as a blank line because it is first
2414 concatonated with the top line. The result is</P
2417 CLASS="PROGRAMLISTING"
2418 >param name = parameter value string with line continuation.</PRE
2421 >The same is true for comment lines.</P
2424 CLASS="PROGRAMLISTING"
2425 > param name = parameter value string \
2427 with a comment.</PRE
2433 CLASS="PROGRAMLISTING"
2434 >param name = parameter value string ; comment with a comment.</PRE
2437 >On a section header line, the closing bracket (']') is considered a
2438 terminating character, and the rest of the line is ignored. The lines</P
2441 CLASS="PROGRAMLISTING"
2442 > [ section name ] garbage \
2443 param name = value</PRE
2449 CLASS="PROGRAMLISTING"
2451 param name = value</PRE
2464 >The syntax of the smb.conf file is as follows:</P
2467 CLASS="PROGRAMLISTING"
2468 > <file> :== { <section> } EOF
2469 <section> :== <section header> { <parameter line> }
2470 <section header> :== '[' NAME ']'
2471 <parameter line> :== NAME '=' VALUE NL</PRE
2474 >Basically, this means that</P
2481 > a file is made up of zero or more sections, and is terminated by
2482 an EOF (we knew that).</P
2486 > A section is made up of a section header followed by zero or more
2491 > A section header is identified by an opening bracket and
2492 terminated by the closing bracket. The enclosed NAME identifies
2497 > A parameter line is divided into a NAME and a VALUE. The *first*
2498 equal sign on the line separates the NAME from the VALUE. The
2499 VALUE is terminated by a newline character (NL = '\n').</P
2509 >6.2.1. About params.c</H3
2511 >The parsing of the config file is a bit unusual if you are used to
2512 lex, yacc, bison, etc. Both lexical analysis (scanning) and parsing
2513 are performed by params.c. Values are loaded via callbacks to
2524 >Chapter 7. NetBIOS in a Unix World</H1
2532 >7.1. Introduction</H2
2534 >This is a short document that describes some of the issues that
2535 confront a SMB implementation on unix, and how Samba copes with
2536 them. They may help people who are looking at unix<->PC
2537 interoperability.</P
2539 >It was written to help out a person who was writing a paper on unix to
2551 >The SMB protocol has only a loose username concept. Early SMB
2552 protocols (such as CORE and COREPLUS) have no username concept at
2553 all. Even in later protocols clients often attempt operations
2554 (particularly printer operations) without first validating a username
2557 >Unix security is based around username/password pairs. A unix box
2558 should not allow clients to do any substantive operation without some
2559 sort of validation. </P
2561 >The problem mostly manifests itself when the unix server is in "share
2562 level" security mode. This is the default mode as the alternative
2563 "user level" security mode usually forces a client to connect to the
2564 server as the same user for each connected share, which is
2565 inconvenient in many sites.</P
2567 >In "share level" security the client normally gives a username in the
2568 "session setup" protocol, but does not supply an accompanying
2569 password. The client then connects to resources using the "tree
2570 connect" protocol, and supplies a password. The problem is that the
2571 user on the PC types the username and the password in different
2572 contexts, unaware that they need to go together to give access to the
2573 server. The username is normally the one the user typed in when they
2574 "logged onto" the PC (this assumes Windows for Workgroups). The
2575 password is the one they chose when connecting to the disk or printer.</P
2577 >The user often chooses a totally different username for their login as
2578 for the drive connection. Often they also want to access different
2579 drives as different usernames. The unix server needs some way of
2580 divining the correct username to combine with each password.</P
2582 >Samba tries to avoid this problem using several methods. These succeed
2583 in the vast majority of cases. The methods include username maps, the
2584 service%user syntax, the saving of session setup usernames for later
2585 validation and the derivation of the username from the service name
2586 (either directly or via the user= option).</P
2595 >7.3. File Ownership</H2
2597 >The commonly used SMB protocols have no way of saying "you can't do
2598 that because you don't own the file". They have, in fact, no concept
2599 of file ownership at all.</P
2601 >This brings up all sorts of interesting problems. For example, when
2602 you copy a file to a unix drive, and the file is world writeable but
2603 owned by another user the file will transfer correctly but will
2604 receive the wrong date. This is because the utime() call under unix
2605 only succeeds for the owner of the file, or root, even if the file is
2606 world writeable. For security reasons Samba does all file operations
2607 as the validated user, not root, so the utime() fails. This can stuff
2608 up shared development diectories as programs like "make" will not get
2609 file time comparisons right.</P
2611 >There are several possible solutions to this problem, including
2612 username mapping, and forcing a specific username for particular
2624 >Many SMB clients uppercase passwords before sending them. I have no
2625 idea why they do this. Interestingly WfWg uppercases the password only
2626 if the server is running a protocol greater than COREPLUS, so
2627 obviously it isn't just the data entry routines that are to blame.</P
2629 >Unix passwords are case sensitive. So if users use mixed case
2630 passwords they are in trouble.</P
2632 >Samba can try to cope with this by either using the "password level"
2633 option which causes Samba to try the offered password with up to the
2634 specified number of case changes, or by using the "password server"
2635 option which allows Samba to do its validation via another machine
2636 (typically a WinNT server).</P
2638 >Samba supports the password encryption method used by SMB
2639 clients. Note that the use of password encryption in Microsoft
2640 networking leads to password hashes that are "plain text equivalent".
2641 This means that it is *VERY* important to ensure that the Samba
2642 smbpasswd file containing these password hashes is only readable
2643 by the root user. See the documentation ENCRYPTION.txt for more
2655 >Since samba 2.2, samba supports other types of locking as well. This
2656 section is outdated.</P
2658 >The locking calls available under a DOS/Windows environment are much
2659 richer than those available in unix. This means a unix server (like
2660 Samba) choosing to use the standard fcntl() based unix locking calls
2661 to implement SMB locking has to improvise a bit.</P
2663 >One major problem is that dos locks can be in a 32 bit (unsigned)
2664 range. Unix locking calls are 32 bits, but are signed, giving only a 31
2665 bit range. Unfortunately OLE2 clients use the top bit to select a
2666 locking range used for OLE semaphores.</P
2668 >To work around this problem Samba compresses the 32 bit range into 31
2669 bits by appropriate bit shifting. This seems to work but is not
2670 ideal. In a future version a separate SMB lockd may be added to cope
2671 with the problem.</P
2673 >It also doesn't help that many unix lockd daemons are very buggy and
2674 crash at the slightest provocation. They normally go mostly unused in
2675 a unix environment because few unix programs use byte range
2676 locking. The stress of huge numbers of lock requests from dos/windows
2677 clients can kill the daemon on some systems.</P
2679 >The second major problem is the "opportunistic locking" requested by
2680 some clients. If a client requests opportunistic locking then it is
2681 asking the server to notify it if anyone else tries to do something on
2682 the same file, at which time the client will say if it is willing to
2683 give up its lock. Unix has no simple way of implementing
2684 opportunistic locking, and currently Samba has no support for it.</P
2693 >7.6. Deny Modes</H2
2695 >When a SMB client opens a file it asks for a particular "deny mode" to
2696 be placed on the file. These modes (DENY_NONE, DENY_READ, DENY_WRITE,
2697 DENY_ALL, DENY_FCB and DENY_DOS) specify what actions should be
2698 allowed by anyone else who tries to use the file at the same time. If
2699 DENY_READ is placed on the file, for example, then any attempt to open
2700 the file for reading should fail.</P
2702 >Unix has no equivalent notion. To implement this Samba uses either lock
2703 files based on the files inode and placed in a separate lock
2704 directory or a shared memory implementation. The lock file method
2705 is clumsy and consumes processing and file resources,
2706 the shared memory implementation is vastly prefered and is turned on
2707 by default for those systems that support it.</P
2716 >7.7. Trapdoor UIDs</H2
2718 >A SMB session can run with several uids on the one socket. This
2719 happens when a user connects to two shares with different
2720 usernames. To cope with this the unix server needs to switch uids
2721 within the one process. On some unixes (such as SCO) this is not
2722 possible. This means that on those unixes the client is restricted to
2725 >Note that you can also get the "trapdoor uid" message for other
2726 reasons. Please see the FAQ for details.</P
2735 >7.8. Port numbers</H2
2737 >There is a convention that clients on sockets use high "unprivilaged"
2738 port numbers (>1000) and connect to servers on low "privilaged" port
2739 numbers. This is enforced in Unix as non-root users can't open a
2740 socket for listening on port numbers less than 1000.</P
2742 >Most PC based SMB clients (such as WfWg and WinNT) don't follow this
2743 convention completely. The main culprit is the netbios nameserving on
2744 udp port 137. Name query requests come from a source port of 137. This
2745 is a problem when you combine it with the common firewalling technique
2746 of not allowing incoming packets on low port numbers. This means that
2747 these clients can't query a netbios nameserver on the other side of a
2748 low port based firewall.</P
2750 >The problem is more severe with netbios node status queries. I've
2751 found that WfWg, Win95 and WinNT3.5 all respond to netbios node status
2752 queries on port 137 no matter what the source port was in the
2753 request. This works between machines that are both using port 137, but
2754 it means it's not possible for a unix user to do a node status request
2755 to any of these OSes unless they are running as root. The answer comes
2756 back, but it goes to port 137 which the unix user can't listen
2757 on. Interestingly WinNT3.1 got this right - it sends node status
2758 responses back to the source port in the request.</P
2767 >7.9. Protocol Complexity</H2
2769 >There are many "protocol levels" in the SMB protocol. It seems that
2770 each time new functionality was added to a Microsoft operating system,
2771 they added the equivalent functions in a new protocol level of the SMB
2772 protocol to "externalise" the new capabilities.</P
2774 >This means the protocol is very "rich", offering many ways of doing
2775 each file operation. This means SMB servers need to be complex and
2776 large. It also means it is very difficult to make them bug free. It is
2777 not just Samba that suffers from this problem, other servers such as
2778 WinNT don't support every variation of every call and it has almost
2779 certainly been a headache for MS developers to support the myriad of
2780 SMB calls that are available.</P
2782 >There are about 65 "top level" operations in the SMB protocol (things
2783 like SMBread and SMBwrite). Some of these include hundreds of
2784 sub-functions (SMBtrans has at least 120 sub-functions, like
2785 DosPrintQAdd and NetSessionEnum). All of them take several options
2786 that can change the way they work. Many take dozens of possible
2787 "information levels" that change the structures that need to be
2788 returned. Samba supports all but 2 of the "top level" functions. It
2789 supports only 8 (so far) of the SMBtrans sub-functions. Even NT
2790 doesn't support them all.</P
2792 >Samba currently supports up to the "NT LM 0.12" protocol, which is the
2793 one preferred by Win95 and WinNT3.5. Luckily this protocol level has a
2794 "capabilities" field which specifies which super-duper new-fangled
2795 options the server suports. This helps to make the implementation of
2796 this protocol level much easier.</P
2798 >There is also a problem with the SMB specications. SMB is a X/Open
2799 spec, but the X/Open book is far from ideal, and fails to cover many
2800 important issues, leaving much to the imagination. Microsoft recently
2801 renamed the SMB protocol CIFS (Common Internet File System) and have
2802 published new specifications. These are far superior to the old
2803 X/Open documents but there are still undocumented calls and features.
2804 This specification is actively being worked on by a CIFS developers
2805 mailing list hosted by Microsft.</P
2814 >Chapter 8. Tracing samba system calls</H1
2816 >This file describes how to do a system call trace on Samba to work out
2817 what its doing wrong. This is not for the faint of heart, but if you
2818 are reading this then you are probably desperate.</P
2820 >Actually its not as bad as the the above makes it sound, just don't
2821 expect the output to be very pretty :-)</P
2823 >Ok, down to business. One of the big advantages of unix systems is
2824 that they nearly all come with a system trace utility that allows you
2825 to monitor all system calls that a program is making. This is
2826 extremely using for debugging and also helps when trying to work out
2827 why something is slower than you expect. You can use system tracing
2828 without any special compilation options. </P
2830 >The system trace utility is called different things on different
2831 systems. On Linux systems its called strace. Under SunOS 4 its called
2832 trace. Under SVR4 style systems (including solaris) its called
2833 truss. Under many BSD systems its called ktrace. </P
2835 >The first thing you should do is read the man page for your native
2836 system call tracer. In the discussion below I'll assume its called
2837 strace as strace is the only portable system tracer (its available for
2838 free for many unix types) and its also got some of the nicest
2841 >Next, try using strace on some simple commands. For example, <B
2847 >strace echo hello</B
2851 You'll notice that it produces a LOT of output. It is showing you the
2852 arguments to every system call that the program makes and the
2853 result. Very little happens in a program without a system call so you
2854 get lots of output. You'll also find that it produces a lot of
2855 "preamble" stuff showing the loading of shared libraries etc. Ignore
2856 this (unless its going wrong!)</P
2858 >For example, the only line that really matters in the <B
2865 CLASS="PROGRAMLISTING"
2866 >write(1, "hello\n", 6) = 6</PRE
2869 >all the rest is just setting up to run the program.</P
2871 >Ok, now you're familiar with strace. To use it on Samba you need to
2872 strace the running smbd daemon. The way I tend ot use it is to first
2873 login from my Windows PC to the Samba server, then use smbstatus to
2874 find which process ID that client is attached to, then as root I do
2878 > to attach to that process. I normally redirect the
2879 stderr output from this command to a file for later perusal. For
2880 example, if I'm using a csh style shell:</P
2884 >strace -f -p 3872 >& strace.out</B
2887 >or with a sh style shell:</P
2891 >strace -f -p 3872 > strace.out 2>&1</B
2894 >Note the "-f" option. This is only available on some systems, and
2895 allows you to trace not just the current process, but any children it
2896 forks. This is great for finding printing problems caused by the
2897 "print command" being wrong.</P
2899 >Once you are attached you then can do whatever it is on the client
2900 that is causing problems and you will capture all the system calls
2901 that smbd makes. </P
2903 >So how do you interpret the results? Generally I search through the
2904 output for strings that I know will appear when the problem
2905 happens. For example, if I am having touble with permissions on a file
2906 I would search for that files name in the strace output and look at
2907 the surrounding lines. Another trick is to match up file descriptor
2908 numbers and "follow" what happens to an open file until it is closed.</P
2910 >Beyond this you will have to use your initiative. To give you an idea
2911 of what you are looking for here is a piece of strace output that
2915 > is not world writeable, which
2916 causes printing to fail with Samba:</P
2919 CLASS="PROGRAMLISTING"
2920 >[pid 28268] open("/dev/null", O_RDWR) = -1 EACCES (Permission denied)
2921 [pid 28268] open("/dev/null", O_WRONLY) = -1 EACCES (Permission denied)</PRE
2924 >The process is trying to first open <TT
2928 then read-only. Both fail. This means <TT
2932 incorrect permissions.</P
2940 >Chapter 9. NT Domain RPC's</H1
2948 >9.1. Introduction</H2
2950 >This document contains information to provide an NT workstation with login
2951 services, without the need for an NT server. It is the sgml version of <A
2952 HREF="http://mailhost.cb1.com/~lkcl/cifsntdomain.txt"
2954 >http://mailhost.cb1.com/~lkcl/cifsntdomain.txt</A
2955 >, controlled by Luke.</P
2957 >It should be possible to select a domain instead of a workgroup (in the NT
2958 workstation's TCP/IP settings) and after the obligatory reboot, type in a
2959 username, password, select a domain and successfully log in. I would
2960 appreciate any feedback on your experiences with this process, and any
2961 comments, corrections and additions to this document.</P
2963 >The packets described here can be easily derived from (and are probably
2964 better understood using) Netmon.exe. You will need to use the version
2965 of Netmon that matches your system, in order to correctly decode the
2966 NETLOGON, lsarpc and srvsvc Transact pipes. This document is derived from
2967 NT Service Pack 1 and its corresponding version of Netmon. It is intended
2968 that an annotated packet trace be produced, which will likely be more
2969 instructive than this document.</P
2971 >Also needed, to fully implement NT Domain Login Services, is the
2972 document describing the cryptographic part of the NT authentication.
2973 This document is available from comp.protocols.smb; from the ntsecurity.net
2974 digest and from the samba digest, amongst other sources.</P
2976 >A copy is available from:</P
2979 HREF="http://ntbugtraq.rc.on.ca/SCRIPTS/WA.EXE?A2=ind9708;L=ntbugtraq;O=A;P=2935"
2981 >http://ntbugtraq.rc.on.ca/SCRIPTS/WA.EXE?A2=ind9708;L=ntbugtraq;O=A;P=2935</A
2985 HREF="http://mailhost.cb1.com/~lkcl/crypt.html"
2987 >http://mailhost.cb1.com/~lkcl/crypt.html</A
2990 >A c-code implementation, provided by <A
2991 HREF="mailto:linus@incolumitas.se"
2995 of this protocol is available from:</P
2998 HREF="http://samba.org/cgi-bin/mfs/01/digest/1997/97aug/0391.html"
3000 >http://samba.org/cgi-bin/mfs/01/digest/1997/97aug/0391.html</A
3004 HREF="http://mailhost.cb1.com/~lkcl/crypt.txt"
3006 >http://mailhost.cb1.com/~lkcl/crypt.txt</A
3009 >Also used to provide debugging information is the Check Build version of
3010 NT workstation, and enabling full debugging in NETLOGON. This is
3011 achieved by setting the following REG_SZ registry key to 0x1ffffff:</P
3015 >HKLM\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters</TT
3022 >Incorrect direct editing of the registry can cause your
3023 machine to fail. Then again, so can incorrect implementation of this
3024 protocol. See "Liability:" above.</I
3028 >Bear in mind that each packet over-the-wire will have its origin in an
3029 API call. Therefore, there are likely to be structures, enumerations
3030 and defines that are usefully documented elsewhere.</P
3032 >This document is by no means complete or authoritative. Missing sections
3033 include, but are not limited to:</P
3040 >Mappings of RIDs to usernames (and vice-versa).</P
3044 >What a User ID is and what a Group ID is.</P
3048 >The exact meaning/definition of various magic constants or enumerations.</P
3052 >The reply error code and use of that error code when a
3053 workstation becomes a member of a domain (to be described later).
3054 Failure to return this error code will make the workstation report
3055 that it is already a member of the domain.</P
3059 >the cryptographic side of the NetrServerPasswordSet command,
3060 which would allow the workstation to change its password. This password is
3061 used to generate the long-term session key. [It is possible to reject this
3062 command, and keep the default workstation password].</P
3080 >cket Traces from Netmonitor (Service Pack 1 and above)</TD
3084 >ul Ashton and Luke Leighton's other "NT Domain" doc.</TD
3088 >FS documentation - cifs6.txt</TD
3092 >FS documentation - cifsrap2.txt</TD
3114 >Paul Ashton: loads of work with Net Monitor; understanding the NT authentication system; reference implementation of the NT domain support on which this document is originally based.</TD
3118 >Duncan Stansfield: low-level analysis of MSRPC Pipes.</TD
3122 >Linus Nordberg: producing c-code from Paul's crypto spec.</TD
3126 >Windows Sourcer development team</TD
3141 >9.2. Notes and Structures</H2
3156 >In the SMB Transact pipes, some "Structures", described here, appear to be
3157 4-byte aligned with the SMB header, at their start. Exactly which
3158 "Structures" need aligning is not precisely known or documented.</P
3162 >In the UDP NTLOGON Mailslots, some "Structures", described here, appear to be
3163 2-byte aligned with the start of the mailslot, at their start.</P
3167 >Domain SID is of the format S-revision-version-auth1-auth2...authN.
3168 e.g S-1-5-123-456-789-123-456. the 5 could be a sub-revision.</P
3172 >any undocumented buffer pointers must be non-zero if the string buffer it
3173 refers to contains characters. exactly what value they should be is unknown.
3174 0x0000 0002 seems to do the trick to indicate that the buffer exists. a
3175 NULL buffer pointer indicates that the string buffer is of zero length.
3176 If the buffer pointer is NULL, then it is suspected that the structure it
3177 refers to is NOT put into (or taken out of) the SMB data stream. This is
3178 empirically derived from, for example, the LSA SAM Logon response packet,
3179 where if the buffer pointer is NULL, the user information is not inserted
3180 into the data stream. Exactly what happens with an array of buffer pointers
3181 is not known, although an educated guess can be made.</P
3185 >an array of structures (a container) appears to have a count and a pointer.
3186 if the count is zero, the pointer is also zero. no further data is put
3187 into or taken out of the SMB data stream. if the count is non-zero, then
3188 the pointer is also non-zero. immediately following the pointer is the
3189 count again, followed by an array of container sub-structures. the count
3190 appears a third time after the last sub-structure.</P
3201 >9.2.2. Enumerations</H3
3209 >9.2.2.1. MSRPC Header type</H4
3211 >command number in the msrpc packet header</P
3215 CLASS="VARIABLELIST"
3224 >MSRPC_Response:</DT
3251 >9.2.2.2. MSRPC Packet info</H4
3253 >The meaning of these flags is undocumented</P
3257 CLASS="VARIABLELIST"
3318 >9.2.3. Structures</H3
3326 >9.2.3.1. VOID *</H4
3328 >sizeof VOID* is 32 bits.</P
3339 >sizeof char is 8 bits.</P
3350 >UTIME is 32 bits, indicating time in seconds since 01jan1970. documented in cifs6.txt (section 3.5 page, page 30).</P
3359 >9.2.3.4. NTTIME</H4
3361 >NTTIME is 64 bits. documented in cifs6.txt (section 3.5 page, page 30).</P
3370 >9.2.3.5. DOM_SID (domain SID structure)</H4
3374 CLASS="VARIABLELIST"
3380 >num of sub-authorities in domain SID</P
3386 >SID revision number</P
3392 >num of sub-authorities in domain SID</P
3398 >6 bytes for domain SID - Identifier Authority.</P
3401 >UINT16[n_subauths]</DT
3404 >domain SID sub-authorities</P
3413 >Note: the domain SID is documented elsewhere.</I
3424 >9.2.3.6. STR (string)</H4
3426 >STR (string) is a char[] : a null-terminated string of ascii characters.</P
3435 >9.2.3.7. UNIHDR (unicode string header)</H4
3439 CLASS="VARIABLELIST"
3445 >length of unicode string</P
3451 >max length of unicode string</P
3457 >4 - undocumented.</P
3469 >9.2.3.8. UNIHDR2 (unicode string header plus buffer pointer)</H4
3473 CLASS="VARIABLELIST"
3479 >unicode string header</P
3485 >undocumented buffer pointer</P
3497 >9.2.3.9. UNISTR (unicode string)</H4
3501 CLASS="VARIABLELIST"
3507 >null-terminated string of unicode characters.</P
3519 >9.2.3.10. NAME (length-indicated unicode string)</H4
3523 CLASS="VARIABLELIST"
3529 >length of unicode string</P
3535 >null-terminated string of unicode characters.</P
3547 >9.2.3.11. UNISTR2 (aligned unicode string)</H4
3551 CLASS="VARIABLELIST"
3557 >padding to get unicode string 4-byte aligned with the start of the SMB header.</P
3563 >max length of unicode string</P
3569 >0 - undocumented</P
3575 >length of unicode string</P
3581 >string of uncode characters</P
3593 >9.2.3.12. OBJ_ATTR (object attributes)</H4
3597 CLASS="VARIABLELIST"
3603 >0x18 - length (in bytes) including the length field.</P
3609 >0 - root directory (pointer)</P
3615 >0 - object name (pointer)</P
3621 >0 - attributes (undocumented)</P
3627 >0 - security descriptior (pointer)</P
3633 >0 - security quality of service</P
3645 >9.2.3.13. POL_HND (LSA policy handle)</H4
3649 CLASS="VARIABLELIST"
3667 >9.2.3.14. DOM_SID2 (domain SID structure, SIDS stored in unicode)</H4
3671 CLASS="VARIABLELIST"
3683 >0 - undocumented</P
3689 >domain SID unicode string header</P
3695 >domain SID unicode string</P
3704 >Note: there is a conflict between the unicode string header and the unicode string itself as to which to use to indicate string length. this will need to be resolved.</I
3712 >Note: the SID type indicates, for example, an alias; a well-known group etc. this is documented somewhere.</I
3723 >9.2.3.15. DOM_RID (domain RID structure)</H4
3727 CLASS="VARIABLELIST"
3733 >5 - well-known SID. 1 - user SID (see ShowACLs)</P
3739 >5 - undocumented</P
3751 >0 - domain index out of above reference domains</P
3763 >9.2.3.16. LOG_INFO (server, account, client structure)</H4
3769 >Note: logon server name starts with two '\' characters and is upper case.</I
3777 >Note: account name is the logon client name from the LSA Request Challenge, with a $ on the end of it, in upper case.</I
3783 CLASS="VARIABLELIST"
3789 >undocumented buffer pointer</P
3795 >logon server unicode string</P
3801 >account name unicode string</P
3807 >sec_chan - security channel type</P
3813 >logon client machine unicode string</P
3825 >9.2.3.17. CLNT_SRV (server, client names structure)</H4
3831 >Note: logon server name starts with two '\' characters and is upper case.</I
3837 CLASS="VARIABLELIST"
3843 >undocumented buffer pointer</P
3849 >logon server unicode string</P
3855 >undocumented buffer pointer</P
3861 >logon client machine unicode string</P
3873 >9.2.3.18. CREDS (credentials + time stamp)</H4
3877 CLASS="VARIABLELIST"
3901 >9.2.3.19. CLNT_INFO2 (server, client structure, client credentials)</H4
3907 >Note: whenever this structure appears in a request, you must take a copy of the client-calculated credentials received, because they will beused in subsequent credential checks. the presumed intention is to
3908 maintain an authenticated request/response trail.</I
3914 CLASS="VARIABLELIST"
3920 >client and server names</P
3926 >???? padding, for 4-byte alignment with SMB header.</P
3932 >pointer to client credentials.</P
3938 >client-calculated credentials + client time</P
3950 >9.2.3.20. CLNT_INFO (server, account, client structure, client credentials)</H4
3956 >Note: whenever this structure appears in a request, you must take a copy of the client-calculated credentials received, because they will be used in subsequent credential checks. the presumed intention is to maintain an authenticated request/response trail.</I
3962 CLASS="VARIABLELIST"
3968 >logon account info</P
3974 >client-calculated credentials + client time</P
3986 >9.2.3.21. ID_INFO_1 (id info structure, auth level 1)</H4
3990 CLASS="VARIABLELIST"
4002 >domain name unicode header</P
4020 >user name unicode header</P
4026 >workgroup name unicode header</P
4032 >arc4 LM OWF Password</P
4038 >arc4 NT OWF Password</P
4044 >domain name unicode string</P
4050 >user name unicode string</P
4056 >workstation name unicode string</P
4068 >9.2.3.22. SAM_INFO (sam logon/logoff id info structure)</H4
4074 >Note: presumably, the return credentials is supposedly for the server to verify that the credential chain hasn't been compromised.</I
4080 CLASS="VARIABLELIST"
4086 >client identification/authentication info</P
4092 >pointer to return credentials.</P
4098 >return credentials - ignored.</P
4116 CLASS="PROGRAMLISTING"
4117 > switch (switch_value)
4120 ID_INFO_1 id_info_1;
4131 >9.2.3.23. GID (group id info)</H4
4135 CLASS="VARIABLELIST"
4147 >user attributes (only used by NT 3.1 and 3.51)</P
4159 >9.2.3.24. DOM_REF (domain reference info)</H4
4163 CLASS="VARIABLELIST"
4169 >undocumented buffer pointer.</P
4175 >num referenced domains?</P
4181 >undocumented domain name buffer pointer.</P
4187 >32 - max number of entries</P
4193 >4 - num referenced domains?</P
4199 >domain name unicode string header</P
4202 >UNIHDR2[num_ref_doms-1]</DT
4205 >referenced domain unicode string headers</P
4211 >domain name unicode string</P
4214 >DOM_SID[num_ref_doms]</DT
4217 >referenced domain SIDs</P
4229 >9.2.3.25. DOM_INFO (domain info, levels 3 and 5 are the same))</H4
4233 CLASS="VARIABLELIST"
4239 >??? padding to get 4-byte alignment with start of SMB header</P
4245 >domain name string length * 2</P
4251 >domain name string length * 2</P
4257 >undocumented domain name string buffer pointer</P
4263 >undocumented domain SID string buffer pointer</P
4269 >domain name (unicode string)</P
4287 >9.2.3.26. USER_INFO (user logon info)</H4
4293 >Note: it would be nice to know what the 16 byte user session key is for.</I
4299 CLASS="VARIABLELIST"
4323 >password last set time</P
4329 >password can change time</P
4335 >password must change time</P
4341 >username unicode string header</P
4347 >user's full name unicode string header</P
4353 >logon script unicode string header</P
4359 >profile path unicode string header</P
4365 >home directory unicode string header</P
4371 >home directory drive unicode string header</P
4383 >bad password count</P
4407 >undocumented buffer pointer to groups.</P
4419 >user session key</P
4425 >logon server unicode string header</P
4431 >logon domain unicode string header</P
4437 >undocumented logon domain id pointer</P
4443 >40 undocumented padding bytes. future expansion?</P
4449 >0 - num_other_sids?</P
4455 >NULL - undocumented pointer to other domain SIDs.</P
4461 >username unicode string</P
4467 >user's full name unicode string</P
4473 >logon script unicode string</P
4479 >profile path unicode string</P
4485 >home directory unicode string</P
4491 >home directory drive unicode string</P
4500 >GID[num_groups]</DT
4509 >logon server unicode string</P
4515 >logon domain unicode string</P
4524 >DOM_SID[num_sids]</DT
4527 >other domain SIDs?</P
4539 >9.2.3.27. SH_INFO_1_PTR (pointers to level 1 share info strings)</H4
4545 >Note: see cifsrap2.txt section5, page 10.</I
4555 >0 for shi1_type indicates a Disk.</TD
4559 >1 for shi1_type indicates a Print Queue.</TD
4563 >2 for shi1_type indicates a Device.</TD
4567 >3 for shi1_type indicates an IPC pipe.</TD
4571 >0x8000 0000 (top bit set in shi1_type) indicates a hidden share.</TD
4580 CLASS="VARIABLELIST"
4586 >shi1_netname - pointer to net name</P
4592 >shi1_type - type of share. 0 - undocumented.</P
4598 >shi1_remark - pointer to comment.</P
4610 >9.2.3.28. SH_INFO_1_STR (level 1 share info strings)</H4
4614 CLASS="VARIABLELIST"
4620 >shi1_netname - unicode string of net name</P
4626 >shi1_remark - unicode string of comment.</P
4638 >9.2.3.29. SHARE_INFO_1_CTR</H4
4640 >share container with 0 entries:</P
4644 CLASS="VARIABLELIST"
4661 >share container with > 0 entries:</P
4665 CLASS="VARIABLELIST"
4677 >non-zero - Buffer</P
4686 >SH_INFO_1_PTR[EntriesRead]</DT
4689 >share entry pointers</P
4692 >SH_INFO_1_STR[EntriesRead]</DT
4695 >share entry strings</P
4701 >padding to get unicode string 4-byte aligned with start of the SMB header.</P
4725 >9.2.3.30. SERVER_INFO_101</H4
4731 >Note: see cifs6.txt section 6.4 - the fields described therein will be of assistance here. for example, the type listed below is the same as fServerType, which is described in 6.4.1. </I
4737 CLASS="VARIABLELIST"
4740 >SV_TYPE_WORKSTATION</DT
4743 >0x00000001 All workstations</P
4749 >0x00000002 All servers</P
4752 >SV_TYPE_SQLSERVER</DT
4755 >0x00000004 Any server running with SQL server</P
4758 >SV_TYPE_DOMAIN_CTRL</DT
4761 >0x00000008 Primary domain controller</P
4764 >SV_TYPE_DOMAIN_BAKCTRL</DT
4767 >0x00000010 Backup domain controller</P
4770 >SV_TYPE_TIME_SOURCE</DT
4773 >0x00000020 Server running the timesource service</P
4779 >0x00000040 Apple File Protocol servers</P
4785 >0x00000080 Novell servers</P
4788 >SV_TYPE_DOMAIN_MEMBER</DT
4791 >0x00000100 Domain Member</P
4794 >SV_TYPE_PRINTQ_SERVER</DT
4797 >0x00000200 Server sharing print queue</P
4800 >SV_TYPE_DIALIN_SERVER</DT
4803 >0x00000400 Server running dialin service.</P
4806 >SV_TYPE_XENIX_SERVER</DT
4809 >0x00000800 Xenix server</P
4815 >0x00001000 NT server</P
4821 >0x00002000 Server running Windows for </P
4824 >SV_TYPE_SERVER_NT</DT
4827 >0x00008000 Windows NT non DC server</P
4830 >SV_TYPE_POTENTIAL_BROWSER</DT
4833 >0x00010000 Server that can run the browser service</P
4836 >SV_TYPE_BACKUP_BROWSER</DT
4839 >0x00020000 Backup browser server</P
4842 >SV_TYPE_MASTER_BROWSER</DT
4845 >0x00040000 Master browser server</P
4848 >SV_TYPE_DOMAIN_MASTER</DT
4851 >0x00080000 Domain Master Browser server</P
4854 >SV_TYPE_LOCAL_LIST_ONLY</DT
4857 >0x40000000 Enumerate only entries marked "local"</P
4860 >SV_TYPE_DOMAIN_ENUM</DT
4863 >0x80000000 Enumerate Domains. The pszServer and pszDomain parameters must be NULL.</P
4870 CLASS="VARIABLELIST"
4876 >500 - platform_id</P
4888 >5 - major version</P
4894 >4 - minor version</P
4900 >type (SV_TYPE_... bit field)</P
4906 >pointer to comment</P
4912 >sv101_name - unicode string of server name</P
4918 >sv_101_comment - unicode string of server comment.</P
4924 >padding to get unicode string 4-byte aligned with start of the SMB header.</P
4938 >9.3. MSRPC over Transact Named Pipe</H2
4940 >For details on the SMB Transact Named Pipe, see cifs6.txt</P
4948 >9.3.1. MSRPC Pipes</H3
4950 >The MSRPC is conducted over an SMB Transact Pipe with a name of
4954 >. You must first obtain a 16 bit file handle, by
4955 sending a SMBopenX with the pipe name <TT
4959 example. You can then perform an SMB Trans,
4960 and must carry out an SMBclose on the file handle once you are finished.</P
4962 >Trans Requests must be sent with two setup UINT16s, no UINT16 params (none
4963 known about), and UINT8 data parameters sufficient to contain the MSRPC
4964 header, and MSRPC data. The first UINT16 setup parameter must be either
4965 0x0026 to indicate an RPC, or 0x0001 to indicate Set Named Pipe Handle
4966 state. The second UINT16 parameter must be the file handle for the pipe,
4969 >The Data section for an API Command of 0x0026 (RPC pipe) in the Trans
4970 Request is the RPC Header, followed by the RPC Data. The Data section for
4971 an API Command of 0x0001 (Set Named Pipe Handle state) is two bytes. The
4972 only value seen for these two bytes is 0x00 0x43.</P
4974 >MSRPC Responses are sent as response data inside standard SMB Trans
4975 responses, with the MSRPC Header, MSRPC Data and MSRPC tail.</P
4977 >It is suspected that the Trans Requests will need to be at least 2-byte
4978 aligned (probably 4-byte). This is standard practice for SMBs. It is also
4979 independent of the observed 4-byte alignments with the start of the MSRPC
4980 header, including the 4-byte alignment between the MSRPC header and the
4983 >First, an SMBtconX connection is made to the IPC$ share. The connection
4984 must be made using encrypted passwords, not clear-text. Then, an SMBopenX
4985 is made on the pipe. Then, a Set Named Pipe Handle State must be sent,
4986 after which the pipe is ready to accept API commands. Lastly, and SMBclose
4991 >lkcl/01nov97 there appear to be two additional bytes after the null-terminated \PIPE\ name for the RPC pipe. Values seen so far are
4995 CLASS="PROGRAMLISTING"
4996 > initial SMBopenX request: RPC API command 0x26 params:
4997 "\\PIPE\\lsarpc" 0x65 0x63; 0x72 0x70; 0x44 0x65;
4998 "\\PIPE\\srvsvc" 0x73 0x76; 0x4E 0x00; 0x5C 0x43;</PRE
5010 >[section to be rewritten, following receipt of work by Duncan Stansfield]</P
5012 >Interesting note: if you set packed data representation to 0x0100 0000
5013 then all 4-byte and 2-byte word ordering is turned around!</P
5015 >The start of each of the NTLSA and NETLOGON named pipes begins with:</P
5027 >5 - RPC major version</P
5039 >0 - RPC minor version</P
5051 >2 - RPC response packet</P
5063 >3 - (FirstFrag bit-wise or with LastFrag)</P
5075 >0x1000 0000 - packed data representation</P
5087 >fragment length - data size (bytes) inc header and tail.</P
5099 >0 - authentication length </P
5111 >call identifier. matches 12th UINT32 of incoming RPC data.</P
5123 >allocation hint - data size (bytes) minus header and tail.</P
5135 >0 - presentation context identifier</P
5147 >0 - cancel count</P
5159 >in replies: 0 - reserved; in requests: opnum - see #defines.</P
5171 >start of data (goes on for allocation_hint bytes)</P
5179 >9.3.2.1. RPC_Packet for request, response, bind and bind acknowledgement</H4
5183 CLASS="VARIABLELIST"
5186 >UINT8 versionmaj</DT
5189 >reply same as request (0x05)</P
5192 >UINT8 versionmin</DT
5195 >reply same as request (0x00)</P
5201 >one of the MSRPC_Type enums</P
5207 >reply same as request (0x00 for Bind, 0x03 for Request)</P
5210 >UINT32 representation</DT
5213 >reply same as request (0x00000010)</P
5216 >UINT16 fraglength</DT
5219 >the length of the data section of the SMB trans packet</P
5222 >UINT16 authlength</DT
5231 >call identifier. (e.g. 0x00149594)</P
5234 >* stub USE TvPacket</DT
5237 >the remainder of the packet depending on the "type"</P
5249 >9.3.2.2. Interface identification</H4
5251 >the interfaces are numbered. as yet I haven't seen more than one interface used on the same pipe name srvsvc</P
5254 CLASS="PROGRAMLISTING"
5255 >abstract (0x4B324FC8, 0x01D31670, 0x475A7812, 0x88E16EBF, 0x00000003)
5256 transfer (0x8A885D04, 0x11C91CEB, 0x0008E89F, 0x6048102B, 0x00000002)</PRE
5266 >9.3.2.3. RPC_Iface RW</H4
5270 CLASS="VARIABLELIST"
5276 >16 bytes of number</P
5282 >the interface number</P
5294 >9.3.2.4. RPC_ReqBind RW</H4
5296 >the remainder of the packet after the header if "type" was Bind in the response header, "type" should be BindAck</P
5300 CLASS="VARIABLELIST"
5303 >UINT16 maxtsize</DT
5306 >maximum transmission fragment size (0x1630)</P
5309 >UINT16 maxrsize</DT
5312 >max receive fragment size (0x1630)</P
5315 >UINT32 assocgid</DT
5318 >associated group id (0x0)</P
5321 >UINT32 numelements</DT
5324 >the number of elements (0x1)</P
5327 >UINT16 contextid</DT
5330 >presentation context identifier (0x0)</P
5333 >UINT8 numsyntaxes</DT
5336 >the number of syntaxes (has always been 1?)(0x1)</P
5342 >4-byte alignment padding, against SMB header</P
5345 >* abstractint USE RPC_Iface</DT
5348 >num and vers. of interface client is using</P
5351 >* transferint USE RPC_Iface</DT
5354 >num and vers. of interface to use for replies</P
5366 >9.3.2.5. RPC_Address RW</H4
5370 CLASS="VARIABLELIST"
5376 >length of the string including null terminator</P
5379 >* port USE string</DT
5382 >the string above in single byte, null terminated form</P
5394 >9.3.2.6. RPC_ResBind RW</H4
5396 >the response to place after the header in the reply packet</P
5400 CLASS="VARIABLELIST"
5403 >UINT16 maxtsize</DT
5409 >UINT16 maxrsize</DT
5415 >UINT32 assocgid</DT
5421 >* secondaddr USE RPC_Address</DT
5424 >the address string, as described earlier</P
5430 >4-byte alignment padding, against SMB header</P
5433 >UINT8 numresults</DT
5436 >the number of results (0x01)</P
5442 >4-byte alignment padding, against SMB header</P
5448 >result (0x00 = accept)</P
5454 >reason (0x00 = no reason specified)</P
5457 >* transfersyntax USE RPC_Iface</DT
5460 >the transfer syntax from the request</P
5472 >9.3.2.7. RPC_ReqNorm RW</H4
5474 >the remainder of the packet after the header for every other other request</P
5478 CLASS="VARIABLELIST"
5481 >UINT32 allochint</DT
5484 >the size of the stub data in bytes</P
5487 >UINT16 prescontext</DT
5490 >presentation context identifier (0x0)</P
5496 >operation number (0x15)</P
5499 >* stub USE TvPacket</DT
5502 >a packet dependent on the pipe name (probably the interface) and the op number)</P
5514 >9.3.2.8. RPC_ResNorm RW</H4
5518 CLASS="VARIABLELIST"
5521 >UINT32 allochint</DT
5524 ># size of the stub data in bytes</P
5527 >UINT16 prescontext</DT
5530 ># presentation context identifier (same as request)</P
5533 >UINT8 cancelcount</DT
5536 ># cancel count? (0x0)</P
5542 ># 0 - one byte padding</P
5545 >* stub USE TvPacket</DT
5548 ># the remainder of the reply</P
5563 >The end of each of the NTLSA and NETLOGON named pipes ends with:</P
5567 CLASS="VARIABLELIST"
5591 >9.3.4. RPC Bind / Bind Ack</H3
5593 >RPC Binds are the process of associating an RPC pipe (e.g \PIPE\lsarpc)
5594 with a "transfer syntax" (see RPC_Iface structure). The purpose for doing
5601 >Note: The RPC_ResBind SMB Transact request is sent with two uint16 setup parameters. The first is 0x0026; the second is the file handle
5602 returned by the SMBopenX Transact response.</I
5610 >Note: The RPC_ResBind members maxtsize, maxrsize and assocgid are the same in the response as the same members in the RPC_ReqBind. The
5611 RPC_ResBind member transfersyntax is the same in the response as
5620 >Note: The RPC_ResBind response member secondaddr contains the name of what is presumed to be the service behind the RPC pipe. The
5621 mapping identified so far is:</I
5627 CLASS="VARIABLELIST"
5630 >initial SMBopenX request:</DT
5633 >RPC_ResBind response:</P
5636 >"\\PIPE\\srvsvc"</DT
5639 >"\\PIPE\\ntsvcs"</P
5648 >"\\PIPE\\lsarpc"</DT
5654 >"\\PIPE\\wkssvc"</DT
5657 >"\\PIPE\\wksvcs"</P
5660 >"\\PIPE\\NETLOGON"</DT
5663 >"\\PIPE\\NETLOGON"</P
5672 >Note: The RPC_Packet fraglength member in both the Bind Request and Bind Acknowledgment must contain the length of the entire RPC data, including the RPC_Packet header.</I
5721 >9.3.5. NTLSA Transact Named Pipe</H3
5723 >The sequence of actions taken on this pipe are:</P
5731 >Establish a connection to the IPC$ share (SMBtconX). use encrypted passwords.</TD
5735 >Open an RPC Pipe with the name "\\PIPE\\lsarpc". Store the file handle.</TD
5739 >Using the file handle, send a Set Named Pipe Handle state to 0x4300.</TD
5743 >Send an LSA Open Policy request. Store the Policy Handle.</TD
5747 >Using the Policy Handle, send LSA Query Info Policy requests, etc.</TD
5751 >Using the Policy Handle, send an LSA Close.</TD
5755 >Close the IPC$ share.</TD
5762 >Defines for this pipe, identifying the query are:</P
5766 CLASS="VARIABLELIST"
5769 >LSA Open Policy:</DT
5775 >LSA Query Info Policy:</DT
5781 >LSA Enumerate Trusted Domains:</DT
5787 >LSA Open Secret:</DT
5793 >LSA Lookup SIDs:</DT
5799 >LSA Lookup Names:</DT
5820 >9.3.6. LSA Open Policy</H3
5826 >Note: The policy handle can be anything you like.</I
5836 >9.3.6.1. Request</H4
5840 CLASS="VARIABLELIST"
5852 >server name - unicode string starting with two '\'s</P
5858 >object attributes</P
5864 >1 - desired access</P
5876 >9.3.6.2. Response</H4
5880 CLASS="VARIABLELIST"
5886 >LSA policy handle</P
5892 >0 - indicates success</P
5905 >9.3.7. LSA Query Info Policy</H3
5911 >Note: The info class in response must be the same as that in the request.</I
5921 >9.3.7.1. Request</H4
5925 CLASS="VARIABLELIST"
5931 >LSA policy handle</P
5937 >info class (also a policy handle?)</P
5949 >9.3.7.2. Response</H4
5953 CLASS="VARIABLELIST"
5959 >undocumented buffer pointer</P
5965 >info class (same as info class in request).</P
5971 CLASS="PROGRAMLISTING"
5972 >switch (info class)
5976 DOM_INFO domain info, levels 3 and 5 (are the same).
5979 return 0 - indicates success</PRE
5990 >9.3.8. LSA Enumerate Trusted Domains</H3
5998 >9.3.8.1. Request</H4
6009 >9.3.8.2. Response</H4
6013 CLASS="VARIABLELIST"
6019 >0 - enumeration context</P
6025 >0 - entries read</P
6031 >0 - trust information</P
6037 >0x8000 001a - "no trusted domains" success code</P
6050 >9.3.9. LSA Open Secret</H3
6058 >9.3.9.1. Request</H4
6069 >9.3.9.2. Response</H4
6073 CLASS="VARIABLELIST"
6079 >0 - undocumented</P
6085 >0 - undocumented</P
6091 >0 - undocumented</P
6097 >0 - undocumented</P
6103 >0 - undocumented</P
6108 >return 0x0C00 0034 - "no such secret" success code</P
6118 >9.3.10. LSA Close</H3
6126 >9.3.10.1. Request</H4
6130 CLASS="VARIABLELIST"
6136 >policy handle to be closed</P
6148 >9.3.10.2. Response</H4
6152 CLASS="VARIABLELIST"
6158 >0s - closed policy handle (all zeros)</P
6163 >return 0 - indicates success</P
6173 >9.3.11. LSA Lookup SIDS</H3
6179 >Note: num_entries in response must be same as num_entries in request.</I
6189 >9.3.11.1. Request</H4
6193 CLASS="VARIABLELIST"
6199 >LSA policy handle</P
6211 >undocumented domain SID buffer pointer</P
6217 >undocumented domain name buffer pointer</P
6220 >VOID*[num_entries] undocumented domain SID pointers to be looked up.</DT
6223 >DOM_SID[num_entries] domain SIDs to be looked up.</P
6229 >completely undocumented 16 bytes.</P
6241 >9.3.11.2. Response</H4
6245 CLASS="VARIABLELIST"
6251 >domain reference response</P
6257 >num_entries (listed above)</P
6263 >undocumented buffer pointer</P
6269 >num_entries (listed above)</P
6272 >DOM_SID2[num_entries]</DT
6275 >domain SIDs (from Request, listed above).</P
6281 >num_entries (listed above)</P
6286 >return 0 - indicates success</P
6296 >9.3.12. LSA Lookup Names</H3
6302 >Note: num_entries in response must be same as num_entries in request.</I
6312 >9.3.12.1. Request</H4
6316 CLASS="VARIABLELIST"
6322 >LSA policy handle</P
6340 >undocumented domain SID buffer pointer</P
6346 >undocumented domain name buffer pointer</P
6349 >NAME[num_entries]</DT
6352 >names to be looked up.</P
6358 >undocumented bytes - falsely translated SID structure?</P
6370 >9.3.12.2. Response</H4
6374 CLASS="VARIABLELIST"
6380 >domain reference response</P
6386 >num_entries (listed above)</P
6392 >undocumented buffer pointer</P
6398 >num_entries (listed above)</P
6401 >DOM_RID[num_entries]</DT
6404 >domain SIDs (from Request, listed above).</P
6410 >num_entries (listed above)</P
6415 >return 0 - indicates success</P
6426 >9.4. NETLOGON rpc Transact Named Pipe</H2
6428 >The sequence of actions taken on this pipe are:</P
6436 >tablish a connection to the IPC$ share (SMBtconX). use encrypted passwords.</TD
6440 >en an RPC Pipe with the name "\\PIPE\\NETLOGON". Store the file handle.</TD
6444 >ing the file handle, send a Set Named Pipe Handle state to 0x4300.</TD
6448 >eate Client Challenge. Send LSA Request Challenge. Store Server Challenge.</TD
6452 >lculate Session Key. Send an LSA Auth 2 Challenge. Store Auth2 Challenge.</TD
6456 >lc/Verify Client Creds. Send LSA Srv PW Set. Calc/Verify Server Creds.</TD
6460 >lc/Verify Client Creds. Send LSA SAM Logon . Calc/Verify Server Creds.</TD
6464 >lc/Verify Client Creds. Send LSA SAM Logoff. Calc/Verify Server Creds.</TD
6468 >ose the IPC$ share.</TD
6475 >Defines for this pipe, identifying the query are</P
6479 CLASS="VARIABLELIST"
6482 >LSA Request Challenge:</DT
6488 >LSA Server Password Set:</DT
6500 >LSA SAM Logoff:</DT
6512 >LSA Logon Control:</DT
6526 >9.4.1. LSA Request Challenge</H3
6532 >Note: logon server name starts with two '\' characters and is upper case.</I
6540 >Note: logon client is the machine, not the user.</I
6548 >Note: the initial LanManager password hash, against which the challenge is issued, is the machine name itself (lower case). there will becalls issued (LSA Server Password Set) which will change this, later. refusing these calls allows you to always deal with the same password (i.e the LM# of the machine name in lower case).</I
6558 >9.4.1.1. Request</H4
6562 CLASS="VARIABLELIST"
6568 >undocumented buffer pointer</P
6574 >logon server unicode string</P
6580 >logon client unicode string</P
6586 >client challenge</P
6598 >9.4.1.2. Response</H4
6602 CLASS="VARIABLELIST"
6608 >server challenge</P
6613 >return 0 - indicates success</P
6623 >9.4.2. LSA Authenticate 2</H3
6629 >Note: in between request and response, calculate the client credentials, and check them against the client-calculated credentials (this process uses the previously received client credentials).</I
6637 >Note: neg_flags in the response is the same as that in the request.</I
6645 >Note: you must take a copy of the client-calculated credentials received here, because they will be used in subsequent authentication packets.</I
6655 >9.4.2.1. Request</H4
6659 CLASS="VARIABLELIST"
6665 >client identification info</P
6671 >client-calculated credentials</P
6677 >padding to 4-byte align with start of SMB header.</P
6683 >neg_flags - negotiated flags (usual value is 0x0000 01ff)</P
6695 >9.4.2.2. Response</H4
6699 CLASS="VARIABLELIST"
6705 >server credentials.</P
6711 >neg_flags - same as neg_flags in request.</P
6716 >return 0 - indicates success. failure value unknown.</P
6726 >9.4.3. LSA Server Password Set</H3
6732 >Note: the new password is suspected to be a DES encryption using the old password to generate the key.</I
6740 >Note: in between request and response, calculate the client credentials, and check them against the client-calculated credentials (this process uses the previously received client credentials).</I
6748 >Note: the server credentials are constructed from the client-calculated credentials and the client time + 1 second.</I
6756 >Note: you must take a copy of the client-calculated credentials received here, because they will be used in subsequent authentication packets.</I
6766 >9.4.3.1. Request</H4
6770 CLASS="VARIABLELIST"
6776 >client identification/authentication info</P
6782 >new password - undocumented.</P
6794 >9.4.3.2. Response</H4
6798 CLASS="VARIABLELIST"
6804 >server credentials. server time stamp appears to be ignored.</P
6809 >return 0 - indicates success; 0xC000 006a indicates failure</P
6819 >9.4.4. LSA SAM Logon</H3
6825 >Note: valid_user is True iff the username and password hash are valid for
6826 the requested domain.</I
6836 >9.4.4.1. Request</H4
6840 CLASS="VARIABLELIST"
6846 >sam_id structure</P
6858 >9.4.4.2. Response</H4
6862 CLASS="VARIABLELIST"
6868 >undocumented buffer pointer</P
6874 >server credentials. server time stamp appears to be ignored.</P
6880 CLASS="PROGRAMLISTING"
6883 UINT16 3 - switch value indicating USER_INFO structure.
6884 VOID* non-zero - pointer to USER_INFO structure
6885 USER_INFO user logon information
6887 UINT32 1 - Authoritative response; 0 - Non-Auth?
6889 return 0 - indicates success
6893 UINT16 0 - switch value. value to indicate no user presumed.
6894 VOID* 0x0000 0000 - indicates no USER_INFO structure.
6896 UINT32 1 - Authoritative response; 0 - Non-Auth?
6898 return 0xC000 0064 - NT_STATUS_NO_SUCH_USER.
6910 >9.4.5. LSA SAM Logoff</H3
6916 >Note: presumably, the SAM_INFO structure is validated, and a (currently
6917 undocumented) error code returned if the Logoff is invalid.</I
6927 >9.4.5.1. Request</H4
6931 CLASS="VARIABLELIST"
6937 >sam_id structure</P
6949 >9.4.5.2. Response</H4
6953 CLASS="VARIABLELIST"
6959 >undocumented buffer pointer</P
6965 >server credentials. server time stamp appears to be ignored.</P
6970 >return 0 - indicates success. undocumented failure indication.</P
6981 >9.5. \\MAILSLOT\NET\NTLOGON</H2
6987 >Note: mailslots will contain a response mailslot, to which the response
6988 should be sent. the target NetBIOS name is REQUEST_NAME<20>, where
6989 REQUEST_NAME is the name of the machine that sent the request.</I
6999 >9.5.1. Query for PDC</H3
7005 >Note: NTversion, LMNTtoken, LM20token in response are the same as those given in the request.</I
7015 >9.5.1.1. Request</H4
7019 CLASS="VARIABLELIST"
7025 >0x0007 - Query for PDC</P
7037 >response mailslot</P
7043 >padding to 2-byte align with start of mailslot.</P
7079 >9.5.1.2. Response</H4
7083 CLASS="VARIABLELIST"
7089 >0x000A - Respose to Query for PDC</P
7095 >machine name (in uppercase)</P
7101 >padding to 2-byte align with start of mailslot.</P
7119 >NTversion (same as received in request)</P
7125 >LMNTtoken (same as received in request)</P
7131 >LM20token (same as received in request)</P
7144 >9.5.2. SAM Logon</H3
7150 >Note: machine name in response is preceded by two '\' characters.</I
7158 >Note: NTversion, LMNTtoken, LM20token in response are the same as those given in the request.</I
7166 >Note: user name in the response is presumably the same as that in the request.</I
7176 >9.5.2.1. Request</H4
7180 CLASS="VARIABLELIST"
7186 >0x0012 - SAM Logon</P
7210 >response mailslot</P
7216 >alloweable account</P
7228 >domain SID, of sid_size bytes.</P
7234 >???? padding to 4? 2? -byte align with start of mailslot.</P
7264 >9.5.2.2. Response</H4
7268 CLASS="VARIABLELIST"
7274 >0x0013 - Response to SAM Logon</P
7286 >user name - workstation trust account</P
7324 >9.6. SRVSVC Transact Named Pipe</H2
7326 >Defines for this pipe, identifying the query are:</P
7330 CLASS="VARIABLELIST"
7339 >Net Server Get Info</DT
7353 >9.6.1. Net Share Enum</H3
7359 >Note: share level and switch value in the response are presumably the same as those in the request.</I
7367 >Note: cifsrap2.txt (section 5) may be of limited assistance here.</I
7377 >9.6.1.1. Request</H4
7381 CLASS="VARIABLELIST"
7387 >pointer (to server name?)</P
7399 >padding to get unicode string 4-byte aligned with the start of the SMB header.</P
7417 >pointer to SHARE_INFO_1_CTR</P
7420 >SHARE_INFO_1_CTR</DT
7423 >share info with 0 entries</P
7429 >preferred maximum length (0xffff ffff)</P
7441 >9.6.1.2. Response</H4
7445 CLASS="VARIABLELIST"
7463 >pointer to SHARE_INFO_1_CTR</P
7466 >SHARE_INFO_1_CTR</DT
7469 >share info (only added if share info ptr is non-zero)</P
7474 >return 0 - indicates success</P
7484 >9.6.2. Net Server Get Info</H3
7490 >Note: level is the same value as in the request.</I
7500 >9.6.2.1. Request</H4
7504 CLASS="VARIABLELIST"
7528 >9.6.2.2. Response</H4
7532 CLASS="VARIABLELIST"
7544 >pointer to SERVER_INFO_101</P
7547 >SERVER_INFO_101</DT
7550 >server info (only added if server info ptr is non-zero)</P
7555 >return 0 - indicates success</P
7566 >9.7. Cryptographic side of NT Domain Authentication</H2
7574 >9.7.1. Definitions</H3
7578 CLASS="VARIABLELIST"
7584 >Intel byte ordered addition of corresponding 4 byte words in arrays A1 and A2</P
7590 >DES ECB encryption of 8 byte data D using 7 byte key K</P
7608 >md4(machine_password) == md4(lsadump $machine.acc) ==
7609 pwdump(machine$) (initially) == md4(lmowf(unicode(machine)))</P
7612 >ARC4(K,Lk,D,Ld)</DT
7615 >ARC4 encryption of data D of length Ld with key K of length Lk</P
7621 >subset of v from bytes m to n, optionally padded with zeroes to length l</P
7627 >E(K[7..7,7],E(K[0..6],D)) computes a credential</P
7633 >4 byte current time</P
7639 >8 byte client and server challenges Rc,Rs: 8 byte client and server credentials</P
7651 >9.7.2. Protocol</H3
7653 >C->S ReqChal,Cc S->C Cs</P
7655 >C & S compute session key Ks = E(PW[9..15],E(PW[0..6],Add(Cc,Cs)))</P
7657 >C: Rc = Cred(Ks,Cc) C->S Authenticate,Rc S: Rs = Cred(Ks,Cs),
7658 assert(Rc == Cred(Ks,Cc)) S->C Rs C: assert(Rs == Cred(Ks,Cs))</P
7660 >On joining the domain the client will optionally attempt to change its
7661 password and the domain controller may refuse to update it depending
7662 on registry settings. This will also occur weekly afterwards.</P
7664 >C: Tc = Time(), Rc' = Cred(Ks,Rc+Tc) C->S ServerPasswordSet,Rc',Tc,
7665 arc4(Ks[0..7,16],lmowf(randompassword()) C: Rc = Cred(Ks,Rc+Tc+1) S:
7666 assert(Rc' == Cred(Ks,Rc+Tc)), Ts = Time() S: Rs' = Cred(Ks,Rs+Tc+1)
7667 S->C Rs',Ts C: assert(Rs' == Cred(Ks,Rs+Tc+1)) S: Rs = Rs'</P
7669 >User: U with password P wishes to login to the domain (incidental data
7670 such as workstation and domain omitted)</P
7672 >C: Tc = Time(), Rc' = Cred(Ks,Rc+Tc) C->S NetLogonSamLogon,Rc',Tc,U,
7673 arc4(Ks[0..7,16],16,ntowf(P),16), arc4(Ks[0..7,16],16,lmowf(P),16) S:
7674 assert(Rc' == Cred(Ks,Rc+Tc)) assert(passwords match those in SAM) S:
7677 >S->C Cred(Ks,Cred(Ks,Rc+Tc+1)),userinfo(logon script,UID,SIDs,etc) C:
7678 assert(Rs == Cred(Ks,Cred(Rc+Tc+1)) C: Rc = Cred(Ks,Rc+Tc+1)</P
7687 >9.7.3. Comments</H3
7689 >On first joining the domain the session key could be computed by
7690 anyone listening in on the network as the machine password has a well
7691 known value. Until the machine is rebooted it will use this session
7692 key to encrypt NT and LM one way functions of passwords which are
7693 password equivalents. Any user who logs in before the machine has been
7694 rebooted a second time will have their password equivalent exposed. Of
7695 course the new machine password is exposed at this time anyway.</P
7697 >None of the returned user info such as logon script, profile path and
7698 SIDs *appear* to be protected by anything other than the TCP checksum.</P
7700 >The server time stamps appear to be ignored.</P
7702 >The client sends a ReturnAuthenticator in the SamLogon request which I
7703 can't find a use for. However its time is used as the timestamp
7704 returned by the server.</P
7706 >The password OWFs should NOT be sent over the network reversibly
7707 encrypted. They should be sent using ARC4(Ks,md4(owf)) with the server
7708 computing the same function using the owf values in the SAM.</P
7718 >9.8. SIDs and RIDs</H2
7720 >SIDs and RIDs are well documented elsewhere.</P
7722 >A SID is an NT Security ID (see DOM_SID structure). They are of the form:</P
7730 >revision-NN-SubAuth1-SubAuth2-SubAuth3... </TD
7734 >revision-0xNNNNNNNNNNNN-SubAuth1-SubAuth2-SubAuth3...</TD
7741 >currently, the SID revision is 1.
7742 The Sub-Authorities are known as Relative IDs (RIDs).</P
7750 >9.8.1. Well-known SIDs</H3
7758 >9.8.1.1. Universal well-known SIDs</H4
7762 CLASS="VARIABLELIST"
7783 >Creator Owner ID</DT
7789 >Creator Group ID</DT
7795 >Creator Owner Server ID</DT
7801 >Creator Group Server ID</DT
7807 >(Non-unique IDs)</DT
7822 >9.8.1.2. NT well-known SIDs</H4
7826 CLASS="VARIABLELIST"
7865 >AnonymousLogon(aka null logon session)</DT
7877 >ServerLogon(aka domain controller account)</DT
7889 >(NT non-unique IDs)</DT
7895 >(Built-in domain)</DT
7911 >9.8.2. Well-known RIDS</H3
7913 >A RID is a sub-authority value, as part of either a SID, or in the case
7914 of Group RIDs, part of the DOM_GID structure, in the USER_INFO_1
7915 structure, in the LSA SAM Logon response.</P
7923 >9.8.2.1. Well-known RID users</H4
7927 >DOMAIN_USER_RID_ADMIN</P
7939 >DOMAIN_USER_RID_GUEST</P
7956 >9.8.2.2. Well-known RID groups</H4
7960 > DOMAIN_GROUP_RID_ADMINS</P
7972 > DOMAIN_GROUP_RID_USERS</P
7984 > DOMAIN_GROUP_RID_GUESTS</P
8001 >9.8.2.3. Well-known RID aliases</H4
8005 > DOMAIN_ALIAS_RID_ADMINS</P
8017 > DOMAIN_ALIAS_RID_USERS</P
8029 > DOMAIN_ALIAS_RID_GUESTS</P
8041 > DOMAIN_ALIAS_RID_POWER_USERS</P
8053 > DOMAIN_ALIAS_RID_ACCOUNT_OPS</P
8065 > DOMAIN_ALIAS_RID_SYSTEM_OPS</P
8077 > DOMAIN_ALIAS_RID_PRINT_OPS</P
8089 > DOMAIN_ALIAS_RID_BACKUP_OPS</P
8101 > DOMAIN_ALIAS_RID_REPLICATOR</P
8120 >Chapter 10. Samba Printing Internals</H1
8130 >The purpose of this document is to provide some insight into
8131 Samba's printing functionality and also to describe the semantics
8132 of certain features of Windows client printing.</P
8141 >10.2. Printing Interface to Various Back ends</H2
8143 >Samba uses a table of function pointers to seven functions. The
8144 function prototypes are defined in the <TT
8147 > structure declared
8157 >retrieve the contents of a print queue</P
8161 >pause the print queue</P
8165 >resume a paused print queue</P
8169 >delete a job from the queue</P
8173 >pause a job in the print queue</P
8177 >result a paused print job in the queue</P
8181 >submit a job to the print queue</P
8185 >Currently there are only two printing back end implementations
8192 >a generic set of functions for working with standard UNIX
8193 printing subsystems</P
8197 >a set of CUPS specific functions (this is only enabled if
8198 the CUPS libraries were located at compile time).</P
8209 >10.3. Print Queue TDB's</H2
8211 >Samba provides periodic caching of the output from the "lpq command"
8212 for performance reasons. This cache time is configurable in seconds.
8213 Obviously the longer the cache time the less often smbd will be
8214 required to exec a copy of lpq. However, the accuracy of the print
8215 queue contents displayed to clients will be diminished as well.</P
8217 >The list of currently opened print queue TDB's can be found
8218 be examining the list of tdb_print_db structures ( see print_db_head
8219 in printing.c ). A queue TDB is opened using the wrapper function
8220 printing.c:get_print_db_byname(). The function ensures that smbd
8221 does not open more than MAX_PRINT_DBS_OPEN in an effort to prevent
8222 a large print server from exhausting all available file descriptors.
8223 If the number of open queue TDB's exceeds the MAX_PRINT_DBS_OPEN
8224 limit, smbd falls back to a most recently used algorithm for maintaining
8225 a list of open TDB's.</P
8227 >There are two ways in which a a print job can be entered into
8228 a print queue's TDB. The first is to submit the job from a Windows
8229 client which will insert the job information directly into the TDB.
8230 The second method is to have the print job picked up by executing the
8234 CLASS="PROGRAMLISTING"
8235 >/* included from printing.h */
8237 pid_t pid; /* which process launched the job */
8238 int sysjob; /* the system (lp) job number */
8239 int fd; /* file descriptor of open file if open */
8240 time_t starttime; /* when the job started spooling */
8241 int status; /* the status of this job */
8242 size_t size; /* the size of the job so far */
8243 int page_count; /* then number of pages so far */
8244 BOOL spooled; /* has it been sent to the spooler yet? */
8245 BOOL smbjob; /* set if the job is a SMB job */
8246 fstring filename; /* the filename used to spool the file */
8247 fstring jobname; /* the job name given to us by the client */
8248 fstring user; /* the user who started the job */
8249 fstring queuename; /* service number of printer for this job */
8250 NT_DEVICEMODE *nt_devmode;
8254 >The current manifestation of the printjob structure contains a field
8255 for the UNIX job id returned from the "lpq command" and a Windows job
8256 ID (32-bit bounded by PRINT_MAX_JOBID). When a print job is returned
8257 by the "lpq command" that does not match an existing job in the queue's
8258 TDB, a 32-bit job ID above the <*vance doesn't know what word is missing here*> is generating by adding UNIX_JOB_START to
8259 the id reported by lpq.</P
8261 >In order to match a 32-bit Windows jobid onto a 16-bit lanman print job
8262 id, smbd uses an in memory TDB to match the former to a number appropriate
8263 for old lanman clients.</P
8265 >When updating a print queue, smbd will perform the following
8266 steps ( refer to <TT
8268 >print.c:print_queue_update()</TT
8276 >Check to see if another smbd is currently in
8277 the process of updating the queue contents by checking the pid
8287 If so, then do not update the TDB.</P
8291 >Lock the mutex entry in the TDB and store our own pid.
8292 Check that this succeeded, else fail.</P
8296 >Store the updated time stamp for the new cache
8301 >Retrieve the queue listing via "lpq command"</P
8306 CLASS="PROGRAMLISTING"
8307 > foreach job in the queue
8309 if the job is a UNIX job, create a new entry;
8310 if the job has a Windows based jobid, then
8312 Lookup the record by the jobid;
8313 if the lookup failed, then
8314 treat it as a UNIX job;
8316 update the job status only
8323 >Delete any jobs in the TDB that are not
8324 in the in the lpq listing</P
8328 >Store the print queue status in the TDB</P
8332 >update the cache time stamp again</P
8336 >Note that it is the contents of this TDB that is returned to Windows
8337 clients and not the actual listing from the "lpq command".</P
8339 >The NT_DEVICEMODE stored as part of the printjob structure is used to
8340 store a pointer to a non-default DeviceMode associated with the print
8341 job. The pointer will be non-null when the client included a Device
8342 Mode in the OpenPrinterEx() call and subsequently submitted a job for
8343 printing on that same handle. If the client did not include a Device
8344 Mode in the OpenPrinterEx() request, the nt_devmode field is NULL
8345 and the job has the printer's device mode associated with it by default.</P
8347 >Only non-default Device Mode are stored with print jobs in the print
8348 queue TDB. Otherwise, the Device Mode is obtained from the printer
8349 object when the client issues a GetJob(level == 2) request.</P
8358 >10.4. ChangeID & Client Caching of Printer Information</H2
8360 >[To be filled in later]</P
8369 >10.5. Windows NT/2K Printer Change Notify</H2
8371 >When working with Windows NT+ clients, it is possible for a
8372 print server to use RPC to send asynchronous change notification
8373 events to clients for certain printer and print job attributes.
8374 This can be useful when the client needs to know that a new
8375 job has been added to the queue for a given printer or that the
8376 driver for a printer has been changed. Note that this is done
8377 entirely orthogonal to cache updates based on a new ChangeID for
8378 a printer object.</P
8380 >The basic set of RPC's used to implement change notification are</P
8386 >RemoteFindFirstPrinterChangeNotifyEx ( RFFPCN )</P
8390 >RemoteFindNextPrinterChangeNotifyEx ( RFNPCN )</P
8394 >FindClosePrinterChangeNotify( FCPCN )</P
8398 >ReplyOpenPrinter</P
8402 >ReplyClosePrinter</P
8406 >RouteRefreshPrinterChangeNotify ( RRPCN )</P
8410 >One additional RPC is available to a server, but is never used by the
8411 Windows spooler service:</P
8417 >RouteReplyPrinter()</P
8421 >The opnum for all of these RPC's are defined in include/rpc_spoolss.h</P
8423 >Windows NT print servers use a bizarre method of sending print
8424 notification event to clients. The process of registering a new change
8425 notification handle is as follows. The 'C' is for client and the
8426 'S' is for server. All error conditions have been eliminated.</P
8429 CLASS="PROGRAMLISTING"
8430 >C: Obtain handle to printer or to the printer
8431 server via the standard OpenPrinterEx() call.
8432 S: Respond with a valid handle to object
8434 C: Send a RFFPCN request with the previously obtained
8435 handle with either (a) set of flags for change events
8436 to monitor, or (b) a PRINTER_NOTIFY_OPTIONS structure
8437 containing the event information to monitor. The windows
8438 spooler has only been observed to use (b).
8439 S: The <* another missing word*> opens a new TCP session to the client (thus requiring
8440 all print clients to be CIFS servers as well) and sends
8441 a ReplyOpenPrinter() request to the client.
8442 C: The client responds with a printer handle that can be used to
8443 send event notification messages.
8444 S: The server replies success to the RFFPCN request.
8446 C: The windows spooler follows the RFFPCN with a RFNPCN
8447 request to fetch the current values of all monitored
8449 S: The server replies with an array SPOOL_NOTIFY_INFO_DATA
8450 structures (contained in a SPOOL_NOTIFY_INFO structure).
8452 C: If the change notification handle is ever released by the
8453 client via a FCPCN request, the server sends a ReplyClosePrinter()
8454 request back to the client first. However a request of this
8455 nature from the client is often an indication that the previous
8456 notification event was not marshalled correctly by the server
8457 or a piece of data was wrong.
8458 S: The server closes the internal change notification handle
8459 (POLICY_HND) and does not send any further change notification
8460 events to the client for that printer or job.</PRE
8463 >The current list of notification events supported by Samba can be
8464 found by examining the internal tables in srv_spoolss_nt.c</P
8470 >printer_notify_table[]</P
8474 >job_notify_table[]</P
8478 >When an event occurs that could be monitored, smbd sends a message
8479 to itself about the change. The list of events to be transmitted
8480 are queued by the smbd process sending the message to prevent an
8481 overload of TDB usage and the internal message is sent during smbd's
8482 idle loop (refer to printing/notify.c and the functions
8483 send_spoolss_notify2_msg() and print_notify_send_messages() ).</P
8485 >The decision of whether or not the change is to be sent to connected
8486 clients is made by the routine which actually sends the notification.
8487 ( refer to srv_spoolss_nt.c:recieve_notify2_message() ).</P
8489 >Because it possible to receive a listing of multiple changes for
8490 multiple printers, the notification events must be split into
8491 categories by the printer name. This makes it possible to group
8492 multiple change events to be sent in a single RPC according to the
8493 printer handle obtained via a ReplyOpenPrinter().</P
8495 >The actual change notification is performed using the RRPCN request
8496 RPC. This packet contains</P
8502 >the printer handle registered with the
8503 client's spooler on which the change occurred</P
8507 >The change_low value which was sent as part
8508 of the last RFNPCN request from the client</P
8512 >The SPOOL_NOTIFY_INFO container with the event
8519 >SPOOL_NOTIFY_INFO</TT
8526 >the version and flags field are predefined
8527 and should not be changed</P
8531 >The count field is the number of entries
8532 in the SPOOL_NOTIFY_INFO_DATA array</P
8538 >SPOOL_NOTIFY_INFO_DATA</TT
8539 > entries contain:</P
8545 >The type defines whether or not this event
8546 is for a printer or a print job</P
8550 >The field is the flag identifying the event</P
8554 >the notify_data union contains the new valuie of the
8559 >The enc_type defines the size of the structure for marshalling
8560 and unmarshalling</P
8564 >(a) the id must be 0 for a printer event on a printer handle.
8565 (b) the id must be the job id for an event on a printer job
8566 (c) the id must be the matching number of the printer index used
8567 in the response packet to the RFNPCN when using a print server
8568 handle for notification. Samba currently uses the snum of
8569 the printer for this which can break if the list of services
8570 has been modified since the notification handle was registered.</P
8574 >The size is either (a) the string length in UNICODE for strings,
8575 (b) the size in bytes of the security descriptor, or (c) 0 for
8587 >Chapter 11. Samba WINS Internals</H1
8595 >11.1. WINS Failover</H2
8597 >The current Samba codebase possesses the capability to use groups of WINS
8598 servers that share a common namespace for NetBIOS name registration and
8599 resolution. The formal parameter syntax is</P
8602 CLASS="PROGRAMLISTING"
8603 > WINS_SERVER_PARAM = SERVER [ SEPARATOR SERVER_LIST ]
8604 WINS_SERVER_PARAM = "wins server"
8606 ADDR = ip_addr | fqdn
8608 SEPARATOR = comma | \s+
8609 SERVER_LIST = SERVER [ SEPARATOR SERVER_LIST ]</PRE
8612 >A simple example of a valid wins server setting is</P
8615 CLASS="PROGRAMLISTING"
8617 wins server = 192.168.1.2 192.168.1.3</PRE
8620 >In the event that no TAG is defined in for a SERVER in the list, smbd assigns a default
8621 TAG of "*". A TAG is used to group servers of a shared NetBIOS namespace together. Upon
8622 startup, nmbd will attempt to register the netbios name value with one server in each
8625 >An example using tags to group WINS servers together is show here. Note that the use of
8626 interface names in the tags is only by convention and is not a technical requirement.</P
8629 CLASS="PROGRAMLISTING"
8631 wins server = 192.168.1.2:eth0 192.168.1.3:eth0 192.168.2.2:eth1</PRE
8634 >Using this configuration, nmbd would attempt to register the server's NetBIOS name
8635 with one WINS server in each group. Because the "eth0" group has two servers, the
8636 second server would only be used when a registration (or resolution) request to
8637 the first server in that group timed out.</P
8639 >NetBIOS name resolution follows a similar pattern as name registration. When resolving
8640 a NetBIOS name via WINS, smbd and other Samba programs will attempt to query a single WINS
8641 server in a tagged group until either a positive response is obtained at least once or
8642 until a server from every tagged group has responded negatively to the name query request.
8643 If a timeout occurs when querying a specific WINS server, that server is marked as down to
8644 prevent further timeouts and the next server in the WINS group is contacted. Once marked as
8645 dead, Samba will not attempt to contact that server for name registration/resolution queries
8646 for a period of 10 minutes.</P
8655 >Chapter 12. The Upcoming SAM System</H1
8663 >12.1. Security in the 'new SAM'</H2
8665 >One of the biggest problems with passdb is it's implementation of
8666 'security'. Access control is on a 'are you root at the moment' basis,
8667 and it has no concept of NT ACLs. Things like ldapsam had to add
8668 'magic' 'are you root' checks.</P
8670 >We took this very seriously when we started work, and the new structure
8671 is designed with this in mind, from the ground up. Each call to the SAM
8672 has a NT_TOKEN and (if relevant) an 'access desired'. This is either
8673 provided as a parameter, or implicitly supplied by the object being
8676 >For example, when you call </P
8678 CLASS="PROGRAMLISTING"
8680 NTSTATUS sam_get_account_by_name(const SAM_CONTEXT *context, const
8681 NT_USER_TOKEN *access_token, uint32 access_desired, const char *domain,
8682 const char *name, SAM_ACCOUNT_HANDLE **account)</PRE
8684 >The context can be NULL (and is used to allow import/export by setting
8685 up 2 contexts, and allowing calls on both simultaneously)</P
8687 >The access token *must* be specified. Normally the user's token out of
8688 current_user, this can also be a global 'system' context.</P
8690 >The access desired is as per the ACL, for passing to the seaccess stuff.</P
8692 >The domain/username are standard. Even if we only have one domain,
8693 keeping this ensures that we don't get 'unqualified' usernames (same
8694 problem as we had with unqualified SIDs).</P
8696 >We return a 'handle'. This is opaque to the rest of Samba, but is
8697 operated on by get/set routines, all of which return NTSTATUS.</P
8699 >The access checking is done by the SAM module. The reason it is not
8700 done 'above' the interface is to ensure a 'choke point'. I put a lot of
8701 effort into the auth subsystem to ensure we never 'accidentally' forgot
8702 to check for null passwords, missed a restriction etc. I intend the SAM
8703 to be written with the same caution.</P
8705 >The reason the access checking is not handled by the interface itself is
8706 due to the different implementations it make take on. For example, on
8707 ADS, you cannot set a password over a non-SSL connection. Other
8708 backends may have similar requirements - we need to leave this policy up
8709 to the modules. They will naturally have access to 'helper' procedures
8710 and good examples to avoid mishaps.</P
8712 >(Furthermore, some backends my actually chose to push the whole ACL
8713 issue to the remote server, and - assuming ldap for this example - bind
8714 as the user directly)</P
8716 >Each returned handle has an internal 'access permitted', which allows
8717 the 'get' and 'set' routines to return 'ACCESS_DENIED' for things that
8718 were not able to be retrieved from the backend. This removes the need
8719 to specify the NT_TOKEN on every operation, and allows for 'object not
8720 present' to be easily distinguished from 'access denied'.</P
8722 >When you 'set' an object (calling sam_update_account) the internal
8723 details are again used. Each change that has been made to the object
8724 has been flagged, so as to avoid race conditions (on unmodified
8725 components) and to avoid violating any extra ACL requirements on the
8726 actual data store (like the LDAP server).</P
8728 >Finally, we have generic get_sec_desc() and set_sec_desc() routines to
8729 allow external ACL manipulation. These do lookups based on SID.</P
8738 >12.2. Standalone from UNIX</H2
8740 >One of the primary tenants of the 'new SAM' is that it would not attempt
8741 to deal with 'what unix id for that'. This would be left to the 'SMS'
8742 (Sid Mapping System') or SID farm, and probably administered via
8743 winbind. We have had constructive discussion on how 'basic' unix
8744 accounts like 'root' would be handled, and we think this can work.
8745 Accounts not preexisting in unix would be served up via winbind.</P
8747 >This is an *optional* part, and my preferred end-game. We have a fare
8748 way to go before things like winbind up to it however.</P
8757 >12.3. Handles and Races in the new SAM</H2
8759 >One of the things that the 'new SAM' work has tried to face is both
8760 compatibility with existing code, and a closer alignment to the SAMR
8761 interface. I consider SAMR to be a 'primary customer' to the this work,
8762 because if we get alignment with that wrong, things get more, rather
8763 than less complex. Also, most other parts of Samba are much more
8764 flexible with what they can allow.</P
8766 >In any case, that was a decision taken as to how the general design
8767 would progress. BTW, my understanding of SAMR may be completely flawed.</P
8769 >One of the most race-prone areas of the new code is the conflicting
8770 update problem. We have taken two approaches: </P
8776 >'Not conflicting' conflicts. Due to the way usrmgr operates, it will
8777 open a user, display all the properties and *save* them all, even if you
8778 don't change any.</P
8780 >For this, see what I've done in rpc_server/srv_samr_util.c. I intend
8781 to take this one step further, and operate on the 'handle' that the
8782 values were read from. This should mean that we only update things that
8783 have *really* changed.</P
8787 >'conflicting' updates: Currently we don't deal with this (in passdb
8788 or the new sam stuff), but the design is sufficiently flexible to 'deny'
8789 a second update. I don't foresee locking records however.</P
8808 >12.4.1. Application</H3
8810 >This is where smbd, samtest and whatever end-user replacement we have
8811 for pdbedit sits. They use only the SAM interface, and do not get
8812 'special knowledge' of what is below them.</P
8821 >12.4.2. SAM Interface</H3
8823 >This level 'owns' the various handle structures, the get/set routines on
8824 those structures and provides the public interface. The application
8825 layer may initialize a 'context' to be passed to all interface routines,
8826 else a default, self-initialising context will be supplied. This layser
8827 finds the appropriate backend module for the task, and tries very hard
8828 not to need to much 'knowledge'. It should just provide the required
8829 abstraction to the modules below, and arrange for their initial loading.</P
8831 >We could possibly add ACL checking at this layer, to avoid discrepancies
8832 in implementation modules.</P
8841 >12.4.3. SAM Modules</H3
8843 >These do not communicate with the application directly, only by setting
8844 values in the handles, and receiving requests from the interface. These
8845 modules are responsible for translating values from the handle's
8846 .private into (say) an LDAP modification list. The module is expected
8847 to 'know' things like it's own domain SID, domain name, and any other
8848 state attached to the SAM. Simpler modules may call back to some helper
8859 >12.5. SAM Modules</H2
8867 >12.5.1. Special Module: sam_passdb</H3
8869 >In order for there to be a smooth transition, kai is writing a module
8870 that reads existing passdb backends, and translates them into SAM
8871 replies. (Also pulling data from the account policy DB etc). We also
8872 intend to write a module that does the reverse - gives the SAM a passdb
8882 >12.5.2. sam_ads</H3
8884 >This is the first of the SAM modules to be committed to the tree -
8885 mainly because I needed to coordinate work with metze (who authored most
8886 of it). This module aims to use Samba's libads code to provide an
8887 Active Directory LDAP client, suitable for use on a mixed-mode DC.
8888 While it is currently being tested against Win2k servers (with a
8889 password in the smb.conf file) it is expected to eventually use a
8890 (possibly modified) OpenLDAP server. We hope that this will assist in
8891 the construction of an Samba AD DC.</P
8893 >We also intend to construct a Samba 2.2/3.0 compatible ldap module,
8894 again using libads code.</P
8904 >12.6. Memory Management</H2
8907 The 'new SAM' development effort also concerned itself with getting a
8908 sane implementation of memory management. It was decided that we would
8909 be (as much as possible) talloc based, using an 'internal talloc
8910 context' on many objects. That is, the creation of an object would
8911 initiate it's own internal talloc context, and this would be used for
8912 all operations on that object. Much of this is already implemented in
8913 passdb. Also, like passdb, it will be possible to specify that some
8914 object actually be created on a specified context. </P
8916 >Memory management is important here because the APIs in the 'new SAM' do
8917 not use 'pdb_init()' or an equivalent. They always allocate new
8918 objects. Enumeration's are slightly different, and occur on a supplied
8919 context that 'owns' the entire list, rather than per-element. (the
8920 enumeration functions return an array of all elements - not full handles
8921 just basic (and public) info) Likewise for things that fill in a char
8927 CLASS="PROGRAMLISTING"
8928 >NTSTATUS sam_lookup_sid(const SAM_CONTEXT *context, const NT_USER_TOKEN
8929 *access_token, TALLOC_CTX *mem_ctx, const DOM_SID *sid, char **name,
8933 >Takes a context to allocate the 'name' on, while:</P
8936 CLASS="PROGRAMLISTING"
8937 >NTSTATUS sam_get_account_by_sid(const SAM_CONTEXT *context, const
8938 NT_USER_TOKEN *access_token, uint32 access_desired, const DOM_SID
8939 *accountsid, SAM_ACCOUNT_HANDLE **account)</PRE
8942 >Allocates a handle and stores the allocation context on that handle.</P
8944 >I think that the following:</P
8947 CLASS="PROGRAMLISTING"
8948 >NTSTATUS sam_enum_accounts(const SAM_CONTEXT *context, const
8949 NT_USER_TOKEN *access_token, const DOM_SID *domainsid, uint16 acct_ctrl,
8950 int32 *account_count, SAM_ACCOUNT_ENUM **accounts)</PRE
8962 >Testing is vital in any piece of software, and Samba is certainly no
8963 exception. In designing this new subsystem, we have taken care to ensure
8964 it is easily tested, independent of outside protocols.</P
8966 >To this end, Jelmer has constructed 'samtest'. </P
8968 >This utility (see torture/samtest.c) is structured like rpcclient, but
8969 instead operates on the SAM subsystem. It creates a 'custom' SAM
8970 context, that may be distinct from the default values used by the rest
8971 of the system, and can load a separate configuration file. </P
8973 >A small number of commands are currently implemented, but these have
8974 already proved vital in testing. I expect SAM module authors will find
8975 it particularly valuable.</P
8988 CLASS="PROGRAMLISTING"
8989 >> context ads:ldap://192.168.1.96</PRE
8991 (this loads a new context, using the new ADS module. The parameter is
8992 the 'location' of the ldap server)</P
8995 CLASS="PROGRAMLISTING"
8996 >> lookup_name DOMAIN abartlet</PRE
9000 >Because the 'new SAM' is NT ACL based, there will be a command to
9001 specify an arbitrary NT ACL, but for now it uses 'system' by default.</P
9010 >Chapter 13. LanMan and NT Password Encryption</H1
9018 >13.1. Introduction</H2
9020 >With the development of LanManager and Windows NT
9021 compatible password encryption for Samba, it is now able
9022 to validate user connections in exactly the same way as
9023 a LanManager or Windows NT server.</P
9025 >This document describes how the SMB password encryption
9026 algorithm works and what issues there are in choosing whether
9027 you want to use it. You should read it carefully, especially
9028 the part about security and the "PROS and CONS" section.</P
9037 >13.2. How does it work?</H2
9039 >LanManager encryption is somewhat similar to UNIX
9040 password encryption. The server uses a file containing a
9041 hashed value of a user's password. This is created by taking
9042 the user's plaintext password, capitalising it, and either
9043 truncating to 14 bytes or padding to 14 bytes with null bytes.
9044 This 14 byte value is used as two 56 bit DES keys to encrypt
9045 a 'magic' eight byte value, forming a 16 byte value which is
9046 stored by the server and client. Let this value be known as
9047 the "hashed password".</P
9049 >Windows NT encryption is a higher quality mechanism,
9050 consisting of doing an MD4 hash on a Unicode version of the user's
9051 password. This also produces a 16 byte hash value that is
9054 >When a client (LanManager, Windows for WorkGroups, Windows
9055 95 or Windows NT) wishes to mount a Samba drive (or use a Samba
9056 resource), it first requests a connection and negotiates the
9057 protocol that the client and server will use. In the reply to this
9058 request the Samba server generates and appends an 8 byte, random
9059 value - this is stored in the Samba server after the reply is sent
9060 and is known as the "challenge". The challenge is different for
9061 every client connection.</P
9063 >The client then uses the hashed password (16 byte values
9064 described above), appended with 5 null bytes, as three 56 bit
9065 DES keys, each of which is used to encrypt the challenge 8 byte
9066 value, forming a 24 byte value known as the "response".</P
9068 >In the SMB call SMBsessionsetupX (when user level security
9069 is selected) or the call SMBtconX (when share level security is
9070 selected), the 24 byte response is returned by the client to the
9071 Samba server. For Windows NT protocol levels the above calculation
9072 is done on both hashes of the user's password and both responses are
9073 returned in the SMB call, giving two 24 byte values.</P
9075 >The Samba server then reproduces the above calculation, using
9076 its own stored value of the 16 byte hashed password (read from the
9080 > file - described later) and the challenge
9081 value that it kept from the negotiate protocol reply. It then checks
9082 to see if the 24 byte value it calculates matches the 24 byte value
9083 returned to it from the client.</P
9085 >If these values match exactly, then the client knew the
9086 correct password (or the 16 byte hashed value - see security note
9087 below) and is thus allowed access. If not, then the client did not
9088 know the correct password and is denied access.</P
9090 >Note that the Samba server never knows or stores the cleartext
9091 of the user's password - just the 16 byte hashed values derived from
9092 it. Also note that the cleartext password or 16 byte hashed values
9093 are never transmitted over the network - thus increasing security.</P
9103 NAME="SMBPASSWDFILEFORMAT"
9105 >The smbpasswd file</H2
9107 >In order for Samba to participate in the above protocol
9108 it must be able to look up the 16 byte hashed values given a user name.
9109 Unfortunately, as the UNIX password value is also a one way hash
9110 function (ie. it is impossible to retrieve the cleartext of the user's
9111 password given the UNIX hash of it), a separate password file
9112 containing this 16 byte value must be kept. To minimise problems with
9113 these two password files, getting out of sync, the UNIX <TT
9123 >, is provided to generate
9124 a smbpasswd file from a UNIX <TT
9130 >To generate the smbpasswd file from your <TT
9134 > file use the following command :</P
9142 >cat /etc/passwd | mksmbpasswd.sh
9143 > /usr/local/samba/private/smbpasswd</B
9147 >If you are running on a system that uses NIS, use</P
9155 >ypcat passwd | mksmbpasswd.sh
9156 > /usr/local/samba/private/smbpasswd</B
9163 > program is found in
9164 the Samba source directory. By default, the smbpasswd file is
9169 >/usr/local/samba/private/smbpasswd</TT
9172 >The owner of the <TT
9174 >/usr/local/samba/private/</TT
9176 directory should be set to root, and the permissions on it should
9179 >chmod 500 /usr/local/samba/private</B
9183 >Likewise, the smbpasswd file inside the private directory should
9184 be owned by root and the permissions on is should be set to 0600
9187 >chmod 600 smbpasswd</B
9190 >The format of the smbpasswd file is (The line has been
9191 wrapped here. It should appear as one entry per line in
9192 your smbpasswd file.)</P
9195 CLASS="PROGRAMLISTING"
9196 >username:uid:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:
9197 [Account type]:LCT-<last-change-time>:Long name
9201 >Although only the <TT
9215 > XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</I
9226 > last-change-time</I
9228 > sections are significant
9229 and are looked at in the Samba code.</P
9237 > important that there by 32
9238 'X' characters between the two ':' characters in the XXX sections -
9239 the smbpasswd and Samba code will fail to validate any entries that
9240 do not have 32 characters between ':' characters. The first XXX
9241 section is for the Lanman password hash, the second is for the
9242 Windows NT version.</P
9244 >When the password file is created all users have password entries
9245 consisting of 32 'X' characters. By default this disallows any access
9246 as this user. When a user has a password set, the 'X' characters change
9247 to 32 ascii hexadecimal digits (0-9, A-F). These are an ascii
9248 representation of the 16 byte hashed value of a user's password.</P
9250 >To set a user to have no password (not recommended), edit the file
9251 using vi, and replace the first 11 characters with the ascii text
9255 > (minus the quotes).</P
9257 >For example, to clear the password for user bob, his smbpasswd file
9258 entry would look like :</P
9261 CLASS="PROGRAMLISTING"
9262 > bob:100:NO PASSWORDXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:[U ]:LCT-00000000:Bob's full name:/bobhome:/bobshell
9266 >If you are allowing users to use the smbpasswd command to set
9267 their own passwords, you may want to give users NO PASSWORD initially
9268 so they do not have to enter a previous password when changing to their
9269 new password (not recommended). In order for you to allow this the
9273 > program must be able to connect to the
9277 > daemon as that user with no password. Enable this
9278 by adding the line :</P
9282 >null passwords = yes</B
9285 >to the [global] section of the smb.conf file (this is why
9286 the above scenario is not recommended). Preferably, allocate your
9287 users a default password to begin with, so you do not have
9288 to enable this on your server.</P
9296 >This file should be protected very
9297 carefully. Anyone with access to this file can (with enough knowledge of
9298 the protocols) gain access to your SMB server. The file is thus more
9299 sensitive than a normal unix <TT