2 Copyright (C) 2006-2021 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 Pwmd is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/types.h>
36 #include "pwmd-error.h"
38 #include "util-misc.h"
39 #include "util-string.h"
45 void log_write (const char *fmt
, ...);
48 valid_filename (const char *filename
)
52 if (!filename
|| !*filename
)
55 if (filename
[0] == '-' && filename
[1] == 0)
58 for (p
= filename
; *p
; p
++)
60 if (*p
== '/' || isspace (*p
))
67 /* If 'user' is null then lookup entry for 'uid'. Caller must free 'buf'. */
69 get_pwd_struct (const char *user
, uid_t uid
, struct passwd
*pw
, char **buf
,
72 struct passwd
*result
= NULL
;
73 #ifdef HAVE_GETPWNAM_R
77 len
= sysconf (_SC_GETPW_R_SIZE_MAX
);
86 err
= getpwnam_r (user
, pw
, *buf
, len
, &result
);
88 err
= getpwuid_r (uid
, pw
, *buf
, len
, &result
);
96 *rc
= gpg_error_from_errno (err
);
104 result
= getpwnam (user
);
106 result
= getpwuid (uid
);
109 *rc
= gpg_error_from_syserror ();
116 get_pwd_entry(uid_t uid
, int which
)
121 struct passwd
*result
= get_pwd_struct (NULL
, uid
, &pw
, &buf
, &rc
);
130 value
= str_dup (result
->pw_dir
);
132 value
= str_dup (result
->pw_name
);
140 get_username (uid_t uid
)
142 return get_pwd_entry (uid
, 1);
149 return home_directory
;
151 home_directory
= get_pwd_entry (getuid(), 0);
152 return home_directory
;
156 expand_homedir (char *str
)
162 char *dir
= get_pwd_entry(getuid(), 0);
168 s
= str_asprintf ("%s%s", dir
, p
);
173 return str_dup (str
);
176 /* The 'more' parameter lets the calling command process remaining
177 * non-option arguments. */
179 parse_options (char **line
, struct argv_s
* args
[], void *data
, int more
)
192 if (*p
== '-' && *(p
+ 1) == '-')
195 char opt
[255] = { 0 }, value
[255] =
201 if (!*p
|| *p
== ' ')
209 for (tp
= opt
, len
= 0; *p
; p
++, len
++)
212 return GPG_ERR_LINE_TOO_LONG
;
214 if (*p
== ' ' || *p
== '=')
224 for (tp
= value
, len
= 0; *p
; p
++, len
++)
227 return GPG_ERR_LINE_TOO_LONG
;
234 else if (*p
== ' ' && !inquote
)
252 for (int i
= 0; args
[i
]; i
++)
254 if (!strcmp (args
[i
]->opt
, opt
))
256 log_write2 ("param: name='%s' value='%s'", opt
, value
);
257 if (args
[i
]->type
== OPTION_TYPE_NOARG
&& *value
)
258 return GPG_ERR_SYNTAX
;
259 else if (args
[i
]->type
== OPTION_TYPE_ARG
&& !*value
)
260 return GPG_ERR_SYNTAX
;
264 rc
= args
[i
]->func (data
, value
);
275 return GPG_ERR_UNKNOWN_OPTION
;
284 return GPG_ERR_SYNTAX
;
294 bin2hex (const unsigned char *data
, size_t len
)
296 size_t size
= len
* 2 + 1;
302 for (n
= 0; n
< len
; n
++)
306 sprintf (c
, "%02X", data
[n
]);
310 return str_dup (buf
);
314 html_escape (const char *line
, size_t len
, int space
)
316 struct string_s
*string
= string_new ("");
323 for (n
= 0; n
< len
; n
++)
328 string
= string_append (string
, "<");
331 string
= string_append (string
, ">");
336 string
= string_append (string
, "%20");
340 string
= string_append_printf (string
, "%c", line
[n
]);
346 string_free (string
, 0);
351 strip_texi_html (const char *line
)
356 struct string_s
*string
= string_new ("<html><body>");
361 /* The command usage text. */
362 p
= strchr (line
, '\n');
365 len
= strlen (line
)-strlen(p
);
366 tmp
= html_escape (line
, len
, 0);
367 string
= string_append_printf (string
, "<b>%s</b><p>", tmp
);
382 if (*p
== '@' && *(p
+ 1) != '@')
386 if (!strncasecmp (p
, "end ", 4))
389 if (!strncasecmp (p
, "table", 5))
391 string
= string_append (string
, "</p>");
393 else if (!strncasecmp (p
, "example", 7))
395 string
= string_append (string
, "</code>");
398 while (*p
++ != '\n');
402 else if (!strncasecmp (p
, "table ", 6))
404 while (*p
++ != '\n');
408 else if (!strncasecmp (p
, "example", 7))
410 string
= string_append (string
, "<code>");
411 while (*p
++ != '\n');
415 else if (!strncasecmp (p
, "sp ", 3))
417 while (*p
++ != '\n');
419 string
= string_append (string
, "<p>");
422 else if (!strncasecmp (p
, "item ", 5))
425 string
= string_append (string
, "<p><b>");
426 tmp
= strchr (p
, '\n');
427 len
= strlen (p
) - strlen (tmp
);
432 string
= string_append (string
, "<");
437 string
= string_append (string
, ">");
441 string
= string_append_printf (string
, "%c", *p
++);
444 string
= string_append (string
, "</b><br/>");
447 else if (!strncasecmp (p
, "pxref", 5)
448 || !strncasecmp (p
, "npxref", 6))
452 if (!strncasecmp (p
, "npxref", 6))
457 string
= string_append_printf (string
, "%s<a href=\"",
461 else if (!strncasecmp (p
, "xref", 4))
465 string
= string_append (string
, "See <a href=\"");
468 else if (!strncasecmp (p
, "url", 3))
472 string
= string_append (string
, "<a href=\"");
475 else if (!strncasecmp (p
, "key", 3))
480 else if (!strncasecmp (p
, "file", 4))
484 string
= string_append (string
, "<var>");
487 else if (!strncasecmp (p
, "var", 3))
491 string
= string_append (string
, "<var>");
494 else if (!strncasecmp (p
, "option", 6))
498 string
= string_append (string
, "<var>");
501 else if (!strncasecmp (p
, "code", 4))
505 string
= string_append (string
, "<b>");
508 else if (!strncasecmp (p
, "emph", 4))
512 string
= string_append (string
, "<em>");
515 else if (!strncasecmp (p
, "command", 7))
519 string
= string_append (string
, "<em>");
522 else if (!strncasecmp (p
, "cite", 4))
526 string
= string_append (string
, "<cite>");
529 else if (!strncasecmp (p
, "abbr", 4))
533 string
= string_append (string
, "<em>");
536 else if (!strncasecmp (p
, "*", 1))
538 string
= string_append (string
, "<br/>");
542 while (*p
&& *p
!= '{')
556 tmp
= strchr (p
, '}');
557 len
= strlen (p
) - strlen (tmp
);
569 tmp
= html_escape (buf
, strlen (buf
), 1);
570 string
= string_append_printf (string
, "%s\">%s</a>", tmp
, buf
);
574 string
= string_append (string
, buf
);
577 string
= string_append (string
, "</var>");
579 string
= string_append (string
, "</b>");
581 string
= string_append (string
, "</em>");
583 string
= string_append (string
, "</cite>");
586 else if (*p
== '@' && *(p
+ 1) == '@')
590 string
= string_append_printf (string
, "%c", *p
);
596 string
= string_append (string
, "</body></html>");
598 string_free (string
, 0);
603 strip_texi (const char *str
, int html
)
610 return strip_texi_html (str
);
616 for (p
= str
; *p
; p
++)
618 if (*p
== '@' && *(p
+ 1) != '@')
622 if (!strncasecmp (p
, "table", 5)
623 || !strncasecmp (p
, "end ", 4))
625 while (*p
++ != '\n');
629 else if (!strncasecmp (p
, "example", 7)
630 || !strncasecmp (p
, "end ", 4))
632 while (*p
++ != '\n');
636 else if (!strncasecmp (p
, "sp ", 3))
638 while (*p
++ != '\n');
642 else if (!strncasecmp (p
, "item", 4))
645 while (*p
&& *p
!= '\n')
651 else if (!strncasecmp (p
, "pxref", 5))
658 else if (!strncasecmp (p
, "xref", 4))
665 else if (!strncasecmp (p
, "*", 1))
672 while (*p
&& *p
!= '{')
686 while (*p
&& *p
!= '}')
696 else if (*p
== '@' && *(p
+ 1) == '@')
712 strip_texi_and_wrap (const char *str
, int html
)
714 char *tmp
= strip_texi (str
, html
);
715 char *help
= xcalloc (1, strlen (tmp
) * 2 + 1);
719 for (p
= tmp
, ph
= help
, i
= 0; *p
; p
++, i
++)
721 if (i
== 78 || *p
== '\n')
728 while (*(--t
) != ' ')
744 while (*p
!= '\n' && *p
== ' ')
760 free_key (void *data
)
766 create_thread (void *(*cb
) (void *), void *data
,
767 pthread_t
* tid
, int detached
)
772 pthread_attr_init (&attr
);
775 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
777 n
= pthread_create (tid
, &attr
, cb
, data
);
778 pthread_attr_destroy (&attr
);
779 return gpg_error_from_errno (n
);
783 release_mutex_cb (void *arg
)
785 pthread_mutex_t
*m
= (pthread_mutex_t
*) arg
;
791 close_fd_cb (void *arg
)
793 int fd
= *(int *)arg
;
800 get_checksum_memory (void *data
, size_t size
, unsigned char **r_crc
,
803 size_t len
= gcry_md_get_algo_dlen (GCRY_MD_CRC32
);
804 unsigned char *crc
= xmalloc (len
);
810 return GPG_ERR_ENOMEM
;
812 gcry_md_hash_buffer (GCRY_MD_CRC32
, crc
, data
, size
);
818 /* The advisory lock should be obtained before calling this function. */
820 get_checksum (const char *filename
, unsigned char **r_crc
, size_t * r_crclen
)
827 rc
= open_check_file (filename
, &fd
, &st
, 1);
831 pthread_cleanup_push (close_fd_cb
, &fd
);
832 buf
= xmalloc (st
.st_size
);
835 pthread_cleanup_push (xfree
, buf
);
837 size_t len
= read (fd
, buf
, st
.st_size
);
838 if (len
== st
.st_size
)
841 rc
= get_checksum_memory (buf
, st
.st_size
, r_crc
, r_crclen
);
846 rc
= GPG_ERR_TOO_SHORT
;
848 pthread_cleanup_pop (1);
853 pthread_cleanup_pop (1);
857 wchar_t *str_to_wchar (const char *str
)
864 memset (&ps
, 0, sizeof(mbstate_t));
865 len
= mbsrtowcs (NULL
, &p
, 0, &ps
);
869 wc
= xcalloc (len
+1, sizeof(wchar_t));
874 memset (&ps
, 0, sizeof(mbstate_t));
875 len
= mbsrtowcs (wc
, &p
, len
, &ps
);
886 gnupg_escape (const char *str
)
891 char *buf
= xmalloc (strlen(str
)*4+1), *b
= buf
;
897 for (p
= str
; p
&& *p
; p
++)
923 /* From Beej's Guide to Network Programming. It's a good tutorial. */
925 get_in_addr (struct sockaddr
*sa
)
927 if (sa
->sa_family
== AF_INET
)
928 return &(((struct sockaddr_in
*) sa
)->sin_addr
);
930 return &(((struct sockaddr_in6
*) sa
)->sin6_addr
);
934 open_check_file (const char *filename
, int *r_fd
, struct stat
*r_st
, int reg
)
940 memset (r_st
, 9, sizeof (struct stat
));
946 if (lstat (filename
, &st
) == -1)
947 return gpg_err_code (gpg_error_from_syserror ());
949 if (!S_ISREG (st
.st_mode
))
950 return GPG_ERR_ENODEV
;
953 memcpy (r_st
, &st
, sizeof (struct stat
));
958 fd
= open (filename
, O_RDONLY
);
960 return gpg_err_code (gpg_error_from_syserror ());
962 if (r_st
&& fstat (fd
, r_st
) == -1)
964 rc
= gpg_err_code (gpg_error_from_syserror ());