]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/mailto.c
2 * "mailto" notifier for CUPS.
4 * Copyright 2007-2011 by Apple Inc.
5 * Copyright 1997-2005 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
23 char mailtoCc
[1024]; /* Cc email address */
24 char mailtoFrom
[1024]; /* From email address */
25 char mailtoReplyTo
[1024]; /* Reply-To email address */
26 char mailtoSubject
[1024]; /* Subject prefix */
27 char mailtoSMTPServer
[1024]; /* SMTP server to use */
28 char mailtoSendmail
[1024]; /* Sendmail program to use */
35 void email_message(const char *to
, const char *subject
,
37 int load_configuration(void);
38 cups_file_t
*pipe_sendmail(const char *to
);
39 void print_attributes(ipp_t
*ipp
, int indent
);
43 * 'main()' - Main entry for the mailto notifier.
46 int /* O - Exit status */
47 main(int argc
, /* I - Number of command-line arguments */
48 char *argv
[]) /* I - Command-line arguments */
50 int i
; /* Looping var */
51 ipp_t
*msg
; /* Event message from scheduler */
52 ipp_state_t state
; /* IPP event state */
53 char *subject
, /* Subject for notification message */
54 *text
; /* Text for notification message */
55 cups_lang_t
*lang
; /* Language info */
56 char temp
[1024]; /* Temporary string */
57 int templen
; /* Length of temporary string */
58 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
59 struct sigaction action
; /* POSIX sigaction data */
60 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
64 * Don't buffer stderr...
70 * Ignore SIGPIPE signals...
74 sigset(SIGPIPE
, SIG_IGN
);
75 #elif defined(HAVE_SIGACTION)
76 memset(&action
, 0, sizeof(action
));
77 action
.sa_handler
= SIG_IGN
;
78 sigaction(SIGPIPE
, &action
, NULL
);
80 signal(SIGPIPE
, SIG_IGN
);
81 #endif /* HAVE_SIGSET */
84 * Validate command-line options...
89 fputs("Usage: mailto mailto:user@domain.com notify-user-data\n", stderr
);
93 if (strncmp(argv
[1], "mailto:", 7))
95 fprintf(stderr
, "ERROR: Bad recipient \"%s\"!\n", argv
[1]);
99 fprintf(stderr
, "DEBUG: argc=%d\n", argc
);
100 for (i
= 0; i
< argc
; i
++)
101 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
104 * Load configuration data...
107 if ((lang
= cupsLangDefault()) == NULL
)
110 if (!load_configuration())
114 * Get the reply-to address...
117 templen
= sizeof(temp
);
118 httpDecode64_2(temp
, &templen
, argv
[2]);
120 if (!strncmp(temp
, "mailto:", 7))
121 strlcpy(mailtoReplyTo
, temp
+ 7, sizeof(mailtoReplyTo
));
123 fprintf(stderr
, "WARNING: Bad notify-user-data value (%d bytes) ignored!\n",
127 * Loop forever until we run out of events...
133 * Get the next event...
137 while ((state
= ippReadFile(0, msg
)) != IPP_DATA
)
139 if (state
<= IPP_IDLE
)
143 fprintf(stderr
, "DEBUG: state=%d\n", state
);
145 if (state
== IPP_ERROR
)
146 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr
);
148 if (state
<= IPP_IDLE
)
151 * Out of messages, free memory and then exit...
159 * Get the subject and text for the message, then email it...
162 subject
= cupsNotifySubject(lang
, msg
);
163 text
= cupsNotifyText(lang
, msg
);
165 fprintf(stderr
, "DEBUG: subject=\"%s\"\n", subject
);
166 fprintf(stderr
, "DEBUG: text=\"%s\"\n", text
);
169 email_message(argv
[1] + 7, subject
, text
);
172 fputs("ERROR: Missing attributes in event notification!\n", stderr
);
173 print_attributes(msg
, 4);
177 * Free the memory used for this event...
192 * 'email_message()' - Email a notification message.
196 email_message(const char *to
, /* I - Recipient of message */
197 const char *subject
, /* I - Subject of message */
198 const char *text
) /* I - Text of message */
200 cups_file_t
*fp
; /* Pipe/socket to mail server */
201 const char *nl
; /* Newline to use */
202 char response
[1024]; /* SMTP response buffer */
206 * Connect to the mail server...
209 if (mailtoSendmail
[0])
212 * Use the sendmail command...
215 fp
= pipe_sendmail(to
);
225 * Use an SMTP server...
228 char hostbuf
[1024]; /* Local hostname */
231 if (strchr(mailtoSMTPServer
, ':'))
232 fp
= cupsFileOpen(mailtoSMTPServer
, "s");
235 char spec
[1024]; /* Host:service spec */
238 snprintf(spec
, sizeof(spec
), "%s:smtp", mailtoSMTPServer
);
239 fp
= cupsFileOpen(spec
, "s");
244 fprintf(stderr
, "ERROR: Unable to connect to SMTP server \"%s\"!\n",
249 fprintf(stderr
, "DEBUG: Connected to \"%s\"...\n", mailtoSMTPServer
);
251 cupsFilePrintf(fp
, "HELO %s\r\n",
252 httpGetHostname(NULL
, hostbuf
, sizeof(hostbuf
)));
253 fprintf(stderr
, "DEBUG: >>> HELO %s\n", hostbuf
);
255 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
257 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
259 cupsFilePrintf(fp
, "MAIL FROM:%s\r\n", mailtoFrom
);
260 fprintf(stderr
, "DEBUG: >>> MAIL FROM:%s\n", mailtoFrom
);
262 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
264 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
266 cupsFilePrintf(fp
, "RCPT TO:%s\r\n", to
);
267 fprintf(stderr
, "DEBUG: >>> RCPT TO:%s\n", to
);
269 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
271 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
273 cupsFilePuts(fp
, "DATA\r\n");
274 fputs("DEBUG: DATA\n", stderr
);
276 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
278 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
284 * Send the message...
287 cupsFilePrintf(fp
, "Date: %s%s", httpGetDateString(time(NULL
)), nl
);
288 cupsFilePrintf(fp
, "From: %s%s", mailtoFrom
, nl
);
289 cupsFilePrintf(fp
, "Subject: %s %s%s", mailtoSubject
, subject
, nl
);
290 if (mailtoReplyTo
[0])
292 cupsFilePrintf(fp
, "Sender: %s%s", mailtoReplyTo
, nl
);
293 cupsFilePrintf(fp
, "Reply-To: %s%s", mailtoReplyTo
, nl
);
295 cupsFilePrintf(fp
, "To: %s%s", to
, nl
);
297 cupsFilePrintf(fp
, "Cc: %s%s", mailtoCc
, nl
);
298 cupsFilePrintf(fp
, "Content-Type: text/plain%s", nl
);
299 cupsFilePuts(fp
, nl
);
300 cupsFilePrintf(fp
, "%s%s", text
, nl
);
301 cupsFilePrintf(fp
, ".%s", nl
);
304 * Close the connection to the mail server...
307 if (mailtoSendmail
[0])
310 * Close the pipe and wait for the sendmail command to finish...
313 int status
; /* Exit status */
318 while (wait(&status
))
322 fprintf(stderr
, "DEBUG: Unable to get child status: %s\n",
330 * Report any non-zero status...
335 if (WIFEXITED(status
))
336 fprintf(stderr
, "ERROR: Sendmail command returned status %d!\n",
337 WEXITSTATUS(status
));
339 fprintf(stderr
, "ERROR: Sendmail command crashed on signal %d!\n",
346 * Finish up the SMTP submission and close the connection...
349 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
351 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
354 * Process SMTP errors here...
359 cupsFilePuts(fp
, "QUIT\r\n");
360 fputs("DEBUG: QUIT\n", stderr
);
362 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
363 fprintf(stderr
, "ERROR: Got \"%s\" trying to QUIT connection.\n",
366 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
370 fprintf(stderr
, "DEBUG: Closed connection to \"%s\"...\n",
377 * 'load_configuration()' - Load the mailto.conf file.
380 int /* I - 1 on success, 0 on failure */
381 load_configuration(void)
383 cups_file_t
*fp
; /* mailto.conf file */
384 const char *server_root
, /* CUPS_SERVERROOT environment variable */
385 *server_admin
; /* SERVER_ADMIN environment variable */
386 char line
[1024], /* Line from file */
387 *value
; /* Value for directive */
388 int linenum
; /* Line number in file */
392 * Initialize defaults...
397 if ((server_admin
= getenv("SERVER_ADMIN")) != NULL
)
398 strlcpy(mailtoFrom
, server_admin
, sizeof(mailtoFrom
));
400 snprintf(mailtoFrom
, sizeof(mailtoFrom
), "root@%s",
401 httpGetHostname(NULL
, line
, sizeof(line
)));
403 strlcpy(mailtoSendmail
, "/usr/sbin/sendmail", sizeof(mailtoSendmail
));
405 mailtoSMTPServer
[0] = '\0';
407 mailtoSubject
[0] = '\0';
410 * Try loading the config file...
413 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
414 server_root
= CUPS_SERVERROOT
;
416 snprintf(line
, sizeof(line
), "%s/mailto.conf", server_root
);
418 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
422 fprintf(stderr
, "ERROR: Unable to open \"%s\" - %s\n", line
,
432 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
436 fprintf(stderr
, "ERROR: No value found for %s directive on line %d!\n",
442 if (!_cups_strcasecmp(line
, "Cc"))
443 strlcpy(mailtoCc
, value
, sizeof(mailtoCc
));
444 else if (!_cups_strcasecmp(line
, "From"))
445 strlcpy(mailtoFrom
, value
, sizeof(mailtoFrom
));
446 else if (!_cups_strcasecmp(line
, "Sendmail"))
448 strlcpy(mailtoSendmail
, value
, sizeof(mailtoSendmail
));
449 mailtoSMTPServer
[0] = '\0';
451 else if (!_cups_strcasecmp(line
, "SMTPServer"))
453 mailtoSendmail
[0] = '\0';
454 strlcpy(mailtoSMTPServer
, value
, sizeof(mailtoSMTPServer
));
456 else if (!_cups_strcasecmp(line
, "Subject"))
457 strlcpy(mailtoSubject
, value
, sizeof(mailtoSubject
));
461 "ERROR: Unknown configuration directive \"%s\" on line %d!\n",
467 * Close file and return...
477 * 'pipe_sendmail()' - Open a pipe to sendmail...
480 cups_file_t
* /* O - CUPS file */
481 pipe_sendmail(const char *to
) /* I - To: address */
483 cups_file_t
*fp
; /* CUPS file */
484 int pid
; /* Process ID */
485 int pipefds
[2]; /* Pipe file descriptors */
486 int argc
; /* Number of arguments */
487 char *argv
[100], /* Argument array */
488 line
[1024], /* Sendmail command + args */
489 *lineptr
; /* Pointer into line */
493 * First break the mailtoSendmail string into arguments...
496 strlcpy(line
, mailtoSendmail
, sizeof(line
));
500 for (lineptr
= strchr(line
, ' '); lineptr
; lineptr
= strchr(lineptr
, ' '))
502 while (*lineptr
== ' ')
508 * Point to the next argument...
511 argv
[argc
++] = lineptr
;
514 * Stop if we have too many...
517 if (argc
>= (int)(sizeof(argv
) / sizeof(argv
[0]) - 2))
522 argv
[argc
++] = (char *)to
;
531 perror("ERROR: Unable to create pipe");
536 * Then run the command...
539 if ((pid
= fork()) == 0)
542 * Child goes here - redirect stdin to the input side of the pipe,
543 * redirect stdout to stderr, and exec...
555 execvp(argv
[0], argv
);
561 * Unable to fork - error out...
564 perror("ERROR: Unable to fork command");
573 * Create a CUPS file using the output side of the pipe and close the
579 if ((fp
= cupsFileOpenFd(pipefds
[1], "w")) == NULL
)
581 int status
; /* Status of command */
593 * 'print_attributes()' - Print the attributes in a request...
597 print_attributes(ipp_t
*ipp
, /* I - IPP request */
598 int indent
) /* I - Indentation */
600 ipp_tag_t group
; /* Current group */
601 ipp_attribute_t
*attr
; /* Current attribute */
602 char buffer
[1024]; /* Value buffer */
605 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
607 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
609 group
= IPP_TAG_ZERO
;
614 if (group
!= attr
->group_tag
)
616 group
= attr
->group_tag
;
618 fprintf(stderr
, "DEBUG: %*s%s:\n\n", indent
- 4, "", ippTagString(group
));
621 ippAttributeString(attr
, buffer
, sizeof(buffer
));
623 fprintf(stderr
, "DEBUG: %*s%s (%s%s) %s", indent
, "", attr
->name
,
624 attr
->num_values
> 1 ? "1setOf " : "",
625 ippTagString(attr
->value_tag
), buffer
);