Article View: news.software.nntp
Article #37371Re: NNTP authentication, revisited (C2 sec)
From: Doug S
Date: Tue, 11 May 1999 00:00
Date: Tue, 11 May 1999 00:00
119 lines
3978 bytes
3978 bytes
When I had to configure authentication into my news-server, I had the
complication that the news-server is on a different subnet than the
machine with the password files, and we don't use NIS.
I borrowed a trick from DNews and wrote a program that can check the
password via POP3. Source is attached, although I admit up-front that I
did not put a lot of scrutiny into handling things like buffer
overflows, interrupted reads, etc. It works for me.
/* this program accepts a user and password (on separate lines) from stdin
and checks the pop server to see if the ID is correct - DMS */
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <netdb.h>
#define POPSERVER "cc.ysu.edu"
/* we "should" use "getservbyname" to get this, but it won't move... */
#define POPPORT 110
#define BUFSIZE 128
int debug;
main(argc, argv)
int argc;
char *argv[];
{
struct sockaddr_in sa;
struct hostent *hp;
int s;
char user[BUFSIZE], pass[BUFSIZE], works[BUFSIZE], resp[BUFSIZE];
debug = 0;
if (strcmp(argv[1], "-d") == 0) debug = 1;
/* set up socket */
hp = gethostbyname(POPSERVER);
if (hp == NULL) {
fprintf(stderr,"DNS failure for %s\n",POPSERVER);
exit(1);
}
bzero(&sa,sizeof(struct sockaddr_in));
bcopy(hp->h_addr,(char *)&sa.sin_addr,hp->h_length);
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons(POPPORT);
if ((s = socket(AF_INET, SOCK_STREAM,0)) < 0) {
fprintf(stderr,"socket create error\n");
exit(2);
}
if (connect(s, &sa, sizeof sa) < 0) {
fprintf(stderr,"connect error\n");
close(s);
exit(3);
}
/* get user and password from stdin so it won't show up in "ps" */
gets(user);
gets(pass);
/* read initial welcome */
if (debug) fprintf(stderr,"waiting for welcome banner\n");
read_data(s,resp,BUFSIZE);
if (debug) fprintf(stderr,"welcome: %s",resp);
if (resp[0] == "-") {
quit_conn(s,resp);
exit(4);
}
/* send the "user" string */
sprintf(works,"user %s\n",user);
if (debug) fprintf(stderr,"sending %s",works);
write(s,works,strlen(works));
read_data(s,resp,BUFSIZE);
if (debug) fprintf(stderr,"response: %s",resp);
if (resp[0] == "-") {
quit_conn(s,resp);
exit(4);
}
/* send the "pass" string */
sprintf(works,"pass %s\n",pass);
if (debug) fprintf(stderr,"sending %s",works);
write(s,works,strlen(works));
read_data(s,resp,BUFSIZE);
if (debug) fprintf(stderr,"response: %s", resp);
if (resp[0] == '-') {
quit_conn(s,resp);
if (debug) fprintf(stderr,"bad password\n");
exit(100);
}
/* close up shop */
if (debug) fprintf(stderr,"password ok\n");
quit_conn(s,resp);
exit(0);
}
/* I really *should* loop in this function in case read() gets interrupted,
but since we're only dealing with short strings - under 80 chars - big deal */
int read_data(s, buf, n)
int s;
char *buf;
int n;
{
int br;
br = read(s,buf,n);
if (br == (BUFSIZE)) buf[BUFSIZE-1] = 0;
else buf[br] = 0;
return(br);
}
int quit_conn(s,resp)
int s;
char *resp;
{
char quit[6];
sprintf(quit,"quit\n");
if (debug) fprintf(stderr,"sending quit\n");
write(s,quit,5);
read_data(s,resp,BUFSIZE); /* quit response */
if (debug) fprintf(stderr,"connection closed\n");
close(s);
}
--
Doug S. (doug@cc.ysu.edu) (http://cc.ysu.edu/~doug/)
The shadow of a dog never bit anyone -- Kenneth Copeland
Technical Details
Message-ID:
<7h9gh0$1g6c@unix1.cc.ysu.edu>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mailReferences:
<7h7u9l$t3v$1@nnrp1.deja.com> <ylyaiwtmbz.fsf@windlord.stanford.edu>Show Full Headers
Complete Article Headers
Message-ID: <7h9gh0$1g6c@unix1.cc.ysu.edu> Subject: Re: NNTP authentication, revisited (C2 sec) Date: Tue, 11 May 1999 00:00:00 +0000 From: doug@cc.ysu.edu (Doug S) References: <7h7u9l$t3v$1@nnrp1.deja.com> <ylyaiwtmbz.fsf@windlord.stanford.edu> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail X-Deja-AN: 476614243 Organization: Youngstown State University Newsgroups: news.software.nntp