1 /* ruserpass.c -- parse .netrc-format file.
3 * Portions of this code are
4 * Copyright (c) 1985 Regents of the University of California.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * Portions of this code are Copyright (c) 2013, by the authors of
20 * nmh. See the COPYRIGHT file in the root directory of the nmh
21 * distribution for complete copyright information.
40 #define MAX_TOKVAL_SIZE 1024 /* Including terminating NUL. */
47 static struct toktab toktabs
[] = {
49 { "default", DEFAULT
},
51 { "password", PASSWD
},
53 { "account", ACCOUNT
},
62 static int token(char *);
66 ruserpass(const char *host
, char **aname
, char **apass
, int flags
)
71 init_credentials_file ();
73 cfile
= fopen (credentials_file
, "r");
76 perror (credentials_file
);
78 char tokval
[MAX_TOKVAL_SIZE
];
81 bool usedefault
= false;
82 while ((t
= token(tokval
))) {
90 if (token(tokval
) != ID
)
93 * Allow match either for user's host name.
95 if (strcasecmp(host
, tokval
) == 0)
100 while ((t
= token(tokval
)) && t
!= MACH
&& t
!= DEFAULT
) {
103 if (token(tokval
) && *aname
== 0)
104 *aname
= mh_xstrdup(tokval
);
108 if (!credentials_no_perm_check
&&
109 fstat(fileno(cfile
), &stb
) >= 0 &&
110 (stb
.st_mode
& 077) != 0) {
111 /* We make this a fatal error to force the
112 user to correct it. */
113 inform("group or other permissions, %#o, "
114 "forbidden: %s", stb
.st_mode
, credentials_file
);
115 die("Remove password or correct file "
118 if (token(tokval
) && *apass
== 0)
119 *apass
= mh_xstrdup(tokval
);
131 "Unknown keyword %s in credentials file %s\n",
132 tokval
, credentials_file
);
141 if (!*aname
&& ! (flags
& RUSERPASS_NO_PROMPT_USER
)) {
145 if ((myname
= getlogin()) == NULL
) {
148 if ((pp
= getpwuid (getuid())) != NULL
)
149 myname
= pp
->pw_name
;
151 printf("Name (%s:%s): ", host
, myname
);
153 if (fgets(tmp
, sizeof tmp
, stdin
) == NULL
) {
154 advise ("tmp", "fgets");
156 trim_suffix_c(tmp
, '\n');
157 if (*tmp
!= '\0' || myname
== NULL
) {
161 *aname
= mh_xstrdup(myname
);
164 if (!*apass
&& ! (flags
& RUSERPASS_NO_PROMPT_PASSWORD
)) {
168 snprintf(prompt
, sizeof(prompt
), "Password (%s:%s): ", host
, *aname
);
169 mypass
= nmh_getpass(prompt
);
171 if (*mypass
== '\0') {
175 *aname
= mh_xstrdup(mypass
);
184 const char normalStop
[] = "\t\n ,"; /* Each breaks a word. */
189 if (feof(cfile
) || ferror(cfile
))
193 while ((c
= getc(cfile
)) != EOF
&& c
&& strchr(stop
, c
))
200 /* FIXME: Where is the quoted-string syntax of netrc documented?
201 * This code treats «"foo""bar"» as two tokens without further
205 /* Might be backslash. Get it again later. It's handled then. */
206 if (ungetc(c
, cfile
) == EOF
)
209 while ((c
= getc(cfile
)) != EOF
&& c
&& !strchr(stop
, c
)) {
210 if (c
== '\\' && (c
= getc(cfile
)) == EOF
)
211 return TOK_EOF
; /* Discard whole token. */
214 if (cp
- tokval
> MAX_TOKVAL_SIZE
-1) {
215 die("credential tokens restricted to length %d",
216 MAX_TOKVAL_SIZE
- 1);
221 for (t
= toktabs
; t
->tokstr
; t
++)
222 if (!strcmp(t
->tokstr
, tokval
))