17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
22113f4232Sakaplan
23113f4232Sakaplan /*
24113f4232Sakaplan * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25113f4232Sakaplan * Use is subject to license terms.
26113f4232Sakaplan */
27113f4232Sakaplan
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * lssmb.c: Contains all code specific to the MS-NET file server.
337c478bd9Sstevel@tonic-gate * Undef SMBSERVER to remove SMB support.
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include "lsparam.h"
437c478bd9Sstevel@tonic-gate #include "lssmbmsg.h"
447c478bd9Sstevel@tonic-gate #include "lsdbf.h"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #ifdef SMBSERVER
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Dlevel - Debug level for DEBUG((level, ... ) type calls
527c478bd9Sstevel@tonic-gate * Msnet - Who is logging this message (the SMB code is)
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #define Dlevel 3
567c478bd9Sstevel@tonic-gate #define Msnet "SMB parser:"
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate extern char *malloc();
597c478bd9Sstevel@tonic-gate char *bytes_to_ascii();
60113f4232Sakaplan void getword(char *addr, short *w);
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate * In the event of an error, it may be necessary to send a response to
647c478bd9Sstevel@tonic-gate * the remote node before closing the virtual circuit. The following
657c478bd9Sstevel@tonic-gate * is the return message that should be sent. (Initially, I am not
667c478bd9Sstevel@tonic-gate * bothering to send the response message; I am assuming that the
677c478bd9Sstevel@tonic-gate * MS-NET client will be able to figure out that things went wrong, but
687c478bd9Sstevel@tonic-gate * we may find that is not the case.
697c478bd9Sstevel@tonic-gate */
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate static unsigned char errbuf[] = {
727c478bd9Sstevel@tonic-gate /* NegProt Return */ 0xff, 'S', 'M', 'B', 0x72,
737c478bd9Sstevel@tonic-gate /* ERRSRV */ 0x2,
747c478bd9Sstevel@tonic-gate 0,
757c478bd9Sstevel@tonic-gate /* SMBerror */ 0x1, 0,
767c478bd9Sstevel@tonic-gate 0,
777c478bd9Sstevel@tonic-gate 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
787c478bd9Sstevel@tonic-gate 0, 0,
797c478bd9Sstevel@tonic-gate 0, 0,
807c478bd9Sstevel@tonic-gate 0, 0, 0, 0,
817c478bd9Sstevel@tonic-gate /* wcnt == 1 */ 1,
827c478bd9Sstevel@tonic-gate /* no dialects */ 0xff, 0xff,
837c478bd9Sstevel@tonic-gate 0, 0
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * s m b s e r v i c e
897c478bd9Sstevel@tonic-gate *
907c478bd9Sstevel@tonic-gate * Function called by listener process when it receives a connect
917c478bd9Sstevel@tonic-gate * request from a node that wants to talk Microsoft's MS-NET Core
927c478bd9Sstevel@tonic-gate * Protocol...the functions gets called after the listener forks.
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate
95113f4232Sakaplan void
smbservice(bp,bufsize,argv)967c478bd9Sstevel@tonic-gate smbservice(bp, bufsize, argv)
97113f4232Sakaplan char *bp; /* pointer to message buffer */
987c478bd9Sstevel@tonic-gate int bufsize; /* size of message */
997c478bd9Sstevel@tonic-gate char **argv; /* server arguments */
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate char *server = *argv; /* path of server */
1027c478bd9Sstevel@tonic-gate char logbuf[256];
103113f4232Sakaplan char **args;
104113f4232Sakaplan int i, m_size;
105113f4232Sakaplan int twos, nulls;
106113f4232Sakaplan char *p, *q;
1077c478bd9Sstevel@tonic-gate short size;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Is this really a correct negotiate protocol message?
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (*(bp+FSP_COM) != FSPnegprot){
114*2a8bcb4eSToomas Soome sprintf(logbuf, "%s: Bad Command Code, 0x%x",
1157c478bd9Sstevel@tonic-gate Msnet, *(bp+FSP_COM));
1167c478bd9Sstevel@tonic-gate goto badexit;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate * Are there exactly 0 argument words in the message?
1217c478bd9Sstevel@tonic-gate */
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if (*(bp+FSP_WCNT) != 0){
1247c478bd9Sstevel@tonic-gate sprintf(logbuf, "%s: Incorrect # of Parameter Words, 0x%x",
1257c478bd9Sstevel@tonic-gate Msnet, *(bp+FSP_WCNT));
1267c478bd9Sstevel@tonic-gate goto badexit;
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * get the size of the data in the message
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate p = bp + FSP_PARMS;
1347c478bd9Sstevel@tonic-gate getword(p, &size);
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * make sure the data is valid; it should have a series of
1387c478bd9Sstevel@tonic-gate * "dialect" strings, which are of the form [02 string 00].
1397c478bd9Sstevel@tonic-gate * if(twos == nulls) then the data is well formed, else something
1407c478bd9Sstevel@tonic-gate * is wrong.
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate twos = nulls = 0;
1447c478bd9Sstevel@tonic-gate p += 2;
1457c478bd9Sstevel@tonic-gate for(q = p; q < p + size; ++q){
1467c478bd9Sstevel@tonic-gate if(*q == '\0')
1477c478bd9Sstevel@tonic-gate nulls++;
1487c478bd9Sstevel@tonic-gate else if(*q == 02)
1497c478bd9Sstevel@tonic-gate twos++;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if(twos != nulls){
1537c478bd9Sstevel@tonic-gate sprintf(logbuf, "%s: Bad Data Format, twos=%d, nulls=%d",
1547c478bd9Sstevel@tonic-gate Msnet, twos, nulls);
1557c478bd9Sstevel@tonic-gate goto badexit;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * Count the number of arguments that were passed
1607c478bd9Sstevel@tonic-gate * to me by the listener...
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate for(i=0, args=argv; *args; ++args, ++i)
1647c478bd9Sstevel@tonic-gate ;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate * There are a few kinds of arguments that I will pass to the server:
1687c478bd9Sstevel@tonic-gate *
1697c478bd9Sstevel@tonic-gate * -D<string> - means "the client speaks this dialect . . ."
1707c478bd9Sstevel@tonic-gate * there me be more than one of these, if the client
1717c478bd9Sstevel@tonic-gate * is able to speak multiple dialects.
1727c478bd9Sstevel@tonic-gate *
1737c478bd9Sstevel@tonic-gate * Any arguments passed to me by the listener will be passed along
1747c478bd9Sstevel@tonic-gate * as is . . .
1757c478bd9Sstevel@tonic-gate *
1767c478bd9Sstevel@tonic-gate * Allocate an array of "char *"s that will let me point to all
1777c478bd9Sstevel@tonic-gate * of the following:
1787c478bd9Sstevel@tonic-gate * 1. As many -D options as are needed (the exact number is
1797c478bd9Sstevel@tonic-gate * contained in the variable "twos"),
1807c478bd9Sstevel@tonic-gate * 2. One -A option for the single logical name
1817c478bd9Sstevel@tonic-gate * of the client,
1827c478bd9Sstevel@tonic-gate * 3. As many positions as are needed to pass along the arguments
1837c478bd9Sstevel@tonic-gate * passed to me by the listener (variable "i"),
1847c478bd9Sstevel@tonic-gate * 4. The name of the Server executable file (always arg[0]), and
1857c478bd9Sstevel@tonic-gate * 5. "Ascii-ized" version of input message as last arg.
1867c478bd9Sstevel@tonic-gate * 6. A NULL terminator.
1877c478bd9Sstevel@tonic-gate */
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate m_size = sizeof(char *) * (twos + i + 4);
1907c478bd9Sstevel@tonic-gate if((args = (char **)malloc((unsigned)m_size)) == 0){
191*2a8bcb4eSToomas Soome sprintf(logbuf, "%s: Can't malloc arg space, %d bytes",
1927c478bd9Sstevel@tonic-gate Msnet, m_size);
1937c478bd9Sstevel@tonic-gate goto badexit;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * put together the first argument to exec(2) which should be
1987c478bd9Sstevel@tonic-gate * the full pathname of the executable server file.
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate args[0] = server;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate * Send dialect strings down, in order of preference
2057c478bd9Sstevel@tonic-gate */
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate for(i=1, q=p; q < p + size; ++i, ++q){
2087c478bd9Sstevel@tonic-gate q = strchr(q, 02); /* find start of string */
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate m_size = strlen(++q) + 1 + 2;
2117c478bd9Sstevel@tonic-gate if((args[i] = malloc((unsigned)m_size)) == 0){
212*2a8bcb4eSToomas Soome sprintf(logbuf,
2137c478bd9Sstevel@tonic-gate "%s: Can't malloc Server Path buf, %d bytes",
2147c478bd9Sstevel@tonic-gate Msnet, m_size);
2157c478bd9Sstevel@tonic-gate goto badexit;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate strcpy(args[i], "-D");
2197c478bd9Sstevel@tonic-gate strcat(args[i], q); /* put -Ddialect\0 in arglist */
2207c478bd9Sstevel@tonic-gate q = strchr(q, '\0'); /* find end of string */
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * Add in arguments that were passed to me by the listener
2257c478bd9Sstevel@tonic-gate * first arg is server path, so we ignore that.
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate for( ++argv; *argv; ++argv, ++i)
2297c478bd9Sstevel@tonic-gate args[i] = *argv;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * add ascii-ized version of message
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate args[i++] = bytes_to_ascii(bp, bufsize);
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * NULL terminate the list
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate args[i] = NULL;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate exec_cmd((dbf_t *)0, args);
244113f4232Sakaplan return; /* error logged in start_server */
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate badexit:
2477c478bd9Sstevel@tonic-gate logmessage(logbuf);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * g e t w o r d
2537c478bd9Sstevel@tonic-gate *
2547c478bd9Sstevel@tonic-gate * move a word from an arbitrary position in a character buffer, into
2557c478bd9Sstevel@tonic-gate * a short, and flip the bytes.
2567c478bd9Sstevel@tonic-gate * (NOTE that word is a 16-bit iapx-286 word).
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate
259113f4232Sakaplan void
getword(char * addr,short * w)260113f4232Sakaplan getword(char *addr, short *w)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate lobyte(*w) = *addr++;
2637c478bd9Sstevel@tonic-gate hibyte(*w) = *addr;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate /* b y t e s _ t o _ a s c i i
2677c478bd9Sstevel@tonic-gate * Routine to convert a binary array to a printable sequence of
2687c478bd9Sstevel@tonic-gate * characters. For example, if the input to this routine were:
2697c478bd9Sstevel@tonic-gate *
2707c478bd9Sstevel@tonic-gate * inbuf = "012", and n = 3
2717c478bd9Sstevel@tonic-gate *
2727c478bd9Sstevel@tonic-gate * then the output would be a pointer to the string:
2737c478bd9Sstevel@tonic-gate *
2747c478bd9Sstevel@tonic-gate * "303132"
2757c478bd9Sstevel@tonic-gate *
2767c478bd9Sstevel@tonic-gate * No assumption is made about NULL terminators on input, because
2777c478bd9Sstevel@tonic-gate * it is probably binary, and not a string.
2787c478bd9Sstevel@tonic-gate */
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate char *
bytes_to_ascii(inbuf,n)2827c478bd9Sstevel@tonic-gate bytes_to_ascii(inbuf, n)
2837c478bd9Sstevel@tonic-gate char *inbuf; /* initialized buffer of binary data */
2847c478bd9Sstevel@tonic-gate int n; /* size of input buffer */
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate char *outbuf; /* return string */
2877c478bd9Sstevel@tonic-gate char *p; /* scratch pointer */
2887c478bd9Sstevel@tonic-gate int i; /* scratch variable */
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate /* malloc 2x space for output plus one for NULL */
2917c478bd9Sstevel@tonic-gate if (outbuf = malloc(n * 2 + 1)) {
2927c478bd9Sstevel@tonic-gate /* Fill in output buffer, with 2 character, capitalized hex. */
2937c478bd9Sstevel@tonic-gate for (i = 0, p = outbuf; i < n; ++inbuf, p += 2, ++i) {
2947c478bd9Sstevel@tonic-gate sprintf(p, "%2.2X", *inbuf);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate return(outbuf);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate else
2997c478bd9Sstevel@tonic-gate return(NULL);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate #else
3057c478bd9Sstevel@tonic-gate
306113f4232Sakaplan void
smbservice(bp,size,argv)3077c478bd9Sstevel@tonic-gate smbservice(bp, size, argv)
3087c478bd9Sstevel@tonic-gate char *bp; /* pointer to message buffer */
3097c478bd9Sstevel@tonic-gate int size; /* size of message */
3107c478bd9Sstevel@tonic-gate char **argv; /* server arguments */
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate logmessage("SMB service NOT supported");
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate #endif /* SMBSERVICE */
316