* `LICENSE' that comes with the fcron source distribution.
*/
- /* $Id: fcrontab.c,v 1.59 2002-08-30 20:04:49 thib Exp $ */
+ /* $Id: fcrontab.c,v 1.60 2002-09-07 13:10:50 thib Exp $ */
/*
* The goal of this program is simple : giving a user interface to fcron
#include "temp_file.h"
#include "read_string.h"
-char rcs_info[] = "$Id: fcrontab.c,v 1.59 2002-08-30 20:04:49 thib Exp $";
+char rcs_info[] = "$Id: fcrontab.c,v 1.60 2002-09-07 13:10:50 thib Exp $";
void info(void);
void usage(void);
copy(char *orig, char *dest)
/* copy orig file to dest */
{
- FILE *from = NULL, *to = NULL;
+ FILE *from = NULL;
+ int to_fd;
int c;
if ( (from = fopen(orig, "r")) == NULL) {
error_e("seteuid(fcrontab_uid[%d])", fcrontab_uid);
}
#endif
- if ((to = fopen(dest, "w")) == NULL) {
+ to_fd = open(dest, O_CREAT | O_TRUNC | O_SYNC);
+ if (to_fd == -1) {
error_e("copy: dest");
return ERR;
}
die_e("seteuid(uid[%d])", uid);
#endif
if (asuid == ROOTUID ) {
- if ( fchmod(fileno(to), S_IWUSR | S_IRUSR) != 0 )
+ if ( fchmod(to_fd, S_IWUSR | S_IRUSR) != 0 )
error_e("Could not fchmod %s to 600", dest);
- if ( fchown(fileno(to), ROOTUID, fcrontab_gid) != 0 )
+ if ( fchown(to_fd, ROOTUID, fcrontab_gid) != 0 )
error_e("Could not fchown %s to root", dest);
}
while ( (c = getc(from)) != EOF )
- if ( putc(c, to) == EOF ) {
+ if ( write(to_fd, &c, 1) != 1 ) {
error("Error while copying file. Aborting.\n");
+ fclose(from);
+ close(to_fd);
return ERR;
}
fclose(from);
- fclose(to);
+ close(to_fd);
return OK;
}
/* note : the binary fcrontab is removed by fcron */
{
int return_val = OK;
- FILE *f;
+ int fd;
if ( rm_orig )
explain("removing %s's fcrontab", user);
/* finally create a file in order to tell the daemon
* a file was removed, and launch a signal to daemon */
snprintf(buf, sizeof(buf), "rm.%s", user);
- f = fopen(buf, "w");
- fclose(f);
+ fd = open(buf, O_CREAT | O_TRUNC);
+ if ( fd == -1 )
+ error_e("Can't create file %s", buf);
+ close(fd);
need_sig = 1;
goto exiterr;
}
}
-
+ else {
+ /* Some programs, like perl, require gid=egid : */
+ if ( setgid(getgid()) < 0 ) {
+ error_e("setgid(getgid())");
+ goto exiterr;
+ }
+ }
snprintf(editorcmd, sizeof(editorcmd), "%s %s", cureditor, tmp_str);
execlp(shell, shell, "-c", editorcmd, tmp_str, NULL);
error_e("Error while running \"%s\"", cureditor);
close(fd);
goto exiterr;
}
- if ( fchown(fd, ROOTUID, ROOTGID) != 0 || fchmod(fd, S_IRUSR|S_IWUSR) != 0 ) {
+ if ( fchown(fd, ROOTUID, ROOTGID) != 0 || fchmod(fd, S_IRUSR|S_IWUSR) != 0 ){
fprintf(stderr, "Can't chown or chmod %s.\n", tmp_str);
close(fd);
goto exiterr;
usage(); break;
case 'u':
- if (getuid() != ROOTUID) {
+ if (uid != ROOTUID) {
fprintf(stderr, "must be privileged to use -u\n");
xexit(EXIT_ERR);
}
else
usage();
- if (getuid() != ROOTUID) {
+ if (uid != ROOTUID) {
fprintf(stderr, "must be privileged to use -u\n");
xexit(EXIT_ERR);
}
if ( list_opt + rm_opt + edit_opt + reinstall_opt == 0 )
file_opt = optind;
else {
- if (getuid() != ROOTUID) {
+ if (uid != ROOTUID) {
fprintf(stderr, "must be privileged to use [user|-u user]\n");
xexit(EXIT_ERR);
}