diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2001-05-19 18:57:55 +0000 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2001-05-19 18:57:55 +0000 |
commit | 1d9ffea78f7b1777eb8c9728af876dc145427121 (patch) | |
tree | 94d48368ab1040083cc1dffad63be0c475531165 /src/crypt.c | |
parent | 8781af4e9fcecc6550e75e76e7f75239be020d4d (diff) | |
download | gnutls-1d9ffea78f7b1777eb8c9728af876dc145427121.tar.gz |
updated
Diffstat (limited to 'src/crypt.c')
-rw-r--r-- | src/crypt.c | 99 |
1 files changed, 93 insertions, 6 deletions
diff --git a/src/crypt.c b/src/crypt.c index 52abd0c560..8750b396d2 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -76,6 +76,39 @@ static int _verify_passwd_int(char* username, char* passwd, char* salt, MPI g, M return -1; } +static int filecopy( char* src, char* dst) { +FILE *fd, *fd2; +char line[5*1024]; +char *p; + + fd = fopen(dst, "w"); + if (fd == NULL) { + fprintf(stderr, "Cannot open '%s' for write\n", + dst); + return -1; + } + + fd2 = fopen(src, "r"); + if (fd2 == NULL) { + /* empty file */ + fclose(fd); + return 0; + } + + line[sizeof(line)-1] = 0; + do { + p = fgets( line, sizeof(line)-1, fd2); + if (p==NULL) break; + + fputs( line, fd); + } while(1); + + fclose(fd); + fclose(fd2); + + return 0; +} + /* accepts password file */ static int find_index(char* username, char* file) { FILE * fd; @@ -254,8 +287,9 @@ int crypt_int(char *username, char *passwd, int crypt, int salt, char *cr; MPI g, n; char line[5 * 1024]; - char *p; + char *p, *pp; int iindex; + char tmpname[1024]; fd = fopen(tpasswd_conf, "r"); if (fd == NULL) { @@ -286,18 +320,71 @@ int crypt_int(char *username, char *passwd, int crypt, int salt, fprintf(stderr, "Cannot gnutls_crypt()...\n"); return -1; } else { -#warning "FIXME: DELETE PREVIOUS ENTRY" /* delete previous entry */ + struct stat st; + FILE * fd2; + int put; + + if (strlen(tpasswd) > sizeof(tmpname)+5) { + fprintf(stderr, "file '%s' is tooooo long\n", tpasswd); + return -1; + } + strcpy( tmpname, tpasswd); + strcat( tmpname, ".tmp"); + + if ( stat( tmpname, &st) != -1) { + fprintf(stderr, "file '%s' is locked\n", tpasswd); + return -1; + } + + if (filecopy( tpasswd, tmpname)!=0) { + fprintf(stderr, "Cannot copy '%s' to '%s'\n", + tpasswd, tmpname); + return -1; + } - fd = fopen(tpasswd, "a"); + fd = fopen(tpasswd, "w"); if (fd == NULL) { - fprintf(stderr, "Cannot open '%s' for append\n", + fprintf(stderr, "Cannot open '%s' for write\n", tpasswd); + remove(tmpname); return -1; } - fprintf(fd, "%s:%s:%u\n", username, cr, iindex); - fclose(fd); + + fd2 = fopen(tmpname, "r"); + if (fd2 == NULL) { + fprintf(stderr, "Cannot open '%s' for read\n", + tmpname); + remove(tmpname); + return -1; + } + + put = 0; + do { + p = fgets( line, sizeof(line)-1, fd2); + if (p==NULL) break; + + pp = index( line, ':'); + if (pp==NULL) continue; + + if ( strncmp( p, username, _MAX(strlen(username), (int)(pp-p)) ) == 0 ) { + put = 1; + fprintf(fd, "%s:%s:%u\n", username, cr, iindex); + } else { + fputs( line, fd); + } + } while(1); + + if (put==0) { + fprintf(fd, "%s:%s:%u\n", username, cr, iindex); + } free(cr); + + fclose(fd); + fclose(fd2); + + remove(tmpname); + } |