Category : Network Files
Archive   : VAR.ZIP
Filename : SVAR.C

 
Output of file : SVAR.C contained in archive : VAR.ZIP

/*
SVAR.C:

A sample program that uses Lan Manager APIs to obtain the following
user information elements:

. Workstation's computer name (from local lanman.ini)
. User logon name (as specified at logon)
. Workstation's default domain (from local lanman.ini)
. User's preferred logon server (from user's account - if specified)
. User's home directory (from user's account - if specified)

A temporary .BAT (DOS) or .CMD (OS2) file [SETFILE] is then created in the local
environment's %TEMP% directory. Each information element requested is
appended to a "SET =" string and written to the temporary
file. The temporary file is then available to run whenever these
information elements are needed as local environment variables. For example,
the temporary batch file can be called from a user's logon script to set
the requested environment variables for use further down in the logon script.


Usage: svar-{dos/os2} [-c [Computer varname]] [-u [User varname]]
[-d [Default domain varname]] [-s [Preferred server varname]]
[-h [Home directory varname]] [-f Temporary file name]

The user may provide any combination of the -c, -u, -d, -s and -h
(at least one is required) flags to indicate which information elements
are desired. Optionally, any of the indicated flags may be followed by a
local variable name that the user wants used in the "set ="
string for the respective information element. If the user does not
specify varnames, the following default varnames will be used:

-c COMPUTER
-u USER
-d DOMAIN
-s SERVER
-h HOME

By default, the file name for the temporary file is "$x$x$x$x".
The user can designate a different file name by using the -f parameter
(see usage, above).


EXAMPLE: "svar-dos -c -u USERNAME -d -h HOMEDIR -f tmpfile"

* If the local workstation's computername was "TW301", the domain was
"SALES", the user's logon name was "JOE" and JOE's home directory was
"\\SERVER1\HOME1", this command would create the following TMPFILE.BAT
file in the %TEMP% directory:

SET COMPUTER=TW301
SET USERNAME=JOE
SET DOMAIN=SALES
SET HOMEDIR=\\SERVER1\HOME1

API Used to...
==================== ====================================================
NetWkstaGetInfo Determine if the workstation is started, retrieve
the user's logon name, the workstation's computername
and default domain.
NetGetDCName Determine if the NETLOGON service is running on the
domain and retrieve the domain controller's name.
NetUserGetInfo Retrieve the user's preferred server and home directory.


Author: Dan Perry
Microsoft Inc.
PSS Lan Manager Developer Support


This code sample is provided for demonstration purposes only.
Microsoft makes no warranty, either express or implied,
as to its usability in any given situation.
*/


#include // C runtime header files
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define INCL_NETDOMAIN
#define INCL_NETUSER
#define INCL_NETWKSTA
#define INCL_NETERRORS
#include // LAN Manager header files

#define SafeMalloc(size) _SafeMallocFunc(size, __FILE__, __LINE__)

// #define DEBUG // Enable for debug comments

void Usage(char * pszString);
void * _SafeMallocFunc(unsigned cbSize, // Count of bytes to allocate
char *pszFilename, // Program calling SafeMalloc
unsigned uLine); // Line in program
void main(int argc, char * argv[])
{
char * pbBuffer; // Pointer to data buffer to hold workstation info
char * pbBuffer2; // Pointer to data buffer to hold user info
char * pbSetfile = "$x$x$x$x"; // Default name of temporary .{BAT/CMD} file
char * pbPathname; // Path to Setfile
char pszComputer[80] = "COMPUTER"; // System variable name for workstation computername
char pszUser[80] = "USER"; // System variable name for user logon name
char pszDomain[80] = "DOMAIN"; // System variable name for default domain name
char pszServer[80] = "SERVER"; // System variable name for preferred logon server
char pszHomeDir[80] = "HOME"; // System variable name for home directory
char pzWriteBuf[80]; // Buffer for strings to be written to Setfile
char pszDCName[UNCLEN+1]; // Name of domain controller
char ch; // Single char var for getch

int iCount; // Index counter
int c=0, u=0, d=0, s=0, h=0; // Information element inclusion flags
short sLevel = 0; // Level of info in pbBuffer
unsigned short cbBuflen; // Count of bytes for workstation info buffer
unsigned short cbBuflen2; // Count of bytes for user info buffer
unsigned short cbTotalAvail; // Count of bytes returned
API_RET_TYPE uRetCode; // API return code
struct user_info_11 * pu11; // User info; level 11
struct wksta_info_0 * pw0; // Workstation info; level 0
int htarget; // File handle for Setfile

//============================================================================
// Determine if a local TEMP environment variable exists -- if not, exit; if
// so, make sure that the TEMP path is terminated by a backslash (\).
//============================================================================

if (! (pbPathname = getenv("TEMP")))
{
putchar('\007');
printf("\nError: %s requires a TEMP system variable\n indicating a temporary disk storage area.\n",argv[0]);
printf("Press any key to continue...\n");
ch=getch();
exit(1);
}
if (pbPathname[strlen(pbPathname)-1] != '\\')
strcat(pbPathname, "\\");

//=============================================================================
// Parse the command line and set flags for requested information elements. If
// parameters are incorrect or nonexistant, show usage.
//=============================================================================

if (argc < 2)
Usage(argv[0]);
for (iCount = 1; iCount < argc; iCount++) // Get command-line switches
{
if ((*argv[iCount] == '-') || (*argv[iCount] == '/'))
{
switch (tolower(*(argv[iCount]+1))) // Process switches
{
case 'c': // -c Workstation's computer name
if ((iCount+1) < argc)
{
if ((*argv[iCount+1] != '-') && (*argv[iCount+1] != '/'))
strcpy(pszComputer, argv[++iCount]);
}
else
++iCount;
c=1;
break;
case 'u': // -u User logon name
if ((iCount+1) < argc)
{
if ((*argv[iCount+1] != '-') && (*argv[iCount+1] != '/'))
strcpy(pszUser, argv[++iCount]);
}
else
++iCount;
u=1;
break;
case 'd': // -d Workstation's default domain
if ((iCount+1) < argc)
{
if ((*argv[iCount+1] != '-') && (*argv[iCount+1] != '/'))
strcpy(pszDomain, argv[++iCount]);
}
else
++iCount;
d=1;
break;
case 's': // -s User's preferred logon server
if ((iCount+1) < argc)
{
if ((*argv[iCount+1] != '-') && (*argv[iCount+1] != '/'))
strcpy(pszServer, argv[++iCount]);
}
else
++iCount;
s=1;
break;
case 'h': // -h User's home directory
if ((iCount+1) < argc)
{
if ((*argv[iCount+1] != '-') && (*argv[iCount+1] != '/'))
strcpy(pszHomeDir, argv[++iCount]);
}
else
++iCount;
h=1;
break;
case 'f': // -f SETFILE name
if ((iCount+1) < argc)
{
if ((*argv[iCount+1] != '-') && (*argv[iCount+1] != '/') && (strlen(argv[iCount+1])<9))
strcpy(pbSetfile, argv[++iCount]);
else
Usage(argv[0]);
}
else
Usage(argv[0]);
break;
default:
Usage(argv[0]);
}
}
else
Usage(argv[0]);
}

if (!(c | u | d | s | h))
Usage(argv[0]);

//============================================================================
// Allocate an appropriate sized buffer and get workstation information.
// Report and exit if the workstation is not started.
//============================================================================

uRetCode = NetWkstaGetInfo("", // Servername
sLevel, // Reporting level (0,1,10)
NULL, // Target buffer for info
0, // Size of target buffer
&cbBuflen); // Count of bytes available

if (uRetCode == NERR_BufTooSmall)
pbBuffer = SafeMalloc(cbBuflen);
else if (uRetCode != NERR_Success)
{
putchar('\007');
if (uRetCode == NERR_WkstaNotStarted)
printf("Error: The workstation service has not been started");
else
printf("NetWkstaGetInfo returned %u\n", uRetCode);
exit(1);
}

uRetCode = NetWkstaGetInfo("", // Servername
sLevel, // Reporting level (0,1,10)
pbBuffer, // Target buffer for info
cbBuflen, // Size of target buffer
&cbTotalAvail); // Total info, in bytes
#ifdef DEBUG
printf("NetWkstaGetInfo returned %u\n", uRetCode);
#endif

if (uRetCode != NERR_Success)
{
putchar('\007');
exit(1);
}
else
pw0 = (struct wksta_info_0 *) pbBuffer;

//============================================================================
// If a user is not logged in, user logon name, preferred server name and home
// directory information is unobtainable -- report appropriately.
//============================================================================

if ((*pw0->wki0_username == '\0') && (u || s || h))
{
putchar('\007');
printf("Error: User not logged on. User name, preferred server\n and home directory will not be set.\n");
u=0;
s=0;
h=0;
}

//============================================================================
// If no domain controller is active, preferred server and home directory
// information is unobtainable. If server name or home directory was requested,
// determine if a domain controller is active. If not, report. If so, allocate
// an appropriate sized buffer and retrieve user information.
//============================================================================

if (s || h)
{
cbBuflen = sizeof(pszDCName);
uRetCode = NetGetDCName("", // Server; NULL means local
"", // Domain; NULL means primary
pszDCName, // Return buffer
cbBuflen); // Size of buffer

#ifdef DEBUG
printf("NetGetDCName returned %u\n", uRetCode);
#endif

if (uRetCode != NERR_Success)
{
s=0;
h=0;
if (uRetCode == NERR_DCNotFound)
{
putchar('\007');
printf("Error: Domain controller not found. Preferred server\n and home directory will not be set.\n");
}
}
else
{
uRetCode = NetUserGetInfo(pszDCName, // Servername
pw0->wki0_username, // Username
11, // Level (0,1,2,10,11)
NULL, // Data buffer
0, // Size of data buffer
&cbBuflen2); // Buffer size required

if (uRetCode != NERR_BufTooSmall)
{
printf("NetUserGetInfo returned %u\n", uRetCode);
s=0;
h=0;
}
else
{
pbBuffer2 = SafeMalloc(cbBuflen2);

uRetCode = NetUserGetInfo(pszDCName, // Servername
pw0->wki0_username, // Username
11, // Level (0,1,2,10,11)
pbBuffer2, // Data buffer
cbBuflen2, // Size of data buffer
&cbTotalAvail); // Count of bytes available

#ifdef DEBUG
printf("NetUserGetInfo returned %u\n", uRetCode);
#endif

if (uRetCode != NERR_Success)
{
s=0;
h=0;
}
else
pu11 = (struct user_info_11 *) pbBuffer2;
}
}
}

#ifdef DEBUG
printf(" Computer Name : %Fs\n", pw0->wki0_computername);
printf(" User Name : %Fs\n", pw0->wki0_username);
printf(" Default Domain : %Fs\n", pw0->wki0_langroup);
if (s || h)
{
printf(" Preferred server : %Fs\n", pu11->usri11_logon_server);
printf(" Home Directory : %Fs\n", pu11->usri11_home_dir);
}
#endif

if (c || u || d || s || h)
{

//=============================================================================
// Determine if the local workstation operating system is DOS or OS/2. Open a
// temporary .BAT or .CMD file accordingly.
//=============================================================================

if (_osmajor >= 10)
strcat(strcat(pbPathname, pbSetfile),".cmd");
else
strcat(strcat(pbPathname, pbSetfile),".bat");
htarget = open(pbPathname, O_TEXT | O_WRONLY | O_TRUNC | O_CREAT,
S_IREAD | S_IWRITE);

//=============================================================================
// Write "SET =" strings to SETFILE for each requested information
// element. Then close the file and exit.
//=============================================================================


if (c)
{
sprintf(pzWriteBuf,"Set %s=%Fs\n",pszComputer,pw0->wki0_computername);
if ((iCount=write(htarget, pzWriteBuf, strlen(pzWriteBuf))) == -1)
printf("C write error = %d",errno);
}
if (u)
{
sprintf(pzWriteBuf,"Set %s=%Fs\n",pszUser,pw0->wki0_username);
if ((iCount=write(htarget, pzWriteBuf, strlen(pzWriteBuf))) == -1)
printf("U write error = %d",errno);
}
if (d)
{
sprintf(pzWriteBuf,"Set %s=%Fs\n",pszDomain,pw0->wki0_langroup);
if ((iCount=write(htarget, pzWriteBuf, strlen(pzWriteBuf))) == -1)
printf("D write error = %d",errno);
}
if (s)
{
sprintf(pzWriteBuf,"Set %s=%Fs\n",pszServer,pu11->usri11_logon_server);
if ((iCount=write(htarget, pzWriteBuf, strlen(pzWriteBuf))) == -1)
printf("S write error = %d",errno);
}
if (h)
{
sprintf(pzWriteBuf,"Set %s=%Fs\n",pszHomeDir,pu11->usri11_home_dir);
if ((iCount=write(htarget, pzWriteBuf, strlen(pzWriteBuf))) == -1)
printf("H write error = %d",errno);
}
close (htarget);
}
exit(0);
}

//=============================================================================
// Command usage report function
//=============================================================================

void Usage(char * pszString)
{
putchar('\007');
fprintf(stderr, "Usage: %s [-c [Computer varname]] [-u [User varname]]\n", pszString);
fprintf(stderr, " [-d [Default domain varname]] [-s [Preferred server varname]]\n");
fprintf(stderr, " [-h [Home directory varname]] [-f Temporary file name (8 char. max)]\n\n");
fprintf(stderr, " NOTE: At least one parameter switch (besides -f) must be specified.\n\n");
exit(1);
}

//=============================================================================
// Safe memory allocation function
//=============================================================================

void *_SafeMallocFunc(unsigned cbSize, char *pszFilename, unsigned usLine)
{
void *ptr;

if ((ptr = malloc(cbSize)) == NULL)
{
fprintf(stderr, "Malloc failed. size:%u file:%s line:%u\n",
cbSize, pszFilename, usLine);
exit(1);
}
else
return (ptr);
}