CASA/c_clientlogin/alogin.c
2005-10-21 01:16:41 +00:00

286 lines
7.8 KiB
C

/***********************************************************************
* File: alogin.c
* Author: (tthrone@novell.com)
*
* Copyright (C) 2005 Novell, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <errno.h>
#include <syslog.h>
#include <nwcalls.h>
#include <nwnet.h>
#include <micasa_mgmd.h>
#define NCL_CONFIG_FILE "/etc/opt/novell/ncl/login.conf"
#define DEFAULT_TREE_VARIABLE_NAME "Default_Tree"
typedef
int
(*PGETCREDENTIAL)(
unsigned long ssFlags, // IN
SSCS_SECRET_ID_T * appSecretID, // IN
SSCS_SECRET_ID_T * sharedSecretID, // Optional IN
int * credentialType, // OUT
void * credential, // IN
SSCS_EXT_T * ext // Reserved
);
extern int errno;
//++=======================================================================
int
GetConfigValue(
char *pConfigFile,
char *pName,
int nameSize,
char *pValue,
int *pValueSize)
//
// Arguments In: pConfigFile - name of configuration file to use
// pName - pointer to name of configuration option
// nameSize - size of configuration option name
// pValueSize - size of the buffer pointed to by pValue
//
// Arguments Out: pValue - contains the value of name
// pValueSize - contains the size of value
//
// Returns: 0 - success, errno - failure
//
// Abstract: Method to obtain the value of a named configuration variable
// within a configuration file.
//
// L2
//=======================================================================--
{
int ccode = -1;
// Open the configuration file
FILE *stream = fopen(pConfigFile, "r");
if (stream != NULL)
{
// Configuration file opened, now seek to
// the beginning and go through each line until
// we find the configuration variable or reach
// the end of the line.
fseek(stream, 0, SEEK_SET);
char line[258] = {0};
while (fgets(line, 258, stream) != NULL)
{
// Check if the configuration variable name is at the beginning
// of the line.
char *p = strstr(line, pName);
if (p == line)
{
// Configuration variable found
//
// Get past the configuration variable and
// try to get its value.
p += nameSize;
int charsLeft = strlen(line) - (p - line);
// Skip any spaces, tabs, or equal signs that may be ahead of the
// value.
for ( ; charsLeft > 0; charsLeft--, p++)
{
if (*p != ' '
&& *p != '\t'
&& *p != '=')
break;
}
// Skip any spaces, tabs, or the newline that may be after the value.
// NULL terminate the value if necessary
char *currLocation = p;
for ( ; charsLeft > 0; charsLeft--, currLocation++)
{
if (*currLocation == ' '
|| *currLocation == '\t'
|| *currLocation == '\n')
{
*currLocation = '\0';
break;
}
}
// Try to obtain the value
if ((currLocation - p) < (*pValueSize - 1))
{
strcpy(pValue, p);
*pValueSize = (currLocation - p);
ccode = 0;
}
else
{
printf("Buffer overflow reading configuration value.\n");
ccode = -1;
}
break;
}
}
// Close configuration file
fclose(stream);
}
else
{
printf("Error opening %s.conf file, error = %d\n", pConfigFile, errno);
ccode = errno;
}
return ccode;
}
int main(int argc, char* argv[])
{
int ccode;
NWDSContextHandle hContext = 0;
char defaultTree[NW_MAX_TREE_NAME_LEN + 1];
int defaultTreeSize;
int authenticated = 0;
SSCS_BASIC_CREDENTIAL basicCredential;
// Get ready to log
openlog("nwautologin", LOG_CONS | LOG_NOWAIT | LOG_ODELAY| LOG_PID, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
syslog(LOG_USER | LOG_INFO, "Attempting Novell client auto login.");
defaultTreeSize = sizeof(defaultTree);
ccode = GetConfigValue(
NCL_CONFIG_FILE,
DEFAULT_TREE_VARIABLE_NAME,
sizeof(DEFAULT_TREE_VARIABLE_NAME) - 1,
defaultTree,
&defaultTreeSize);
if (ccode != SUCCESS)
{
syslog(LOG_USER | LOG_ERR, "Error reading configuration value \"Default_Tree=\" from /etc/opt/novell/ncl/login.conf file.");
exit (1);
}
// printf("Default tree returned from GetConfigValue [%s], tree length %d\n", defaultTree, defaultTreeSize);
authenticated = 0;
ccode = NWDSCreateContextHandle(&hContext);
if (ccode == SUCCESS)
{
// Set tree name in context handle.
ccode = NWDSSetContext(hContext, DCK_TREE_NAME, defaultTree);
if (ccode == SUCCESS)
{
// Do we already have a identity for this tree (logged in to tree)?
if (NWDSCanDSAuthenticate(hContext) == TRUE)
{
syslog(LOG_USER | LOG_INFO, "Novell client is already authenticated to %s.", defaultTree);
authenticated = 1;
}
}
NWDSFreeContext(hContext);
}
ccode = -1;
if (authenticated == 0)
{
SSCS_SECRET_ID_T tree;
void * hModule;
PGETCREDENTIAL pfnGetCredential;
int credentialType;
//
// Set the new credentials in miCASA
//
if ((hModule = dlopen("libmicasa.so", RTLD_LAZY)) != NULL)
{
if ((pfnGetCredential =
(PGETCREDENTIAL)dlsym(hModule, "miCASAGetCredential")) != NULL)
{
strcpy((char *)&tree.id, defaultTree);
tree.len = defaultTreeSize + 1;
// printf("tree name passed to miCASAGetCredential [%s], length %d\n", tree.id, tree.len);
basicCredential.unFlags = USERNAME_TYPE_NDS_FDN_F;
basicCredential.unLen = 0;
basicCredential.pwordLen = 0;
credentialType = SSCS_CRED_TYPE_BASIC_F;
ccode = (*pfnGetCredential)(
0,
&tree,
NULL,
&credentialType,
&basicCredential,
NULL);
// printf("miCASAGetCredential returned %d\n", ccode);
if (ccode != SUCCESS)
{
syslog(LOG_USER | LOG_INFO, "miCASAGetCredential failed with %d.", ccode);
}
/*
else
{
printf("Credential type %d\n", credentialType);
printf("Credential - username [%s], password [%s]\n", basicCredential.username, basicCredential.password);
}
*/
}
dlclose(hModule);
}
else
{
syslog(LOG_USER | LOG_INFO, "dlopen on libmicasa.so failed, errno - %d.", errno);
}
}
if (ccode == SUCCESS)
{
char command[258] = {0};
sprintf(command, "/opt/novell/ncl/bin/nwlogin -t %s -u %s -p %s -r", defaultTree, basicCredential.username, basicCredential.password);
ccode = system(command);
syslog(LOG_USER | LOG_INFO, "/opt/novell/ncl/bin/nwlogin returned %d.", ccode);
}
closelog();
return ccode;
}