]> git.ipfire.org Git - thirdparty/cups.git/blob - notifier/mailto.c
Load cups into easysw/current.
[thirdparty/cups.git] / notifier / mailto.c
1 /*
2 * "$Id: mailto.c 4961 2006-01-20 22:19:13Z mike $"
3 *
4 * "mailto" notifier for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for the mailto notifier.
27 * email_message() - Email a notification message.
28 * load_configuration() - Load the mailto.conf file.
29 * pipe_sendmail() - Open a pipe to sendmail...
30 * print_attributes() - Print the attributes in a request...
31 */
32
33 /*
34 * Include necessary headers...
35 */
36
37 #include <cups/cups.h>
38 #include <cups/i18n.h>
39 #include <cups/string.h>
40 #include <errno.h>
41 #include <sys/wait.h>
42 #include <signal.h>
43
44
45 /*
46 * Globals...
47 */
48
49 char mailtoCc[1024]; /* Cc email address */
50 char mailtoFrom[1024]; /* From email address */
51 char mailtoReplyTo[1024]; /* Reply-To email address */
52 char mailtoSubject[1024]; /* Subject prefix */
53 char mailtoSMTPServer[1024]; /* SMTP server to use */
54 char mailtoSendmail[1024]; /* Sendmail program to use */
55
56
57 /*
58 * Local functions...
59 */
60
61 void email_message(const char *to, const char *subject,
62 const char *text);
63 int load_configuration(void);
64 cups_file_t *pipe_sendmail(const char *to);
65 void print_attributes(ipp_t *ipp, int indent);
66
67
68 /*
69 * 'main()' - Main entry for the mailto notifier.
70 */
71
72 int /* O - Exit status */
73 main(int argc, /* I - Number of command-line arguments */
74 char *argv[]) /* I - Command-line arguments */
75 {
76 int i; /* Looping var */
77 ipp_t *msg; /* Event message from scheduler */
78 ipp_state_t state; /* IPP event state */
79 char *subject, /* Subject for notification message */
80 *text; /* Text for notification message */
81 cups_lang_t *lang; /* Language info */
82 char temp[1024]; /* Temporary string */
83 int templen; /* Length of temporary string */
84 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
85 struct sigaction action; /* POSIX sigaction data */
86 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
87
88
89 /*
90 * Don't buffer stderr...
91 */
92
93 setbuf(stderr, NULL);
94
95 /*
96 * Ignore SIGPIPE signals...
97 */
98
99 #ifdef HAVE_SIGSET
100 sigset(SIGPIPE, SIG_IGN);
101 #elif defined(HAVE_SIGACTION)
102 memset(&action, 0, sizeof(action));
103 action.sa_handler = SIG_IGN;
104 sigaction(SIGPIPE, &action, NULL);
105 #else
106 signal(SIGPIPE, SIG_IGN);
107 #endif /* HAVE_SIGSET */
108
109 /*
110 * Validate command-line options...
111 */
112
113 if (argc != 3)
114 {
115 fputs("Usage: mailto mailto:user@domain.com notify-user-data\n", stderr);
116 return (1);
117 }
118
119 if (strncmp(argv[1], "mailto:", 7))
120 {
121 fprintf(stderr, "ERROR: Bad recipient \"%s\"!\n", argv[1]);
122 return (1);
123 }
124
125 fprintf(stderr, "DEBUG: argc=%d\n", argc);
126 for (i = 0; i < argc; i ++)
127 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
128
129 /*
130 * Load configuration data...
131 */
132
133 if ((lang = cupsLangDefault()) == NULL)
134 return (1);
135
136 if (!load_configuration())
137 return (1);
138
139 /*
140 * Get the reply-to address...
141 */
142
143 templen = sizeof(temp);
144 httpDecode64_2(temp, &templen, argv[2]);
145
146 if (!strncmp(temp, "mailto:", 7))
147 strlcpy(mailtoReplyTo, temp + 7, sizeof(mailtoReplyTo));
148 else if (temp[0])
149 fprintf(stderr, "WARNING: Bad notify-user-data value (%d bytes) ignored!\n",
150 templen);
151
152 /*
153 * Loop forever until we run out of events...
154 */
155
156 for (;;)
157 {
158 /*
159 * Get the next event...
160 */
161
162 msg = ippNew();
163 while ((state = ippReadFile(0, msg)) != IPP_DATA)
164 {
165 if (state <= IPP_IDLE)
166 break;
167 }
168
169 fprintf(stderr, "DEBUG: state=%d\n", state);
170
171 if (state == IPP_ERROR)
172 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr);
173
174 if (state <= IPP_IDLE)
175 {
176 /*
177 * Out of messages, free memory and then exit...
178 */
179
180 ippDelete(msg);
181 return (0);
182 }
183
184 /*
185 * Get the subject and text for the message, then email it...
186 */
187
188 subject = cupsNotifySubject(lang, msg);
189 text = cupsNotifyText(lang, msg);
190
191 fprintf(stderr, "DEBUG: subject=\"%s\"\n", subject);
192 fprintf(stderr, "DEBUG: text=\"%s\"\n", text);
193
194 if (subject && text)
195 email_message(argv[1] + 7, subject, text);
196 else
197 {
198 fputs("ERROR: Missing attributes in event notification!\n", stderr);
199 print_attributes(msg, 4);
200 }
201
202 /*
203 * Free the memory used for this event...
204 */
205
206 if (subject)
207 free(subject);
208
209 if (text)
210 free(text);
211
212 ippDelete(msg);
213 }
214 }
215
216
217 /*
218 * 'email_message()' - Email a notification message.
219 */
220
221 void
222 email_message(const char *to, /* I - Recipient of message */
223 const char *subject, /* I - Subject of message */
224 const char *text) /* I - Text of message */
225 {
226 cups_file_t *fp; /* Pipe/socket to mail server */
227 const char *nl; /* Newline to use */
228 char response[1024]; /* SMTP response buffer */
229
230
231 /*
232 * Connect to the mail server...
233 */
234
235 if (mailtoSendmail[0])
236 {
237 /*
238 * Use the sendmail command...
239 */
240
241 fp = pipe_sendmail(to);
242
243 if (!fp)
244 return;
245
246 nl = "\n";
247 }
248 else
249 {
250 /*
251 * Use an SMTP server...
252 */
253
254 char hostbuf[1024]; /* Local hostname */
255
256
257 if (strchr(mailtoSMTPServer, ':'))
258 fp = cupsFileOpen(mailtoSMTPServer, "s");
259 else
260 {
261 char spec[1024]; /* Host:service spec */
262
263
264 snprintf(spec, sizeof(spec), "%s:smtp", mailtoSMTPServer);
265 fp = cupsFileOpen(spec, "s");
266 }
267
268 if (!fp)
269 {
270 fprintf(stderr, "ERROR: Unable to connect to SMTP server \"%s\"!\n",
271 mailtoSMTPServer);
272 return;
273 }
274
275 fprintf(stderr, "DEBUG: Connected to \"%s\"...\n", mailtoSMTPServer);
276
277 cupsFilePrintf(fp, "HELO %s\r\n", httpGetHostname(hostbuf, sizeof(hostbuf)));
278 fprintf(stderr, "DEBUG: >>> HELO %s\n", hostbuf);
279
280 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
281 goto smtp_error;
282 fprintf(stderr, "DEBUG: <<< %s\n", response);
283
284 cupsFilePrintf(fp, "MAIL FROM:%s\r\n", mailtoFrom);
285 fprintf(stderr, "DEBUG: >>> MAIL FROM:%s\n", mailtoFrom);
286
287 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
288 goto smtp_error;
289 fprintf(stderr, "DEBUG: <<< %s\n", response);
290
291 cupsFilePrintf(fp, "RCPT TO:%s\r\n", to);
292 fprintf(stderr, "DEBUG: >>> RCPT TO:%s\n", to);
293
294 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
295 goto smtp_error;
296 fprintf(stderr, "DEBUG: <<< %s\n", response);
297
298 cupsFilePuts(fp, "DATA\r\n");
299 fputs("DEBUG: DATA\n", stderr);
300
301 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
302 goto smtp_error;
303 fprintf(stderr, "DEBUG: <<< %s\n", response);
304
305 nl = "\r\n";
306 }
307
308 /*
309 * Send the message...
310 */
311
312 cupsFilePrintf(fp, "Date: %s%s", httpGetDateString(time(NULL)), nl);
313 cupsFilePrintf(fp, "From: %s%s", mailtoFrom, nl);
314 cupsFilePrintf(fp, "Subject: %s %s%s", mailtoSubject, subject, nl);
315 if (mailtoReplyTo[0])
316 {
317 cupsFilePrintf(fp, "Sender: %s%s", mailtoReplyTo, nl);
318 cupsFilePrintf(fp, "Reply-To: %s%s", mailtoReplyTo, nl);
319 }
320 cupsFilePrintf(fp, "To: %s%s", to, nl);
321 if (mailtoCc[0])
322 cupsFilePrintf(fp, "Cc: %s%s", mailtoCc, nl);
323 cupsFilePrintf(fp, "Content-Type: text/plain%s", nl);
324 cupsFilePuts(fp, nl);
325 cupsFilePrintf(fp, "%s%s", text, nl);
326 cupsFilePrintf(fp, ".\n", nl);
327
328 /*
329 * Close the connection to the mail server...
330 */
331
332 if (mailtoSendmail[0])
333 {
334 /*
335 * Close the pipe and wait for the sendmail command to finish...
336 */
337
338 int status; /* Exit status */
339
340
341 cupsFileClose(fp);
342
343 if (wait(&status))
344 status = errno << 8;
345
346 /*
347 * Report any non-zero status...
348 */
349
350 if (status)
351 {
352 if (WIFEXITED(status))
353 fprintf(stderr, "ERROR: Sendmail command returned status %d!\n",
354 WEXITSTATUS(status));
355 else
356 fprintf(stderr, "ERROR: Sendmail command crashed on signal %d!\n",
357 WTERMSIG(status));
358 }
359 }
360 else
361 {
362 /*
363 * Finish up the SMTP submission and close the connection...
364 */
365
366 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
367 goto smtp_error;
368 fprintf(stderr, "DEBUG: <<< %s\n", response);
369
370 /*
371 * Process SMTP errors here...
372 */
373
374 smtp_error:
375
376 cupsFilePuts(fp, "QUIT\r\n");
377 fputs("DEBUG: QUIT\n", stderr);
378
379 if (!cupsFileGets(fp, response, sizeof(response)) || atoi(response) >= 500)
380 goto smtp_error;
381 fprintf(stderr, "DEBUG: <<< %s\n", response);
382
383 cupsFileClose(fp);
384
385 fprintf(stderr, "DEBUG: Closed connection to \"%s\"...\n",
386 mailtoSMTPServer);
387 }
388 }
389
390
391 /*
392 * 'load_configuration()' - Load the mailto.conf file.
393 */
394
395 int /* I - 1 on success, 0 on failure */
396 load_configuration(void)
397 {
398 cups_file_t *fp; /* mailto.conf file */
399 const char *server_root, /* CUPS_SERVERROOT environment variable */
400 *server_admin; /* SERVER_ADMIN environment variable */
401 char line[1024], /* Line from file */
402 *value; /* Value for directive */
403 int linenum; /* Line number in file */
404
405
406 /*
407 * Initialize defaults...
408 */
409
410 mailtoCc[0] = '\0';
411
412 if ((server_admin = getenv("SERVER_ADMIN")) != NULL)
413 strlcpy(mailtoFrom, server_admin, sizeof(mailtoFrom));
414 else
415 snprintf(mailtoFrom, sizeof(mailtoFrom), "root@%s",
416 httpGetHostname(line, sizeof(line)));
417
418 strlcpy(mailtoSendmail, "/usr/sbin/sendmail", sizeof(mailtoSendmail));
419
420 mailtoSMTPServer[0] = '\0';
421
422 mailtoSubject[0] = '\0';
423
424 /*
425 * Try loading the config file...
426 */
427
428 if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
429 server_root = CUPS_SERVERROOT;
430
431 snprintf(line, sizeof(line), "%s/mailto.conf", server_root);
432
433 if ((fp = cupsFileOpen(line, "r")) == NULL)
434 {
435 fprintf(stderr, "ERROR: Unable to open \"%s\" - %s\n", line,
436 strerror(errno));
437 return (1);
438 }
439
440 linenum = 0;
441
442 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
443 {
444 if (!value)
445 {
446 fprintf(stderr, "ERROR: No value found for %s directive on line %d!\n",
447 line, linenum);
448 cupsFileClose(fp);
449 return (0);
450 }
451
452 if (!strcasecmp(line, "Cc"))
453 strlcpy(mailtoCc, value, sizeof(mailtoCc));
454 else if (!strcasecmp(line, "From"))
455 strlcpy(mailtoFrom, value, sizeof(mailtoFrom));
456 else if (!strcasecmp(line, "Sendmail"))
457 {
458 strlcpy(mailtoSendmail, value, sizeof(mailtoSendmail));
459 mailtoSMTPServer[0] = '\0';
460 }
461 else if (!strcasecmp(line, "SMTPServer"))
462 {
463 mailtoSendmail[0] = '\0';
464 strlcpy(mailtoSMTPServer, value, sizeof(mailtoSMTPServer));
465 }
466 else if (!strcasecmp(line, "Subject"))
467 strlcpy(mailtoSubject, value, sizeof(mailtoSubject));
468 else
469 {
470 fprintf(stderr,
471 "ERROR: Unknown configuration directive \"%s\" on line %d!\n",
472 line, linenum);
473 }
474 }
475
476 /*
477 * Close file and return...
478 */
479
480 cupsFileClose(fp);
481
482 return (1);
483 }
484
485
486 /*
487 * 'pipe_sendmail()' - Open a pipe to sendmail...
488 */
489
490 cups_file_t * /* O - CUPS file */
491 pipe_sendmail(const char *to) /* I - To: address */
492 {
493 cups_file_t *fp; /* CUPS file */
494 int pid; /* Process ID */
495 int pipefds[2]; /* Pipe file descriptors */
496 int argc; /* Number of arguments */
497 char *argv[100], /* Argument array */
498 line[1024], /* Sendmail command + args */
499 *lineptr; /* Pointer into line */
500
501
502 /*
503 * First break the mailtoSendmail string into arguments...
504 */
505
506 strlcpy(line, mailtoSendmail, sizeof(line));
507 argv[0] = line;
508 argc = 1;
509
510 for (lineptr = strchr(line, ' '); lineptr; lineptr = strchr(lineptr, ' '))
511 {
512 while (*lineptr == ' ')
513 *lineptr++ = '\0';
514
515 if (*lineptr)
516 {
517 /*
518 * Point to the next argument...
519 */
520
521 argv[argc ++] = lineptr;
522
523 /*
524 * Stop if we have too many...
525 */
526
527 if (argc >= (int)(sizeof(argv) / sizeof(argv[0]) - 2))
528 break;
529 }
530 }
531
532 argv[argc ++] = (char *)to;
533 argv[argc] = NULL;
534
535 /*
536 * Create the pipe...
537 */
538
539 if (pipe(pipefds))
540 {
541 perror("ERROR: Unable to create pipe");
542 return (NULL);
543 }
544
545 /*
546 * Then run the command...
547 */
548
549 if ((pid = fork()) == 0)
550 {
551 /*
552 * Child goes here - redirect stdin to the input side of the pipe,
553 * redirect stdout to stderr, and exec...
554 */
555
556 close(0);
557 dup(pipefds[0]);
558
559 close(1);
560 dup(2);
561
562 close(pipefds[0]);
563 close(pipefds[1]);
564
565 execvp(argv[0], argv);
566 exit(errno);
567 }
568 else if (pid < 0)
569 {
570 /*
571 * Unable to fork - error out...
572 */
573
574 perror("ERROR: Unable to fork command");
575
576 close(pipefds[0]);
577 close(pipefds[1]);
578
579 return (NULL);
580 }
581
582 /*
583 * Create a CUPS file using the output side of the pipe and close the
584 * input side...
585 */
586
587 close(pipefds[0]);
588
589 if ((fp = cupsFileOpenFd(pipefds[1], "w")) == NULL)
590 {
591 int status; /* Status of command */
592
593
594 close(pipefds[1]);
595 wait(&status);
596 }
597
598 return (fp);
599 }
600
601
602 /*
603 * 'print_attributes()' - Print the attributes in a request...
604 */
605
606 void
607 print_attributes(ipp_t *ipp, /* I - IPP request */
608 int indent) /* I - Indentation */
609 {
610 int i; /* Looping var */
611 ipp_tag_t group; /* Current group */
612 ipp_attribute_t *attr; /* Current attribute */
613 ipp_value_t *val; /* Current value */
614 static const char * const tags[] = /* Value/group tag strings */
615 {
616 "reserved-00",
617 "operation-attributes-tag",
618 "job-attributes-tag",
619 "end-of-attributes-tag",
620 "printer-attributes-tag",
621 "unsupported-attributes-tag",
622 "subscription-attributes-tag",
623 "event-attributes-tag",
624 "reserved-08",
625 "reserved-09",
626 "reserved-0A",
627 "reserved-0B",
628 "reserved-0C",
629 "reserved-0D",
630 "reserved-0E",
631 "reserved-0F",
632 "unsupported",
633 "default",
634 "unknown",
635 "no-value",
636 "reserved-14",
637 "not-settable",
638 "delete-attr",
639 "admin-define",
640 "reserved-18",
641 "reserved-19",
642 "reserved-1A",
643 "reserved-1B",
644 "reserved-1C",
645 "reserved-1D",
646 "reserved-1E",
647 "reserved-1F",
648 "reserved-20",
649 "integer",
650 "boolean",
651 "enum",
652 "reserved-24",
653 "reserved-25",
654 "reserved-26",
655 "reserved-27",
656 "reserved-28",
657 "reserved-29",
658 "reserved-2a",
659 "reserved-2b",
660 "reserved-2c",
661 "reserved-2d",
662 "reserved-2e",
663 "reserved-2f",
664 "octetString",
665 "dateTime",
666 "resolution",
667 "rangeOfInteger",
668 "begCollection",
669 "textWithLanguage",
670 "nameWithLanguage",
671 "endCollection",
672 "reserved-38",
673 "reserved-39",
674 "reserved-3a",
675 "reserved-3b",
676 "reserved-3c",
677 "reserved-3d",
678 "reserved-3e",
679 "reserved-3f",
680 "reserved-40",
681 "textWithoutLanguage",
682 "nameWithoutLanguage",
683 "reserved-43",
684 "keyword",
685 "uri",
686 "uriScheme",
687 "charset",
688 "naturalLanguage",
689 "mimeMediaType",
690 "memberName"
691 };
692
693
694 for (group = IPP_TAG_ZERO, attr = ipp->attrs; attr; attr = attr->next)
695 {
696 if ((attr->group_tag == IPP_TAG_ZERO && indent <= 8) || !attr->name)
697 {
698 group = IPP_TAG_ZERO;
699 fputc('\n', stderr);
700 continue;
701 }
702
703 if (group != attr->group_tag)
704 {
705 group = attr->group_tag;
706
707 fprintf(stderr, "DEBUG: %*s%s:\n\n", indent - 4, "", tags[group]);
708 }
709
710 fprintf(stderr, "DEBUG: %*s%s (", indent, "", attr->name);
711 if (attr->num_values > 1)
712 fputs("1setOf ", stderr);
713 fprintf(stderr, "%s):", tags[attr->value_tag]);
714
715 switch (attr->value_tag)
716 {
717 case IPP_TAG_ENUM :
718 case IPP_TAG_INTEGER :
719 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
720 fprintf(stderr, " %d", val->integer);
721 fputc('\n', stderr);
722 break;
723
724 case IPP_TAG_BOOLEAN :
725 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
726 fprintf(stderr, " %s", val->boolean ? "true" : "false");
727 fputc('\n', stderr);
728 break;
729
730 case IPP_TAG_RANGE :
731 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
732 fprintf(stderr, " %d-%d", val->range.lower, val->range.upper);
733 fputc('\n', stderr);
734 break;
735
736 case IPP_TAG_DATE :
737 {
738 time_t vtime; /* Date/Time value */
739 struct tm *vdate; /* Date info */
740 char vstring[256]; /* Formatted time */
741
742 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
743 {
744 vtime = ippDateToTime(val->date);
745 vdate = localtime(&vtime);
746 strftime(vstring, sizeof(vstring), "%c", vdate);
747 fprintf(stderr, " (%s)", vstring);
748 }
749 }
750 fputc('\n', stderr);
751 break;
752
753 case IPP_TAG_RESOLUTION :
754 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
755 fprintf(stderr, " %dx%d%s", val->resolution.xres,
756 val->resolution.yres,
757 val->resolution.units == IPP_RES_PER_INCH ? "dpi" : "dpc");
758 fputc('\n', stderr);
759 break;
760
761 case IPP_TAG_STRING :
762 case IPP_TAG_TEXTLANG :
763 case IPP_TAG_NAMELANG :
764 case IPP_TAG_TEXT :
765 case IPP_TAG_NAME :
766 case IPP_TAG_KEYWORD :
767 case IPP_TAG_URI :
768 case IPP_TAG_URISCHEME :
769 case IPP_TAG_CHARSET :
770 case IPP_TAG_LANGUAGE :
771 case IPP_TAG_MIMETYPE :
772 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
773 fprintf(stderr, " \"%s\"", val->string.text);
774 fputc('\n', stderr);
775 break;
776
777 case IPP_TAG_BEGIN_COLLECTION :
778 fputc('\n', stderr);
779
780 for (i = 0, val = attr->values; i < attr->num_values; i ++, val ++)
781 {
782 if (i)
783 fputc('\n', stderr);
784 print_attributes(val->collection, indent + 4);
785 }
786 break;
787
788 default :
789 fprintf(stderr, "UNKNOWN (%d values)\n", attr->num_values);
790 break;
791 }
792 }
793 }
794
795
796 /*
797 * End of "$Id: mailto.c 4961 2006-01-20 22:19:13Z mike $".
798 */