1 /* *************************************************************************
6 * This program creates a pipe for the chat process to read. The user
7 * can supply information (like a password) that will be picked up
8 * by chat and sent just like the regular contents of a chat script.
14 * where <filename> matches the option given in the chat script.
16 * for instance the chat script fragment:
23 * (note: leave some whitespace after the filename)
25 * expect "name:", reply with a delay followed by "myname"
26 * expect "word:", reply with the data read from the pipe /var/tmp/p
28 * the matching usage of chatchat would be:
34 * $chatchat /var/tmp/p
36 * some other process eventually starts:
38 * chat parses the "@/var/tmp/p" option and opens
42 * type PIN into SecurID card
43 * enter resulting passcode: [user inputs something]
45 * chat reads /var/tmp/p & gets what the
46 * user typed at chatchat's "enter string" prompt
47 * chat removes the pipe file
48 * chat sends the user's input as a response in
49 * place of "@/var/tmp/p"
53 * gcc -g -o chatchat chatchat.c
60 * see the man pages and documentation that come with the 'chat' program
61 * (part of the ppp package). you will need to use the modified chat
62 * program that accepts the '@' operator.
68 * STR Description Author
70 * 23-Mar-99 initial coding gpk
71 * 12-May-99 unlink the pipe after closing paulus
74 * This program is in the public domain.
77 * ************************************************************************* */
84 #include <sys/types.h>
90 /* MAXINPUT - the data typed into chatchat must be fewer */
91 /* characters than this. */
100 /* *************************************************************************
116 if the pipe file name is given on the command line,
117 create the pipe, prompt the user and put whatever
118 is typed into the pipe.
121 else # characters entered
136 STR Description of Revision Author
138 25-Mar-99 initial coding gpk
140 ************************************************************************* */
142 int main(int argc
, char * argv
[])
146 int create_and_write_pipe(char * pipename
);
150 fprintf(stderr
, "usage: %s pipename\n", argv
[0]);
155 retval
= create_and_write_pipe(argv
[1]);
163 /* *************************************************************************
166 NAME: create_and_write_pipe
174 some_int = create_and_write_pipe(pipename);
179 given the pipename, create the pipe, open it,
180 prompt the user for a string to put into the
181 pipe, write the string, and close the pipe
183 on error, print out an error message and return -1
186 else #bytes written into the pipe
201 STR Description of Revision Author
203 25-Mar-99 initial coding gpk
204 12-May-99 remove pipe after closing paulus
206 ************************************************************************* */
208 int create_and_write_pipe(char * pipename
)
210 int retval
, created
, pipefd
, nread
, nwritten
;
211 char input
[MAXINPUT
];
214 int create_pipe(char * pipename
);
215 int write_to_pipe(int pipefd
, char * input
, int nchar
);
217 created
= create_pipe(pipename
);
221 sprintf(errstring
, "unable to create pipe '%s'", pipename
);
228 /* note: this open won't succeed until chat has the pipe */
229 /* open and ready to read. this makes for nice timing. */
231 pipefd
= open(pipename
, O_WRONLY
);
235 sprintf(errstring
, "unable to open pipe '%s'", pipename
);
241 fprintf(stderr
, "%s \n %s",
242 "type PIN into SecurID card and",
243 "enter resulting passcode:");
244 nread
= read(STDIN_FILENO
, (void *)input
, MAXINPUT
);
249 perror("unable to read from stdin");
254 /* munch off the newline character, chat supplies */
255 /* a return when it sends the string out. */
258 nwritten
= write_to_pipe(pipefd
, input
, nread
);
259 /* printf("wrote [%d]: '%s'\n", nwritten, input); */
264 /* Now make the pipe go away. It won't actually go away
265 completely until chat closes it. */
266 if (unlink(pipename
) < 0)
267 perror("Warning: couldn't remove pipe");
279 /* *************************************************************************
290 some_int = create_pipe(pipename);
295 create a pipe of the given name
297 if there is an error (like the pipe already exists)
298 print an error message and return
300 return -1 on failure else success
316 STR Description of Revision Author
318 25-Mar-99 initial coding gpk
320 ************************************************************************* */
322 int create_pipe(char * pipename
)
327 /* hijack the umask temporarily to get the mode I want */
330 old_umask
= umask(000);
332 created
= mknod(pipename
, S_IFIFO
| S_IRWXU
| S_IWGRP
| S_IWOTH
,
335 /* now restore umask. */
337 (void)umask(old_umask
);
341 perror("unable to create pipe");
352 /* *************************************************************************
365 some_int = write_to_pipe(pipefd, input, nchar);
370 write nchars of data from input to pipefd
372 on error print a message to stderr
374 return -1 on error, else # bytes written
389 STR Description of Revision Author
391 25-Mar-99 initial coding gpk
392 12-May-99 don't write count word first paulus
394 ************************************************************************* */
396 int write_to_pipe(int pipefd
, char * input
, int nchar
)
400 /* nwritten = write(pipefd, (void *)&nchar, sizeof(nchar)); */
401 nwritten
= write(pipefd
, (void *)input
, nchar
);
405 perror("unable to write to pipe");