2 * "$Id: mailto.c 9793 2011-05-20 03:49:49Z mike $"
4 * "mailto" notifier for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Main entry for the mailto notifier.
18 * email_message() - Email a notification message.
19 * load_configuration() - Load the mailto.conf file.
20 * pipe_sendmail() - Open a pipe to sendmail...
21 * print_attributes() - Print the attributes in a request...
25 * Include necessary headers...
28 #include <cups/cups-private.h>
37 char mailtoCc[1024]; /* Cc email address */
38 char mailtoFrom[1024]; /* From email address */
39 char mailtoReplyTo[1024]; /* Reply-To email address */
40 char mailtoSubject[1024]; /* Subject prefix */
41 char mailtoSMTPServer[1024]; /* SMTP server to use */
42 char mailtoSendmail[1024]; /* Sendmail program to use */
49 void email_message(const char *to, const char *subject,
51 int load_configuration(void);
52 cups_file_t *pipe_sendmail(const char *to);
53 void print_attributes(ipp_t *ipp, int indent);
57 * 'main()' - Main entry for the mailto notifier.
60 int /* O - Exit status */
61 main(int argc, /* I - Number of command-line arguments */
62 char *argv[]) /* I - Command-line arguments */
64 int i; /* Looping var */
65 ipp_t *msg; /* Event message from scheduler */
66 ipp_state_t state; /* IPP event state */
67 char *subject, /* Subject for notification message */
68 *text; /* Text for notification message */
69 cups_lang_t *lang; /* Language info */
70 char temp[1024]; /* Temporary string */
71 int templen; /* Length of temporary string */
72 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
73 struct sigaction action; /* POSIX sigaction data */
74 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
78 * Don't buffer stderr...
84 * Ignore SIGPIPE signals...
88 sigset(SIGPIPE, SIG_IGN);
89 #elif defined(HAVE_SIGACTION)
90 memset(&action, 0, sizeof(action));
91 action.sa_handler = SIG_IGN;
92 sigaction(SIGPIPE, &action, NULL);
94 signal(SIGPIPE, SIG_IGN);
95 #endif /* HAVE_SIGSET */
98 * Validate command-line options...
103 fputs("Usage: mailto mailto:user@domain.com notify-user-data\n", stderr);
107 if (strncmp(argv[1], "mailto:", 7))
109 fprintf(stderr, "ERROR: Bad recipient \"%s\"!\n", argv[1]);
113 fprintf(stderr, "DEBUG: argc=%d\n", argc);
114 for (i = 0; i < argc; i ++)
115 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
118 * Load configuration data...
121 if ((lang = cupsLangDefault()) == NULL)
124 if (!load_configuration())
128 * Get the reply-to address...
131 templen = sizeof(temp);
132 httpDecode64_2(temp, &templen, argv[2]);
134 if (!strncmp(temp, "mailto:", 7))
135 strlcpy(mailtoReplyTo, temp + 7, sizeof(mailtoReplyTo));
137 fprintf(stderr, "WARNING: Bad notify-user-data value (%d bytes) ignored!\n",
141 * Loop forever until we run out of events...
147 * Get the next event...
151 while ((state = ippReadFile(0, msg)) != IPP_DATA)
153 if (state <= IPP_IDLE)
157 fprintf(stderr, "DEBUG: state=%d\n", state);
159 if (state == IPP_ERROR)
160 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr);
162 if (state <= IPP_IDLE)
165 * Out of messages, free memory and then exit...
173 * Get the subject and text for the message, then email it...
176 subject = cupsNotifySubject(lang, msg);
177 text = cupsNotifyText(lang, msg);
179 fprintf(stderr, "DEBUG: subject=\"%s\"\n", subject);
180 fprintf(stderr, "DEBUG: text=\"%s\"\n", text);
183 email_message(argv[1] + 7, subject, text);
186 fputs("ERROR: Missing attributes in event notification!\n", stderr);
187 print_attributes(msg, 4);
191 * Free the memory used for this event...
206 * 'email_message()' - Email a notification message.
210 email_message(const char *to, /* I - Recipient of message */
211 const char *subject, /* I - Subject of message */
212 const char *text) /* I - Text of message */
214 cups_file_t *fp; /* Pipe/socket to mail server */
215 const char *nl; /* Newline to use */
216 char response[1024]; /* SMTP response buffer */
220 * Connect to the mail server...
223 if (mailtoSendmail[0])
226 * Use the sendmail command...
229 fp = pipe_sendmail(to);
239 * Use an SMTP server...
242 char hostbuf[1024]; /* Local hostname */
245 if (strchr(mailtoSMTPServer, ':'))
246 fp = cupsFileOpen(mailtoSMTPServer, "s");
249 char spec[1024]; /* Host:service spec */
252 snprintf(spec, sizeof(spec), "%s:smtp", mailtoSMTPServer);
253 fp = cupsFileOpen(spec, "s");
258 fprintf(stderr, "ERROR: Unable to connect to SMTP server \"%s\"!\n",
263 fprintf(stderr, "DEBUG: Connected to \"%s\"...\n", mailtoSMTPServer);
265 cupsFilePrintf(fp, "HELO %s\r\n",
266 httpGetHostname(NULL, hostbuf, sizeof(hostbuf)));
267 fprintf(stderr, "DEBUG: >>> HELO %s\n", hostbuf);
269 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
271 fprintf(stderr, "DEBUG: <<< %s\n", response);
273 cupsFilePrintf(fp, "MAIL FROM:%s\r\n", mailtoFrom);
274 fprintf(stderr, "DEBUG: >>> MAIL FROM:%s\n", mailtoFrom);
276 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
278 fprintf(stderr, "DEBUG: <<< %s\n", response);
280 cupsFilePrintf(fp, "RCPT TO:%s\r\n", to);
281 fprintf(stderr, "DEBUG: >>> RCPT TO:%s\n", to);
283 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
285 fprintf(stderr, "DEBUG: <<< %s\n", response);
287 cupsFilePuts(fp, "DATA\r\n");
288 fputs("DEBUG: DATA\n", stderr);
290 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
292 fprintf(stderr, "DEBUG: <<< %s\n", response);
298 * Send the message...
301 cupsFilePrintf(fp, "Date: %s%s", httpGetDateString(time(NULL)), nl);
302 cupsFilePrintf(fp, "From: %s%s", mailtoFrom, nl);
303 cupsFilePrintf(fp, "Subject: %s %s%s", mailtoSubject, subject, nl);
304 if (mailtoReplyTo[0])
306 cupsFilePrintf(fp, "Sender: %s%s", mailtoReplyTo, nl);
307 cupsFilePrintf(fp, "Reply-To: %s%s", mailtoReplyTo, nl);
309 cupsFilePrintf(fp, "To: %s%s", to, nl);
311 cupsFilePrintf(fp, "Cc: %s%s", mailtoCc, nl);
312 cupsFilePrintf(fp, "Content-Type: text/plain%s", nl);
313 cupsFilePuts(fp, nl);
314 cupsFilePrintf(fp, "%s%s", text, nl);
315 cupsFilePrintf(fp, ".%s", nl);
318 * Close the connection to the mail server...
321 if (mailtoSendmail[0])
324 * Close the pipe and wait for the sendmail command to finish...
327 int status; /* Exit status */
332 while (wait(&status))
336 fprintf(stderr, "DEBUG: Unable to get child status: %s\n",
344 * Report any non-zero status...
349 if (WIFEXITED(status))
350 fprintf(stderr, "ERROR: Sendmail command returned status %d!\n",
351 WEXITSTATUS(status));
353 fprintf(stderr, "ERROR: Sendmail command crashed on signal %d!\n",
360 * Finish up the SMTP submission and close the connection...
363 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
365 fprintf(stderr, "DEBUG: <<< %s\n", response);
368 * Process SMTP errors here...
373 cupsFilePuts(fp, "QUIT\r\n");
374 fputs("DEBUG: QUIT\n", stderr);
376 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
377 fprintf(stderr, "ERROR: Got \"%s\" trying to QUIT connection.\n",
380 fprintf(stderr, "DEBUG: <<< %s\n", response);
384 fprintf(stderr, "DEBUG: Closed connection to \"%s\"...\n",
391 * 'load_configuration()' - Load the mailto.conf file.
394 int /* I - 1 on success, 0 on failure */
395 load_configuration(void)
397 cups_file_t *fp; /* mailto.conf file */
398 const char *server_root, /* CUPS_SERVERROOT environment variable */
399 *server_admin; /* SERVER_ADMIN environment variable */
400 char line[1024], /* Line from file */
401 *value; /* Value for directive */
402 int linenum; /* Line number in file */
406 * Initialize defaults...
411 if ((server_admin = getenv("SERVER_ADMIN")) != NULL)
412 strlcpy(mailtoFrom, server_admin, sizeof(mailtoFrom));
414 snprintf(mailtoFrom, sizeof(mailtoFrom), "root@%s",
415 httpGetHostname(NULL, line, sizeof(line)));
417 strlcpy(mailtoSendmail, "/usr/sbin/sendmail", sizeof(mailtoSendmail));
419 mailtoSMTPServer[0] = '\0';
421 mailtoSubject[0] = '\0';
424 * Try loading the config file...
427 if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
428 server_root = CUPS_SERVERROOT;
430 snprintf(line, sizeof(line), "%s/mailto.conf", server_root);
432 if ((fp = cupsFileOpen(line, "r")) == NULL)
436 fprintf(stderr, "ERROR: Unable to open \"%s\" - %s\n", line,
446 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
450 fprintf(stderr, "ERROR: No value found for %s directive on line %d!\n",
456 if (!_cups_strcasecmp(line, "Cc"))
457 strlcpy(mailtoCc, value, sizeof(mailtoCc));
458 else if (!_cups_strcasecmp(line, "From"))
459 strlcpy(mailtoFrom, value, sizeof(mailtoFrom));
460 else if (!_cups_strcasecmp(line, "Sendmail"))
462 strlcpy(mailtoSendmail, value, sizeof(mailtoSendmail));
463 mailtoSMTPServer[0] = '\0';
465 else if (!_cups_strcasecmp(line, "SMTPServer"))
467 mailtoSendmail[0] = '\0';
468 strlcpy(mailtoSMTPServer, value, sizeof(mailtoSMTPServer));
470 else if (!_cups_strcasecmp(line, "Subject"))
471 strlcpy(mailtoSubject, value, sizeof(mailtoSubject));
475 "ERROR: Unknown configuration directive \"%s\" on line %d!\n",
481 * Close file and return...
491 * 'pipe_sendmail()' - Open a pipe to sendmail...
494 cups_file_t * /* O - CUPS file */
495 pipe_sendmail(const char *to) /* I - To: address */
497 cups_file_t *fp; /* CUPS file */
498 int pid; /* Process ID */
499 int pipefds[2]; /* Pipe file descriptors */
500 int argc; /* Number of arguments */
501 char *argv[100], /* Argument array */
502 line[1024], /* Sendmail command + args */
503 *lineptr; /* Pointer into line */
507 * First break the mailtoSendmail string into arguments...
510 strlcpy(line, mailtoSendmail, sizeof(line));
514 for (lineptr = strchr(line, ' '); lineptr; lineptr = strchr(lineptr, ' '))
516 while (*lineptr == ' ')
522 * Point to the next argument...
525 argv[argc ++] = lineptr;
528 * Stop if we have too many...
531 if (argc >= (int)(sizeof(argv) / sizeof(argv[0]) - 2))
536 argv[argc ++] = (char *)to;
545 perror("ERROR: Unable to create pipe");
550 * Then run the command...
553 if ((pid = fork()) == 0)
556 * Child goes here - redirect stdin to the input side of the pipe,
557 * redirect stdout to stderr, and exec...
569 execvp(argv[0], argv);
575 * Unable to fork - error out...
578 perror("ERROR: Unable to fork command");
587 * Create a CUPS file using the output side of the pipe and close the
593 if ((fp = cupsFileOpenFd(pipefds[1], "w")) == NULL)
595 int status; /* Status of command */
607 * 'print_attributes()' - Print the attributes in a request...
611 print_attributes(ipp_t *ipp, /* I - IPP request */
612 int indent) /* I - Indentation */
614 ipp_tag_t group; /* Current group */
615 ipp_attribute_t *attr; /* Current attribute */
616 char buffer[1024]; /* Value buffer */
619 for (group = IPP_TAG_ZERO, attr = ipp->attrs; attr; attr = attr->next)
621 if ((attr->group_tag == IPP_TAG_ZERO && indent <= 8) || !attr->name)
623 group = IPP_TAG_ZERO;
628 if (group != attr->group_tag)
630 group = attr->group_tag;
632 fprintf(stderr, "DEBUG: %*s%s:\n\n", indent - 4, "", ippTagString(group));
635 _ippAttrString(attr, buffer, sizeof(buffer));
637 fprintf(stderr, "DEBUG: %*s%s (%s%s) %s", indent, "", attr->name,
638 attr->num_values > 1 ? "1setOf " : "",
639 ippTagString(attr->value_tag), buffer);
645 * End of "$Id: mailto.c 9793 2011-05-20 03:49:49Z mike $".