2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of libpwmd.
7 Libpwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Libpwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Libpwmd. If not, see <http://www.gnu.org/licenses/>.
45 #ifdef HAVE_GETPWUID_R
47 _expand_homedir (char *str
, struct passwd
*pw
)
56 if (*p
!= '~' || *(p
+ 1) != '/')
57 return pwmd_strdup (p
);
63 pwbuf
= _getpwuid (&t
);
71 result
= pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
77 _getpwuid (struct passwd
*pwd
)
79 size_t size
= sysconf (_SC_GETPW_R_SIZE_MAX
);
80 struct passwd
*result
;
87 buf
= pwmd_malloc (size
);
91 n
= getpwuid_r (getuid (), pwd
, buf
, size
, &result
);
110 _expand_homedir (char *str
, struct passwd
*pw
)
117 if (*p
!= '~' || *(p
+ 1) != '/')
118 return pwmd_strdup (p
);
122 pw
= getpwuid (getuid ());
128 return pwmd_strdup_printf ("%s/%s", pw
->pw_dir
, p
);
132 _getpwuid (struct passwd
*pwd
)
134 struct passwd
*pw
= getpwuid (getuid ());
140 return pwmd_strdup ("");
145 * Borrowed from libassuan.
148 _percent_escape (const char *atext
)
150 const unsigned char *s
;
157 len
= strlen (atext
) * 3 + 1;
158 buf
= (char *) pwmd_malloc (len
);
165 for (s
= (const unsigned char *) atext
; *s
; s
++)
169 sprintf (p
, "%%%02X", *s
);
181 update_pinentry_settings (pwm_t
* pwm
)
187 char *pwbuf
= _getpwuid (&pw
);
192 snprintf (buf
, sizeof (buf
), "%s/.pwmd/pinentry.conf", pw
.pw_dir
);
194 if ((fp
= fopen (buf
, "r")) == NULL
)
200 while ((p
= fgets (buf
, sizeof (buf
), fp
)) != NULL
)
202 char name
[32] = {0}, val
[256] = {0};
204 if (sscanf (p
, " %31[a-zA-Z] = %255s", name
, val
) != 2)
207 if (strcasecmp (name
, "TTYNAME") == 0)
209 pwmd_free (pwm
->pinentry_tty
);
210 pwm
->pinentry_tty
= pwmd_strdup (val
);
211 if (!pwm
->pinentry_tty
)
214 else if (strcasecmp (name
, "TTYTYPE") == 0)
216 pwmd_free (pwm
->pinentry_term
);
217 pwm
->pinentry_term
= pwmd_strdup (val
);
218 if (!pwm
->pinentry_term
)
221 else if (strcasecmp (name
, "DISPLAY") == 0)
223 pwmd_free (pwm
->pinentry_display
);
224 pwm
->pinentry_display
= pwmd_strdup (val
);
225 if (!pwm
->pinentry_display
)
228 else if (strcasecmp (name
, "PATH") == 0)
230 pwmd_free (pwm
->pinentry_path
);
231 pwm
->pinentry_path
= _expand_homedir (val
, &pw
);
232 if (!pwm
->pinentry_path
)
235 else if (strcasecmp (name
, "LC_MESSAGES") == 0)
237 pwmd_free (pwm
->pinentry_lcmessages
);
238 pwm
->pinentry_lcmessages
= pwmd_strdup (val
);
239 if (!pwm
->pinentry_lcmessages
)
242 else if (strcasecmp (name
, "LC_CTYPE") == 0)
244 pwmd_free (pwm
->pinentry_lcctype
);
245 pwm
->pinentry_lcctype
= pwmd_strdup (val
);
246 if (!pwm
->pinentry_lcctype
)
256 /* Common hostname parsing for urls. Handles both IPv4 and IPv6 hostname and
257 * port specification.
260 parse_hostname_common (const char *str
, char **host
, int *port
)
265 /* IPv6 with optional port. */
276 /* Handle IPv6 without proper braces around the IP. */
289 size_t len
= strlen (p
) - strlen (t
) + 1;
291 *host
= pwmd_malloc (len
);
293 return gpg_error_from_errno (ENOMEM
);
295 snprintf (*host
, len
, "%s", p
);
302 *port
= strtol (t
, NULL
, 10);
307 while (*t
&& isdigit (*t
))
314 *host
= pwmd_strdup (str
);
316 return gpg_error_from_errno (ENOMEM
);
323 bin2hex (const unsigned char *data
, size_t len
)
325 size_t size
= len
* 2 + 1;
326 char buf
[size
]; // C99
331 for (n
= 0; n
< len
; n
++)
335 sprintf (c
, "%02X", data
[n
]);
339 return pwmd_strdup (buf
);