From 3b2decebe9c24651fe0bd3fe5844a0080b5f2ff9 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Thu, 10 Jun 1999 03:39:50 +0000 Subject: [PATCH] * Replaced all code that generated temporary filenames with calls to tmpnam(). Rewrote using tmpfile() where possible. --- citadel/ChangeLog | 4 + citadel/aidepost.c | 12 +- citadel/citadel.c | 13 +- citadel/citserver.c | 2 +- citadel/cux2ascii.c | 297 ++++++++++++++++++++++++------------------ citadel/mailinglist.c | 2 +- citadel/messages.c | 7 +- citadel/msgbase.c | 13 +- citadel/netmailer.c | 7 +- citadel/netpoll.c | 5 +- citadel/netproc.c | 35 +++-- citadel/rcit.c | 4 +- citadel/setup.c | 2 +- 13 files changed, 217 insertions(+), 186 deletions(-) diff --git a/citadel/ChangeLog b/citadel/ChangeLog index b4169858c..cc152a6bb 100644 --- a/citadel/ChangeLog +++ b/citadel/ChangeLog @@ -1,3 +1,7 @@ +Wed Jun 9 23:34:25 EDT 1999 Art Cancro + * Replaced all code that generated temporary filenames with calls to + tmpnam(). Rewrote using tmpfile() where possible. + Thu Jun 3 11:35:18 EDT 1999 Art Cancro * base64.c: mapped fi and fo to stdin and stdout using actual code rather than assignment at declaration time diff --git a/citadel/aidepost.c b/citadel/aidepost.c index 9cdb92a36..1e25cd3a3 100644 --- a/citadel/aidepost.c +++ b/citadel/aidepost.c @@ -51,8 +51,6 @@ void make_message(FILE *fp, char *target_room) int main(int argc, char **argv) { - char tempbase[32]; - char temptmp[64]; char tempspool[64]; char target_room[ROOMNAMELEN]; FILE *tempfp, *spoolfp; @@ -74,18 +72,14 @@ int main(int argc, char **argv) } - snprintf(tempbase, sizeof tempbase, "ap.%d", getpid()); - snprintf(temptmp, sizeof temptmp, "/tmp/%s", tempbase); - snprintf(tempspool, sizeof tempspool, "./network/spoolin/%s", tempbase); + snprintf(tempspool, sizeof tempspool, "./network/spoolin/ap.%d", + getpid()); - tempfp = fopen(temptmp, "wb+"); + tempfp = tmpfile(); if (tempfp == NULL) { perror("cannot open temp file"); exit(errno); } - /* Unlink the temp file, so it automatically gets deleted by the OS if - * this program is interrupted or crashes. - */ unlink(temptmp); /* Generate a message from stdin */ make_message(tempfp, target_room); diff --git a/citadel/citadel.c b/citadel/citadel.c index 36e878d97..79d403b65 100644 --- a/citadel/citadel.c +++ b/citadel/citadel.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -53,9 +54,9 @@ struct march { struct march *march = NULL; /* globals associated with the client program */ -char temp[16]; /* Name of general temp file */ -char temp2[16]; /* Name of general temp file */ -char tempdir[16]; /* Name of general temp dir */ +char temp[PATH_MAX]; /* Name of general temp file */ +char temp2[PATH_MAX]; /* Name of general temp file */ +char tempdir[PATH_MAX]; /* Name of general temp dir */ char editor_path[256]; /* path to external editor */ char printcmd[256]; /* print command */ int editor_pid = (-1); @@ -968,9 +969,9 @@ int main(int argc, char **argv) * program. Don't mess with these once they've been set, because we * will be unlinking them later on in the program and we don't * want to delete something that we didn't create. */ - snprintf(temp, sizeof temp, "/tmp/citA%d", getpid()); - snprintf(temp2, sizeof temp2, "/tmp/citB%d", getpid()); - snprintf(tempdir, sizeof tempdir, "/tmp/citC%d", getpid()); + snprintf(temp, sizeof temp, tmpnam(NULL)); + snprintf(temp2, sizeof temp2, tmpnam(NULL)); + snprintf(tempdir, sizeof tempdir, tmpnam(NULL)); /* Get screen dimensions. First we go to a default of 80x24. Then * we try to get the user's actual screen dimensions off the server. diff --git a/citadel/citserver.c b/citadel/citserver.c index 44a98d021..693357e3d 100644 --- a/citadel/citserver.c +++ b/citadel/citserver.c @@ -824,7 +824,7 @@ void *context_loop(struct CitContext *con) strcpy(CC->cs_clientname, "(unknown)"); strcpy(CC->curr_user,"(not logged in)"); strcpy(CC->net_node,""); - snprintf(CC->temp, sizeof CC->temp, "/tmp/CitServer.%d.%d", getpid(), CC->cs_pid); + snprintf(CC->temp, sizeof CC->temp, tmpnam(NULL)); strcpy(CC->cs_room, "(no room)"); strncpy(CC->cs_host, config.c_fqdn, sizeof CC->cs_host); CC->cs_host[sizeof CC->cs_host - 1] = 0; diff --git a/citadel/cux2ascii.c b/citadel/cux2ascii.c index c69d96f26..281ea7a84 100644 --- a/citadel/cux2ascii.c +++ b/citadel/cux2ascii.c @@ -21,164 +21,207 @@ void get_config(); struct config config; -main(argc,argv) +main(argc, argv) int argc; -char *argv[]; { +char *argv[]; +{ struct tm *tm; - int a,b,e,mtype,aflag; - char bbb[100],ngn[100]; + int a, b, e, mtype, aflag; + char bbb[100], ngn[100]; long tmid; char tsid[64]; char tuid[64]; - FILE *fp,*tfp; - long now,msglen; - char tflnm[16]; + FILE *fp, *tfp; + long now, msglen; int cuunbatch = 0; /* experimental cuunbatch header generation */ - for (a=0; a\n",tmid,tsid); - fprintf(tfp,"\n"); - if (aflag!=1) fmout(80,fp,tfp); - else while(a=getc(fp), a>0) { - putc(a,tfp); if (a==13) putc(10,tfp); + fp = stdin; + while (1) { + do { + e = getc(fp); + if (e < 0) + exit(0); + } while (e != 255); + mtype = getc(fp); + aflag = getc(fp); + + tmid = 0L; + strcpy(tsid, FQDN); + strcpy(tuid, "postmaster"); + + tfp = tmpfile(); + do { + b = getc(fp); + if (b == 'M') { + fprintf(tfp, "Message-ID: <%ld@%s>\n", tmid, tsid); + fprintf(tfp, "\n"); + if (aflag != 1) + fmout(80, fp, tfp); + else + while (a = getc(fp), a > 0) { + putc(a, tfp); + if (a == 13) + putc(10, tfp); + } } - } - if ((b!='M')&&(b>0)) fpgetfield(fp,bbb); - if (b=='I') tmid=atol(bbb); - if (b=='N') { - strcpy(tsid,bbb); - if (!strcmp(tsid,NODENAME)) strcpy(tsid,FQDN); - for (a=0; a0)); - msglen=ftell(tfp); - fclose(tfp); + if ((b != 'M') && (b > 0)) + fpgetfield(fp, bbb); + if (b == 'I') + tmid = atol(bbb); + if (b == 'N') { + strcpy(tsid, bbb); + if (!strcmp(tsid, NODENAME)) + strcpy(tsid, FQDN); + for (a = 0; a < strlen(tuid); ++a) + if (tuid[a] == ' ') + tuid[a] = '_'; + fprintf(tfp, "From: %s@%s ", tuid, tsid); + for (a = 0; a < strlen(tuid); ++a) + if (tuid[a] == '_') + tuid[a] = ' '; + fprintf(tfp, "(%s)\n", tuid); + } + if (b == 'P') + fprintf(tfp, "Path: %s\n", bbb); + if (b == 'A') + strcpy(tuid, bbb); + if (b == 'O') { + xref(bbb, ngn); + fprintf(tfp, "Newsgroups: %s\n", ngn); + } + if (b == 'R') + fprintf(tfp, "To: %s\n", bbb); + if (b == 'U') + fprintf(tfp, "Subject: %s\n", bbb); + if (b == 'T') { + now = atol(bbb); + tm = (struct tm *) localtime(&now); + fprintf(tfp, "Date: %s", asctime(tm)); + } + } while ((b != 'M') && (b > 0)); + msglen = ftell(tfp); - if (cuunbatch) { - printf("#! cuunbatch %ld\n",msglen); - } - else { - printf("#! rnews %ld\n",msglen); + if (cuunbatch) { + printf("#! cuunbatch %ld\n", msglen); + } else { + printf("#! rnews %ld\n", msglen); } - tfp=fopen(tflnm,"r"); - while(msglen--) putc(getc(tfp),stdout); - fclose(tfp); - unlink(tflnm); - } -exit(0); + rewind(tfp); + while (msglen--) + putc(getc(tfp), stdout); + fclose(tfp); + } + exit(0); } -fpgetfield(fp,string) /* level-2 break out next null-terminated string */ +fpgetfield(fp, string) /* level-2 break out next null-terminated string */ FILE *fp; char string[]; { -int a,b; -strcpy(string,""); -a=0; + int a, b; + strcpy(string, ""); + a = 0; do { - b=getc(fp); - if (b<1) { string[a]=0; return(0); } - string[a]=b; + b = getc(fp); + if (b < 1) { + string[a] = 0; + return (0); + } + string[a] = b; ++a; - } while(b!=0); - return(0); + } while (b != 0); + return (0); } -fmout(width,fp,mout) +fmout(width, fp, mout) int width; -FILE *fp,*mout; - { - int a,b,c,real,old; +FILE *fp, *mout; +{ + int a, b, c, real, old; char aaa[140]; - - strcpy(aaa,""); old=255; - c=1; /* c is the current pos */ -FMTA: old=real; a=getc(fp); real=a; - if (a<=0) goto FMTEND; - - if ( ((a==13)||(a==10)) && (old!=13) && (old!=10) ) a=32; - if ( ((old==13)||(old==10)) && (isspace(real)) ) { - fprintf(mout,"\n"); c=1; } - if (a>126) goto FMTA; - - if (a>32) { - if ( ((strlen(aaa)+c)>(width-1)) && (strlen(aaa)>(width-1)) ) - { fprintf(mout,"\n%s",aaa); c=strlen(aaa); aaa[0]=0; } - b=strlen(aaa); aaa[b]=a; aaa[b+1]=0; } - if (a==32) { if ((strlen(aaa)+c)>(width-1)) { - fprintf(mout,"\n"); - c=1; - } - fprintf(mout,"%s ",aaa); ++c; c=c+strlen(aaa); - strcpy(aaa,""); goto FMTA; } - if ((a==13)||(a==10)) { - fprintf(mout,"%s\n",aaa); c=1; - strcpy(aaa,""); goto FMTA; } + + strcpy(aaa, ""); + old = 255; + c = 1; /* c is the current pos */ + FMTA:old = real; + a = getc(fp); + real = a; + if (a <= 0) + goto FMTEND; + + if (((a == 13) || (a == 10)) && (old != 13) && (old != 10)) + a = 32; + if (((old == 13) || (old == 10)) && (isspace(real))) { + fprintf(mout, "\n"); + c = 1; + } + if (a > 126) + goto FMTA; + + if (a > 32) { + if (((strlen(aaa) + c) > (width - 1)) && (strlen(aaa) > (width - 1))) { + fprintf(mout, "\n%s", aaa); + c = strlen(aaa); + aaa[0] = 0; + } + b = strlen(aaa); + aaa[b] = a; + aaa[b + 1] = 0; + } + if (a == 32) { + if ((strlen(aaa) + c) > (width - 1)) { + fprintf(mout, "\n"); + c = 1; + } + fprintf(mout, "%s ", aaa); + ++c; + c = c + strlen(aaa); + strcpy(aaa, ""); + goto FMTA; + } + if ((a == 13) || (a == 10)) { + fprintf(mout, "%s\n", aaa); + c = 1; + strcpy(aaa, ""); + goto FMTA; + } goto FMTA; -FMTEND: fprintf(mout,"\n"); - return(0); + FMTEND:fprintf(mout, "\n"); + return (0); } -xref(roomname,newsgroup) -char *roomname,*newsgroup; { +xref(roomname, newsgroup) +char *roomname, *newsgroup; +{ char tbuf[128]; FILE *fp; - int commapos,a; - - strcpy(newsgroup,roomname); - fp=fopen("./network/rnews.xref","r"); - if (fp==NULL) return(1); - while (fgets(tbuf,128,fp)!=NULL) { - tbuf[strlen(tbuf)-1]=0; /* strip off the newline */ - a=strlen(tbuf); - while (a--) if (tbuf[a]==',') commapos=a; - tbuf[commapos]=0; - if (!strcasecmp(&tbuf[commapos+1],roomname)) - strcpy(newsgroup,tbuf); - } - fclose(fp); - return(0); + int commapos, a; + + strcpy(newsgroup, roomname); + fp = fopen("./network/rnews.xref", "r"); + if (fp == NULL) + return (1); + while (fgets(tbuf, 128, fp) != NULL) { + tbuf[strlen(tbuf) - 1] = 0; /* strip off the newline */ + a = strlen(tbuf); + while (a--) + if (tbuf[a] == ',') + commapos = a; + tbuf[commapos] = 0; + if (!strcasecmp(&tbuf[commapos + 1], roomname)) + strcpy(newsgroup, tbuf); } + fclose(fp); + return (0); +} diff --git a/citadel/mailinglist.c b/citadel/mailinglist.c index 9bd5e087c..796bd665e 100644 --- a/citadel/mailinglist.c +++ b/citadel/mailinglist.c @@ -83,7 +83,7 @@ int main(void) { get_config(); LoadInternetConfig(); - sprintf(tempfile, "/tmp/mlist.%ld", (long)getpid() ); + sprintf(tempfile, tmpnam(NULL)); while(1) { diff --git a/citadel/messages.c b/citadel/messages.c index 2749d39b0..0b5bee04c 100644 --- a/citadel/messages.c +++ b/citadel/messages.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -913,7 +914,7 @@ void readmsgs(int c, int rdir, int q) /* read contents of a room */ char arcflag = 0; char quotflag = 0; int hold_color = 0; - char prtfile[16]; + char prtfile[PATH_MAX]; char pagin; char cmd[256]; char targ[ROOMNAMELEN]; @@ -925,7 +926,7 @@ void readmsgs(int c, int rdir, int q) /* read contents of a room */ if (c<0) b=(MAXMSGS-1); else b=0; - sprintf(prtfile,"/tmp/CPrt%ld",(long)getpid()); + sprintf(prtfile, tmpnam(NULL)); num_msgs = 0; strcpy(cmd,"MSGS "); @@ -1000,7 +1001,7 @@ RMSGREAD: fflush(stdout); enable_color = hold_color; f=fork(); if (f==0) { - freopen(prtfile,"r",stdin); + freopen(prtfile, "r", stdin); sttybbs(SB_RESTORE); ka_system(printcmd); sttybbs(SB_NO_INTR); diff --git a/citadel/msgbase.c b/citadel/msgbase.c index 7d325aeba..aca331a54 100644 --- a/citadel/msgbase.c +++ b/citadel/msgbase.c @@ -364,26 +364,15 @@ void fixed_output(char *name, char *filename, char *partnum, char *disp, void mime_download(char *name, char *filename, char *partnum, char *disp, void *content, char *cbtype, size_t length) { - char tmpname[PATH_MAX]; - static int seq = 0; - /* Silently go away if there's already a download open... */ if (CC->download_fp != NULL) return; /* ...or if this is not the desired section */ if (strcasecmp(desired_section, partnum)) return; - snprintf(tmpname, sizeof tmpname, - "/tmp/CitServer.download.%4x.%4x", getpid(), ++seq); - - CC->download_fp = fopen(tmpname, "wb+"); + CC->download_fp = tmpfile(); if (CC->download_fp == NULL) return; - /* Unlink the file while it's open, to guarantee that the - * temp file will always be deleted. - */ - unlink(tmpname); - fwrite(content, length, 1, CC->download_fp); fflush(CC->download_fp); rewind(CC->download_fp); diff --git a/citadel/netmailer.c b/citadel/netmailer.c index 42da180be..08540e5b3 100644 --- a/citadel/netmailer.c +++ b/citadel/netmailer.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include "citadel.h" @@ -20,7 +21,7 @@ void LoadInternetConfig(void); void get_config(void); struct config config; -char temp[32]; +char temp[PATH_MAX]; char ALIASES[128]; char CIT86NET[128]; @@ -173,7 +174,7 @@ int main(int argc, char **argv) openlog("netmailer", LOG_PID, LOG_USER); get_config(); LoadInternetConfig(); - sprintf(temp,"/tmp/netmailer.%ld",(long)getpid()); /* temp file name */ + sprintf(temp, tmpnam(NULL)); /* temp file name */ if ( (argc < 2) || (argc > 3) ) { fprintf(stderr, "netmailer: usage: netmailer recipient@node.dom [mlist]\n"); @@ -191,7 +192,7 @@ int main(int argc, char **argv) } /* convert to ASCII & get info */ - fp=fopen(temp,"w"); + fp=fopen(temp, "w"); msgform(argv[1], fp, sbuf, rbuf, nbuf, pbuf, &mid_buf, rmname, subject); fclose(fp); diff --git a/citadel/netpoll.c b/citadel/netpoll.c index d9d018cef..a69ec8747 100644 --- a/citadel/netpoll.c +++ b/citadel/netpoll.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "citadel.h" #include "tools.h" @@ -40,11 +41,11 @@ void receive_spool(void) long bytes_received; char buf[256]; static char pbuf[IGNET_PACKET_SIZE]; - char tempfilename[64]; + char tempfilename[PATH_MAX]; long plen; FILE *fp; - sprintf(tempfilename, "/tmp/netpoll.%ld", (long) getpid()); + sprintf(tempfilename, tmpnam(NULL)); serv_puts("NDOP"); serv_gets(buf); printf("%s\n", buf); diff --git a/citadel/netproc.c b/citadel/netproc.c index 7a137c222..9f6321115 100644 --- a/citadel/netproc.c +++ b/citadel/netproc.c @@ -120,7 +120,7 @@ int load_syslist(void) { char insys = 0; char buf[128]; - fp=fopen("network/mail.sysinfo","rb"); + fp=fopen("network/mail.sysinfo", "r"); if (fp==NULL) return(1); while(1) { @@ -658,9 +658,9 @@ void inprocess(void) { long msglen; int bloklen; - - sprintf(tname,"/tmp/net.t%ld",(long)getpid()); /* temp file name */ - sprintf(iname,"/tmp/net.i%ld",(long)getpid()); /* temp file name */ + /* temp file names */ + sprintf(tname, tmpnam(NULL)); + sprintf(iname, tmpnam(NULL)); load_filterlist(); @@ -703,7 +703,7 @@ NXMSG: /* Seek to the beginning of the next message */ } while((a!=255)&&(a>=0)); if (a<0) goto ENDSTR; - message = fopen(tname,"wb"); /* This crates the temporary file. */ + message = fopen(tname, "wb"); /* This crates the temporary file. */ if (message == NULL) { syslog(LOG_ERR, "error creating %s: %s", tname,strerror(errno)); goto ENDSTR; @@ -938,14 +938,14 @@ int ismsgok(long int mpos, FILE *mmfp, char *sysname) return(ok); } -int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list of messages to a file */ - /* returns # of msgs spooled */ - - + + +/* spool list of messages to a file */ +/* returns # of msgs spooled */ +int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) { struct msglist *cmptr; FILE *mmfp; - char mmtemp[128]; char fbuf[128]; int a; int msgs_spooled = 0; @@ -956,7 +956,6 @@ int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list char curr_rm[256]; strcpy(curr_rm, ""); - sprintf(mmtemp, "/tmp/net.m%ld", (long)getpid()); /* for each message in the list... */ for (cmptr=cmlist; cmptr!=NULL; cmptr=cmptr->next) { @@ -975,7 +974,7 @@ int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list } /* download the message from the server... */ - mmfp = fopen(mmtemp, "wb"); + mmfp = tmpfile(); sprintf(buf, "MSG3 %ld", cmptr->m_num); serv_puts(buf); serv_gets(buf); @@ -991,9 +990,8 @@ int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list else { /* or print the err */ syslog(LOG_ERR, "%s", buf); } - fclose(mmfp); - - mmfp = fopen(mmtemp,"rb"); + + rewind(mmfp); if (ismsgok(0L,mmfp,sysname)) { ++msgs_spooled; @@ -1021,7 +1019,6 @@ int spool_out(struct msglist *cmlist, FILE *destfp, char *sysname) /* spool list fclose(mmfp); } - unlink(mmtemp); return(msgs_spooled); } @@ -1041,9 +1038,9 @@ void outprocess(char *sysname) /* send new room messages to sysname */ int outgoing_msgs; long thismsg; - sprintf(tempflnm,"/tmp/%s.%ld",NODENAME,(long)getpid()); - tempflfp=fopen(tempflnm,"w"); - if (tempflfp==NULL) return; + sprintf(tempflnm, tmpnam(NULL)); + tempflfp = fopen(tempflnm,"w"); + if (tempflfp == NULL) return; /* diff --git a/citadel/rcit.c b/citadel/rcit.c index 81dec46fa..7ccd116e9 100644 --- a/citadel/rcit.c +++ b/citadel/rcit.c @@ -85,8 +85,8 @@ int main(int argc, char **argv) char spool_only = 0; get_config(); - sprintf(flnm,"./network/spoolin/rnews.%ld",(long)getpid()); - sprintf(tname,"/tmp/rnews.%ld",(long)getpid()); + sprintf(flnm,"./network/spoolin/rnews.%d", getpid()); + sprintf(tname, tmpnam(NULL)); for (a=1; a