1 /* Portions of this code are
2 * Copyright (c) 1985 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 * Portions of this code are Copyright (c) 2013, by the authors of
18 * nmh. See the COPYRIGHT file in the root directory of the nmh
19 * distribution for complete copyright information.
37 #define MAX_TOKVAL_SIZE 1024 /* Including terminating NUL. */
44 static struct toktab toktabs
[] = {
46 { "default", DEFAULT
},
48 { "password", PASSWD
},
50 { "account", ACCOUNT
},
59 static int token(char *);
63 ruserpass(const char *host
, char **aname
, char **apass
, int flags
)
65 int t
, usedefault
= 0;
68 init_credentials_file ();
70 cfile
= fopen (credentials_file
, "r");
73 perror (credentials_file
);
75 char tokval
[MAX_TOKVAL_SIZE
];
78 while ((t
= token(tokval
))) {
86 if (token(tokval
) != ID
)
89 * Allow match either for user's host name.
91 if (strcasecmp(host
, tokval
) == 0)
96 while ((t
= token(tokval
)) && t
!= MACH
&& t
!= DEFAULT
) {
99 if (token(tokval
) && *aname
== 0)
100 *aname
= mh_xstrdup(tokval
);
104 if (!credentials_no_perm_check
&&
105 fstat(fileno(cfile
), &stb
) >= 0 &&
106 (stb
.st_mode
& 077) != 0) {
107 /* We make this a fatal error to force the
108 user to correct it. */
109 inform("group or other permissions, %#o, "
110 "forbidden: %s", stb
.st_mode
, credentials_file
);
111 adios(NULL
, "Remove password or correct file "
114 if (token(tokval
) && *apass
== 0)
115 *apass
= mh_xstrdup(tokval
);
127 "Unknown keyword %s in credentials file %s\n",
128 tokval
, credentials_file
);
137 if (!*aname
&& ! (flags
& RUSERPASS_NO_PROMPT_USER
)) {
141 if ((myname
= getlogin()) == NULL
) {
144 if ((pp
= getpwuid (getuid())) != NULL
)
145 myname
= pp
->pw_name
;
147 printf("Name (%s:%s): ", host
, myname
);
149 if (fgets(tmp
, sizeof tmp
, stdin
) == NULL
) {
150 advise ("tmp", "fgets");
152 trim_suffix_c(tmp
, '\n');
153 if (*tmp
!= '\0' || myname
== NULL
) {
157 *aname
= mh_xstrdup(myname
);
160 if (!*apass
&& ! (flags
& RUSERPASS_NO_PROMPT_PASSWORD
)) {
164 snprintf(prompt
, sizeof(prompt
), "Password (%s:%s): ", host
, *aname
);
165 mypass
= nmh_getpass(prompt
);
167 if (*mypass
== '\0') {
171 *aname
= mh_xstrdup(mypass
);
180 const char normalStop
[] = "\t\n ,"; /* Each breaks a word. */
185 if (feof(cfile
) || ferror(cfile
))
189 while ((c
= getc(cfile
)) != EOF
&& c
&& strchr(stop
, c
))
196 /* FIXME: Where is the quoted-string syntax of netrc documented?
197 * This code treats «"foo""bar"» as two tokens without further
201 /* Might be backslash. Get it again later. It's handled then. */
202 if (ungetc(c
, cfile
) == EOF
)
205 while ((c
= getc(cfile
)) != EOF
&& c
&& !strchr(stop
, c
)) {
206 if (c
== '\\' && (c
= getc(cfile
)) == EOF
)
207 return TOK_EOF
; /* Discard whole token. */
210 if (cp
- tokval
> MAX_TOKVAL_SIZE
-1) {
211 adios(NULL
, "credential tokens restricted to length %d",
212 MAX_TOKVAL_SIZE
- 1);
217 for (t
= toktabs
; t
->tokstr
; t
++)
218 if (!strcmp(t
->tokstr
, tokval
))