]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/mailto.c
2 * "$Id: mailto.c 7460 2008-04-16 02:19:54Z mike $"
4 * "mailto" notifier for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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.h>
29 #include <cups/i18n.h>
30 #include <cups/string.h>
40 char mailtoCc
[1024]; /* Cc email address */
41 char mailtoFrom
[1024]; /* From email address */
42 char mailtoReplyTo
[1024]; /* Reply-To email address */
43 char mailtoSubject
[1024]; /* Subject prefix */
44 char mailtoSMTPServer
[1024]; /* SMTP server to use */
45 char mailtoSendmail
[1024]; /* Sendmail program to use */
52 void email_message(const char *to
, const char *subject
,
54 int load_configuration(void);
55 cups_file_t
*pipe_sendmail(const char *to
);
56 void print_attributes(ipp_t
*ipp
, int indent
);
60 * 'main()' - Main entry for the mailto notifier.
63 int /* O - Exit status */
64 main(int argc
, /* I - Number of command-line arguments */
65 char *argv
[]) /* I - Command-line arguments */
67 int i
; /* Looping var */
68 ipp_t
*msg
; /* Event message from scheduler */
69 ipp_state_t state
; /* IPP event state */
70 char *subject
, /* Subject for notification message */
71 *text
; /* Text for notification message */
72 cups_lang_t
*lang
; /* Language info */
73 char temp
[1024]; /* Temporary string */
74 int templen
; /* Length of temporary string */
75 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
76 struct sigaction action
; /* POSIX sigaction data */
77 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
81 * Don't buffer stderr...
87 * Ignore SIGPIPE signals...
91 sigset(SIGPIPE
, SIG_IGN
);
92 #elif defined(HAVE_SIGACTION)
93 memset(&action
, 0, sizeof(action
));
94 action
.sa_handler
= SIG_IGN
;
95 sigaction(SIGPIPE
, &action
, NULL
);
97 signal(SIGPIPE
, SIG_IGN
);
98 #endif /* HAVE_SIGSET */
101 * Validate command-line options...
106 fputs("Usage: mailto mailto:user@domain.com notify-user-data\n", stderr
);
110 if (strncmp(argv
[1], "mailto:", 7))
112 fprintf(stderr
, "ERROR: Bad recipient \"%s\"!\n", argv
[1]);
116 fprintf(stderr
, "DEBUG: argc=%d\n", argc
);
117 for (i
= 0; i
< argc
; i
++)
118 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
121 * Load configuration data...
124 if ((lang
= cupsLangDefault()) == NULL
)
127 if (!load_configuration())
131 * Get the reply-to address...
134 templen
= sizeof(temp
);
135 httpDecode64_2(temp
, &templen
, argv
[2]);
137 if (!strncmp(temp
, "mailto:", 7))
138 strlcpy(mailtoReplyTo
, temp
+ 7, sizeof(mailtoReplyTo
));
140 fprintf(stderr
, "WARNING: Bad notify-user-data value (%d bytes) ignored!\n",
144 * Loop forever until we run out of events...
150 * Get the next event...
154 while ((state
= ippReadFile(0, msg
)) != IPP_DATA
)
156 if (state
<= IPP_IDLE
)
160 fprintf(stderr
, "DEBUG: state=%d\n", state
);
162 if (state
== IPP_ERROR
)
163 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr
);
165 if (state
<= IPP_IDLE
)
168 * Out of messages, free memory and then exit...
176 * Get the subject and text for the message, then email it...
179 subject
= cupsNotifySubject(lang
, msg
);
180 text
= cupsNotifyText(lang
, msg
);
182 fprintf(stderr
, "DEBUG: subject=\"%s\"\n", subject
);
183 fprintf(stderr
, "DEBUG: text=\"%s\"\n", text
);
186 email_message(argv
[1] + 7, subject
, text
);
189 fputs("ERROR: Missing attributes in event notification!\n", stderr
);
190 print_attributes(msg
, 4);
194 * Free the memory used for this event...
209 * 'email_message()' - Email a notification message.
213 email_message(const char *to
, /* I - Recipient of message */
214 const char *subject
, /* I - Subject of message */
215 const char *text
) /* I - Text of message */
217 cups_file_t
*fp
; /* Pipe/socket to mail server */
218 const char *nl
; /* Newline to use */
219 char response
[1024]; /* SMTP response buffer */
223 * Connect to the mail server...
226 if (mailtoSendmail
[0])
229 * Use the sendmail command...
232 fp
= pipe_sendmail(to
);
242 * Use an SMTP server...
245 char hostbuf
[1024]; /* Local hostname */
248 if (strchr(mailtoSMTPServer
, ':'))
249 fp
= cupsFileOpen(mailtoSMTPServer
, "s");
252 char spec
[1024]; /* Host:service spec */
255 snprintf(spec
, sizeof(spec
), "%s:smtp", mailtoSMTPServer
);
256 fp
= cupsFileOpen(spec
, "s");
261 fprintf(stderr
, "ERROR: Unable to connect to SMTP server \"%s\"!\n",
266 fprintf(stderr
, "DEBUG: Connected to \"%s\"...\n", mailtoSMTPServer
);
268 cupsFilePrintf(fp
, "HELO %s\r\n",
269 httpGetHostname(NULL
, hostbuf
, sizeof(hostbuf
)));
270 fprintf(stderr
, "DEBUG: >>> HELO %s\n", hostbuf
);
272 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
274 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
276 cupsFilePrintf(fp
, "MAIL FROM:%s\r\n", mailtoFrom
);
277 fprintf(stderr
, "DEBUG: >>> MAIL FROM:%s\n", mailtoFrom
);
279 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
281 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
283 cupsFilePrintf(fp
, "RCPT TO:%s\r\n", to
);
284 fprintf(stderr
, "DEBUG: >>> RCPT TO:%s\n", to
);
286 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
288 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
290 cupsFilePuts(fp
, "DATA\r\n");
291 fputs("DEBUG: DATA\n", stderr
);
293 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
295 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
301 * Send the message...
304 cupsFilePrintf(fp
, "Date: %s%s", httpGetDateString(time(NULL
)), nl
);
305 cupsFilePrintf(fp
, "From: %s%s", mailtoFrom
, nl
);
306 cupsFilePrintf(fp
, "Subject: %s %s%s", mailtoSubject
, subject
, nl
);
307 if (mailtoReplyTo
[0])
309 cupsFilePrintf(fp
, "Sender: %s%s", mailtoReplyTo
, nl
);
310 cupsFilePrintf(fp
, "Reply-To: %s%s", mailtoReplyTo
, nl
);
312 cupsFilePrintf(fp
, "To: %s%s", to
, nl
);
314 cupsFilePrintf(fp
, "Cc: %s%s", mailtoCc
, nl
);
315 cupsFilePrintf(fp
, "Content-Type: text/plain%s", nl
);
316 cupsFilePuts(fp
, nl
);
317 cupsFilePrintf(fp
, "%s%s", text
, nl
);
318 cupsFilePrintf(fp
, ".%s", nl
);
321 * Close the connection to the mail server...
324 if (mailtoSendmail
[0])
327 * Close the pipe and wait for the sendmail command to finish...
330 int status
; /* Exit status */
339 * Report any non-zero status...
344 if (WIFEXITED(status
))
345 fprintf(stderr
, "ERROR: Sendmail command returned status %d!\n",
346 WEXITSTATUS(status
));
348 fprintf(stderr
, "ERROR: Sendmail command crashed on signal %d!\n",
355 * Finish up the SMTP submission and close the connection...
358 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
360 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
363 * Process SMTP errors here...
368 cupsFilePuts(fp
, "QUIT\r\n");
369 fputs("DEBUG: QUIT\n", stderr
);
371 if (!cupsFileGets(fp
, response
, sizeof(response
)) || atoi(response
) >= 500)
373 fprintf(stderr
, "DEBUG: <<< %s\n", response
);
377 fprintf(stderr
, "DEBUG: Closed connection to \"%s\"...\n",
384 * 'load_configuration()' - Load the mailto.conf file.
387 int /* I - 1 on success, 0 on failure */
388 load_configuration(void)
390 cups_file_t
*fp
; /* mailto.conf file */
391 const char *server_root
, /* CUPS_SERVERROOT environment variable */
392 *server_admin
; /* SERVER_ADMIN environment variable */
393 char line
[1024], /* Line from file */
394 *value
; /* Value for directive */
395 int linenum
; /* Line number in file */
399 * Initialize defaults...
404 if ((server_admin
= getenv("SERVER_ADMIN")) != NULL
)
405 strlcpy(mailtoFrom
, server_admin
, sizeof(mailtoFrom
));
407 snprintf(mailtoFrom
, sizeof(mailtoFrom
), "root@%s",
408 httpGetHostname(NULL
, line
, sizeof(line
)));
410 strlcpy(mailtoSendmail
, "/usr/sbin/sendmail", sizeof(mailtoSendmail
));
412 mailtoSMTPServer
[0] = '\0';
414 mailtoSubject
[0] = '\0';
417 * Try loading the config file...
420 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
421 server_root
= CUPS_SERVERROOT
;
423 snprintf(line
, sizeof(line
), "%s/mailto.conf", server_root
);
425 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
427 fprintf(stderr
, "ERROR: Unable to open \"%s\" - %s\n", line
,
434 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
438 fprintf(stderr
, "ERROR: No value found for %s directive on line %d!\n",
444 if (!strcasecmp(line
, "Cc"))
445 strlcpy(mailtoCc
, value
, sizeof(mailtoCc
));
446 else if (!strcasecmp(line
, "From"))
447 strlcpy(mailtoFrom
, value
, sizeof(mailtoFrom
));
448 else if (!strcasecmp(line
, "Sendmail"))
450 strlcpy(mailtoSendmail
, value
, sizeof(mailtoSendmail
));
451 mailtoSMTPServer
[0] = '\0';
453 else if (!strcasecmp(line
, "SMTPServer"))
455 mailtoSendmail
[0] = '\0';
456 strlcpy(mailtoSMTPServer
, value
, sizeof(mailtoSMTPServer
));
458 else if (!strcasecmp(line
, "Subject"))
459 strlcpy(mailtoSubject
, value
, sizeof(mailtoSubject
));
463 "ERROR: Unknown configuration directive \"%s\" on line %d!\n",
469 * Close file and return...
479 * 'pipe_sendmail()' - Open a pipe to sendmail...
482 cups_file_t
* /* O - CUPS file */
483 pipe_sendmail(const char *to
) /* I - To: address */
485 cups_file_t
*fp
; /* CUPS file */
486 int pid
; /* Process ID */
487 int pipefds
[2]; /* Pipe file descriptors */
488 int argc
; /* Number of arguments */
489 char *argv
[100], /* Argument array */
490 line
[1024], /* Sendmail command + args */
491 *lineptr
; /* Pointer into line */
495 * First break the mailtoSendmail string into arguments...
498 strlcpy(line
, mailtoSendmail
, sizeof(line
));
502 for (lineptr
= strchr(line
, ' '); lineptr
; lineptr
= strchr(lineptr
, ' '))
504 while (*lineptr
== ' ')
510 * Point to the next argument...
513 argv
[argc
++] = lineptr
;
516 * Stop if we have too many...
519 if (argc
>= (int)(sizeof(argv
) / sizeof(argv
[0]) - 2))
524 argv
[argc
++] = (char *)to
;
533 perror("ERROR: Unable to create pipe");
538 * Then run the command...
541 if ((pid
= fork()) == 0)
544 * Child goes here - redirect stdin to the input side of the pipe,
545 * redirect stdout to stderr, and exec...
557 execvp(argv
[0], argv
);
563 * Unable to fork - error out...
566 perror("ERROR: Unable to fork command");
575 * Create a CUPS file using the output side of the pipe and close the
581 if ((fp
= cupsFileOpenFd(pipefds
[1], "w")) == NULL
)
583 int status
; /* Status of command */
595 * 'print_attributes()' - Print the attributes in a request...
599 print_attributes(ipp_t
*ipp
, /* I - IPP request */
600 int indent
) /* I - Indentation */
602 int i
; /* Looping var */
603 ipp_tag_t group
; /* Current group */
604 ipp_attribute_t
*attr
; /* Current attribute */
605 ipp_value_t
*val
; /* Current value */
606 static const char * const tags
[] = /* Value/group tag strings */
609 "operation-attributes-tag",
610 "job-attributes-tag",
611 "end-of-attributes-tag",
612 "printer-attributes-tag",
613 "unsupported-attributes-tag",
614 "subscription-attributes-tag",
615 "event-attributes-tag",
673 "textWithoutLanguage",
674 "nameWithoutLanguage",
686 for (group
= IPP_TAG_ZERO
, attr
= ipp
->attrs
; attr
; attr
= attr
->next
)
688 if ((attr
->group_tag
== IPP_TAG_ZERO
&& indent
<= 8) || !attr
->name
)
690 group
= IPP_TAG_ZERO
;
695 if (group
!= attr
->group_tag
)
697 group
= attr
->group_tag
;
699 fprintf(stderr
, "DEBUG: %*s%s:\n\n", indent
- 4, "", tags
[group
]);
702 fprintf(stderr
, "DEBUG: %*s%s (", indent
, "", attr
->name
);
703 if (attr
->num_values
> 1)
704 fputs("1setOf ", stderr
);
705 fprintf(stderr
, "%s):", tags
[attr
->value_tag
]);
707 switch (attr
->value_tag
)
710 case IPP_TAG_INTEGER
:
711 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
712 fprintf(stderr
, " %d", val
->integer
);
716 case IPP_TAG_BOOLEAN
:
717 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
718 fprintf(stderr
, " %s", val
->boolean
? "true" : "false");
723 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
724 fprintf(stderr
, " %d-%d", val
->range
.lower
, val
->range
.upper
);
730 time_t vtime
; /* Date/Time value */
731 struct tm
*vdate
; /* Date info */
732 char vstring
[256]; /* Formatted time */
734 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
736 vtime
= ippDateToTime(val
->date
);
737 vdate
= localtime(&vtime
);
738 strftime(vstring
, sizeof(vstring
), "%c", vdate
);
739 fprintf(stderr
, " (%s)", vstring
);
745 case IPP_TAG_RESOLUTION
:
746 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
747 fprintf(stderr
, " %dx%d%s", val
->resolution
.xres
,
748 val
->resolution
.yres
,
749 val
->resolution
.units
== IPP_RES_PER_INCH
? "dpi" : "dpc");
753 case IPP_TAG_STRING
:
754 case IPP_TAG_TEXTLANG
:
755 case IPP_TAG_NAMELANG
:
758 case IPP_TAG_KEYWORD
:
760 case IPP_TAG_URISCHEME
:
761 case IPP_TAG_CHARSET
:
762 case IPP_TAG_LANGUAGE
:
763 case IPP_TAG_MIMETYPE
:
764 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
765 fprintf(stderr
, " \"%s\"", val
->string
.text
);
769 case IPP_TAG_BEGIN_COLLECTION
:
772 for (i
= 0, val
= attr
->values
; i
< attr
->num_values
; i
++, val
++)
776 print_attributes(val
->collection
, indent
+ 4);
781 fprintf(stderr
, "UNKNOWN (%d values)\n", attr
->num_values
);
789 * End of "$Id: mailto.c 7460 2008-04-16 02:19:54Z mike $".