]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/mailto.c
2 * "mailto" notifier for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2005 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include <cups/cups-private.h>
24 char mailtoCc
[1024]; /* Cc email address */
25 char mailtoFrom
[1024]; /* From email address */
26 char mailtoReplyTo
[1024]; /* Reply-To email address */
27 char mailtoSubject
[1024]; /* Subject prefix */
28 char mailtoSMTPServer
[1024]; /* SMTP server to use */
29 char mailtoSendmail
[1024]; /* Sendmail program to use */
36 void email_message(const char *to
, const char *subject
, const char *text
);
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
, ':'))
233 fp
= cupsFileOpen(mailtoSMTPServer
, "s");
237 char spec
[1024]; /* Host:service spec */
240 snprintf(spec
, sizeof(spec
), "%s:smtp", mailtoSMTPServer
);
241 fp
= cupsFileOpen(spec
, "s");
246 fprintf(stderr
, "ERROR: Unable to connect to SMTP server \"%s\"!\n",
251 fprintf(stderr
, "DEBUG: Connected to \"%s\"...\n", mailtoSMTPServer
);
253 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
255 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
257 cupsFilePrintf(fp
, "HELO %s\r\n",
258 httpGetHostname(NULL
, hostbuf
, sizeof(hostbuf
)));
259 fprintf(stderr
, "DEBUG: >>> HELO %s\n", hostbuf
);
261 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
263 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
265 cupsFilePrintf(fp
, "MAIL FROM:%s\r\n", mailtoFrom
);
266 fprintf(stderr
, "DEBUG: >>> MAIL FROM:%s\n", mailtoFrom
);
268 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
270 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
272 cupsFilePrintf(fp
, "RCPT TO:%s\r\n", to
);
273 fprintf(stderr
, "DEBUG: >>> RCPT TO:%s\n", to
);
275 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
277 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
279 cupsFilePuts(fp
, "DATA\r\n");
280 fputs("DEBUG: DATA\n", stderr
);
282 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
284 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
290 * Send the message...
293 cupsFilePrintf(fp
, "Date: %s%s", httpGetDateString(time(NULL
)), nl
);
294 cupsFilePrintf(fp
, "From: %s%s", mailtoFrom
, nl
);
295 cupsFilePrintf(fp
, "Subject: %s %s%s", mailtoSubject
, subject
, nl
);
296 if (mailtoReplyTo
[0])
298 cupsFilePrintf(fp
, "Sender: %s%s", mailtoReplyTo
, nl
);
299 cupsFilePrintf(fp
, "Reply-To: %s%s", mailtoReplyTo
, nl
);
301 cupsFilePrintf(fp
, "To: %s%s", to
, nl
);
303 cupsFilePrintf(fp
, "Cc: %s%s", mailtoCc
, nl
);
304 cupsFilePrintf(fp
, "Content-Type: text/plain%s", nl
);
305 cupsFilePuts(fp
, nl
);
306 cupsFilePrintf(fp
, "%s%s", text
, nl
);
307 cupsFilePrintf(fp
, ".%s", nl
);
310 * Close the connection to the mail server...
313 if (mailtoSendmail
[0])
316 * Close the pipe and wait for the sendmail command to finish...
319 int status
; /* Exit status */
324 while (wait(&status
))
328 fprintf(stderr
, "DEBUG: Unable to get child status: %s\n",
336 * Report any non-zero status...
341 if (WIFEXITED(status
))
342 fprintf(stderr
, "ERROR: Sendmail command returned status %d!\n",
343 WEXITSTATUS(status
));
345 fprintf(stderr
, "ERROR: Sendmail command crashed on signal %d!\n",
352 * Finish up the SMTP submission and close the connection...
355 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
357 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
360 * Process SMTP errors here...
365 cupsFilePuts(fp
, "QUIT\r\n");
366 fputs("DEBUG: QUIT\n", stderr
);
368 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
369 fprintf(stderr
, "ERROR: Got \"%s\" trying to QUIT connection.\n",
372 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
376 fprintf(stderr
, "DEBUG: Closed connection to \"%s\"...\n",
383 * 'load_configuration()' - Load the mailto.conf file.
386 int /* I - 1 on success, 0 on failure */
387 load_configuration(void)
389 cups_file_t
*fp
; /* mailto.conf file */
390 const char *server_root
, /* CUPS_SERVERROOT environment variable */
391 *server_admin
; /* SERVER_ADMIN environment variable */
392 char line
[1024], /* Line from file */
393 *value
; /* Value for directive */
394 int linenum
; /* Line number in file */
398 * Initialize defaults...
403 if ((server_admin
= getenv("SERVER_ADMIN")) != NULL
)
404 strlcpy(mailtoFrom
, server_admin
, sizeof(mailtoFrom
));
406 snprintf(mailtoFrom
, sizeof(mailtoFrom
), "root@%s",
407 httpGetHostname(NULL
, line
, sizeof(line
)));
409 strlcpy(mailtoSendmail
, "/usr/sbin/sendmail", sizeof(mailtoSendmail
));
411 mailtoSMTPServer
[0] = '\0';
413 mailtoSubject
[0] = '\0';
416 * Try loading the config file...
419 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
420 server_root
= CUPS_SERVERROOT
;
422 snprintf(line
, sizeof(line
), "%s/mailto.conf", server_root
);
424 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
428 fprintf(stderr
, "ERROR: Unable to open \"%s\" - %s\n", line
,
438 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
442 fprintf(stderr
, "ERROR: No value found for %s directive on line %d!\n",
448 if (!_cups_strcasecmp(line
, "Cc"))
449 strlcpy(mailtoCc
, value
, sizeof(mailtoCc
));
450 else if (!_cups_strcasecmp(line
, "From"))
451 strlcpy(mailtoFrom
, value
, sizeof(mailtoFrom
));
452 else if (!_cups_strcasecmp(line
, "Sendmail"))
454 strlcpy(mailtoSendmail
, value
, sizeof(mailtoSendmail
));
455 mailtoSMTPServer
[0] = '\0';
457 else if (!_cups_strcasecmp(line
, "SMTPServer"))
459 mailtoSendmail
[0] = '\0';
460 strlcpy(mailtoSMTPServer
, value
, sizeof(mailtoSMTPServer
));
462 else if (!_cups_strcasecmp(line
, "Subject"))
463 strlcpy(mailtoSubject
, value
, sizeof(mailtoSubject
));
467 "ERROR: Unknown configuration directive \"%s\" on line %d!\n",
473 * Close file and return...
483 * 'pipe_sendmail()' - Open a pipe to sendmail...
486 cups_file_t
* /* O - CUPS file */
487 pipe_sendmail(const char *to
) /* I - To: address */
489 cups_file_t
*fp
; /* CUPS file */
490 int pid
; /* Process ID */
491 int pipefds
[2]; /* Pipe file descriptors */
492 int argc
; /* Number of arguments */
493 char *argv
[100], /* Argument array */
494 line
[1024], /* Sendmail command + args */
495 *lineptr
; /* Pointer into line */
499 * First break the mailtoSendmail string into arguments...
502 strlcpy(line
, mailtoSendmail
, sizeof(line
));
506 for (lineptr
= strchr(line
, ' '); lineptr
; lineptr
= strchr(lineptr
, ' '))
508 while (*lineptr
== ' ')
514 * Point to the next argument...
517 argv
[argc
++] = lineptr
;
520 * Stop if we have too many...
523 if (argc
>= (int)(sizeof(argv
) / sizeof(argv
[0]) - 2))
528 argv
[argc
++] = (char *)to
;
537 perror("ERROR: Unable to create pipe");
542 * Then run the command...
545 if ((pid
= fork()) == 0)
548 * Child goes here - redirect stdin to the input side of the pipe,
549 * redirect stdout to stderr, and exec...
561 execvp(argv
[0], argv
);
567 * Unable to fork - error out...
570 perror("ERROR: Unable to fork command");
579 * Create a CUPS file using the output side of the pipe and close the
585 if ((fp
= cupsFileOpenFd(pipefds
[1], "w")) == NULL
)
587 int status
; /* Status of command */
599 * 'print_attributes()' - Print the attributes in a request...
603 print_attributes(ipp_t
*ipp
, /* I - IPP request */
604 int indent
) /* I - Indentation */
606 ipp_tag_t group
; /* Current group */
607 ipp_attribute_t
*attr
; /* Current attribute */
608 char buffer
[1024]; /* Value buffer */
611 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
613 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
615 group
= IPP_TAG_ZERO
;
620 if (group
!= attr
->group_tag
)
622 group
= attr
->group_tag
;
624 fprintf(stderr
, "DEBUG: %*s%s:\n\n", indent
- 4, "", ippTagString(group
));
627 ippAttributeString(attr
, buffer
, sizeof(buffer
));
629 fprintf(stderr
, "DEBUG: %*s%s (%s%s) %s", indent
, "", attr
->name
,
630 attr
->num_values
> 1 ? "1setOf " : "",
631 ippTagString(attr
->value_tag
), buffer
);