1996-06-10 03:39:58 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
1996-06-10 03:39:58 +00:00
replacement routines for broken systems
1998-01-22 13:27:43 +00:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
1996-06-10 03:39:58 +00:00
This program 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 program 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 General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
1998-10-17 17:41:13 +00:00
void replace_dummy ( void ) ;
void replace_dummy ( void ) { }
1996-06-10 03:39:58 +00:00
1998-07-29 03:08:05 +00:00
# ifndef HAVE_FTRUNCATE
1996-06-10 03:39:58 +00:00
/*******************************************************************
ftruncate for operating systems that don ' t have it
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-03 18:40:31 +00:00
int ftruncate ( int f , SMB_OFF_T l )
1996-06-10 03:39:58 +00:00
{
struct flock fl ;
fl . l_whence = 0 ;
fl . l_len = 0 ;
fl . l_start = l ;
fl . l_type = F_WRLCK ;
return fcntl ( f , F_FREESP , & fl ) ;
}
1998-11-18 01:19:32 +00:00
# endif /* HAVE_FTRUNCATE */
1996-06-10 03:39:58 +00:00
2001-11-20 08:50:57 +00:00
# ifndef HAVE_STRLCPY
/* like strncpy but does not 0 fill the buffer and always null
terminates . bufsize is the size of the destination buffer */
size_t strlcpy ( char * d , const char * s , size_t bufsize )
{
size_t len = strlen ( s ) ;
size_t ret = len ;
if ( bufsize < = 0 ) return 0 ;
if ( len > = bufsize ) len = bufsize - 1 ;
memcpy ( d , s , len ) ;
d [ len ] = 0 ;
return ret ;
}
# endif
# ifndef HAVE_STRLCAT
/* like strncat but does not 0 fill the buffer and always null
terminates . bufsize is the length of the buffer , which should
be one more than the maximum resulting string length */
size_t strlcat ( char * d , const char * s , size_t bufsize )
{
size_t len1 = strlen ( d ) ;
size_t len2 = strlen ( s ) ;
size_t ret = len1 + len2 ;
if ( len1 + len2 > = bufsize ) {
len2 = bufsize - ( len1 + 1 ) ;
}
if ( len2 > 0 ) {
memcpy ( d + len1 , s , len2 ) ;
d [ len1 + len2 ] = 0 ;
}
return ret ;
}
# endif
1998-07-29 03:08:05 +00:00
# ifndef HAVE_MKTIME
1996-06-10 03:39:58 +00:00
/*******************************************************************
a mktime ( ) replacement for those who don ' t have it - contributed by
C . A . Lademann < cal @ zls . com >
1998-11-11 20:03:19 +00:00
Corrections by richard . kettlewell @ kewill . com
1996-06-10 03:39:58 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-11 20:03:19 +00:00
1996-06-10 03:39:58 +00:00
# define MINUTE 60
# define HOUR 60*MINUTE
# define DAY 24*HOUR
# define YEAR 365*DAY
1998-07-29 03:08:05 +00:00
time_t mktime ( struct tm * t )
1996-06-10 03:39:58 +00:00
{
struct tm * u ;
time_t epoch = 0 ;
1998-11-11 20:03:19 +00:00
int n ;
1996-06-10 03:39:58 +00:00
int mon [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ,
y , m , i ;
if ( t - > tm_year < 70 )
return ( ( time_t ) - 1 ) ;
1998-11-11 20:03:19 +00:00
n = t - > tm_year + 1900 - 1 ;
1996-06-10 03:39:58 +00:00
epoch = ( t - > tm_year - 70 ) * YEAR +
1998-11-11 20:03:19 +00:00
( ( n / 4 - n / 100 + n / 400 ) - ( 1969 / 4 - 1969 / 100 + 1969 / 400 ) ) * DAY ;
1996-06-10 03:39:58 +00:00
1999-12-13 13:27:58 +00:00
y = t - > tm_year + 1900 ;
1996-06-10 03:39:58 +00:00
m = 0 ;
for ( i = 0 ; i < t - > tm_mon ; i + + ) {
epoch + = mon [ m ] * DAY ;
if ( m = = 1 & & y % 4 = = 0 & & ( y % 100 ! = 0 | | y % 400 = = 0 ) )
epoch + = DAY ;
if ( + + m > 11 ) {
m = 0 ;
y + + ;
}
}
epoch + = ( t - > tm_mday - 1 ) * DAY ;
epoch + = t - > tm_hour * HOUR + t - > tm_min * MINUTE + t - > tm_sec ;
if ( ( u = localtime ( & epoch ) ) ! = NULL ) {
t - > tm_sec = u - > tm_sec ;
t - > tm_min = u - > tm_min ;
t - > tm_hour = u - > tm_hour ;
t - > tm_mday = u - > tm_mday ;
t - > tm_mon = u - > tm_mon ;
t - > tm_year = u - > tm_year ;
t - > tm_wday = u - > tm_wday ;
t - > tm_yday = u - > tm_yday ;
t - > tm_isdst = u - > tm_isdst ;
}
return ( epoch ) ;
}
1998-07-29 03:08:05 +00:00
# endif /* !HAVE_MKTIME */
1996-06-10 03:39:58 +00:00
1998-07-29 03:08:05 +00:00
# ifndef HAVE_RENAME
1996-06-10 03:39:58 +00:00
/* Rename a file. (from libiberty in GNU binutils) */
1998-07-29 03:08:05 +00:00
int rename ( const char * zfrom , const char * zto )
1996-06-10 03:39:58 +00:00
{
if ( link ( zfrom , zto ) < 0 )
{
if ( errno ! = EEXIST )
return - 1 ;
if ( unlink ( zto ) < 0
| | link ( zfrom , zto ) < 0 )
return - 1 ;
}
return unlink ( zfrom ) ;
}
1998-11-18 01:19:32 +00:00
# endif /* HAVE_RENAME */
1996-06-10 03:39:58 +00:00
1998-07-29 03:08:05 +00:00
# ifndef HAVE_INNETGR
1998-11-18 01:19:32 +00:00
# if defined(HAVE_SETNETGRENT) && defined(HAVE_GETNETGRENT) && defined(HAVE_ENDNETGRENT)
1996-06-10 03:39:58 +00:00
/*
* Search for a match in a netgroup . This replaces it on broken systems .
*/
This commit is number 4 of 4.
In particular this commit focuses on:
Actually adding the 'const' to the passdb interface, and the flow-on changes.
Also kill off the 'disp_info' stuff, as its no longer used.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes introduces a large dose of 'const' to the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
---
This finishes this line of commits off, your tree should now compile again :-)
Andrew Bartlett
(This used to be commit c95f5aeb9327347674589ae313b75bee3bf8e317)
2001-10-29 07:35:11 +00:00
int innetgr ( const char * group , const char * host , const char * user , const char * dom )
1996-06-10 03:39:58 +00:00
{
1998-07-29 03:08:05 +00:00
char * hst , * usr , * dm ;
1996-06-10 03:39:58 +00:00
1998-07-29 03:08:05 +00:00
setnetgrent ( group ) ;
while ( getnetgrent ( & hst , & usr , & dm ) ) {
if ( ( ( host = = 0 ) | | ( hst = = 0 ) | | ! strcmp ( host , hst ) ) & &
( ( user = = 0 ) | | ( usr = = 0 ) | | ! strcmp ( user , usr ) ) & &
( ( dom = = 0 ) | | ( dm = = 0 ) | | ! strcmp ( dom , dm ) ) ) {
endnetgrent ( ) ;
return ( 1 ) ;
}
}
endnetgrent ( ) ;
return ( 0 ) ;
1996-06-10 03:39:58 +00:00
}
1998-11-18 01:19:32 +00:00
# endif /* HAVE_SETNETGRENT HAVE_GETNETGRENT HAVE_ENDNETGRENT */
# endif /* HAVE_INNETGR */
1996-06-10 03:39:58 +00:00
1998-07-29 03:08:05 +00:00
# ifndef HAVE_INITGROUPS
1996-06-10 03:39:58 +00:00
/****************************************************************************
some systems don ' t have an initgroups call
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int initgroups ( char * name , gid_t id )
{
1998-07-29 03:08:05 +00:00
# ifndef HAVE_SETGROUPS
static int done ;
if ( ! done ) {
DEBUG ( 1 , ( " WARNING: running without setgroups \n " ) ) ;
done = 1 ;
}
/* yikes! no SETGROUPS or INITGROUPS? how can this work? */
return ( 0 ) ;
1998-11-18 01:19:32 +00:00
# else /* HAVE_SETGROUPS */
2000-02-15 19:36:47 +00:00
gid_t * grouplst = NULL ;
int max_gr = groups_max ( ) ;
int ret ;
1998-07-29 03:08:05 +00:00
int i , j ;
struct group * g ;
char * gr ;
2000-02-15 19:36:47 +00:00
if ( ( grouplst = ( gid_t * ) malloc ( sizeof ( gid_t ) * max_gr ) ) = = NULL ) {
2000-10-19 21:49:48 +00:00
DEBUG ( 0 , ( " initgroups: malloc fail ! \n " ) ) ;
2000-02-15 19:36:47 +00:00
return - 1 ;
}
1998-07-29 03:08:05 +00:00
grouplst [ 0 ] = id ;
i = 1 ;
2000-02-15 19:36:47 +00:00
while ( i < max_gr & & ( ( g = ( struct group * ) getgrent ( ) ) ! = ( struct group * ) NULL ) ) {
1998-07-29 03:08:05 +00:00
if ( g - > gr_gid = = id )
continue ;
j = 0 ;
gr = g - > gr_mem [ 0 ] ;
while ( gr & & ( * gr ! = ( char ) NULL ) ) {
if ( strcmp ( name , gr ) = = 0 ) {
grouplst [ i ] = g - > gr_gid ;
i + + ;
gr = ( char * ) NULL ;
break ;
}
gr = g - > gr_mem [ + + j ] ;
}
1996-06-10 03:39:58 +00:00
}
1998-07-29 03:08:05 +00:00
endgrent ( ) ;
2000-02-15 19:36:47 +00:00
ret = sys_setgroups ( i , grouplst ) ;
2001-09-17 02:19:44 +00:00
SAFE_FREE ( grouplst ) ;
2000-02-15 19:36:47 +00:00
return ret ;
1998-11-18 01:19:32 +00:00
# endif /* HAVE_SETGROUPS */
1996-06-10 03:39:58 +00:00
}
1998-11-18 01:19:32 +00:00
# endif /* HAVE_INITGROUPS */
1996-06-10 03:39:58 +00:00
# if (defined(SecureWare) && defined(SCO))
/* This is needed due to needing the nap() function but we don't want
to include the Xenix libraries since that will break other things . . .
BTW : system call # 0x0c28 is the same as calling nap ( ) */
1998-07-29 03:08:05 +00:00
long nap ( long milliseconds ) {
return syscall ( 0x0c28 , milliseconds ) ;
}
1996-06-10 03:39:58 +00:00
# endif
1998-07-29 03:08:05 +00:00
# ifndef HAVE_MEMMOVE
1996-06-10 03:39:58 +00:00
/*******************************************************************
1998-07-29 03:08:05 +00:00
safely copies memory , ensuring no overlap problems .
this is only used if the machine does not have it ' s own memmove ( ) .
this is not the fastest algorithm in town , but it will do for our
needs .
1996-06-10 03:39:58 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-07-29 03:08:05 +00:00
void * memmove ( void * dest , const void * src , int size )
1996-06-10 03:39:58 +00:00
{
1998-07-29 03:08:05 +00:00
unsigned long d , s ;
int i ;
if ( dest = = src | | ! size ) return ( dest ) ;
d = ( unsigned long ) dest ;
s = ( unsigned long ) src ;
if ( ( d > = ( s + size ) ) | | ( s > = ( d + size ) ) ) {
/* no overlap */
memcpy ( dest , src , size ) ;
return ( dest ) ;
}
if ( d < s ) {
/* we can forward copy */
if ( s - d > = sizeof ( int ) & &
! ( s % sizeof ( int ) ) & &
! ( d % sizeof ( int ) ) & &
! ( size % sizeof ( int ) ) ) {
/* do it all as words */
int * idest = ( int * ) dest ;
int * isrc = ( int * ) src ;
size / = sizeof ( int ) ;
for ( i = 0 ; i < size ; i + + ) idest [ i ] = isrc [ i ] ;
} else {
/* simplest */
char * cdest = ( char * ) dest ;
char * csrc = ( char * ) src ;
for ( i = 0 ; i < size ; i + + ) cdest [ i ] = csrc [ i ] ;
}
} else {
/* must backward copy */
if ( d - s > = sizeof ( int ) & &
! ( s % sizeof ( int ) ) & &
! ( d % sizeof ( int ) ) & &
! ( size % sizeof ( int ) ) ) {
/* do it all as words */
int * idest = ( int * ) dest ;
int * isrc = ( int * ) src ;
size / = sizeof ( int ) ;
for ( i = size - 1 ; i > = 0 ; i - - ) idest [ i ] = isrc [ i ] ;
} else {
/* simplest */
char * cdest = ( char * ) dest ;
char * csrc = ( char * ) src ;
for ( i = size - 1 ; i > = 0 ; i - - ) cdest [ i ] = csrc [ i ] ;
}
}
return ( dest ) ;
}
1998-11-18 01:19:32 +00:00
# endif /* HAVE_MEMMOVE */
1998-07-29 03:08:05 +00:00
# ifndef HAVE_STRDUP
/****************************************************************************
duplicate a string
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char * strdup ( const char * s )
{
1998-11-18 01:19:32 +00:00
size_t len ;
1998-07-29 03:08:05 +00:00
char * ret ;
if ( ! s ) return ( NULL ) ;
len = strlen ( s ) + 1 ;
ret = ( char * ) malloc ( len ) ;
if ( ! ret ) return ( NULL ) ;
memcpy ( ret , s , len ) ;
return ( ret ) ;
1996-06-10 03:39:58 +00:00
}
1998-11-18 01:19:32 +00:00
# endif /* HAVE_STRDUP */
1996-06-10 03:39:58 +00:00
1998-07-29 07:02:06 +00:00
# ifdef REPLACE_INET_NTOA
char * rep_inet_ntoa ( struct in_addr ip )
{
1998-07-29 07:07:08 +00:00
unsigned char * p = ( unsigned char * ) & ip . s_addr ;
1998-07-29 07:02:06 +00:00
static char buf [ 18 ] ;
slprintf ( buf , 17 , " %d.%d.%d.%d " ,
( int ) p [ 0 ] , ( int ) p [ 1 ] , ( int ) p [ 2 ] , ( int ) p [ 3 ] ) ;
return buf ;
}
1998-11-18 01:19:32 +00:00
# endif /* REPLACE_INET_NTOA */
1998-10-04 15:54:04 +00:00
1998-11-25 21:17:20 +00:00
# ifndef HAVE_STRTOUL
# ifndef ULONG_MAX
# define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
# endif
/*
* Convert a string to an unsigned long integer .
* Taken from libg + + - libiberty code .
*
* Ignores ` locale ' stuff . Assumes that the upper and lower case
* alphabets and digits are each contiguous .
*/
unsigned long strtoul ( const char * nptr , char * * endptr , int base )
{
const char * s = nptr ;
unsigned long acc ;
int c ;
unsigned long cutoff ;
int neg = 0 , any , cutlim ;
/*
* See strtol for comments as to the logic used .
*/
do {
c = * s + + ;
} while ( isspace ( c ) ) ;
if ( c = = ' - ' ) {
neg = 1 ;
c = * s + + ;
} else if ( c = = ' + ' )
c = * s + + ;
if ( ( base = = 0 | | base = = 16 ) & &
c = = ' 0 ' & & ( * s = = ' x ' | | * s = = ' X ' ) ) {
c = s [ 1 ] ;
s + = 2 ;
base = 16 ;
}
if ( base = = 0 )
base = c = = ' 0 ' ? 8 : 10 ;
cutoff = ( unsigned long ) ULONG_MAX / ( unsigned long ) base ;
cutlim = ( int ) ( ( unsigned long ) ULONG_MAX % ( unsigned long ) base ) ;
for ( acc = 0 , any = 0 ; ; c = * s + + ) {
if ( isdigit ( c ) )
c - = ' 0 ' ;
else if ( isalpha ( c ) )
c - = isupper ( c ) ? ' A ' - 10 : ' a ' - 10 ;
else
break ;
if ( c > = base )
break ;
if ( any < 0 | | acc > cutoff | | acc = = cutoff & & c > cutlim )
any = - 1 ;
else {
any = 1 ;
acc * = base ;
acc + = c ;
}
}
if ( any < 0 ) {
acc = ULONG_MAX ;
errno = ERANGE ;
} else if ( neg )
acc = - acc ;
if ( endptr ! = 0 )
* endptr = ( char * ) ( any ? s - 1 : nptr ) ;
return ( acc ) ;
}
# endif /* HAVE_STRTOUL */
2001-03-31 19:06:45 +00:00
# ifndef HAVE_SETLINEBUF
2001-04-02 22:27:40 +00:00
int setlinebuf ( FILE * stream )
2001-03-31 19:06:45 +00:00
{
2001-04-02 22:27:40 +00:00
return setvbuf ( stream , ( char * ) NULL , _IOLBF , 0 ) ;
2001-03-31 19:06:45 +00:00
}
# endif /* HAVE_SETLINEBUF */
2002-07-15 10:35:28 +00:00
# ifndef HAVE_VSYSLOG
# ifdef HAVE_SYSLOG
void vsyslog ( int facility_priority , char * format , va_list arglist )
{
char * msg = NULL ;
vasprintf ( & msg , format , arglist ) ;
if ( ! msg )
return ;
syslog ( facility_priority , " %s " , msg ) ;
SAFE_FREE ( msg ) ;
}
# endif /* HAVE_SYSLOG */
# endif /* HAVE_VSYSLOG */
2002-08-17 17:00:51 +00:00
2002-11-09 16:57:45 +00:00
# ifndef HAVE_TIMEGM
/*
2003-01-03 01:41:44 +00:00
yes , I know this looks insane , but its really needed . The function in the
Linux timegm ( ) manpage does not work on solaris .
2002-11-09 16:57:45 +00:00
*/
time_t timegm ( struct tm * tm )
{
2003-01-03 01:41:44 +00:00
struct tm tm2 , tm3 ;
time_t t ;
tm2 = * tm ;
t = mktime ( & tm2 ) ;
tm3 = * localtime ( & t ) ;
tm2 = * tm ;
tm2 . tm_isdst = tm3 . tm_isdst ;
t = mktime ( & tm2 ) ;
t - = TimeDiff ( t ) ;
return t ;
2002-11-09 16:57:45 +00:00
}
# endif