[497] in Kerberos-V5-bugs

home help back first fref pref prev next nref lref last post

stray uses of locking functions rather than krb5_lock_file()

daemon@ATHENA.MIT.EDU (John T Kohl)
Wed Jun 15 00:57:25 1994

Date: Wed, 15 Jun 1994 00:44:47 -0400
From: John T Kohl <jtk@kolvir.blrc.ma.us>
To: krb5-bugs@MIT.EDU

-----BEGIN PGP SIGNED MESSAGE-----

Here are diffs relative to krb5-prebeta4 to remove some instances of
end-runs around the libos portion of -lkrb5.

==John

p.s. I suppose I should talk with <eichin> about a whole raft of
GNU configure/autoconfig related build problems?

===================================================================
RCS file: slave/kprop.c,v
retrieving revision 1.1
diff -c -r1.1 slave/kprop.c
*** 1.1	1994/06/15 02:45:11
- --- slave/kprop.c	1994/06/15 04:39:56
***************
*** 36,46 ****
  #include <krb5/kdb_dbm.h>
  #include <krb5/ext-proto.h>
  #include <krb5/los-proto.h>
  #include <com_err.h>
  #include <errno.h>
- - #ifdef POSIX_FILE_LOCKS
- - #include <fcntl.h>
- - #endif
  
  #include <stdio.h>
  #include <ctype.h>
- --- 36,44 ----
  #include <krb5/kdb_dbm.h>
  #include <krb5/ext-proto.h>
  #include <krb5/los-proto.h>
+ #include <krb5/libos.h>
  #include <com_err.h>
  #include <errno.h>
  
  #include <stdio.h>
  #include <ctype.h>
***************
*** 77,89 ****
  
  void	PRS();
  void	get_tickets();
! void	usage();
! krb5_error_code open_connection();
! void	kerberos_authenticate();
! int	open_database();
! void	xmit_database();
! void	send_error();
! void	update_last_prop_file();
  
  static void usage()
  {
- --- 75,88 ----
  
  void	PRS();
  void	get_tickets();
! void	usage NPROTOTYPE((void));
! krb5_error_code open_connection NPROTOTYPE((char *, int *, char *));
! void	kerberos_authenticate NPROTOTYPE((int, krb5_principal));
! int	open_database NPROTOTYPE((char *, int *));
! void	close_database NPROTOTYPE((int));
! void	xmit_database NPROTOTYPE((int, int, int));
! void	send_error NPROTOTYPE((int, char *, krb5_error_code));
! void	update_last_prop_file NPROTOTYPE((char *, char *));
  
  static void usage()
  {
***************
*** 123,128 ****
- --- 122,128 ----
  	xmit_database(fd, database_fd, database_size);
  	update_last_prop_file(slave_host, file);
  	printf("Database propagation to %s: SUCCEEDED\n", slave_host);
+ 	close_database(database_fd);
  	exit(0);
  }
  
***************
*** 395,400 ****
- --- 395,402 ----
  	krb5_free_ap_rep_enc_part(rep_result);
  }
  
+ FILE * dbfp;
+ char * dbpathname;
  /*
   * Open the Kerberos database dump file.  Takes care of locking it
   * and making sure that the .ok file is more recent that the database
***************
*** 409,449 ****
  	int	*size;
  {
  	int		fd;
  	struct stat 	stbuf, stbuf_ok;
  	char		*data_ok_fn;
  	static char ok[] = ".dump_ok";
- - #ifdef POSIX_FILE_LOCKS
- - 	struct flock lock_arg;
- - #endif
  
! 	if ((fd = open(data_fn, O_RDONLY)) < 0) {
  		com_err(progname, errno, "while trying to open %s",
! 			data_fn);
  		exit(1);
  	}
  	
! #ifdef POSIX_FILE_LOCKS
! 	lock_arg.l_whence = 0;
! 	lock_arg.l_start = 0;
! 	lock_arg.l_len = 0;
! 	if (fcntl(fd, F_SETLK, &lock_arg) == -1) {
! 		if (errno == EACCES || errno == EAGAIN)
! 			com_err(progname, 0, "database locked");
! 		else
! 			com_err(progname, errno, "while trying to flock %s",
! 				data_fn);
! 		exit(1);
! 	}
! #else
! 	if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
! 		if (errno == EWOULDBLOCK || errno == EAGAIN)
! 			com_err(progname, 0, "database locked");
! 		else
! 			com_err(progname, errno, "while trying to flock %s",
! 				data_fn);
! 		exit(1);
! 	}
! #endif
  	if (fstat(fd, &stbuf)) {
  		com_err(progname, errno, "while trying to stat %s",
  			data_fn);
- --- 411,443 ----
  	int	*size;
  {
  	int		fd;
+ 	int		err;
  	struct stat 	stbuf, stbuf_ok;
  	char		*data_ok_fn;
  	static char ok[] = ".dump_ok";
  
! 	dbpathname = strdup(data_fn);
! 	if (!dbpathname) {
! 	    com_err(progname, ENOMEM, "allocating database file name '%s'",
! 		    data_fn);
! 	    exit(1);
! 	}
! 	if ((dbfp = fopen(dbpathname, "r")) == 0) {
  		com_err(progname, errno, "while trying to open %s",
! 			dbpathname);
  		exit(1);
  	}
  	
! 	err = krb5_lock_file(dbfp, dbpathname,
! 			     KRB5_LOCKMODE_SHARED|KRB5_LOCKMODE_DONTBLOCK);
! 	if (err == EAGAIN || err == EWOULDBLOCK || errno == EACCES) {
! 	    com_err(progname, 0, "database locked");
! 	    exit(1);
! 	} else if (err) {
! 	    com_err(progname, err, "while trying to lock '%s'", dbpathname);
! 	    exit(1);
! 	}	    
! 	fd = fileno(dbfp);
  	if (fstat(fd, &stbuf)) {
  		com_err(progname, errno, "while trying to stat %s",
  			data_fn);
***************
*** 469,474 ****
- --- 463,485 ----
  	}
  	*size = stbuf.st_size;
  	return(fd);
+ }
+ 
+ void
+ close_database(fd)
+ int fd;
+ {
+     int err;
+     if (fd != fileno(dbfp)) {
+ 	com_err(progname, 0, "bad fd passed to close_database");
+ 	exit(1);
+     }
+     err = krb5_file_lock(dbfp, dbpathname, KRB5_LOCKMODE_UNLOCK);
+     if (err)
+ 	com_err(progname, err, "while unlocking database '%s'", dbpathname);
+     free(dbpathname);
+     (void) fclose(dbfp);
+     return;
  }
  
  /*
===================================================================
RCS file: slave/kpropd.c,v
retrieving revision 1.1
diff -c -r1.1 slave/kpropd.c
*** 1.1	1994/06/15 02:58:59
- --- slave/kpropd.c	1994/06/15 04:39:37
***************
*** 42,47 ****
- --- 42,48 ----
  #include <krb5/kdb_dbm.h>
  #include <krb5/ext-proto.h>
  #include <krb5/los-proto.h>
+ #include <krb5/libos.h>
  #include <com_err.h>
  #include <errno.h>
  
***************
*** 198,204 ****
  	int on = 1, fromlen;
  	struct hostent	*hp;
  	krb5_error_code	retval;
! 	int	lock_fd;
  
  	fromlen = sizeof (from);
  	if (getpeername(fd, (struct sockaddr *) &from, &fromlen) < 0) {
- --- 199,206 ----
  	int on = 1, fromlen;
  	struct hostent	*hp;
  	krb5_error_code	retval;
! 	FILE *lock_fp;
! 	int omask;
  
  	fromlen = sizeof (from);
  	if (getpeername(fd, (struct sockaddr *) &from, &fromlen) < 0) {
***************
*** 247,283 ****
  		printf("My sequence number: %d\n", my_seq_num);
  		printf("His sequence number: %d\n", his_seq_num);
  	}
! 	if ((lock_fd = (open(temp_file_name, O_WRONLY | O_CREAT, 0600))) < 0) {
! 		com_err(progname, errno,
! 			"while opening database file, '%s'",
! 			temp_file_name);
! 		exit(1);
! 	}
! #ifdef POSIX_FILE_LOCKS
! 	{
! 		int lock_cmd = F_SETLK;
! 		struct flock lock_arg;
! 
! 		lock_arg.l_type = F_WRLCK;
! 		lock_arg.l_whence = 0;
! 		lock_arg.l_start = 0;
! 		lock_arg.l_len = 0;
! 		
! 		if (fcntl(lock_fd, lock_cmd, &lock_arg) == -1) {
! 			/* see POSIX/IEEE 1003.1-1988, 6.5.2.4 */
! 			if (errno == EACCES || errno == EAGAIN)
! 				errno = EAGAIN;
! 			com_err(progname, errno, "while trying to lock '%s'",
! 				temp_file_name);
! 		}
  	}
- - #else
- - 	if (flock(lock_fd, LOCK_EX | LOCK_NB)) {
- - 		com_err(progname, errno, "while trying to lock '%s'",
- - 			temp_file_name);
- - 		exit(1);
- - 	}
- - #endif
  	if ((database_fd = open(temp_file_name,
  				O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
  		com_err(progname, errno,
- --- 249,264 ----
  		printf("My sequence number: %d\n", my_seq_num);
  		printf("His sequence number: %d\n", his_seq_num);
  	}
! 	omask = umask(077);
! 	lock_fp = fopen(temp_file_name, "a");
! 	(void) umask(omask);
! 	retval = krb5_lock_file(lock_fp, temp_file_name,
! 				KRB5_LOCKMODE_EXCLUSIVE|KRB5_LOCKMODE_DONTBLOCK);
! 	if (retval) {
! 	    com_err(progname, retval, "while trying to lock '%s'",
! 		    temp_file_name);
! 	    exit(1);
  	}
  	if ((database_fd = open(temp_file_name,
  				O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
  		com_err(progname, errno,
***************
*** 297,303 ****
  		exit(1);
  	}
  	load_database(kdb5_edit, file);
! 	close(lock_fd);
  	exit(0);
  }
  
- --- 278,289 ----
  		exit(1);
  	}
  	load_database(kdb5_edit, file);
! 	retval = krb5_lock_file(lock_fp, temp_file_name, KRB5_LOCKMODE_UNLOCK);
! 	if (retval) {
! 	    com_err(progname, retval, "while unlocking '%s'", temp_file_name);
! 	    exit(1);
! 	}
! 	(void) fclose(lock_fp);
  	exit(0);
  }
  
===================================================================
RCS file: lib/kdb/kdb_dbm.c,v
retrieving revision 1.1
diff -c -r1.1 lib/kdb/kdb_dbm.c
*** 1.1	1994/06/15 02:26:29
- --- lib/kdb/kdb_dbm.c	1994/06/15 04:40:19
***************
*** 34,39 ****
- --- 34,40 ----
  #include <krb5/kdb_dbm.h>
  #include <krb5/ext-proto.h>
  #include <krb5/sysincl.h>
+ #include <krb5/libos.h>			/* lock stuff */
  #include <stdio.h>
  #include <errno.h>
  
***************
*** 41,50 ****
  #include <sys/time.h>
  #endif
  
- - #if defined (POSIX_FILE_LOCKS) && !defined(unicos61)
- - #include <fcntl.h>
- - #endif
- - 
  #define KRB5_DBM_MAX_RETRY 5
  
  /* exclusive or shared lock flags */
- --- 42,47 ----
***************
*** 57,63 ****
  extern char *progname;
  #endif
  
! static int dblfd = -1;
  static int mylock = 0;
  static int lockmode = 0;
  static int inited = 0;
- --- 54,61 ----
  extern char *progname;
  #endif
  
! static FILE *dblfp = 0;
! static char *dblfname = 0;
  static int mylock = 0;
  static int lockmode = 0;
  static int inited = 0;
***************
*** 187,211 ****
      filename = gen_dbsuffix (current_db_name, ".ok");
      if (!filename)
  	return ENOMEM;
- - #ifdef POSIX_FILE_LOCKS
      /*
!      * needs be open read/write so that write locking can work with
       * POSIX systems
       */
!     if ((dblfd = open(filename, O_RDWR, 0)) == -1) {
  	if (errno == EACCES) {
! 	    if ((dblfd = open(filename, O_RDONLY, 0)) == -1)
  		goto err_out;
  	} else
  	    goto err_out;
      }
- - #else
- -     if ((dblfd = open(filename, 0, 0)) == -1)
- - 	goto err_out;
- - #endif
      inited++;
      errno = 0;
!     
  err_out:
      free(filename);
      return (errno);
- --- 185,207 ----
      filename = gen_dbsuffix (current_db_name, ".ok");
      if (!filename)
  	return ENOMEM;
      /*
!      * should be open read/write so that write locking can work with
       * POSIX systems
       */
!     dblfp = fopen(filename, "r+");
!     if ((dblfp = fopen(filename, "r+")) == 0) {
  	if (errno == EACCES) {
! 	    if ((dblfp = fopen(filename, "r")) == 0)
  		goto err_out;
  	} else
  	    goto err_out;
      }
      inited++;
+     dblfname = filename;
      errno = 0;
!     return 0;
! 
  err_out:
      free(filename);
      return (errno);
***************
*** 232,242 ****
  	current_db_ptr = 0;
      }
  
!     if (close(dblfd) == -1)
  	retval = errno;
      else
  	retval = 0;
!     dblfd = -1;
      inited = 0;
      mylock = 0;
      return retval;
- --- 228,240 ----
  	current_db_ptr = 0;
      }
  
!     if (fclose(dblfp) == EOF)
  	retval = errno;
      else
  	retval = 0;
!     dblfp = 0;
!     free(dblfname);
!     dblfname = 0;
      inited = 0;
      mylock = 0;
      return retval;
***************
*** 782,833 ****
  krb5_dbm_db_lock(mode)
  int mode;
  {
! #ifdef POSIX_FILE_LOCKS
!     struct flock fl;
! #else
!     int flock_mode;
! #endif
      if (mylock && (lockmode >= mode)) {
  	    mylock++;		/* No need to upgrade lock, just return */
  	    return(0);
      }
  
- - #ifdef POSIX_FILE_LOCKS
- -     if (mode == KRB5_DBM_EXCLUSIVE)
- - 	fl.l_type = F_WRLCK;
- -     else if (mode == KRB5_DBM_SHARED)
- - 	fl.l_type = F_RDLCK;
- -     else
- - 	return KRB5_KDB_BADLOCKMODE;
- -     fl.l_whence = 0;
- -     fl.l_start = 0;
- -     fl.l_len = 0;
- -     if (fcntl(dblfd, non_blocking ? F_SETLK : F_SETLKW, &fl) == -1) {
- - 	if (errno == EBADF && mode == KRB5_DBM_EXCLUSIVE) {
- - 	    /* tried to exclusive-lock something we don't have write access
- - 	       to. */
- - 	    return KRB5_KDB_CANTLOCK_DB;
- - 	}
- - 	return errno;
- -     }
- - #else
      switch (mode) {
      case KRB5_DBM_EXCLUSIVE:
! 	flock_mode = LOCK_EX;
  	break;
      case KRB5_DBM_SHARED:
! 	flock_mode = LOCK_SH;
  	break;
      default:
  	return KRB5_KDB_BADLOCKMODE;
      }
- -     lockmode = mode;
      if (non_blocking)
! 	flock_mode |= LOCK_NB;
  
!     if (flock(dblfd, flock_mode) < 0) 
! 	return errno;
! #endif
      mylock++;
      return 0;
  }
- --- 780,811 ----
  krb5_dbm_db_lock(mode)
  int mode;
  {
!     int krb5_lock_mode;
!     int error;
      if (mylock && (lockmode >= mode)) {
  	    mylock++;		/* No need to upgrade lock, just return */
  	    return(0);
      }
  
      switch (mode) {
      case KRB5_DBM_EXCLUSIVE:
! 	krb5_lock_mode = KRB5_LOCKMODE_EXCLUSIVE;
  	break;
      case KRB5_DBM_SHARED:
! 	krb5_lock_mode = KRB5_LOCKMODE_SHARED;
  	break;
      default:
  	return KRB5_KDB_BADLOCKMODE;
      }
      if (non_blocking)
! 	krb5_lock_mode |= KRB5_LOCKMODE_DONTBLOCK;
  
!     error = krb5_lock_file(dblfp, dblfname, krb5_lock_mode);
! 
!     if (error == EBADF && mode == KRB5_DBM_EXCLUSIVE)
! 	return KRB5_KDB_CANTLOCK_DB;
!     if (error)
! 	return error;
      mylock++;
      return 0;
  }
***************
*** 838,857 ****
      if (!mylock)		/* lock already unlocked */
  	return KRB5_KDB_NOTLOCKED;
  
!     if (--mylock == 0) {
! #ifdef POSIX_FILE_LOCKS
! 	    struct flock fl;
! 	    fl.l_type = F_UNLCK;
! 	    fl.l_whence = 0;
! 	    fl.l_start = 0;
! 	    fl.l_len = 0;
! 	    if (fcntl(dblfd, F_SETLK, &fl) == -1)
! 		    return errno;
! #else
! 	    if (flock(dblfd, LOCK_UN) < 0)
! 		    return errno;
! #endif
!     }
      return 0;
  }
  
- --- 816,823 ----
      if (!mylock)		/* lock already unlocked */
  	return KRB5_KDB_NOTLOCKED;
  
!     if (--mylock == 0)
! 	return krb5_lock_file(dblfp, dblfname, KRB5_LOCKMODE_UNLOCK);
      return 0;
  }
  

-----BEGIN PGP SIGNATURE-----
Version: 2.6

iQCVAgUBLf6HDFTdX6I8ZiRnAQGMzAP+MvSl79JE14iw97T2L3dvHu538knv9/rF
97dVCkluNikw/t9Z2nzjavvwvdiBPa4lIGo6zflMFFjVQRmH99L6rI5BGkS+LH6L
Mm+dqD7ej/Dy/2G5MXAuc+cDy/oUckLsqX0JEs3IgyCpZcRfdau3JPbjjtTdPXRA
fY/DGMsYYWo=
=1Mbx
-----END PGP SIGNATURE-----

home help back first fref pref prev next nref lref last post