2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
36 #include "pwmd-error.h"
38 #include "util-misc.h"
39 #include "util-string.h"
44 extern void log_write (const char *fmt
, ...);
47 valid_filename (const char *filename
)
51 if (!filename
|| !*filename
)
54 if (filename
[0] == '-' && filename
[1] == 0)
57 for (p
= filename
; *p
; p
++)
59 if (*p
== '/' || isspace (*p
))
66 /* If 'user' is null then lookup entry for 'uid'. Caller must free 'buf'. */
68 get_pwd_struct (const char *user
, uid_t uid
, struct passwd
*pw
, char **buf
,
71 struct passwd
*result
;
72 #ifdef HAVE_GETPWNAM_R
76 len
= sysconf (_SC_GETPW_R_SIZE_MAX
);
86 err
= getpwnam_r (user
, pw
, *buf
, len
, &result
);
92 err
= getpwuid_r (uid
, pw
, *buf
, len
, &result
);
98 *rc
= gpg_error_from_errno (err
);
106 result
= getpwnam (user
);
108 result
= getpwuid (uid
);
111 *rc
= gpg_error_from_syserror ();
118 get_pwd_entry(uid_t uid
, int which
)
123 struct passwd
*result
= get_pwd_struct (NULL
, uid
, &pw
, &buf
, &rc
);
132 value
= str_dup (result
->pw_dir
);
134 value
= str_dup (result
->pw_name
);
142 get_username (uid_t uid
)
144 return get_pwd_entry (uid
, 1);
151 return home_directory
;
153 home_directory
= get_pwd_entry (getuid(), 0);
154 return home_directory
;
158 expand_homedir (char *str
)
164 char *dir
= get_pwd_entry(getuid(), 0);
170 s
= str_asprintf ("%s%s", dir
, p
);
175 return str_dup (str
);
178 /* The 'more' parameter lets the calling command process remaining
179 * non-option arguments. */
181 parse_options (char **line
, struct argv_s
* args
[], void *data
, int more
)
194 if (*p
== '-' && *(p
+ 1) == '-')
197 char opt
[255] = { 0 }, value
[255] =
203 if (!*p
|| *p
== ' ')
211 for (tp
= opt
, len
= 0; *p
; p
++, len
++)
214 return GPG_ERR_LINE_TOO_LONG
;
216 if (*p
== ' ' || *p
== '=')
226 for (tp
= value
, len
= 0; *p
; p
++, len
++)
229 return GPG_ERR_LINE_TOO_LONG
;
236 else if (*p
== ' ' && !inquote
)
254 for (int i
= 0; args
[i
]; i
++)
256 if (!strcmp (args
[i
]->opt
, opt
))
258 log_write2 ("param: name='%s' value='%s'", opt
, value
);
259 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
260 return GPG_ERR_SYNTAX
;
261 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
262 return GPG_ERR_SYNTAX
;
266 rc
= args
[i
]->func (data
, value
);
277 return GPG_ERR_UNKNOWN_OPTION
;
286 return GPG_ERR_SYNTAX
;
296 bin2hex (const unsigned char *data
, size_t len
)
298 size_t size
= len
* 2 + 1;
304 for (n
= 0; n
< len
; n
++)
308 sprintf (c
, "%02X", data
[n
]);
312 return str_dup (buf
);
316 html_escape (const char *line
, size_t len
, int space
)
318 struct string_s
*string
= string_new ("");
325 for (n
= 0; n
< len
; n
++)
330 string
= string_append (string
, "<");
333 string
= string_append (string
, ">");
338 string
= string_append (string
, "%20");
342 string
= string_append_printf (string
, "%c", line
[n
]);
348 string_free (string
, 0);
353 strip_texi_html (const char *line
)
358 struct string_s
*string
= string_new ("<html><body>");
363 /* The command usage text. */
364 p
= strchr (line
, '\n');
367 len
= strlen (line
)-strlen(p
);
368 tmp
= html_escape (line
, len
, 0);
369 string
= string_append_printf (string
, "<b>%s</b><p>", tmp
);
384 if (*p
== '@' && *(p
+ 1) != '@')
388 if (!strncasecmp (p
, "end ", 4))
391 if (!strncasecmp (p
, "table", 5))
393 string
= string_append (string
, "</p>");
395 else if (!strncasecmp (p
, "example", 7))
397 string
= string_append (string
, "</code>");
400 while (*p
++ != '\n');
404 else if (!strncasecmp (p
, "table ", 6))
406 while (*p
++ != '\n');
410 else if (!strncasecmp (p
, "example", 7))
412 string
= string_append (string
, "<code>");
413 while (*p
++ != '\n');
417 else if (!strncasecmp (p
, "sp ", 3))
419 while (*p
++ != '\n');
421 string
= string_append (string
, "<p>");
424 else if (!strncasecmp (p
, "item ", 5))
427 string
= string_append (string
, "<p><b>");
428 tmp
= strchr (p
, '\n');
429 len
= strlen (p
) - strlen (tmp
);
431 string
= string_append_printf (string
, "%c", *p
++);
433 string
= string_append (string
, "</b><br/>");
436 else if (!strncasecmp (p
, "pxref", 5)
437 || !strncasecmp (p
, "npxref", 6))
441 if (!strncasecmp (p
, "npxref", 6))
446 string
= string_append_printf (string
, "%s<a href=\"",
450 else if (!strncasecmp (p
, "xref", 4))
454 string
= string_append (string
, "See <a href=\"");
457 else if (!strncasecmp (p
, "url", 3))
461 string
= string_append (string
, "<a href=\"");
464 else if (!strncasecmp (p
, "key", 3))
469 else if (!strncasecmp (p
, "file", 4))
473 string
= string_append (string
, "<var>");
476 else if (!strncasecmp (p
, "var", 3))
480 string
= string_append (string
, "<var>");
483 else if (!strncasecmp (p
, "option", 6))
487 string
= string_append (string
, "<var>");
490 else if (!strncasecmp (p
, "code", 4))
494 string
= string_append (string
, "<b>");
497 else if (!strncasecmp (p
, "emph", 4))
501 string
= string_append (string
, "<em>");
504 else if (!strncasecmp (p
, "command", 7))
508 string
= string_append (string
, "<em>");
511 else if (!strncasecmp (p
, "cite", 4))
515 string
= string_append (string
, "<cite>");
518 else if (!strncasecmp (p
, "abbr", 4))
522 string
= string_append (string
, "<em>");
525 else if (!strncasecmp (p
, "*", 1))
527 string
= string_append (string
, "<br/>");
531 while (*p
&& *p
!= '{')
545 tmp
= strchr (p
, '}');
546 len
= strlen (p
) - strlen (tmp
);
558 tmp
= html_escape (buf
, strlen (buf
), 1);
559 string
= string_append_printf (string
, "%s\">%s</a>", tmp
, buf
);
563 string
= string_append (string
, buf
);
566 string
= string_append (string
, "</var>");
568 string
= string_append (string
, "</b>");
570 string
= string_append (string
, "</em>");
572 string
= string_append (string
, "</cite>");
575 else if (*p
== '@' && *(p
+ 1) == '@')
579 string
= string_append_printf (string
, "%c", *p
);
585 string
= string_append (string
, "</body></html>");
587 string_free (string
, 0);
592 strip_texi (const char *str
, int html
)
599 return strip_texi_html (str
);
605 for (p
= str
; *p
; p
++)
607 if (*p
== '@' && *(p
+ 1) != '@')
611 if (!strncasecmp (p
, "table", 5)
612 || !strncasecmp (p
, "end ", 4))
614 while (*p
++ != '\n');
618 else if (!strncasecmp (p
, "example", 7)
619 || !strncasecmp (p
, "end ", 4))
621 while (*p
++ != '\n');
625 else if (!strncasecmp (p
, "sp ", 3))
627 while (*p
++ != '\n');
631 else if (!strncasecmp (p
, "item", 4))
634 while (*p
&& *p
!= '\n')
640 else if (!strncasecmp (p
, "pxref", 5))
647 else if (!strncasecmp (p
, "xref", 4))
654 else if (!strncasecmp (p
, "*", 1))
661 while (*p
&& *p
!= '{')
675 while (*p
&& *p
!= '}')
685 else if (*p
== '@' && *(p
+ 1) == '@')
701 strip_texi_and_wrap (const char *str
, int html
)
703 char *tmp
= strip_texi (str
, html
);
704 char *help
= xmalloc (strlen (tmp
) * 2 + 1);
708 for (p
= tmp
, ph
= help
, i
= 0; *p
; p
++, i
++)
710 if (i
== 78 || *p
== '\n')
717 while (!(*--t
== ' '))
733 while (*p
!= '\n' && *p
== ' ')
749 free_key (void *data
)
755 create_thread (void *(*cb
) (void *), void *data
,
756 pthread_t
* tid
, int detached
)
761 pthread_attr_init (&attr
);
764 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
766 n
= pthread_create (tid
, &attr
, cb
, data
);
767 pthread_attr_destroy (&attr
);
768 return gpg_error_from_errno (n
);
772 release_mutex_cb (void *arg
)
774 pthread_mutex_t
*m
= (pthread_mutex_t
*) arg
;
780 close_fd_cb (void *arg
)
782 int fd
= *(int *) arg
;
788 /* The advisory lock should be obtained before calling this function. */
790 get_checksum (const char *filename
, unsigned char **r_crc
, size_t * r_crclen
)
797 if (stat (filename
, &st
) == -1)
798 return gpg_error_from_errno (errno
);
800 fd
= open (filename
, O_RDONLY
);
802 return gpg_error_from_errno (errno
);
804 pthread_cleanup_push (close_fd_cb
, &fd
);
805 buf
= xmalloc (st
.st_size
);
808 pthread_cleanup_push (xfree
, buf
);
810 size_t len
= read (fd
, buf
, st
.st_size
);
811 if (len
== st
.st_size
)
817 len
= gcry_md_get_algo_dlen (GCRY_MD_CRC32
);
821 pthread_cleanup_push (xfree
, crc
);
822 gcry_md_hash_buffer (GCRY_MD_CRC32
, crc
, buf
, st
.st_size
);
825 pthread_cleanup_pop (0);
832 rc
= GPG_ERR_TOO_SHORT
;
834 pthread_cleanup_pop (1);
839 pthread_cleanup_pop (1);
843 wchar_t *str_to_wchar (const char *str
)
850 memset (&ps
, 0, sizeof(mbstate_t));
851 len
= mbsrtowcs (NULL
, &p
, 0, &ps
);
855 wc
= xcalloc (len
+1, sizeof(wchar_t));
860 memset (&ps
, 0, sizeof(mbstate_t));
861 len
= mbsrtowcs (wc
, &p
, len
, &ps
);
872 gnupg_escape (const char *str
)
877 char *buf
= xmalloc (strlen(str
)*4+1), *b
= buf
;
883 for (p
= str
; p
&& *p
; p
++)
909 /* From Beej's Guide to Network Programming. It's a good tutorial. */
911 get_in_addr (struct sockaddr
*sa
)
913 if (sa
->sa_family
== AF_INET
)
914 return &(((struct sockaddr_in
*) sa
)->sin_addr
);
916 return &(((struct sockaddr_in6
*) sa
)->sin6_addr
);